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