1 /*
2  * Copyright (C) 2022 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.server.pm;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.text.TextUtils;
22 
23 import com.android.internal.util.ArrayUtils;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * Helps {@link PackageManagerService} keep track of the names of special packages like SetupWizard.
30  */
31 public final class KnownPackages {
32     @IntDef(prefix = "PACKAGE_", value = {
33             PACKAGE_SYSTEM,
34             PACKAGE_SETUP_WIZARD,
35             PACKAGE_INSTALLER,
36             PACKAGE_UNINSTALLER,
37             PACKAGE_VERIFIER,
38             PACKAGE_BROWSER,
39             PACKAGE_SYSTEM_TEXT_CLASSIFIER,
40             PACKAGE_PERMISSION_CONTROLLER,
41             PACKAGE_CONFIGURATOR,
42             PACKAGE_INCIDENT_REPORT_APPROVER,
43             PACKAGE_APP_PREDICTOR,
44             PACKAGE_OVERLAY_CONFIG_SIGNATURE,
45             PACKAGE_WIFI,
46             PACKAGE_COMPANION,
47             PACKAGE_RETAIL_DEMO,
48             PACKAGE_RECENTS,
49             PACKAGE_AMBIENT_CONTEXT_DETECTION,
50             PACKAGE_WEARABLE_SENSING,
51     })
52     @Retention(RetentionPolicy.SOURCE)
53     public @interface KnownPackage {
54     }
55 
56     public static final int PACKAGE_SYSTEM = 0;
57     public static final int PACKAGE_SETUP_WIZARD = 1;
58     public static final int PACKAGE_INSTALLER = 2;
59     public static final int PACKAGE_UNINSTALLER = 3;
60     public static final int PACKAGE_VERIFIER = 4;
61     public static final int PACKAGE_BROWSER = 5;
62     public static final int PACKAGE_SYSTEM_TEXT_CLASSIFIER = 6;
63     public static final int PACKAGE_PERMISSION_CONTROLLER = 7;
64     public static final int PACKAGE_WELLBEING = 8;
65     public static final int PACKAGE_DOCUMENTER = 9;
66     public static final int PACKAGE_CONFIGURATOR = 10;
67     public static final int PACKAGE_INCIDENT_REPORT_APPROVER = 11;
68     public static final int PACKAGE_APP_PREDICTOR = 12;
69     public static final int PACKAGE_OVERLAY_CONFIG_SIGNATURE = 13;
70     public static final int PACKAGE_WIFI = 14;
71     public static final int PACKAGE_COMPANION = 15;
72     public static final int PACKAGE_RETAIL_DEMO = 16;
73     public static final int PACKAGE_RECENTS = 17;
74     public static final int PACKAGE_AMBIENT_CONTEXT_DETECTION = 18;
75     public static final int PACKAGE_WEARABLE_SENSING = 19;
76     // Integer value of the last known package ID. Increases as new ID is added to KnownPackage.
77     // Please note the numbers should be continuous.
78     public static final int LAST_KNOWN_PACKAGE = PACKAGE_WEARABLE_SENSING;
79 
80     static final String SYSTEM_PACKAGE_NAME = "android";
81 
82     private final DefaultAppProvider mDefaultAppProvider;
83     private final String mRequiredInstallerPackage;
84     private final String mRequiredUninstallerPackage;
85     private final String mSetupWizardPackage;
86     private final String[] mRequiredVerifierPackages;
87     private final String mDefaultTextClassifierPackage;
88     private final String mSystemTextClassifierPackageName;
89     private final String mRequiredPermissionControllerPackage;
90     private final String mConfiguratorPackage;
91     private final String mIncidentReportApproverPackage;
92     private final String mAmbientContextDetectionPackage;
93     private final String mWearableSensingPackage;
94     private final String mAppPredictionServicePackage;
95     private final String mCompanionPackage;
96     private final String mRetailDemoPackage;
97     private final String mOverlayConfigSignaturePackage;
98     private final String mRecentsPackage;
99 
KnownPackages(DefaultAppProvider defaultAppProvider, String requiredInstallerPackage, String requiredUninstallerPackage, String setupWizardPackage, String[] requiredVerifierPackages, String defaultTextClassifierPackage, String systemTextClassifierPackageName, String requiredPermissionControllerPackage, String configuratorPackage, String incidentReportApproverPackage, String ambientContextDetectionPackage, String wearableSensingPackage, String appPredictionServicePackage, String companionPackageName, String retailDemoPackage, String overlayConfigSignaturePackage, String recentsPackage)100     KnownPackages(DefaultAppProvider defaultAppProvider, String requiredInstallerPackage,
101             String requiredUninstallerPackage, String setupWizardPackage,
102             String[] requiredVerifierPackages, String defaultTextClassifierPackage,
103             String systemTextClassifierPackageName, String requiredPermissionControllerPackage,
104             String configuratorPackage, String incidentReportApproverPackage,
105             String ambientContextDetectionPackage, String wearableSensingPackage,
106             String appPredictionServicePackage, String companionPackageName,
107             String retailDemoPackage, String overlayConfigSignaturePackage, String recentsPackage) {
108         mDefaultAppProvider = defaultAppProvider;
109         mRequiredInstallerPackage = requiredInstallerPackage;
110         mRequiredUninstallerPackage = requiredUninstallerPackage;
111         mSetupWizardPackage = setupWizardPackage;
112         mRequiredVerifierPackages = requiredVerifierPackages;
113         mDefaultTextClassifierPackage = defaultTextClassifierPackage;
114         mSystemTextClassifierPackageName = systemTextClassifierPackageName;
115         mRequiredPermissionControllerPackage = requiredPermissionControllerPackage;
116         mConfiguratorPackage = configuratorPackage;
117         mIncidentReportApproverPackage = incidentReportApproverPackage;
118         mAmbientContextDetectionPackage = ambientContextDetectionPackage;
119         mWearableSensingPackage = wearableSensingPackage;
120         mAppPredictionServicePackage = appPredictionServicePackage;
121         mCompanionPackage = companionPackageName;
122         mRetailDemoPackage = retailDemoPackage;
123         mOverlayConfigSignaturePackage = overlayConfigSignaturePackage;
124         mRecentsPackage = recentsPackage;
125     }
126 
127     /**
128      * Returns the string representation of a known package. For example,
129      * {@link #PACKAGE_SETUP_WIZARD} is represented by the string Setup Wizard.
130      *
131      * @param knownPackage The known package.
132      * @return The string representation.
133      */
knownPackageToString(@nownPackage int knownPackage)134     static @NonNull String knownPackageToString(@KnownPackage int knownPackage) {
135         switch (knownPackage) {
136             case PACKAGE_SYSTEM:
137                 return "System";
138             case PACKAGE_SETUP_WIZARD:
139                 return "Setup Wizard";
140             case PACKAGE_INSTALLER:
141                 return "Installer";
142             case PACKAGE_UNINSTALLER:
143                 return "Uninstaller";
144             case PACKAGE_VERIFIER:
145                 return "Verifier";
146             case PACKAGE_BROWSER:
147                 return "Browser";
148             case PACKAGE_SYSTEM_TEXT_CLASSIFIER:
149                 return "System Text Classifier";
150             case PACKAGE_PERMISSION_CONTROLLER:
151                 return "Permission Controller";
152             case PACKAGE_WELLBEING:
153                 return "Wellbeing";
154             case PACKAGE_DOCUMENTER:
155                 return "Documenter";
156             case PACKAGE_CONFIGURATOR:
157                 return "Configurator";
158             case PACKAGE_INCIDENT_REPORT_APPROVER:
159                 return "Incident Report Approver";
160             case PACKAGE_APP_PREDICTOR:
161                 return "App Predictor";
162             case PACKAGE_WIFI:
163                 return "Wi-Fi";
164             case PACKAGE_COMPANION:
165                 return "Companion";
166             case PACKAGE_RETAIL_DEMO:
167                 return "Retail Demo";
168             case PACKAGE_OVERLAY_CONFIG_SIGNATURE:
169                 return "Overlay Config Signature";
170             case PACKAGE_RECENTS:
171                 return "Recents";
172             case PACKAGE_AMBIENT_CONTEXT_DETECTION:
173                 return "Ambient Context Detection";
174             case PACKAGE_WEARABLE_SENSING:
175                 return "Wearable sensing";
176         }
177         return "Unknown";
178     }
179 
getKnownPackageNames(@onNull Computer snapshot, int knownPackage, int userId)180     String[] getKnownPackageNames(@NonNull Computer snapshot, int knownPackage, int userId) {
181         switch (knownPackage) {
182             case PACKAGE_BROWSER:
183                 return new String[]{mDefaultAppProvider.getDefaultBrowser(userId)};
184             case PACKAGE_INSTALLER:
185                 return snapshot.filterOnlySystemPackages(mRequiredInstallerPackage);
186             case PACKAGE_UNINSTALLER:
187                 return snapshot.filterOnlySystemPackages(mRequiredUninstallerPackage);
188             case PACKAGE_SETUP_WIZARD:
189                 return snapshot.filterOnlySystemPackages(mSetupWizardPackage);
190             case PACKAGE_SYSTEM:
191                 return new String[]{SYSTEM_PACKAGE_NAME};
192             case PACKAGE_VERIFIER:
193                 return snapshot.filterOnlySystemPackages(mRequiredVerifierPackages);
194             case PACKAGE_SYSTEM_TEXT_CLASSIFIER:
195                 return snapshot.filterOnlySystemPackages(
196                         mDefaultTextClassifierPackage, mSystemTextClassifierPackageName);
197             case PACKAGE_PERMISSION_CONTROLLER:
198                 return snapshot.filterOnlySystemPackages(mRequiredPermissionControllerPackage);
199             case PACKAGE_CONFIGURATOR:
200                 return snapshot.filterOnlySystemPackages(mConfiguratorPackage);
201             case PACKAGE_INCIDENT_REPORT_APPROVER:
202                 return snapshot.filterOnlySystemPackages(mIncidentReportApproverPackage);
203             case PACKAGE_AMBIENT_CONTEXT_DETECTION:
204                 return snapshot.filterOnlySystemPackages(mAmbientContextDetectionPackage);
205             case PACKAGE_WEARABLE_SENSING:
206                 return snapshot.filterOnlySystemPackages(mWearableSensingPackage);
207             case PACKAGE_APP_PREDICTOR:
208                 return snapshot.filterOnlySystemPackages(mAppPredictionServicePackage);
209             case PACKAGE_COMPANION:
210                 return snapshot.filterOnlySystemPackages(mCompanionPackage);
211             case PACKAGE_RETAIL_DEMO:
212                 return TextUtils.isEmpty(mRetailDemoPackage)
213                         ? ArrayUtils.emptyArray(String.class)
214                         : new String[]{mRetailDemoPackage};
215             case PACKAGE_OVERLAY_CONFIG_SIGNATURE:
216                 return snapshot.filterOnlySystemPackages(mOverlayConfigSignaturePackage);
217             case PACKAGE_RECENTS:
218                 return snapshot.filterOnlySystemPackages(mRecentsPackage);
219             default:
220                 return ArrayUtils.emptyArray(String.class);
221         }
222     }
223 }
224