Class ShadowPackageManager

java.lang.Object
org.robolectric.shadows.ShadowPackageManager
Direct Known Subclasses:
ShadowApplicationPackageManager

@Implements(PackageManager.class) public class ShadowPackageManager extends Object
  • Field Details

  • Constructor Details

    • ShadowPackageManager

      public ShadowPackageManager()
  • Method Details

    • addActivityIfNotPresent

      public ActivityInfo addActivityIfNotPresent(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 ServiceInfo addServiceIfNotPresent(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 ActivityInfo addReceiverIfNotPresent(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 ProviderInfo addProviderIfNotPresent(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(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(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(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(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 ActivityInfo removeActivity(ComponentName componentName)
      Removes activity from the package manager.
      Returns:
      the removed component or null if no such component existed.
    • removeService

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

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

      @Nullable public ActivityInfo removeReceiver(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(Intent intent, List<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(Intent intent, List<ResolveInfo> info)
    • addResolveInfoForIntent

      @Deprecated public void addResolveInfoForIntent(Intent intent, ResolveInfo info)
      Deprecated.
      use addIntentFilterForComponent(ComponentName, IntentFilter, Map) instead and if the component doesn't exist add it using any of installPackage(PackageInfo), addOrUpdateActivity(ActivityInfo), addActivityIfNotPresent(ComponentName) or their counterparts for other types of components.
      Adds extra resolve info for an intent.

      Note that this resolve info will have ResolveInfo.isDefault field set to true to allow its resolution for implicit intents. If this is not what you want, then please use addResolveInfoForIntentNoDefaults(Intent, ResolveInfo) instead.

    • addResolveInfoForIntentNoDefaults

      @Deprecated public void addResolveInfoForIntentNoDefaults(Intent intent, 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(PackageManager) and

      invalid reference
      Context#startActivity
      .
    • removeResolveInfosForIntent

      @Deprecated public void removeResolveInfosForIntent(Intent intent, String packageName)
    • addActivityIcon

      public void addActivityIcon(ComponentName component, Drawable drawable)
    • addActivityIcon

      public void addActivityIcon(Intent intent, Drawable drawable)
    • setApplicationIcon

      public void setApplicationIcon(String packageName, Drawable drawable)
    • setUnbadgedApplicationIcon

      public void setUnbadgedApplicationIcon(String packageName, Drawable drawable)
    • getComponentEnabledSettingFlags

      public int getComponentEnabledSettingFlags(ComponentName componentName)
      Parameters:
      componentName - The component name.
      Returns:
      The flags.
    • installModule

      public void installModule(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 Object deleteModule(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(PackageInfo packageInfo)
      Installs a package with the PackageManager.

      In order to create PackageInfo objects in a valid state please use

      invalid reference
      androidx.test.core.content.pm.PackageInfoBuilder
      .

      This method automatically simulates installation 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(PackageInfo) instead.

      To simulate a scenario in which a system image app was updated, in which the system image APK exists, but the update in the data partition is the active version of the APK, call installPackage() with the system image PackageInfo, and then again with the data partition update PackageInfo. In this case getPackageInfo() with the MATCH_FACTORY_ONLY flag will return the system image's PackageInfo, while other calls will return the data partition's info.

    • setInstallSourceInfo

      public void setInstallSourceInfo(String packageName, String initiatingPackage, String installerPackage)
      Adds install source information for a package.
    • setInstallSourceInfo

      public void setInstallSourceInfo(String packageName, @Nullable String initiatingPackageName, @Nullable SigningInfo initiatingPackageSigningInfo, @Nullable String originatingPackageName, @Nullable String installingPackageName, @Nullable String updateOwnerPackageName, int packageSource)
      Adds install source information for a package.
    • addPackageNoDefaults

      public void addPackageNoDefaults(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(PackageInfo packageInfo, 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(String packageName)
      Deprecated.
    • addPackage

      @Deprecated @InlineMe(replacement="this.installPackage(packageInfo)") public final void addPackage(PackageInfo packageInfo)
      Deprecated.
    • tryRetrievingFactoryInfo

      @Nullable protected PackageInfo tryRetrievingFactoryInfo(String packageName, long flags) throws PackageManager.NameNotFoundException
      Throws:
      PackageManager.NameNotFoundException
    • getPackageInfo

      @Implementation @Nullable protected PackageInfo getPackageInfo(String packageName, int flags) throws PackageManager.NameNotFoundException
      Throws:
      PackageManager.NameNotFoundException
    • getInternalMutablePackageInfo

      public PackageInfo getInternalMutablePackageInfo(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(String, int) / PackageManager.getApplicationInfo(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(PermissionInfo permissionInfo)
    • addChangedPackage

      public void addChangedPackage(int sequenceNumber, 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(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:
    • removePackage

      public void removePackage(String packageName)
    • setSystemFeature

      public void setSystemFeature(String name, boolean supported)
    • addDrawableResolution

      public void addDrawableResolution(String packageName, int resourceId, Drawable drawable)
    • setNameForUid

      public void setNameForUid(int uid, String name)
    • setPackagesForCallingUid

      public void setPackagesForCallingUid(String... packagesForCallingUid)
    • setPackagesForUid

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

      @Implementation @Nullable protected String[] getPackagesForUid(int uid)
    • setPackageArchiveInfo

      public void setPackageArchiveInfo(String archiveFilePath, PackageInfo packageInfo)
    • getVerificationResult

      public int getVerificationResult(int id)
    • getVerificationExtendedTimeout

      public long getVerificationExtendedTimeout(int id)
    • getVerificationCodeAtTimeoutExtension

      public int getVerificationCodeAtTimeoutExtension(int id)
    • triggerInstallVerificationTimeout

      public void triggerInstallVerificationTimeout(Application appContext, int id)
    • setShouldShowRequestPermissionRationale

      public void setShouldShowRequestPermissionRationale(String permission, boolean show)
    • addSystemAvailableFeature

      public void addSystemAvailableFeature(FeatureInfo featureInfo)
    • clearSystemAvailableFeatures

      public void clearSystemAvailableFeatures()
    • addSystemSharedLibraryName

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

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

      public void addCanonicalName(String currentName, 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 List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, UserHandle userHandle)
    • queryBroadcastReceivers

      @Implementation protected List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId)
    • getPackageArchiveInfo

      @Implementation protected PackageInfo getPackageArchiveInfo(String archiveFilePath, int flags)
    • getShadowPackageArchiveInfo

      protected PackageInfo getShadowPackageArchiveInfo(String archiveFilePath, int flags)
    • freeStorageAndNotify

      @Implementation protected void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer)
    • freeStorage

      @Implementation protected void freeStorage(long freeStorageSize, IntentSender pi)
    • deletePackage

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

      @Implementation protected void deletePackage(String packageName, IPackageDeleteObserver observer, int flags)
    • doPendingUninstallCallbacks

      public void doPendingUninstallCallbacks()
    • getDeletedPackages

      public Set<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 List<ResolveInfo> queryOverriddenIntents(Intent intent, int flags)
    • addPackageInternal

      public void addPackageInternal(PackageParser.Package appPackage)
      Internal use only.
      Parameters:
      appPackage -
    • generatePackageInfo

      protected PackageInfo generatePackageInfo(PackageParser.Package appPackage, int flags)
    • getIntentFiltersForActivity

      public List<IntentFilter> getIntentFiltersForActivity(ComponentName componentName)
      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:
      IllegalArgumentException - if component with given name doesn't exist.
    • getIntentFiltersForService

      public List<IntentFilter> getIntentFiltersForService(ComponentName componentName)
      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:
      IllegalArgumentException - if component with given name doesn't exist.
    • getIntentFiltersForReceiver

      public List<IntentFilter> getIntentFiltersForReceiver(ComponentName componentName)
      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:
      IllegalArgumentException - if component with given name doesn't exist.
    • getIntentFiltersForProvider

      public List<IntentFilter> getIntentFiltersForProvider(ComponentName componentName)
      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:
      IllegalArgumentException - if component with given name doesn't exist.
    • addIntentFilterForActivity

      public void addIntentFilterForActivity(ComponentName componentName, IntentFilter filter)
      Add intent filter for given activity.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • addIntentFilterForService

      public void addIntentFilterForService(ComponentName componentName, IntentFilter filter)
      Add intent filter for given service.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • addIntentFilterForReceiver

      public void addIntentFilterForReceiver(ComponentName componentName, IntentFilter filter)
      Add intent filter for given receiver.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • addIntentFilterForProvider

      public void addIntentFilterForProvider(ComponentName componentName, IntentFilter filter)
      Add intent filter for given provider.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • clearIntentFilterForActivity

      public void clearIntentFilterForActivity(ComponentName componentName)
      Clears intent filters for given activity.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • clearIntentFilterForService

      public void clearIntentFilterForService(ComponentName componentName)
      Clears intent filters for given service.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • clearIntentFilterForReceiver

      public void clearIntentFilterForReceiver(ComponentName componentName)
      Clears intent filters for given receiver.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • clearIntentFilterForProvider

      public void clearIntentFilterForProvider(ComponentName componentName)
      Clears intent filters for given provider.
      Throws:
      IllegalArgumentException - if component with given name doesn't exist.
    • getPersistentPreferredActivities

      public int getPersistentPreferredActivities(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName)
      Method to retrieve persistent preferred activities as set by DevicePolicyManager.addPersistentPreferredActivity(ComponentName, IntentFilter, ComponentName).

      Works the same way as analogous PackageManager.getPreferredActivities(List, List, String) for regular preferred activities.

    • getPreferredActivitiesInternal

      protected static int getPreferredActivitiesInternal(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap)
    • clearPackagePreferredActivitiesInternal

      protected static void clearPackagePreferredActivitiesInternal(String packageName, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap)
    • addPreferredActivityInternal

      protected static void addPreferredActivityInternal(IntentFilter filter, ComponentName activity, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap)
    • mapForPackage

      protected static <V> SortedMap<ComponentName, V> mapForPackage(SortedMap<ComponentName, V> input, @Nullable String packageName)
    • getPackageSetting

      public ShadowPackageManager.PackageSetting getPackageSetting(String packageName)
      Returns the current ShadowPackageManager.PackageSetting of packageName.

      If packageName is not present in this ShadowPackageManager, this method will return null.

    • 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

      @Implementation(minSdk=10000) public int getDistractingPackageRestrictions(String pkg)
      Returns the last value provided to setDistractingPackageRestrictions for pkg.

      Defaults to PackageManager.RESTRICTION_NONE if setDistractingPackageRestrictions has not been called for pkg.

    • addStringResource

      public void addStringResource(String packageName, int resId, String text)
      Adds a String resource with resId corresponding to packageName. This is retrieved in shadow implementation of PackageManager.getText(String, int, ApplicationInfo).
    • setAutoRevokeWhitelisted

      public void setAutoRevokeWhitelisted(boolean whitelisted)
    • setDeviceUpgrading

      public static void setDeviceUpgrading(boolean deviceUpgrading)
      Sets the value to be returned by PackageManager.isDeviceUpgrading()
    • reset

      @Resetter public static void reset()