package com.sheep.gamegroup.util;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.WindowManager;

/**
 * Created by ljy on 2018/3/8.
 */
public class AppUtil {

    /**
     * 获取app版本名
     */
    public static String getAppVersionName(Context context) {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi;
        try {
            pi = pm.getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取应用程序版本名称信息
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取app版本号
     */
    public static int getAppVersionCode(Context context) {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi;
        try {
            pi = pm.getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        return tm.getDeviceId();
    }
    public static int dp2px(Context paramContext, float paramFloat)
    {
        float scale = paramContext.getResources().getDisplayMetrics().density;
        return (int)(0.5F + paramFloat * scale);
    }
    public static int getWindowWidth(Context paramContext)
    {
        return getWindowManager(paramContext).getDefaultDisplay().getWidth();
    }
    public static WindowManager getWindowManager(Context paramContext)
    {
        return (WindowManager)paramContext.getSystemService(Context.WINDOW_SERVICE);
    }

    public static boolean isMainProcess(Context context) {
        return TextUtils.equals(context.getPackageName(), getCurProcessName(context));
    }


    public static String getCurProcessName(Context context){
        int pid = android.os.Process.myPid();
        String processName = "";
        android.app.ActivityManager manager = (android.app.ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        if(manager != null) {
            for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
                LogUtil.println("getRunningAppProcesses",process.processName);
                if (process.pid == pid) {
                    processName = process.processName;
                    return processName;
                }
            }
        }
        return processName;
    }
}
