package com.app.baselibrary.commonUtil;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.app.baselibrary.bean.RunningAppInfo;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 在此写用途
 * author: pl
 * date: 2017-02-09 09:59
 */

public class SystemAppProcessUtil {

    private static SystemAppProcessUtil mSystemAppProcessUtil;
//    private List<RunningAppInfo> list = new ArrayList<>();
    public static SystemAppProcessUtil getInstance() {
        Class obj = SystemAppProcessUtil.class;
        if (mSystemAppProcessUtil == null) {
            synchronized (obj) {
                if (mSystemAppProcessUtil == null)
                    mSystemAppProcessUtil = new SystemAppProcessUtil();
            }
        }
        return mSystemAppProcessUtil;
    }

    private SystemAppProcessUtil(){
    }

    /**
     * 获取程序包名(本程序包名5.0版本上下都可获取)
     *
     * @return RunningAppProcessInfo
     */
    public ActivityManager.RunningAppProcessInfo getCurrentAppInfo() {
        ActivityManager.RunningAppProcessInfo currentInfo = null;
        Field field = null;
        int START_TASK_TO_FRONT = 2;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            try {
                field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
            } catch (Exception e) {
                e.printStackTrace();
            }
            ActivityManager am = (ActivityManager) ContextUtil.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appList = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo app : appList) {
                if (app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    Integer state = null;
                    try {
                        if (null != field) {
                            state = field.getInt(app);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (state != null && state == START_TASK_TO_FRONT) {
                        currentInfo = app;
                        break;
                    }
                }
            }

        } else {
            ActivityManager am = (ActivityManager) ContextUtil.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            currentInfo = tasks.get(0);
        }
        return currentInfo;
    }

    // 某一特定经常里所有正在运行的应用程序
    public List<RunningAppInfo> querySpecailPIDRunningAppInfo(String[] pkgNameList , String processName, int pid) {

        PackageManager pm = ContextUtil.getAppContext().getPackageManager();

        // 保存所有正在运行的应用程序信息
        List<RunningAppInfo> runningAppInfos = new ArrayList<RunningAppInfo>(); // 保存过滤查到的AppInfo

        for(int i = 0 ; i<pkgNameList.length ;i++){
            //根据包名查询特定的ApplicationInfo对象
            ApplicationInfo appInfo;
            try {
                appInfo = pm.getApplicationInfo(pkgNameList[i], 0);
                runningAppInfos.add(getAppInfo(pm,appInfo, pid, processName));
            }
            catch (PackageManager.NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  // 0代表没有任何标记;
        }
        return runningAppInfos ;
    }


    // 构造一个RunningAppInfo对象 ，并赋值
    private RunningAppInfo getAppInfo(PackageManager pm, ApplicationInfo app, int pid, String processName) {
        RunningAppInfo appInfo = new RunningAppInfo();
        appInfo.setAppLabel((String) app.loadLabel(pm));
//        appInfo.setAppIcon(app.loadIcon(pm));
        appInfo.setPkgName(app.packageName);

        appInfo.setPid(pid);
        appInfo.setProcessName(processName);

        return appInfo;
    }



}


