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.server.pm;
18 
19 
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.UserIdInt;
23 import android.content.ComponentName;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.pm.ActivityInfo;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.ComponentInfo;
29 import android.content.pm.InstallSourceInfo;
30 import android.content.pm.InstrumentationInfo;
31 import android.content.pm.KeySet;
32 import android.content.pm.PackageInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.PackageManagerInternal;
35 import android.content.pm.ParceledListSlice;
36 import android.content.pm.ProcessInfo;
37 import android.content.pm.ProviderInfo;
38 import android.content.pm.ResolveInfo;
39 import android.content.pm.ServiceInfo;
40 import android.content.pm.SharedLibraryInfo;
41 import android.content.pm.SigningDetails;
42 import android.content.pm.UserInfo;
43 import android.content.pm.VersionedPackage;
44 import android.os.UserHandle;
45 import android.util.ArrayMap;
46 import android.util.ArraySet;
47 import android.util.Pair;
48 import android.util.SparseArray;
49 import android.util.proto.ProtoOutputStream;
50 
51 import com.android.internal.annotations.VisibleForTesting;
52 import com.android.server.pm.pkg.AndroidPackage;
53 import com.android.server.pm.pkg.PackageState;
54 import com.android.server.pm.pkg.PackageStateInternal;
55 import com.android.server.pm.pkg.SharedUserApi;
56 import com.android.server.pm.resolution.ComponentResolverApi;
57 import com.android.server.pm.snapshot.PackageDataSnapshot;
58 import com.android.server.utils.WatchedArrayMap;
59 import com.android.server.utils.WatchedLongSparseArray;
60 
61 import java.io.FileDescriptor;
62 import java.io.PrintWriter;
63 import java.util.List;
64 import java.util.Set;
65 
66 /**
67  * A {@link Computer} provides a set of functions that can operate on live data or snapshot
68  * data.  At this time, the {@link Computer} is implemented by the
69  * {@link ComputerEngine}, which is in turn extended by {@link ComputerLocked}.
70  *
71  * New functions must be added carefully.
72  * <ol>
73  * <li> New functions must be true functions with respect to data collected in a
74  * {@link PackageManagerService.Snapshot}.  Such data may never be modified from inside a {@link Computer}
75  * function.
76  * </li>
77  *
78  * <li> A new function must be implemented in {@link ComputerEngine}.
79  * </li>
80  *
81  * <li> A new function must be overridden in {@link ComputerLocked} if the function
82  * cannot safely access live data without holding the PackageManagerService lock.  The
83  * form of the {@link ComputerLocked} function must be a single call to the
84  * {@link ComputerEngine} implementation, wrapped in a <code>synchronized</code>
85  * block.  Functions in {@link ComputerLocked} should never include any other code.
86  * </li>
87  *
88  * Care must be taken when deciding if a function should be overridden in
89  * {@link ComputerLocked}.  The complex lock relationships of PackageManagerService
90  * and other managers (like PermissionManager) mean deadlock is possible.  On the
91  * other hand, not overriding in {@link ComputerLocked} may leave a function walking
92  * unstable data.
93  */
94 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
95 public interface Computer extends PackageDataSnapshot {
96 
getVersion()97     int getVersion();
98 
99     /**
100      * Administrative statistics: record that the snapshot has been used.  Every call
101      * to use() increments the usage counter.
102      */
use()103     Computer use();
104     /**
105      * Fetch the snapshot usage counter.
106      * @return The number of times this snapshot was used.
107      */
getUsed()108     default int getUsed() {
109         return 0;
110     }
queryIntentActivitiesInternal( Intent intent, String resolvedType, @PackageManager.ResolveInfoFlagsBits long flags, @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags, int filterCallingUid, int callingPid, int userId, boolean resolveForStart, boolean allowDynamicSplits)111     @NonNull List<ResolveInfo> queryIntentActivitiesInternal(
112             Intent intent, String resolvedType,
113             @PackageManager.ResolveInfoFlagsBits long flags,
114             @PackageManagerInternal.PrivateResolveFlags long privateResolveFlags,
115             int filterCallingUid, int callingPid, int userId,
116             boolean resolveForStart, boolean allowDynamicSplits);
queryIntentActivitiesInternal(Intent intent, String resolvedType, long flags, int filterCallingUid, int userId)117     @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent, String resolvedType,
118             long flags, int filterCallingUid, int userId);
queryIntentActivitiesInternal(Intent intent, String resolvedType, long flags, int userId)119     @NonNull List<ResolveInfo> queryIntentActivitiesInternal(Intent intent, String resolvedType,
120             long flags, int userId);
queryIntentServicesInternal( Intent intent, String resolvedType, long flags, int userId, int callingUid, int callingPid, boolean includeInstantApps, boolean resolveForStart)121     @NonNull List<ResolveInfo> queryIntentServicesInternal(
122             Intent intent, String resolvedType, long flags,
123             int userId, int callingUid, int callingPid,
124             boolean includeInstantApps, boolean resolveForStart);
queryIntentActivitiesInternalBody(Intent intent, String resolvedType, long flags, int filterCallingUid, int userId, boolean resolveForStart, boolean allowDynamicSplits, String pkgName, String instantAppPkgName)125     @NonNull QueryIntentActivitiesResult queryIntentActivitiesInternalBody(Intent intent,
126             String resolvedType, long flags, int filterCallingUid, int userId,
127             boolean resolveForStart, boolean allowDynamicSplits, String pkgName,
128             String instantAppPkgName);
getActivityInfo(ComponentName component, long flags, int userId)129     ActivityInfo getActivityInfo(ComponentName component, long flags, int userId);
130 
131     /**
132      * Similar to {@link Computer#getActivityInfo(android.content.ComponentName, long, int)} but
133      * only visible as internal service. This method bypass INTERACT_ACROSS_USERS or
134      * INTERACT_ACROSS_USERS_FULL permission checks and only to be used for intent resolution across
135      * chained cross profiles
136      */
getActivityInfoCrossProfile(ComponentName component, long flags, int userId)137     ActivityInfo getActivityInfoCrossProfile(ComponentName component, long flags, int userId);
138 
139     /**
140      * Important: The provided filterCallingUid is used exclusively to filter out activities
141      * that can be seen based on user state. It's typically the original caller uid prior
142      * to clearing. Because it can only be provided by trusted code, its value can be
143      * trusted and will be used as-is; unlike userId which will be validated by this method.
144      */
getActivityInfoInternal(ComponentName component, long flags, int filterCallingUid, int userId)145     ActivityInfo getActivityInfoInternal(ComponentName component, long flags,
146             int filterCallingUid, int userId);
getPackage(String packageName)147     AndroidPackage getPackage(String packageName);
getPackage(int uid)148     AndroidPackage getPackage(int uid);
generateApplicationInfoFromSettings(String packageName, long flags, int filterCallingUid, int userId)149     ApplicationInfo generateApplicationInfoFromSettings(String packageName, long flags,
150             int filterCallingUid, int userId);
getApplicationInfo(String packageName, long flags, int userId)151     ApplicationInfo getApplicationInfo(String packageName, long flags, int userId);
152 
153     /**
154      * Important: The provided filterCallingUid is used exclusively to filter out applications
155      * that can be seen based on user state. It's typically the original caller uid prior
156      * to clearing. Because it can only be provided by trusted code, its value can be
157      * trusted and will be used as-is; unlike userId which will be validated by this method.
158      */
getApplicationInfoInternal(String packageName, long flags, int filterCallingUid, int userId)159     ApplicationInfo getApplicationInfoInternal(String packageName, long flags,
160             int filterCallingUid, int userId);
161 
162     /**
163      * Report the 'Home' activity which is currently set as "always use this one". If non is set
164      * then reports the most likely home activity or null if there are more than one.
165      */
getDefaultHomeActivity(int userId)166     ComponentName getDefaultHomeActivity(int userId);
getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates, int userId)167     ComponentName getHomeActivitiesAsUser(List<ResolveInfo> allHomeCandidates, int userId);
getCrossProfileDomainPreferredLpr(Intent intent, String resolvedType, long flags, int sourceUserId, int parentUserId)168     CrossProfileDomainInfo getCrossProfileDomainPreferredLpr(Intent intent, String resolvedType,
169             long flags, int sourceUserId, int parentUserId);
getHomeIntent()170     Intent getHomeIntent();
getMatchingCrossProfileIntentFilters(Intent intent, String resolvedType, int userId)171     List<CrossProfileIntentFilter> getMatchingCrossProfileIntentFilters(Intent intent,
172             String resolvedType, int userId);
173 
174     /**
175      * Filters out ephemeral activities.
176      * <p>When resolving for an ephemeral app, only activities that 1) are defined in the
177      * ephemeral app or 2) marked with {@code visibleToEphemeral} are returned.
178      *
179      * @param resolveInfos The pre-filtered list of resolved activities
180      * @param ephemeralPkgName The ephemeral package name. If {@code null}, no filtering
181      *          is performed.
182      * @param intent
183      * @return A filtered list of resolved activities.
184      */
applyPostResolutionFilter(@onNull List<ResolveInfo> resolveInfos, String ephemeralPkgName, boolean allowDynamicSplits, int filterCallingUid, boolean resolveForStart, int userId, Intent intent)185     List<ResolveInfo> applyPostResolutionFilter(@NonNull List<ResolveInfo> resolveInfos,
186             String ephemeralPkgName, boolean allowDynamicSplits, int filterCallingUid,
187             boolean resolveForStart, int userId, Intent intent);
generatePackageInfo(PackageStateInternal ps, long flags, int userId)188     PackageInfo generatePackageInfo(PackageStateInternal ps, long flags, int userId);
getPackageInfo(String packageName, long flags, int userId)189     PackageInfo getPackageInfo(String packageName, long flags, int userId);
getPackageInfoInternal(String packageName, long versionCode, long flags, int filterCallingUid, int userId)190     PackageInfo getPackageInfoInternal(String packageName, long versionCode, long flags,
191             int filterCallingUid, int userId);
192 
193     /**
194      * @return package names of all available {@link AndroidPackage} instances. This means any
195      * known {@link PackageState} instances without a {@link PackageState#getAndroidPackage()}
196      * will not be represented.
197      */
getAllAvailablePackageNames()198     String[] getAllAvailablePackageNames();
199 
getPackageStateInternal(String packageName)200     PackageStateInternal getPackageStateInternal(String packageName);
getPackageStateInternal(String packageName, int callingUid)201     PackageStateInternal getPackageStateInternal(String packageName, int callingUid);
getPackageStateFiltered(@onNull String packageName, int callingUid, @UserIdInt int userId)202     PackageStateInternal getPackageStateFiltered(@NonNull String packageName, int callingUid,
203             @UserIdInt int userId);
getInstalledPackages(long flags, int userId)204     ParceledListSlice<PackageInfo> getInstalledPackages(long flags, int userId);
createForwardingResolveInfoUnchecked(WatchedIntentFilter filter, int sourceUserId, int targetUserId)205     ResolveInfo createForwardingResolveInfoUnchecked(WatchedIntentFilter filter,
206             int sourceUserId, int targetUserId);
getServiceInfo(ComponentName component, long flags, int userId)207     ServiceInfo getServiceInfo(ComponentName component, long flags, int userId);
getSharedLibraryInfo(String name, long version)208     SharedLibraryInfo getSharedLibraryInfo(String name, long version);
getInstantAppPackageName(int callingUid)209     String getInstantAppPackageName(int callingUid);
resolveExternalPackageName(AndroidPackage pkg)210     String resolveExternalPackageName(AndroidPackage pkg);
resolveInternalPackageName(String packageName, long versionCode)211     String resolveInternalPackageName(String packageName, long versionCode);
getPackagesForUid(int uid)212     String[] getPackagesForUid(int uid);
getProfileParent(int userId)213     UserInfo getProfileParent(int userId);
canViewInstantApps(int callingUid, int userId)214     boolean canViewInstantApps(int callingUid, int userId);
filterSharedLibPackage(@ullable PackageStateInternal ps, int uid, int userId, long flags)215     boolean filterSharedLibPackage(@Nullable PackageStateInternal ps, int uid, int userId,
216             long flags);
isCallerSameApp(String packageName, int uid)217     boolean isCallerSameApp(String packageName, int uid);
218     /**
219      * Returns true if the package name and the uid represent the same app.
220      *
221      * @param resolveIsolatedUid if true, resolves an isolated uid into the real uid.
222      */
isCallerSameApp(String packageName, int uid, boolean resolveIsolatedUid)223     boolean isCallerSameApp(String packageName, int uid, boolean resolveIsolatedUid);
isComponentVisibleToInstantApp(@ullable ComponentName component)224     boolean isComponentVisibleToInstantApp(@Nullable ComponentName component);
isComponentVisibleToInstantApp(@ullable ComponentName component, @PackageManager.ComponentType int type)225     boolean isComponentVisibleToInstantApp(@Nullable ComponentName component,
226             @PackageManager.ComponentType int type);
227 
228     /**
229      * From Android R, camera intents have to match system apps. The only exception to this is if
230      * the DPC has set the camera persistent preferred activity. This case was introduced
231      * because it is important that the DPC has the ability to set both system and non-system
232      * camera persistent preferred activities.
233      *
234      * @return {@code true} if the intent is a camera intent and the persistent preferred
235      * activity was not set by the DPC.
236      */
isImplicitImageCaptureIntentAndNotSetByDpc(Intent intent, int userId, String resolvedType, long flags)237     boolean isImplicitImageCaptureIntentAndNotSetByDpc(Intent intent, int userId,
238             String resolvedType, long flags);
isInstantApp(String packageName, int userId)239     boolean isInstantApp(String packageName, int userId);
isInstantAppInternal(String packageName, @UserIdInt int userId, int callingUid)240     boolean isInstantAppInternal(String packageName, @UserIdInt int userId, int callingUid);
isSameProfileGroup(@serIdInt int callerUserId, @UserIdInt int userId)241     boolean isSameProfileGroup(@UserIdInt int callerUserId, @UserIdInt int userId);
shouldFilterApplication(@ullable PackageStateInternal ps, int callingUid, @Nullable ComponentName component, @PackageManager.ComponentType int componentType, int userId, boolean filterUninstall)242     boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid,
243             @Nullable ComponentName component, @PackageManager.ComponentType int componentType,
244             int userId, boolean filterUninstall);
shouldFilterApplication(@ullable PackageStateInternal ps, int callingUid, @Nullable ComponentName component, @PackageManager.ComponentType int componentType, int userId)245     boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid,
246             @Nullable ComponentName component, @PackageManager.ComponentType int componentType,
247             int userId);
shouldFilterApplication(@ullable PackageStateInternal ps, int callingUid, int userId)248     boolean shouldFilterApplication(@Nullable PackageStateInternal ps, int callingUid,
249             int userId);
shouldFilterApplication(@onNull SharedUserSetting sus, int callingUid, int userId)250     boolean shouldFilterApplication(@NonNull SharedUserSetting sus, int callingUid,
251             int userId);
252     /**
253      * Different form {@link #shouldFilterApplication(PackageStateInternal, int, int)}, the function
254      * returns {@code true} if the target package is not found in the device or uninstalled in the
255      * current user. Unless the caller's function needs to handle the package's uninstalled state
256      * by itself, using this function to keep the consistent behavior between conditions of package
257      * uninstalled and visibility not allowed to avoid the side channel leakage of package
258      * existence.
259      * <p>
260      * Package with {@link PackageManager#SYSTEM_APP_STATE_HIDDEN_UNTIL_INSTALLED_HIDDEN} is not
261      * treated as an uninstalled package for the carrier apps customization. Bypassing the
262      * uninstalled package check if the caller is system, shell or root uid.
263      */
shouldFilterApplicationIncludingUninstalled(@ullable PackageStateInternal ps, int callingUid, int userId)264     boolean shouldFilterApplicationIncludingUninstalled(@Nullable PackageStateInternal ps,
265             int callingUid, int userId);
266 
267     /**
268      * Different from
269      * {@link #shouldFilterApplicationIncludingUninstalled(PackageStateInternal, int, int)}, the
270      * function returns {@code true} if:
271      * <ul>
272      * <li>The target package is not archived.
273      * <li>The package cannot be found in the device or has been uninstalled in the current user.
274      * </ul>
275      */
shouldFilterApplicationIncludingUninstalledNotArchived( @ullable PackageStateInternal ps, int callingUid, int userId)276     boolean shouldFilterApplicationIncludingUninstalledNotArchived(
277             @Nullable PackageStateInternal ps,
278             int callingUid, int userId);
279     /**
280      * Different from {@link #shouldFilterApplication(SharedUserSetting, int, int)}, the function
281      * returns {@code true} if packages with the same shared user are all uninstalled in the current
282      * user.
283      *
284      * @see #shouldFilterApplicationIncludingUninstalled(PackageStateInternal, int, int)
285      */
shouldFilterApplicationIncludingUninstalled(@onNull SharedUserSetting sus, int callingUid, int userId)286     boolean shouldFilterApplicationIncludingUninstalled(@NonNull SharedUserSetting sus,
287             int callingUid, int userId);
checkUidPermission(String permName, int uid)288     int checkUidPermission(String permName, int uid);
getPackageUidInternal(String packageName, long flags, int userId, int callingUid)289     int getPackageUidInternal(String packageName, long flags, int userId, int callingUid);
updateFlagsForApplication(long flags, int userId)290     long updateFlagsForApplication(long flags, int userId);
updateFlagsForComponent(long flags, int userId)291     long updateFlagsForComponent(long flags, int userId);
updateFlagsForPackage(long flags, int userId)292     long updateFlagsForPackage(long flags, int userId);
293 
294     /**
295      * Update given flags when being used to request {@link ResolveInfo}.
296      * <p>Instant apps are resolved specially, depending upon context. Minimally,
297      * {@code}flags{@code} must have the {@link PackageManager#MATCH_INSTANT}
298      * flag set. However, this flag is only honoured in three circumstances:
299      * <ul>
300      * <li>when called from a system process</li>
301      * <li>when the caller holds the permission {@code android.permission.ACCESS_INSTANT_APPS}</li>
302      * <li>when resolution occurs to start an activity with a {@code android.intent.action.VIEW}
303      * action and a {@code android.intent.category.BROWSABLE} category</li>
304      * </ul>
305      */
updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps, boolean isImplicitImageCaptureIntentAndNotSetByDpc)306     long updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps,
307             boolean isImplicitImageCaptureIntentAndNotSetByDpc);
updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps, boolean onlyExposedExplicitly, boolean isImplicitImageCaptureIntentAndNotSetByDpc)308     long updateFlagsForResolve(long flags, int userId, int callingUid, boolean wantInstantApps,
309             boolean onlyExposedExplicitly, boolean isImplicitImageCaptureIntentAndNotSetByDpc);
310 
311     /**
312      * Checks if the request is from the system or an app that has the appropriate cross-user
313      * permissions defined as follows:
314      * <ul>
315      * <li>INTERACT_ACROSS_USERS_FULL if {@code requireFullPermission} is true.</li>
316      * <li>INTERACT_ACROSS_USERS if the given {@code userId} is in a different profile group
317      * to the caller.</li>
318      * <li>Otherwise, INTERACT_ACROSS_PROFILES if the given {@code userId} is in the same profile
319      * group as the caller.</li>
320      * </ul>
321      *
322      * @param checkShell whether to prevent shell from access if there's a debugging restriction
323      * @param message the message to log on security exception
324      */
enforceCrossUserOrProfilePermission(int callingUid, @UserIdInt int userId, boolean requireFullPermission, boolean checkShell, String message)325     void enforceCrossUserOrProfilePermission(int callingUid, @UserIdInt int userId,
326             boolean requireFullPermission, boolean checkShell, String message);
327 
328     /**
329      * Enforces the request is from the system or an app that has INTERACT_ACROSS_USERS
330      * or INTERACT_ACROSS_USERS_FULL permissions, if the {@code userId} is not for the caller.
331      *
332      * @param checkShell whether to prevent shell from access if there's a debugging restriction
333      * @param message the message to log on security exception
334      */
enforceCrossUserPermission(int callingUid, @UserIdInt int userId, boolean requireFullPermission, boolean checkShell, String message)335     void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
336             boolean requireFullPermission, boolean checkShell, String message);
enforceCrossUserPermission(int callingUid, @UserIdInt int userId, boolean requireFullPermission, boolean checkShell, boolean requirePermissionWhenSameUser, String message)337     void enforceCrossUserPermission(int callingUid, @UserIdInt int userId,
338             boolean requireFullPermission, boolean checkShell,
339             boolean requirePermissionWhenSameUser, String message);
getSigningDetails(@onNull String packageName)340     SigningDetails getSigningDetails(@NonNull String packageName);
getSigningDetails(int uid)341     SigningDetails getSigningDetails(int uid);
filterAppAccess(AndroidPackage pkg, int callingUid, int userId)342     boolean filterAppAccess(AndroidPackage pkg, int callingUid, int userId);
filterAppAccess(String packageName, int callingUid, int userId, boolean filterUninstalled)343     boolean filterAppAccess(String packageName, int callingUid, int userId,
344             boolean filterUninstalled);
filterAppAccess(int uid, int callingUid)345     boolean filterAppAccess(int uid, int callingUid);
dump(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState)346     void dump(int type, FileDescriptor fd, PrintWriter pw, DumpState dumpState);
findPreferredActivityInternal( Intent intent, String resolvedType, long flags, List<ResolveInfo> query, boolean always, boolean removeMatches, boolean debug, int userId, boolean queryMayBeFiltered)347     PackageManagerService.FindPreferredActivityBodyResult findPreferredActivityInternal(
348             Intent intent, String resolvedType, long flags, List<ResolveInfo> query, boolean always,
349             boolean removeMatches, boolean debug, int userId, boolean queryMayBeFiltered);
findPersistentPreferredActivity(Intent intent, String resolvedType, long flags, List<ResolveInfo> query, boolean debug, int userId)350     ResolveInfo findPersistentPreferredActivity(Intent intent, String resolvedType, long flags,
351             List<ResolveInfo> query, boolean debug, int userId);
352 
getPreferredActivities(@serIdInt int userId)353     PreferredIntentResolver getPreferredActivities(@UserIdInt int userId);
354 
355     @NonNull
getPackageStates()356     ArrayMap<String, ? extends PackageStateInternal> getPackageStates();
357 
358     @NonNull
getDisabledSystemPackageStates()359     ArrayMap<String, ? extends PackageStateInternal> getDisabledSystemPackageStates();
360 
361     @Nullable
getRenamedPackage(@onNull String packageName)362     String getRenamedPackage(@NonNull String packageName);
363 
364     /**
365      * @return set of packages to notify
366      */
367     @NonNull
getNotifyPackagesForReplacedReceived(@onNull String[] packages)368     ArraySet<String> getNotifyPackagesForReplacedReceived(@NonNull String[] packages);
369 
370     @PackageManagerService.PackageStartability
getPackageStartability(boolean safeMode, @NonNull String packageName, int callingUid, @UserIdInt int userId)371     int getPackageStartability(boolean safeMode, @NonNull String packageName, int callingUid,
372             @UserIdInt int userId);
373 
isPackageAvailable(String packageName, @UserIdInt int userId)374     boolean isPackageAvailable(String packageName, @UserIdInt int userId);
375 
isApexPackage(String packageName)376     boolean isApexPackage(String packageName);
377 
378     @NonNull
currentToCanonicalPackageNames(@onNull String[] names)379     String[] currentToCanonicalPackageNames(@NonNull String[] names);
380 
381     @NonNull
canonicalToCurrentPackageNames(@onNull String[] names)382     String[] canonicalToCurrentPackageNames(@NonNull String[] names);
383 
384     @NonNull
getPackageGids(@onNull String packageName, @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId)385     int[] getPackageGids(@NonNull String packageName,
386             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
387 
getTargetSdkVersion(@onNull String packageName)388     int getTargetSdkVersion(@NonNull String packageName);
389 
activitySupportsIntentAsUser(@onNull ComponentName resolveComponentName, @NonNull ComponentName component, @NonNull Intent intent, String resolvedType, int userId)390     boolean activitySupportsIntentAsUser(@NonNull ComponentName resolveComponentName,
391             @NonNull ComponentName component, @NonNull Intent intent, String resolvedType,
392             int userId);
393 
394     @Nullable
getReceiverInfo(@onNull ComponentName component, @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId)395     ActivityInfo getReceiverInfo(@NonNull ComponentName component,
396             @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId);
397 
398     @Nullable
getSharedLibraries(@onNull String packageName, @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId)399     ParceledListSlice<SharedLibraryInfo> getSharedLibraries(@NonNull String packageName,
400             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
401 
canRequestPackageInstalls(@onNull String packageName, int callingUid, int userId, boolean throwIfPermNotDeclared)402     boolean canRequestPackageInstalls(@NonNull String packageName, int callingUid,
403             int userId, boolean throwIfPermNotDeclared);
404 
405     /**
406      * Returns true if the system or user is explicitly preventing an otherwise valid installer to
407      * complete an install. This includes checks like unknown sources and user restrictions.
408      */
isInstallDisabledForPackage(@onNull String packageName, int uid, @UserIdInt int userId)409     boolean isInstallDisabledForPackage(@NonNull String packageName, int uid,
410             @UserIdInt int userId);
411 
412     /**
413      * Returns a Pair that contains a list of packages that depend on the target library and the
414      * package library dependency information. The List&lt;VersionedPackage&gt; indicates a list of
415      * packages that depend on the target library, it may be null if no package depends on
416      * the target library. The List&lt;Boolean&gt; indicates whether each VersionedPackage in
417      * the List&lt;VersionedPackage&gt; optionally depends on the target library, where true means
418      * optional and false means required. It may be null if no package depends on
419      * the target library or without dependency information, e.g. uses-static-library.
420      */
421     @NonNull
getPackagesUsingSharedLibrary( @onNull SharedLibraryInfo libInfo, @PackageManager.PackageInfoFlagsBits long flags, int callingUid, @UserIdInt int userId)422     Pair<List<VersionedPackage>, List<Boolean>> getPackagesUsingSharedLibrary(
423             @NonNull SharedLibraryInfo libInfo, @PackageManager.PackageInfoFlagsBits long flags,
424             int callingUid, @UserIdInt int userId);
425 
426     @Nullable
getDeclaredSharedLibraries( @onNull String packageName, @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId)427     ParceledListSlice<SharedLibraryInfo> getDeclaredSharedLibraries(
428             @NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
429             @UserIdInt int userId);
430 
431     @Nullable
getProviderInfo(@onNull ComponentName component, @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId)432     ProviderInfo getProviderInfo(@NonNull ComponentName component,
433             @PackageManager.ComponentInfoFlagsBits long flags, @UserIdInt int userId);
434 
getSystemSharedLibraryNamesAndPaths()435     ArrayMap<String, String> getSystemSharedLibraryNamesAndPaths();
436 
437     /**
438      * @return the state if the given package is installed and isn't filtered by visibility.
439      * Provides no guarantee that the package is in any usable state.
440      */
441     @Nullable
getPackageStateForInstalledAndFiltered(@onNull String packageName, int callingUid, @UserIdInt int userId)442     PackageStateInternal getPackageStateForInstalledAndFiltered(@NonNull String packageName,
443             int callingUid, @UserIdInt int userId);
444 
checkSignatures(@onNull String pkg1, @NonNull String pkg2, int userId)445     int checkSignatures(@NonNull String pkg1, @NonNull String pkg2, int userId);
446 
checkUidSignatures(int uid1, int uid2)447     int checkUidSignatures(int uid1, int uid2);
448 
checkUidSignaturesForAllUsers(int uid1, int uid2)449     int checkUidSignaturesForAllUsers(int uid1, int uid2);
450 
hasSigningCertificate(@onNull String packageName, @NonNull byte[] certificate, @PackageManager.CertificateInputType int type)451     boolean hasSigningCertificate(@NonNull String packageName, @NonNull byte[] certificate,
452             @PackageManager.CertificateInputType int type);
453 
hasUidSigningCertificate(int uid, @NonNull byte[] certificate, @PackageManager.CertificateInputType int type)454     boolean hasUidSigningCertificate(int uid, @NonNull byte[] certificate,
455             @PackageManager.CertificateInputType int type);
456 
457     @NonNull
getAllPackages()458     List<String> getAllPackages();
459 
460     @Nullable
getNameForUid(int uid)461     String getNameForUid(int uid);
462 
463     @Nullable
getNamesForUids(@onNull int[] uids)464     String[] getNamesForUids(@NonNull int[] uids);
465 
getUidForSharedUser(@onNull String sharedUserName)466     int getUidForSharedUser(@NonNull String sharedUserName);
467 
getFlagsForUid(int uid)468     int getFlagsForUid(int uid);
469 
getPrivateFlagsForUid(int uid)470     int getPrivateFlagsForUid(int uid);
471 
isUidPrivileged(int uid)472     boolean isUidPrivileged(int uid);
473 
474     @NonNull
getAppOpPermissionPackages(@onNull String permissionName, int userId)475     String[] getAppOpPermissionPackages(@NonNull String permissionName, int userId);
476 
477     @NonNull
getPackagesHoldingPermissions(@onNull String[] permissions, @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId)478     ParceledListSlice<PackageInfo> getPackagesHoldingPermissions(@NonNull String[] permissions,
479             @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId);
480 
481     @NonNull
getInstalledApplications( @ackageManager.ApplicationInfoFlagsBits long flags, @UserIdInt int userId, int callingUid, boolean forceAllowCrossUser)482     List<ApplicationInfo> getInstalledApplications(
483             @PackageManager.ApplicationInfoFlagsBits long flags, @UserIdInt int userId,
484             int callingUid, boolean forceAllowCrossUser);
485 
486     @Nullable
resolveContentProvider(@onNull String name, @PackageManager.ResolveInfoFlagsBits long flags, @UserIdInt int userId, int callingUid)487     ProviderInfo resolveContentProvider(@NonNull String name,
488             @PackageManager.ResolveInfoFlagsBits long flags, @UserIdInt int userId, int callingUid);
489 
490     @Nullable
getGrantImplicitAccessProviderInfo(int recipientUid, @NonNull String visibleAuthority)491     ProviderInfo getGrantImplicitAccessProviderInfo(int recipientUid,
492             @NonNull String visibleAuthority);
493 
querySyncProviders(boolean safeMode, @NonNull List<String> outNames, @NonNull List<ProviderInfo> outInfo)494     void querySyncProviders(boolean safeMode, @NonNull List<String> outNames,
495             @NonNull List<ProviderInfo> outInfo);
496 
497     @NonNull
queryContentProviders(@ullable String processName, int uid, @PackageManager.ComponentInfoFlagsBits long flags, @Nullable String metaDataKey)498     ParceledListSlice<ProviderInfo> queryContentProviders(@Nullable String processName, int uid,
499             @PackageManager.ComponentInfoFlagsBits long flags, @Nullable String metaDataKey);
500 
501     @Nullable
getInstrumentationInfoAsUser(@onNull ComponentName component, int flags, int userId)502     InstrumentationInfo getInstrumentationInfoAsUser(@NonNull ComponentName component, int flags,
503             int userId);
504 
505     @NonNull
queryInstrumentationAsUser( @onNull String targetPackage, int flags, int userId)506     ParceledListSlice<InstrumentationInfo> queryInstrumentationAsUser(
507             @NonNull String targetPackage, int flags, int userId);
508 
509     @NonNull
findSharedNonSystemLibraries( @onNull PackageStateInternal pkgSetting)510     List<PackageStateInternal> findSharedNonSystemLibraries(
511             @NonNull PackageStateInternal pkgSetting);
512 
getApplicationHiddenSettingAsUser(@onNull String packageName, @UserIdInt int userId)513     boolean getApplicationHiddenSettingAsUser(@NonNull String packageName, @UserIdInt int userId);
514 
isPackageSuspendedForUser(@onNull String packageName, @UserIdInt int userId)515     boolean isPackageSuspendedForUser(@NonNull String packageName, @UserIdInt int userId)
516             throws PackageManager.NameNotFoundException;
517 
isPackageQuarantinedForUser(@onNull String packageName, @UserIdInt int userId)518     boolean isPackageQuarantinedForUser(@NonNull String packageName, @UserIdInt int userId)
519             throws PackageManager.NameNotFoundException;
520 
521     /** Check if the package is in a stopped state for a given user. */
isPackageStoppedForUser(@onNull String packageName, @UserIdInt int userId)522     boolean isPackageStoppedForUser(@NonNull String packageName, @UserIdInt int userId)
523             throws PackageManager.NameNotFoundException;
524 
525     /** Check if the package is suspending any package. */
isSuspendingAnyPackages(@onNull String suspendingPackage, @UserIdInt int suspendingUserId, int targetUserId)526     boolean isSuspendingAnyPackages(@NonNull String suspendingPackage,
527             @UserIdInt int suspendingUserId, int targetUserId);
528 
529     @NonNull
getAllIntentFilters(@onNull String packageName)530     ParceledListSlice<IntentFilter> getAllIntentFilters(@NonNull String packageName);
531 
getBlockUninstallForUser(@onNull String packageName, @UserIdInt int userId)532     boolean getBlockUninstallForUser(@NonNull String packageName, @UserIdInt int userId);
533 
534     @Nullable
getInstallerPackageName(@onNull String packageName, @UserIdInt int userId)535     String getInstallerPackageName(@NonNull String packageName, @UserIdInt int userId);
536 
537     @Nullable
getInstallSourceInfo(@onNull String packageName, @UserIdInt int userId)538     InstallSourceInfo getInstallSourceInfo(@NonNull String packageName, @UserIdInt int userId);
539 
540     @PackageManager.EnabledState
getApplicationEnabledSetting(@onNull String packageName, @UserIdInt int userId)541     int getApplicationEnabledSetting(@NonNull String packageName, @UserIdInt int userId);
542 
543     @PackageManager.EnabledState
getComponentEnabledSetting(@onNull ComponentName component, int callingUid, @UserIdInt int userId)544     int getComponentEnabledSetting(@NonNull ComponentName component, int callingUid,
545             @UserIdInt int userId);
546 
547     @PackageManager.EnabledState
getComponentEnabledSettingInternal(@onNull ComponentName component, int callingUid, @UserIdInt int userId)548     int getComponentEnabledSettingInternal(@NonNull ComponentName component, int callingUid,
549             @UserIdInt int userId);
550 
551     /**
552      * @return true if the runtime app user enabled state, runtime component user enabled state,
553      * install-time app manifest enabled state, and install-time component manifest enabled state
554      * are all effectively enabled for the given component. Or if the component cannot be found,
555      * returns false.
556      */
isComponentEffectivelyEnabled(@onNull ComponentInfo componentInfo, @NonNull UserHandle userHandle)557     boolean isComponentEffectivelyEnabled(@NonNull ComponentInfo componentInfo,
558             @NonNull UserHandle userHandle);
559 
560     /**
561      * @return true if the runtime app user enabled state and the install-time app manifest enabled
562      * state are both effectively enabled for the given app. Or if the app cannot be found,
563      * returns false.
564      */
isApplicationEffectivelyEnabled(@onNull String packageName, @NonNull UserHandle userHandle)565     boolean isApplicationEffectivelyEnabled(@NonNull String packageName,
566             @NonNull UserHandle userHandle);
567 
568     @Nullable
getKeySetByAlias(@onNull String packageName, @NonNull String alias)569     KeySet getKeySetByAlias(@NonNull String packageName, @NonNull String alias);
570 
571     @Nullable
getSigningKeySet(@onNull String packageName)572     KeySet getSigningKeySet(@NonNull String packageName);
573 
isPackageSignedByKeySet(@onNull String packageName, @NonNull KeySet ks)574     boolean isPackageSignedByKeySet(@NonNull String packageName, @NonNull KeySet ks);
575 
isPackageSignedByKeySetExactly(@onNull String packageName, @NonNull KeySet ks)576     boolean isPackageSignedByKeySetExactly(@NonNull String packageName, @NonNull KeySet ks);
577 
578     /**
579      * See {@link AppsFilterSnapshot#getVisibilityAllowList(PackageStateInternal, int[], ArrayMap)}
580      */
581     @Nullable
getVisibilityAllowLists(@onNull String packageName, @UserIdInt int[] userIds)582     SparseArray<int[]> getVisibilityAllowLists(@NonNull String packageName,
583             @UserIdInt int[] userIds);
584 
585     /**
586      * See {@link AppsFilterSnapshot#getVisibilityAllowList(PackageStateInternal, int[], ArrayMap)}
587      */
588     @Nullable
getVisibilityAllowList(@onNull String packageName, @UserIdInt int userId)589     int[] getVisibilityAllowList(@NonNull String packageName, @UserIdInt int userId);
590 
591     /**
592      * Returns whether the given UID either declares &lt;queries&gt; element with the given package
593      * name in its app's manifest, has {@link android.Manifest.permission.QUERY_ALL_PACKAGES}, or
594      * package visibility filtering is enabled on it. If the UID is part of a shared user ID,
595      * return {@code true} if any one application belongs to the shared user ID meets the criteria.
596      */
canQueryPackage(int callingUid, @Nullable String targetPackageName)597     boolean canQueryPackage(int callingUid, @Nullable String targetPackageName);
598 
getPackageUid(@onNull String packageName, @PackageManager.PackageInfoFlagsBits long flags, @UserIdInt int userId)599     int getPackageUid(@NonNull String packageName, @PackageManager.PackageInfoFlagsBits long flags,
600             @UserIdInt int userId);
601 
canAccessComponent(int callingUid, @NonNull ComponentName component, @UserIdInt int userId)602     boolean canAccessComponent(int callingUid, @NonNull ComponentName component,
603             @UserIdInt int userId);
604 
isCallerInstallerOfRecord(@onNull AndroidPackage pkg, int callingUid)605     boolean isCallerInstallerOfRecord(@NonNull AndroidPackage pkg, int callingUid);
606 
607     @PackageManager.InstallReason
getInstallReason(@onNull String packageName, @UserIdInt int userId)608     int getInstallReason(@NonNull String packageName, @UserIdInt int userId);
609 
610     @NonNull
canPackageQuery(@onNull String sourcePackageName, @NonNull String[] targetPackageNames, @UserIdInt int userId)611     boolean[] canPackageQuery(@NonNull String sourcePackageName,
612             @NonNull String[] targetPackageNames, @UserIdInt int userId);
613 
canForwardTo(@onNull Intent intent, @Nullable String resolvedType, @UserIdInt int sourceUserId, @UserIdInt int targetUserId)614     boolean canForwardTo(@NonNull Intent intent, @Nullable String resolvedType,
615             @UserIdInt int sourceUserId, @UserIdInt int targetUserId);
616 
617     @NonNull
getPersistentApplications(boolean safeMode, int flags)618     List<ApplicationInfo> getPersistentApplications(boolean safeMode, int flags);
619 
620     @NonNull
getAppsWithSharedUserIds()621     SparseArray<String> getAppsWithSharedUserIds();
622 
623     @NonNull
getSharedUserPackagesForPackage(@onNull String packageName, @UserIdInt int userId)624     String[] getSharedUserPackagesForPackage(@NonNull String packageName, @UserIdInt int userId);
625 
626     @NonNull
getUnusedPackages(long downgradeTimeThresholdMillis)627     Set<String> getUnusedPackages(long downgradeTimeThresholdMillis);
628 
629     @Nullable
getHarmfulAppWarning(@onNull String packageName, @UserIdInt int userId)630     CharSequence getHarmfulAppWarning(@NonNull String packageName, @UserIdInt int userId);
631 
632     /**
633      * Only keep package names that refer to {@link PackageState#isSystem system} packages.
634      *
635      * @param pkgNames The packages to filter
636      *
637      * @return The filtered packages
638      */
639     @NonNull
filterOnlySystemPackages(@ullable String... pkgNames)640     String[] filterOnlySystemPackages(@Nullable String... pkgNames);
641 
642     // The methods in this block should be removed once SettingBase is interface snapshotted
643     @NonNull
getPackagesForAppId(int appId)644     List<AndroidPackage> getPackagesForAppId(int appId);
645 
getUidTargetSdkVersion(int uid)646     int getUidTargetSdkVersion(int uid);
647 
648     /**
649      * @see PackageManagerInternal#getProcessesForUid(int)
650      */
651     @Nullable
getProcessesForUid(int uid)652     ArrayMap<String, ProcessInfo> getProcessesForUid(int uid);
653     // End block
654 
getBlockUninstall(@serIdInt int userId, @NonNull String packageName)655     boolean getBlockUninstall(@UserIdInt int userId, @NonNull String packageName);
656 
657     @NonNull
getSharedLibraries()658     WatchedArrayMap<String, WatchedLongSparseArray<SharedLibraryInfo>> getSharedLibraries();
659 
660 
661     @Nullable
getPackageOrSharedUser(int appId)662     Pair<PackageStateInternal, SharedUserApi> getPackageOrSharedUser(int appId);
663 
664     @Nullable
getSharedUser(int sharedUserAppIde)665     SharedUserApi getSharedUser(int sharedUserAppIde);
666 
667     @NonNull
getSharedUserPackages(int sharedUserAppId)668     ArraySet<PackageStateInternal> getSharedUserPackages(int sharedUserAppId);
669 
670     @NonNull
getComponentResolver()671     ComponentResolverApi getComponentResolver();
672 
673     @Nullable
getDisabledSystemPackage(@onNull String packageName)674     PackageStateInternal getDisabledSystemPackage(@NonNull String packageName);
675 
676     @Nullable
getInstantAppInstallerInfo()677     ResolveInfo getInstantAppInstallerInfo();
678 
679     @NonNull
getFrozenPackages()680     WatchedArrayMap<String, Integer> getFrozenPackages();
681 
682     /**
683      * Verify that given package is currently frozen.
684      */
checkPackageFrozen(@onNull String packageName)685     void checkPackageFrozen(@NonNull String packageName);
686 
687     @Nullable
getInstantAppInstallerComponent()688     ComponentName getInstantAppInstallerComponent();
689 
dumpPermissions(@onNull PrintWriter pw, @NonNull String packageName, @NonNull ArraySet<String> permissionNames, @NonNull DumpState dumpState)690     void dumpPermissions(@NonNull PrintWriter pw, @NonNull String packageName,
691             @NonNull ArraySet<String> permissionNames, @NonNull DumpState dumpState);
692 
dumpPackages(PrintWriter pw, @NonNull String packageName, @NonNull ArraySet<String> permissionNames, @NonNull DumpState dumpState, boolean checkin)693     void dumpPackages(PrintWriter pw, @NonNull String packageName,
694             @NonNull ArraySet<String> permissionNames, @NonNull DumpState dumpState,
695             boolean checkin);
696 
dumpKeySet(@onNull PrintWriter pw, @NonNull String packageName, @NonNull DumpState dumpState)697     void dumpKeySet(@NonNull PrintWriter pw, @NonNull String packageName,
698             @NonNull DumpState dumpState);
699 
dumpSharedUsers(@onNull PrintWriter pw, @NonNull String packageName, @NonNull ArraySet<String> permissionNames, @NonNull DumpState dumpState, boolean checkin)700     void dumpSharedUsers(@NonNull PrintWriter pw, @NonNull String packageName,
701             @NonNull ArraySet<String> permissionNames,
702             @NonNull DumpState dumpState, boolean checkin);
703 
dumpSharedUsersProto(@onNull ProtoOutputStream proto)704     void dumpSharedUsersProto(@NonNull ProtoOutputStream proto);
705 
dumpPackagesProto(@onNull ProtoOutputStream proto)706     void dumpPackagesProto(@NonNull ProtoOutputStream proto);
707 
dumpSharedLibrariesProto(@onNull ProtoOutputStream protoOutputStream)708     void dumpSharedLibrariesProto(@NonNull ProtoOutputStream protoOutputStream);
709 
710     @NonNull
getVolumePackages(@onNull String volumeUuid)711     List<? extends PackageStateInternal> getVolumePackages(@NonNull String volumeUuid);
712 
713     @NonNull
getUserInfos()714     UserInfo[] getUserInfos();
715 
716     @NonNull
getSharedUsers()717     ArrayMap<String, ? extends SharedUserApi> getSharedUsers();
718 }
719