package com.mogo.cloud.passport.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 设备信息
 */
public class DevicesUtils {

    private static final String PROPERTIES = "android.os.SystemProperties";
    private static final String GSM_SERIAL = "gsm.serial";
    private static final String GET = "get";

    public static String getSn(){
        return getSystemProperties(GSM_SERIAL);
    }

    public static String getSystemProperties(String name ) {
        String value = "";
        try {
            Class< ? > c = Class.forName( PROPERTIES );
            Method get = c.getMethod( GET, String.class );
            value = (String) get.invoke( c, name );
        } catch ( ClassNotFoundException var3 ) {
            var3.printStackTrace();
        } catch ( NoSuchMethodException var4 ) {
            var4.printStackTrace();
        } catch ( InvocationTargetException var5 ) {
            var5.printStackTrace();
        } catch ( IllegalAccessException var6 ) {
            var6.printStackTrace();
        }
        return value;
    }


    public static String getNetworkTypeName(Context context) {
        String typeName = "unknown";
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager == null) {
                return typeName;
            }
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null) {
                typeName = networkInfo.getTypeName();
            }
        }
        return typeName;
    }

    public static int getNetworkType(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
                if (activeNetInfo != null) {
                    return activeNetInfo.getType();
                }
            }
        }
        return -1;
    }

    public static boolean isSdkHigherThan18() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    public static String getVersionName(Context context) {
        String appVersion = "";

        try {
            if (context != null) {
                String packageName = context.getApplicationInfo().packageName;
                appVersion = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return appVersion;
    }

    public static int getVersionCode(Context context) {
        String pkgName = context.getPackageName();

        try {
            PackageInfo e = context.getPackageManager().getPackageInfo(pkgName, 0);
            if (e != null) {
                return e.versionCode;
            }
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return 1;
    }
}
