1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.applications;
16 
17 import android.content.Context;
18 import android.content.Intent;
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.os.UserHandle;
23 
24 import java.util.List;
25 
26 public abstract class InstalledAppCounter extends AppCounter {
27 
28     /**
29      * Count all installed packages, irrespective of install reason.
30      */
31     public static final int IGNORE_INSTALL_REASON = -1;
32 
33     private final int mInstallReason;
34 
InstalledAppCounter(Context context, int installReason, PackageManager packageManager)35     public InstalledAppCounter(Context context, int installReason,
36             PackageManager packageManager) {
37         super(context, packageManager);
38         mInstallReason = installReason;
39     }
40 
41     @Override
includeInCount(ApplicationInfo info)42     protected boolean includeInCount(ApplicationInfo info) {
43         return includeInCount(mInstallReason, mPm, info);
44     }
45 
includeInCount(int installReason, PackageManager pm, ApplicationInfo info)46     public static boolean includeInCount(int installReason, PackageManager pm,
47             ApplicationInfo info) {
48         final int userId = UserHandle.getUserId(info.uid);
49         if (installReason != IGNORE_INSTALL_REASON
50                 && pm.getInstallReason(info.packageName,
51                         new UserHandle(userId)) != installReason) {
52             return false;
53         }
54         if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
55             return true;
56         }
57         if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
58             return true;
59         }
60         Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
61                 .addCategory(Intent.CATEGORY_LAUNCHER)
62                 .setPackage(info.packageName);
63         List<ResolveInfo> intents = pm.queryIntentActivitiesAsUser(
64                 launchIntent,
65                 PackageManager.GET_DISABLED_COMPONENTS
66                         | PackageManager.MATCH_DIRECT_BOOT_AWARE
67                         | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
68                 userId);
69         return intents != null && intents.size() != 0;
70     }
71 }
72