Class ShadowPackageManager

  • Direct Known Subclasses:
    ShadowApplicationPackageManager

    @Implements(android.content.pm.PackageManager.class)
    public class ShadowPackageManager
    extends java.lang.Object
    • Field Detail

      • resources

        public static java.util.Map<java.lang.String,​android.content.res.Resources> resources
    • Constructor Detail

      • ShadowPackageManager

        public ShadowPackageManager()
    • Method Detail

      • addActivityIfNotPresent

        public android.content.pm.ActivityInfo addActivityIfNotPresent​(android.content.ComponentName componentName)
        Makes sure that given activity exists. If the activity doesn't exist yet, it will be created with applicationInfo set to an existing application, or if it doesn't exist, a new package will be created.
        Returns:
        existing or newly created activity info.
      • addServiceIfNotPresent

        public android.content.pm.ServiceInfo addServiceIfNotPresent​(android.content.ComponentName componentName)
        Makes sure that given service exists. If the service doesn't exist yet, it will be created with applicationInfo set to an existing application, or if it doesn't exist, a new package will be created.
        Returns:
        existing or newly created service info.
      • addReceiverIfNotPresent

        public android.content.pm.ActivityInfo addReceiverIfNotPresent​(android.content.ComponentName componentName)
        Makes sure that given receiver exists. If the receiver doesn't exist yet, it will be created with applicationInfo set to an existing application, or if it doesn't exist, a new package will be created.
        Returns:
        existing or newly created receiver info.
      • addProviderIfNotPresent

        public android.content.pm.ProviderInfo addProviderIfNotPresent​(android.content.ComponentName componentName)
        Makes sure that given provider exists. If the provider doesn't exist yet, it will be created with applicationInfo set to an existing application, or if it doesn't exist, a new package will be created.
        Returns:
        existing or newly created provider info.
      • addOrUpdateActivity

        public void addOrUpdateActivity​(android.content.pm.ActivityInfo activityInfo)
        Adds or updates given activity in the system. If activity with the same PackageItemInfo.name and ComponentInfo#packageName exists it will be updated. Its ComponentInfo.applicationInfo is always set to ApplicationInfo already existing in the system, but if no application exists a new one will be created using ComponentInfo.applicationInfo in this component.
      • addOrUpdateService

        public void addOrUpdateService​(android.content.pm.ServiceInfo serviceInfo)
        Adds or updates given service in the system. If service with the same PackageItemInfo.name and ComponentInfo#packageName exists it will be updated. Its ComponentInfo.applicationInfo is always set to ApplicationInfo already existing in the system, but if no application exists a new one will be created using ComponentInfo.applicationInfo in this component.
      • addOrUpdateReceiver

        public void addOrUpdateReceiver​(android.content.pm.ActivityInfo receiverInfo)
        Adds or updates given broadcast receiver in the system. If broadcast receiver with the same PackageItemInfo.name and ComponentInfo#packageName exists it will be updated. Its ComponentInfo.applicationInfo is always set to ApplicationInfo already existing in the system, but if no application exists a new one will be created using ComponentInfo.applicationInfo in this component.
      • addOrUpdateProvider

        public void addOrUpdateProvider​(android.content.pm.ProviderInfo providerInfo)
        Adds or updates given content provider in the system. If content provider with the same PackageItemInfo.name and ComponentInfo#packageName exists it will be updated. Its ComponentInfo.applicationInfo is always set to ApplicationInfo already existing in the system, but if no application exists a new one will be created using ComponentInfo.applicationInfo in this component.
      • removeActivity

        @Nullable
        public android.content.pm.ActivityInfo removeActivity​(android.content.ComponentName componentName)
        Removes activity from the package manager.
        Returns:
        the removed component or null if no such component existed.
      • removeService

        @Nullable
        public android.content.pm.ServiceInfo removeService​(android.content.ComponentName componentName)
        Removes service from the package manager.
        Returns:
        the removed component or null if no such component existed.
      • removeProvider

        @Nullable
        public android.content.pm.ProviderInfo removeProvider​(android.content.ComponentName componentName)
        Removes content provider from the package manager.
        Returns:
        the removed component or null if no such component existed.
      • removeReceiver

        @Nullable
        public android.content.pm.ActivityInfo removeReceiver​(android.content.ComponentName componentName)
        Removes broadcast receiver from the package manager.
        Returns:
        the removed component or null if no such component existed.
      • setResolveInfosForIntent

        @Deprecated
        public void setResolveInfosForIntent​(android.content.Intent intent,
                                             java.util.List<android.content.pm.ResolveInfo> info)
        Deprecated.
        Sets extra resolve infos for an intent. Those entries are added to whatever might be in the manifest already. Note that all resolve infos will have ResolveInfo.isDefault field set to true to allow their resolution for implicit intents. If this is not what you want, then you still have the reference to those ResolveInfos, and you can set the field back to false.
      • addResolveInfoForIntent

        @Deprecated
        public void addResolveInfoForIntent​(android.content.Intent intent,
                                            java.util.List<android.content.pm.ResolveInfo> info)
      • addResolveInfoForIntentNoDefaults

        @Deprecated
        public void addResolveInfoForIntentNoDefaults​(android.content.Intent intent,
                                                      android.content.pm.ResolveInfo info)
        Deprecated.
        Adds the info as ResolveInfo for the intent but without applying any default values. In particular it will not make the ResolveInfo.isDefault field true, that means that this resolve info will not resolve for Intent.resolveActivity(android.content.pm.PackageManager) and Context.startActivity(android.content.Intent).
      • addActivityIcon

        public void addActivityIcon​(android.content.ComponentName component,
                                    android.graphics.drawable.Drawable drawable)
      • addActivityIcon

        public void addActivityIcon​(android.content.Intent intent,
                                    android.graphics.drawable.Drawable drawable)
      • setApplicationIcon

        public void setApplicationIcon​(java.lang.String packageName,
                                       android.graphics.drawable.Drawable drawable)
      • setUnbadgedApplicationIcon

        public void setUnbadgedApplicationIcon​(java.lang.String packageName,
                                               android.graphics.drawable.Drawable drawable)
      • getComponentEnabledSettingFlags

        public int getComponentEnabledSettingFlags​(android.content.ComponentName componentName)
        Return the flags set in call to ApplicationPackageManager.setComponentEnabledSetting(ComponentName, int, int).
        Parameters:
        componentName - The component name.
        Returns:
        The flags.
      • installModule

        public void installModule​(java.lang.Object moduleInfoObject)
        Installs a module with the PackageManager as long as it is not null

        In order to create ModuleInfo objects in a valid state please use ModuleInfoBuilder.

      • deleteModule

        public java.lang.Object deleteModule​(java.lang.String packageName)
        Deletes a module when given the module's package name ModuleInfo be sure to give the correct name as this method does not ensure existence of the module before deletion. Since module installation ensures that a package exists in the device, also delete the package for full deletion.
        Parameters:
        packageName - should be the value of ModuleInfo.getPackageName().
        Returns:
        deleted module of null if no module with this name exists.
      • installPackage

        public void installPackage​(android.content.pm.PackageInfo packageInfo)
        Installs a package with the PackageManager. In order to create PackageInfo objects in a valid state please use androidx.test.core.content.pm.PackageInfoBuilder. This method automatically simulates instalation of a package in the system, so it adds a flag ApplicationInfo.FLAG_INSTALLED to the application info and makes sure it exits. It will update applicationInfo in package components as well. If you don't want the package to be installed, use addPackageNoDefaults(android.content.pm.PackageInfo) instead.
      • addPackageNoDefaults

        public void addPackageNoDefaults​(android.content.pm.PackageInfo packageInfo)
        Adds a package to the PackageManager, but doesn't set any default values on it. Right now it will not set ApplicationInfo.FLAG_INSTALLED flag on its application, so if not set explicitly, it will be treated as not installed.
      • addPackage

        public void addPackage​(android.content.pm.PackageInfo packageInfo,
                               android.content.pm.PackageStats packageStats)
        Installs a package with its stats with the PackageManager. This method doesn't add any defaults to the packageInfo parameters. You should make sure it is valid (see installPackage(PackageInfo)).
      • addPackage

        @Deprecated
        public void addPackage​(java.lang.String packageName)
        Deprecated.
      • getInternalMutablePackageInfo

        public android.content.pm.PackageInfo getInternalMutablePackageInfo​(java.lang.String packageName)
        Testing API allowing to retrieve internal package representation. This will allow to modify the package in a way visible to Robolectric, as this is Robolectric's internal full package representation. Note that maybe a better way is to just modify the test manifest to make those modifications in a standard way. Retrieving package info using PackageManager.getPackageInfo(java.lang.String, int) / PackageManager.getApplicationInfo(java.lang.String, int) will return defensive copies that will be stripped out of information according to provided flags. Don't use it to modify Robolectric state.
      • addPermissionInfo

        public void addPermissionInfo​(android.content.pm.PermissionInfo permissionInfo)
      • addChangedPackage

        public void addChangedPackage​(int sequenceNumber,
                                      java.lang.String packageName)
        Adds packageName to the list of changed packages for the particular sequenceNumber.
        Parameters:
        sequenceNumber - has to be >= 0
        packageName - name of the package that was changed
      • addPermissionGroupInfo

        public void addPermissionGroupInfo​(android.content.pm.PermissionGroupInfo permissionGroupInfo)
        Allows overriding or adding permission-group elements. These would be otherwise specified by either (the system)[https://developer.android.com/guide/topics/permissions/requesting.html#perm-groups] or by (the app itself)[https://developer.android.com/guide/topics/manifest/permission-group-element.html], as part of its manifest PackageParser.PermissionGroups added through this method have precedence over those specified with the same name by one of the aforementioned methods.
        See Also:
        PackageManager.getAllPermissionGroups(int), PackageManager.getPermissionGroupInfo(String, int)
      • removePackage

        public void removePackage​(java.lang.String packageName)
      • setSystemFeature

        public void setSystemFeature​(java.lang.String name,
                                     boolean supported)
      • addDrawableResolution

        public void addDrawableResolution​(java.lang.String packageName,
                                          int resourceId,
                                          android.graphics.drawable.Drawable drawable)
      • setNameForUid

        public void setNameForUid​(int uid,
                                  java.lang.String name)
      • setPackagesForCallingUid

        public void setPackagesForCallingUid​(java.lang.String... packagesForCallingUid)
      • setPackagesForUid

        public void setPackagesForUid​(int uid,
                                      java.lang.String... packagesForCallingUid)
      • getPackagesForUid

        @Implementation
        @Nullable
        protected java.lang.String[] getPackagesForUid​(int uid)
      • setPackageArchiveInfo

        public void setPackageArchiveInfo​(java.lang.String archiveFilePath,
                                          android.content.pm.PackageInfo packageInfo)
      • getVerificationResult

        public int getVerificationResult​(int id)
      • getVerificationExtendedTimeout

        public long getVerificationExtendedTimeout​(int id)
      • setShouldShowRequestPermissionRationale

        public void setShouldShowRequestPermissionRationale​(java.lang.String permission,
                                                            boolean show)
      • addSystemAvailableFeature

        public void addSystemAvailableFeature​(android.content.pm.FeatureInfo featureInfo)
      • clearSystemAvailableFeatures

        public void clearSystemAvailableFeatures()
      • addSystemSharedLibraryName

        public void addSystemSharedLibraryName​(java.lang.String name)
        Adds a value to be returned by PackageManager.getSystemSharedLibraryNames().
      • clearSystemSharedLibraryNames

        public void clearSystemSharedLibraryNames()
        Clears the values returned by PackageManager.getSystemSharedLibraryNames().
      • addCurrentToCannonicalName

        @Deprecated
        public void addCurrentToCannonicalName​(java.lang.String currentName,
                                               java.lang.String canonicalName)
        Deprecated.
      • addCanonicalName

        public void addCanonicalName​(java.lang.String currentName,
                                     java.lang.String canonicalName)
        Adds a canonical package name for a package.

        This will be reflected when calling PackageManager.currentToCanonicalPackageNames(String[]) or PackageManager.canonicalToCurrentPackageNames(String[]) (String[])}.

      • setCanRequestPackageInstalls

        public void setCanRequestPackageInstalls​(boolean canRequestPackageInstalls)
        Sets if the PackageManager is allowed to request package installs through package installer.
      • queryBroadcastReceiversAsUser

        @Implementation(minSdk=24)
        protected java.util.List<android.content.pm.ResolveInfo> queryBroadcastReceiversAsUser​(android.content.Intent intent,
                                                                                               int flags,
                                                                                               android.os.UserHandle userHandle)
      • queryBroadcastReceivers

        @Implementation(minSdk=17)
        protected java.util.List<android.content.pm.ResolveInfo> queryBroadcastReceivers​(android.content.Intent intent,
                                                                                         int flags,
                                                                                         int userId)
      • getPackageArchiveInfo

        @Implementation
        protected android.content.pm.PackageInfo getPackageArchiveInfo​(java.lang.String archiveFilePath,
                                                                       int flags)
      • freeStorageAndNotify

        @Implementation
        protected void freeStorageAndNotify​(long freeStorageSize,
                                            android.content.pm.IPackageDataObserver observer)
      • freeStorage

        @Implementation
        protected void freeStorage​(long freeStorageSize,
                                   android.content.IntentSender pi)
      • deletePackage

        public void deletePackage​(java.lang.String packageName)
        Uninstalls the package from the system in a way, that will allow its discovery through PackageManager.MATCH_UNINSTALLED_PACKAGES.
      • deletePackage

        protected void deletePackage​(java.lang.String packageName,
                                     android.content.pm.IPackageDeleteObserver observer,
                                     int flags)
      • doPendingUninstallCallbacks

        public void doPendingUninstallCallbacks()
        Runs the callbacks pending from calls to PackageManager.deletePackage(String, IPackageDeleteObserver, int)
      • getDeletedPackages

        public java.util.Set<java.lang.String> getDeletedPackages()
        Returns package names successfully deleted with PackageManager.deletePackage(String, IPackageDeleteObserver, int) Note that like real PackageManager the calling context must have Manifest.permission.DELETE_PACKAGES permission set.
      • queryOverriddenIntents

        protected java.util.List<android.content.pm.ResolveInfo> queryOverriddenIntents​(android.content.Intent intent,
                                                                                        int flags)
      • addPackageInternal

        public void addPackageInternal​(android.content.pm.PackageParser.Package appPackage)
        Internal use only.
        Parameters:
        appPackage -
      • getIntentFiltersForActivity

        public java.util.List<android.content.IntentFilter> getIntentFiltersForActivity​(android.content.ComponentName componentName)
                                                                                 throws android.content.pm.PackageManager.NameNotFoundException
        Get list of intent filters defined for given activity.
        Parameters:
        componentName - Name of the activity whose intent filters are to be retrieved
        Returns:
        the activity's intent filters
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • getIntentFiltersForService

        public java.util.List<android.content.IntentFilter> getIntentFiltersForService​(android.content.ComponentName componentName)
                                                                                throws android.content.pm.PackageManager.NameNotFoundException
        Get list of intent filters defined for given service.
        Parameters:
        componentName - Name of the service whose intent filters are to be retrieved
        Returns:
        the service's intent filters
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • getIntentFiltersForReceiver

        public java.util.List<android.content.IntentFilter> getIntentFiltersForReceiver​(android.content.ComponentName componentName)
                                                                                 throws android.content.pm.PackageManager.NameNotFoundException
        Get list of intent filters defined for given receiver.
        Parameters:
        componentName - Name of the receiver whose intent filters are to be retrieved
        Returns:
        the receiver's intent filters
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • getIntentFiltersForProvider

        public java.util.List<android.content.IntentFilter> getIntentFiltersForProvider​(android.content.ComponentName componentName)
                                                                                 throws android.content.pm.PackageManager.NameNotFoundException
        Get list of intent filters defined for given provider.
        Parameters:
        componentName - Name of the provider whose intent filters are to be retrieved
        Returns:
        the provider's intent filters
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • addIntentFilterForActivity

        public void addIntentFilterForActivity​(android.content.ComponentName componentName,
                                               android.content.IntentFilter filter)
                                        throws android.content.pm.PackageManager.NameNotFoundException
        Add intent filter for given activity.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • addIntentFilterForService

        public void addIntentFilterForService​(android.content.ComponentName componentName,
                                              android.content.IntentFilter filter)
                                       throws android.content.pm.PackageManager.NameNotFoundException
        Add intent filter for given service.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • addIntentFilterForReceiver

        public void addIntentFilterForReceiver​(android.content.ComponentName componentName,
                                               android.content.IntentFilter filter)
                                        throws android.content.pm.PackageManager.NameNotFoundException
        Add intent filter for given receiver.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • addIntentFilterForProvider

        public void addIntentFilterForProvider​(android.content.ComponentName componentName,
                                               android.content.IntentFilter filter)
                                        throws android.content.pm.PackageManager.NameNotFoundException
        Add intent filter for given provider.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • clearIntentFilterForActivity

        public void clearIntentFilterForActivity​(android.content.ComponentName componentName)
                                          throws android.content.pm.PackageManager.NameNotFoundException
        Clears intent filters for given activity.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • clearIntentFilterForService

        public void clearIntentFilterForService​(android.content.ComponentName componentName)
                                         throws android.content.pm.PackageManager.NameNotFoundException
        Clears intent filters for given service.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • clearIntentFilterForReceiver

        public void clearIntentFilterForReceiver​(android.content.ComponentName componentName)
                                          throws android.content.pm.PackageManager.NameNotFoundException
        Clears intent filters for given receiver.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • clearIntentFilterForProvider

        public void clearIntentFilterForProvider​(android.content.ComponentName componentName)
                                          throws android.content.pm.PackageManager.NameNotFoundException
        Clears intent filters for given provider.
        Throws:
        android.content.pm.PackageManager.NameNotFoundException - if component with given name doesn't exist.
      • getPersistentPreferredActivities

        public int getPersistentPreferredActivities​(java.util.List<android.content.IntentFilter> outFilters,
                                                    java.util.List<android.content.ComponentName> outActivities,
                                                    java.lang.String packageName)
        Method to retrieve persistent preferred activities as set by DevicePolicyManager.addPersistentPreferredActivity(android.content.ComponentName, android.content.IntentFilter, android.content.ComponentName). Works the same way as analogous PackageManager.getPreferredActivities(java.util.List<android.content.IntentFilter>, java.util.List<android.content.ComponentName>, java.lang.String) for regular preferred activities.
      • getPreferredActivitiesInternal

        protected static int getPreferredActivitiesInternal​(java.util.List<android.content.IntentFilter> outFilters,
                                                            java.util.List<android.content.ComponentName> outActivities,
                                                            java.lang.String packageName,
                                                            java.util.SortedMap<android.content.ComponentName,​java.util.List<android.content.IntentFilter>> preferredActivitiesMap)
      • clearPackagePreferredActivitiesInternal

        protected static void clearPackagePreferredActivitiesInternal​(java.lang.String packageName,
                                                                      java.util.SortedMap<android.content.ComponentName,​java.util.List<android.content.IntentFilter>> preferredActivitiesMap)
      • addPreferredActivityInternal

        protected static void addPreferredActivityInternal​(android.content.IntentFilter filter,
                                                           android.content.ComponentName activity,
                                                           java.util.SortedMap<android.content.ComponentName,​java.util.List<android.content.IntentFilter>> preferredActivitiesMap)
      • mapForPackage

        protected static <V> java.util.SortedMap<android.content.ComponentName,​V> mapForPackage​(java.util.SortedMap<android.content.ComponentName,​V> input,
                                                                                                      @Nullable
                                                                                                      java.lang.String packageName)
      • setShouldShowActivityChooser

        public void setShouldShowActivityChooser​(boolean shouldShowActivityChooser)
        If this method has been called with true, then in cases where many activities match a filter, an activity chooser will be resolved instead of just the first pick.
      • setSafeMode

        public void setSafeMode​(boolean safeMode)
        Set value to be returned by PackageManager.isSafeMode().
      • getDistractingPackageRestrictions

        public int getDistractingPackageRestrictions​(java.lang.String pkg)
        Returns the last value provided to setDistractingPackageRestrictions for pkg. Defaults to PackageManager.RESTRICTION_NONE if setDistractingPackageRestrictions has not been called for pkg.
      • reset

        @Resetter
        public static void reset()