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