26 展讯Sprd设置-电池-识别桌面应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/su749520/article/details/84392236

1. 目的

识别桌面应用,并进行拦截处理

2. 源码相关

主要使用 Intent.CATEGORY_HOME 进行过滤判断

    // using disable component instead of disable the whole app
    // when enter/exit ultra-saving mode. See bug#819868
    // to save the component that will be disable/enable during switch
    private ArrayList<String> mLauncherAppComponentList = new ArrayList<>();

    private ArrayList<String> mLauncherAppList = new ArrayList<>();
    private String mCurrentHomeLauncher;

    // default launcher app component list
    private String[] mDefaultLauncherAppComponentList = new String[] {
        "com.android.launcher3/.Launcher"
    };
 
    // apps that have HOME category, but is not a launcher app
    private String[] mExceptionLauncherAppList = new String[] {
        "android",
        "com.android.settings",
        "com.android.managedprovisioning",
        "com.android.provision",
        "com.google.android.gms",
        "com.google.android.setupwizard",
        "com.google.android.googlequicksearchbox",
        "com.android.provision2"
    };

    private void loadLauncherApps() {
        try {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);

            final List<UserInfo> users = mUserManager.getUsers();
            for (int ui = users.size() - 1; ui >= 0; ui--) {
                UserInfo user = users.get(ui);
                if (DEBUG) Slog.d(TAG, "- loadLauncherApps() for user: " + user.id);
                List<ResolveInfo> resolves =
                    mContext.getPackageManager().queryIntentActivitiesAsUser(intent, PackageManager.MATCH_DISABLED_COMPONENTS, user.id);

                // Look for the original activity in the list...
                final int N = resolves != null ? resolves.size() : 0;
                for (int i=0; i<N; i++) {
                    final ResolveInfo candidate = resolves.get(i);
                    final ActivityInfo info = candidate.activityInfo;
                    if (info != null && info.packageName != null
                        && !mLauncherAppList.contains(info.packageName)) {
                        mLauncherAppList.add(info.packageName);
                    }
                    Slog.d(TAG, "homeApp:" + info.packageName);

                    // using disable component instead of disable the whole app
                    // when enter/exit ultra-saving mode. See bug#819868
                    if (USE_COMPONENT) {
                        if (info != null && info.packageName != null
                             && "com.inone.powersavemodelauncher".equals(info.packageName)) {
                            if (!mLauncherAppComponentList.contains(info.packageName)) {
                                mLauncherAppComponentList.add(info.packageName);
                            }
                            Slog.d(TAG, "homeComponent:" + info.packageName);
                            continue;
                        }

                        if (info != null && info.packageName != null) {
                            if (ArrayUtils.contains(mExceptionLauncherAppList, info.packageName)) {
                                if (DEBUG) Slog.d(TAG, "loadLauncherApps:" + info.packageName
                                    + " NOT a launcher app!");
                                continue;
                            }

                            ComponentName cn = new ComponentName(info.packageName, info.name);
                            String componetName = cn.flattenToShortString();
                            if (!mLauncherAppComponentList.contains(componetName)) {
                                mLauncherAppComponentList.add(componetName);
                            }
                            Slog.d(TAG, "homeComponent:" + componetName);
                        }
                    }
                }

                // using disable component instead of disable the whole app
                // when enter/exit ultra-saving mode. See bug#819868
                if (USE_COMPONENT) {
                    loadLauncherComponent(user.id);
                }
            }

            /*check if contains the default launcher*/
            for(String s : mDefaultLauncherAppList) {
                if(!mLauncherAppList.contains(s)) {
                    mLauncherAppList.add(s);
                }
            }

            // using disable component instead of disable the whole app
            // when enter/exit ultra-saving mode. See bug#819868
            if (USE_COMPONENT) {
                /*check if contains the default launcher*/
                for(String s : mDefaultLauncherAppComponentList) {
                    if(!mLauncherAppComponentList.contains(s)) {
                        mLauncherAppComponentList.add(s);
                    }
                }
            }

            ResolveInfo resolvedHome = mContext.getPackageManager().resolveActivity(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (resolvedHome != null) {
                mCurrentHomeLauncher = resolvedHome.activityInfo.packageName;
            }
        } catch (Exception e) {
            Slog.d(TAG, "e:" + e);
        }

        //disbleLauncherAppIfNeed();

        if (DEBUG) {
            Slog.d(TAG, "mCurrentHomeLauncher:" + mCurrentHomeLauncher);
            Slog.d(TAG, "mLauncherAppList: " + mLauncherAppList.size());
            for (int i=0;i<mLauncherAppList.size();i++) {
                Slog.d(TAG, "App:" + mLauncherAppList.get(i));
            }

            Slog.d(TAG, "mLauncherAppComponentList: " + mLauncherAppComponentList.size());
            for (int i=0;i<mLauncherAppComponentList.size();i++) {
                Slog.d(TAG, "Component:" + mLauncherAppComponentList.get(i));
            }
        }
    }

    private void loadLauncherComponent(int userId) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        for (int i=0;i<mLauncherAppList.size();i++) {
            String packageName = mLauncherAppList.get(i);
            if (ArrayUtils.contains(mExceptionLauncherAppList, packageName)) {
                if (DEBUG) Slog.d(TAG, "loadLauncherComponent:" + packageName
                    + " NOT a launcher app!");
                continue;
            }

            intent.setPackage(packageName);

            List<ResolveInfo> resolves =
                mContext.getPackageManager().queryIntentActivitiesAsUser(intent, PackageManager.MATCH_DISABLED_COMPONENTS, userId);

            // Look for the original activity in the list...
            final int SIZE = resolves != null ? resolves.size() : 0;
            for (int n=0; n<SIZE; n++) {
                final ResolveInfo candidate = resolves.get(n);
                final ActivityInfo info = candidate.activityInfo;
                if (info != null && info.packageName != null
                     && "com.inone.powersavemode.launcher".equals(info.packageName)) {
                    if (!mLauncherAppComponentList.contains(info.packageName)) {
                        mLauncherAppComponentList.add(info.packageName);
                    }
                    Slog.d(TAG, "homeLauncherComponent:" + info.packageName);
                    continue;
                }

                if (info != null && info.packageName != null) {
                    ComponentName cn = new ComponentName(info.packageName, info.name);
                    String componetName = cn.flattenToShortString();
                    if (!mLauncherAppComponentList.contains(componetName)) {
                        mLauncherAppComponentList.add(componetName);
                    }
                    Slog.d(TAG, "homeLauncherComponent:" + componetName);
                }
            }
        }

    }

    private boolean isLauncherApp(String pkgName) {
        int index = mLauncherAppList.indexOf(pkgName);
        if (index >= 0) {
            return true;
        }
        return false;
    }

3. 上层的桌面应用识别接口

主要根据 Intent.CATEGORY_HOME 进行条件过滤

    public static String getLauncherApp(Context mContext) {
        String result = "com.android.launcher3";

        List<String> homeAppList = new ArrayList<String>();

        PackageManager packageManager = mContext.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, 0);

        if (resolveInfo != null && resolveInfo.size() > 0) {
            for (ResolveInfo ri : resolveInfo) {
                homeAppList.add(ri.activityInfo.packageName);
            }
        }

        if (homeAppList != null && homeAppList.size() > 0) {
            ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

            List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(20);
            if (tasksInfo != null && tasksInfo.size() > 0) {
                for (int i = 0; i < tasksInfo.size(); i++) {
                    if (homeAppList.contains(tasksInfo.get(i).topActivity.getPackageName())) {
                        result = tasksInfo.get(i).topActivity.getPackageName();
                        Log.d(TAG, "Current home app is " + result);
                        break;
                    }
                }
            }
        }

        return result;
    }

猜你喜欢

转载自blog.csdn.net/su749520/article/details/84392236
26