package com.growthbeat.utils;

import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
import com.growthbeat.GrowthbeatCore;

public final class DeviceUtils {

    public static Future<Boolean> getTrackingEnabled() {
        FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
            public Boolean call() throws Exception {
                try {
                    Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(GrowthbeatCore.getInstance().getContext());
                    return !adInfo.isLimitAdTrackingEnabled();
                } catch (Throwable e) {
                    return null;
                }
            }
        });
        GrowthbeatCore.getInstance().getExecutor().execute(future);
        return future;
    }

    public static Future<String> getAdvertisingId() {
        FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
            public String call() throws Exception {
                try {
                    Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(GrowthbeatCore.getInstance().getContext());
                    return adInfo.getId();
                } catch (Throwable e) {
                    return null;
                }
            }
        });
        GrowthbeatCore.getInstance().getExecutor().execute(future);
        return future;
    }

    public static String getModel() {
        return Build.MODEL;
    }

    public static String getDevice() {
        return Build.DEVICE;
    }

    public static String getOsVersion() {
        return Build.VERSION.RELEASE;
    }

    public static int getApiVersion() {
        return Build.VERSION.SDK_INT;
    }

    public static String getCountry() {
        if (Locale.getDefault() == null)
            return null;
        return Locale.getDefault().getCountry();
    }

    public static String getLanguage() {
        if (Locale.getDefault() == null)
            return null;
        return Locale.getDefault().getLanguage();
    }

    public static String getTimeZone() {
        if (Locale.getDefault() == null)
            return null;
        return TimeZone.getDefault().getID();
    }

    public static Integer getTimeZoneOffset() {
        if (TimeZone.getDefault() == null)
            return null;
        return TimeZone.getDefault().getRawOffset() / (60 * 60 * 1000);
    }

    public static boolean connectedToWiFi(Context context) {
        try {
            ConnectivityManager connectivityManager = SystemServiceUtils.getConnectivityManager(context);
            State wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            if (wifi == State.CONNECTED || wifi == State.CONNECTING)
                return true;
        } catch (Exception e) {
        }

        return false;
    }

    public static long getAvailableMemory(Context context) {
        ActivityManager activityManager = SystemServiceUtils.getActivityManager(context);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.availMem;
    }

    public static boolean isLowMemory(Context context) {
        ActivityManager activityManager = SystemServiceUtils.getActivityManager(context);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.lowMemory;
    }

    @SuppressWarnings("deprecation")
    public static Point getDisplaySize(Context context) {
        WindowManager windowManager = SystemServiceUtils.getWindowManager(context);
        Point point = new Point();
        Display display = windowManager.getDefaultDisplay();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
            display.getSize(point);
        else {
            point.x = display.getWidth();
            point.y = display.getHeight();
        }

        return point;
    }

}

@@ -364,14 +364,8 @@
 Info
-.State
 ;%0Aim
@@ -2929,34 +2929,16 @@
 text) %7B%0A
-        try %7B%0A    
         
@@ -2979,38 +2979,17 @@
 nager = 
-SystemServiceUtils.get
+(
 Connecti
@@ -3003,67 +3003,92 @@
 ager
-(
+) 
 context
-);%0A            State wifi = connectivityManager.get
+.getSystemService(Context.CONNECTIVITY_SERVICE);%0A        NetworkInfo active
 Netw
@@ -3094,18 +3094,20 @@
 workInfo
-(C
+ = c
 onnectiv
@@ -3121,104 +3121,137 @@
 ger.
-TYPE_WIFI).getState();%0A            if (wifi == State.CONNECTED %7C%7C wifi == State.CONNECTING)%0A    
+getActiveNetworkInfo();%0A        if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) %7B%0A
     
@@ -3284,41 +3284,8 @@
    %7D
- catch (Exception e) %7B%0A        %7D%0A
 %0A   

Fix deprecated method