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 android.car.builtin.content.pm;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.UserIdInt;
23 import android.app.ActivityManager;
24 import android.app.ActivityThread;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.ComponentInfo;
29 import android.content.pm.IPackageManager;
30 import android.content.pm.PackageInfo;
31 import android.content.pm.PackageManager;
32 import android.os.RemoteException;
33 import android.text.TextUtils;
34 
35 /**
36  * Helper class for {@code PackageManager}.
37  *
38  * @hide
39  */
40 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
41 public final class PackageManagerHelper {
42 
43     /**
44      * Read-only property to define the package name of car service updatable
45      * package.
46      *
47      * <p>This property must be defined and will be set to {@code "com.android.car.updatable"} for
48      * car service created from AOSP build. It can be set to the different package name depending on
49      * who is signing the car framework apex module.
50      */
51     public static final String PROPERTY_CAR_SERVICE_PACKAGE_NAME =
52             "ro.android.car.carservice.package";
53 
54     /**
55      * Read only property which contains semicolon (;) separated list of RRO packages.
56      *
57      * <p>
58      * RRO packages would be enabled if they are overlaying {@code CarServiceUpdatable}.
59      * {@code CarServiceUpdatable} can have different package names and this property may include
60      * all RROs to cover different {@code CarServiceUpdatable} package names but only those
61      * overriding the current {@code CarServiceUpdatable} package name will be selected.
62      */
63     public static final String PROPERTY_CAR_SERVICE_OVERLAY_PACKAGES =
64             "ro.android.car.carservice.overlay.packages";
65 
PackageManagerHelper()66     private PackageManagerHelper() {
67         throw new UnsupportedOperationException("provides only static methods");
68     }
69 
70     /**
71      * Gets the name of the {@code SystemUI} package.
72      * @param context
73      * @return
74      */
75     @NonNull
getSystemUiPackageName(@onNull Context context)76     public static String getSystemUiPackageName(@NonNull Context context) {
77         // TODO(157082995): This information can be taken from
78         // PackageManageInternalImpl.getSystemUiServiceComponent()
79         String flattenName = context.getResources()
80                 .getString(com.android.internal.R.string.config_systemUIServiceComponent);
81         if (TextUtils.isEmpty(flattenName)) {
82             throw new IllegalStateException("No "
83                     + "com.android.internal.R.string.config_systemUIServiceComponent resource");
84         }
85         try {
86             ComponentName componentName = ComponentName.unflattenFromString(flattenName);
87             return componentName.getPackageName();
88         } catch (RuntimeException e) {
89             throw new IllegalStateException("Invalid component name defined by "
90                     + "com.android.internal.R.string.config_systemUIServiceComponent resource: "
91                     + flattenName, e);
92         }
93     }
94 
95     /** Check {@link PackageManager#getPackageInfoAsUser(String, int, int)}. */
getPackageInfoAsUser(@onNull PackageManager pm, @NonNull String packageName, int packageInfoFlags, @UserIdInt int userId)96     public static PackageInfo getPackageInfoAsUser(@NonNull PackageManager pm,
97             @NonNull String packageName, int packageInfoFlags,
98             @UserIdInt int userId) throws PackageManager.NameNotFoundException {
99         return pm.getPackageInfoAsUser(packageName, packageInfoFlags, userId);
100     }
101 
102     /** Check {@link PackageManager#getPackageUidAsUser(String, int)}. */
getPackageUidAsUser(@onNull PackageManager pm, @NonNull String packageName, @UserIdInt int userId)103     public static int getPackageUidAsUser(@NonNull PackageManager pm, @NonNull String packageName,
104             @UserIdInt int userId) throws PackageManager.NameNotFoundException {
105         return pm.getPackageUidAsUser(packageName, userId);
106     }
107 
108     /** Check {@link PackageManager#getNamesForUids(int[])}. */
109     @Nullable
getNamesForUids(@onNull PackageManager pm, int[] uids)110     public static String[] getNamesForUids(@NonNull PackageManager pm, int[] uids) {
111         return pm.getNamesForUids(uids);
112     }
113 
114     /** Check {@link PackageManager#getApplicationEnabledSetting(String)}. */
getApplicationEnabledSettingForUser(@onNull String packageName, @UserIdInt int userId)115     public static int getApplicationEnabledSettingForUser(@NonNull String packageName,
116             @UserIdInt int userId) throws RemoteException {
117         IPackageManager pm = ActivityThread.getPackageManager();
118         return pm.getApplicationEnabledSetting(packageName, userId);
119     }
120 
121     /** Check {@link PackageManager#setApplicationEnabledSetting(String, int, int)}. */
setApplicationEnabledSettingForUser(@onNull String packageName, @PackageManager.EnabledState int newState, @PackageManager.EnabledFlags int flags, @UserIdInt int userId, @NonNull String callingPackage)122     public static void setApplicationEnabledSettingForUser(@NonNull String packageName,
123             @PackageManager.EnabledState int newState, @PackageManager.EnabledFlags int flags,
124             @UserIdInt int userId, @NonNull String callingPackage) throws RemoteException {
125         IPackageManager pm = ActivityThread.getPackageManager();
126         pm.setApplicationEnabledSetting(packageName, newState, flags, userId, callingPackage);
127     }
128 
129     /** Tells if the passed app is OEM app or not. */
isOemApp(@onNull ApplicationInfo appInfo)130     public static boolean isOemApp(@NonNull ApplicationInfo appInfo) {
131         return (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_OEM) != 0;
132     }
133 
134     /** Tells if the passed app is ODM app or not. */
isOdmApp(@onNull ApplicationInfo appInfo)135     public static boolean isOdmApp(@NonNull ApplicationInfo appInfo) {
136         return (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_ODM) != 0;
137     }
138 
139     /** Tells if the passed app is vendor app or not. */
isVendorApp(@onNull ApplicationInfo appInfo)140     public static boolean isVendorApp(@NonNull ApplicationInfo appInfo) {
141         return (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_VENDOR) != 0;
142     }
143 
144     /** Tells if the passed app is system app or not. */
isSystemApp(@onNull ApplicationInfo appInfo)145     public static boolean isSystemApp(@NonNull ApplicationInfo appInfo) {
146         return (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
147     }
148 
149     /** Tells if the passed app is updated system app or not. */
isUpdatedSystemApp(@onNull ApplicationInfo appInfo)150     public static boolean isUpdatedSystemApp(@NonNull ApplicationInfo appInfo) {
151         return (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
152     }
153 
154     /** Tells if the passed app is product app or not. */
isProductApp(@onNull ApplicationInfo appInfo)155     public static boolean isProductApp(@NonNull ApplicationInfo appInfo) {
156         return (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRODUCT) != 0;
157     }
158 
159     /** Tells if the passed app is system ext vendor app or not. */
isSystemExtApp(@onNull ApplicationInfo appInfo)160     public static boolean isSystemExtApp(@NonNull ApplicationInfo appInfo) {
161         return (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_SYSTEM_EXT) != 0;
162     }
163 
164     /** Check {@link ComponentInfo#getComponentName()}. */
getComponentName(ComponentInfo info)165     public static ComponentName getComponentName(ComponentInfo info) {
166         return info.getComponentName();
167     }
168 
169     /** Check PackageManagerInternal#getSystemUiServiceComponent(). */
170     @NonNull
getSystemUiServiceComponent(@onNull Context context)171     public static ComponentName getSystemUiServiceComponent(@NonNull Context context) {
172         String flattenName = context.getResources()
173                 .getString(com.android.internal.R.string.config_systemUIServiceComponent);
174         if (TextUtils.isEmpty(flattenName)) {
175             throw new IllegalStateException("No "
176                     + "com.android.internal.R.string.config_systemUIServiceComponent resource");
177         }
178         return ComponentName.unflattenFromString(flattenName);
179     }
180 
181     /** Check {@link ActivityManager#forceStopPackageAsUser}. */
forceStopPackageAsUser(@onNull Context context, @NonNull String packageName, @UserIdInt int userId)182     public static void forceStopPackageAsUser(@NonNull Context context, @NonNull String packageName,
183             @UserIdInt int userId) {
184         ActivityManager am = context.getSystemService(ActivityManager.class);
185         am.forceStopPackageAsUser(packageName, userId);
186     }
187 
188     /** Check {@link ActivityManager#forceStopPackageAsUserEvenWhenStopping}. */
forceStopPackageAsUserEvenWhenStopping(@onNull Context context, @NonNull String packageName, @UserIdInt int userId)189     public static void forceStopPackageAsUserEvenWhenStopping(@NonNull Context context,
190             @NonNull String packageName, @UserIdInt int userId) {
191         ActivityManager am = context.getSystemService(ActivityManager.class);
192         am.forceStopPackageAsUserEvenWhenStopping(packageName, userId);
193     }
194 }
195