package com.zhangxin.starlight.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.util.List;

/**
 * Created by lidandan
 * Date : 2022/1/17
 * Description :
 */
public class AndroidDeviceUtils {
    public static final String SYS_MIUI = "sys_miui";
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";

    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";

    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    public static boolean isXiaoMiDevice() {
//        String model = Build.MODEL;
        //请使用android.os.Build对象，查询MANUFACTURER和MODEL的值，MANUFACTURER值为Xiaomi即为小米设备
        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            return true;
        }
        return false;
    }
//    public static String getSystem() {
//        String SYS = "";
//        try {
//            if (!TextUtils.isEmpty(getSystemProperty(KEY_MIUI_VERSION_NAME))) {
//                SYS = SYS_MIUI;//小米
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return SYS;
//        }
//        return SYS;
//    }
//
//    private static String getSystemProperty(String name) throws IOException {
//        BufferedReader bufferedReader = null;
//        String versionName;
//        try {
//            Process process = Runtime.getRuntime().exec("getprop " + name);
//            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);
//            versionName = bufferedReader.readLine();
//            bufferedReader.close();
//        } catch (Exception e) {
//            return null;
//        } finally {
//            if (bufferedReader != null) {
//                bufferedReader.close();
//            }
//        }
//        return versionName;
//    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
            /*
                BACKGROUND=400 EMPTY=500 FOREGROUND=100
                GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
            */
                Log.i(context.getPackageName(), "此appimportace =" + appProcess.importance + ",context.getClass().getName()=" + context.getClass().getName());

                if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Log.i(context.getPackageName(), "处于后台" + appProcess.processName);
                    return true;
                } else {
                    Log.i(context.getPackageName(), "处于前台" + appProcess.processName);
                    return false;
                }
            }
        }
        return false;
    }
}
