Package org.robolectric.shadows
Class ShadowPackageManager
java.lang.Object
org.robolectric.shadows.ShadowPackageManager
- Direct Known Subclasses:
ShadowApplicationPackageManager
@Implements(android.content.pm.PackageManager.class)
public class ShadowPackageManager
extends Object
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
static class
static class
Settings for a particular package. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addActivityIcon
(ComponentName component, Drawable drawable) void
addActivityIcon
(Intent intent, Drawable drawable) addActivityIfNotPresent
(ComponentName componentName) Makes sure that given activity exists.void
addCanonicalName
(String currentName, String canonicalName) Adds a canonical package name for a package.void
addChangedPackage
(int sequenceNumber, String packageName) AddspackageName
to the list of changed packages for the particularsequenceNumber
.void
addCurrentToCannonicalName
(String currentName, String canonicalName) Deprecated.void
addDrawableResolution
(String packageName, int resourceId, Drawable drawable) void
addIntentFilterForActivity
(ComponentName componentName, IntentFilter filter) Add intent filter for given activity.void
addIntentFilterForProvider
(ComponentName componentName, IntentFilter filter) Add intent filter for given provider.void
addIntentFilterForReceiver
(ComponentName componentName, IntentFilter filter) Add intent filter for given receiver.void
addIntentFilterForService
(ComponentName componentName, IntentFilter filter) Add intent filter for given service.void
addOrUpdateActivity
(ActivityInfo activityInfo) Adds or updates given activity in the system.void
addOrUpdateProvider
(ProviderInfo providerInfo) Adds or updates given content provider in the system.void
addOrUpdateReceiver
(ActivityInfo receiverInfo) Adds or updates given broadcast receiver in the system.void
addOrUpdateService
(ServiceInfo serviceInfo) Adds or updates given service in the system.final void
addPackage
(PackageInfo packageInfo) Deprecated.void
addPackage
(PackageInfo packageInfo, PackageStats packageStats) Installs a package with its stats with thePackageManager
.void
addPackage
(String packageName) Deprecated.UseinstallPackage(PackageInfo)
instead.void
addPackageInternal
(PackageParser.Package appPackage) Internal use only.void
addPackageNoDefaults
(PackageInfo packageInfo) Adds a package to thePackageManager
, but doesn't set any default values on it.void
addPermissionGroupInfo
(PermissionGroupInfo permissionGroupInfo) Allows overriding or adding permission-group elements.void
addPermissionInfo
(PermissionInfo permissionInfo) protected static void
addPreferredActivityInternal
(IntentFilter filter, ComponentName activity, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap) addProviderIfNotPresent
(ComponentName componentName) Makes sure that given provider exists.addReceiverIfNotPresent
(ComponentName componentName) Makes sure that given receiver exists.void
addResolveInfoForIntent
(Intent intent, ResolveInfo info) Deprecated.useaddIntentFilterForComponent(android.content.ComponentName, android.content.IntentFilter, java.util.Map<android.content.ComponentName, java.util.List<android.content.IntentFilter>>)
instead and if the component doesn't exist add it using any ofinstallPackage(android.content.pm.PackageInfo)
,addOrUpdateActivity(android.content.pm.ActivityInfo)
,addActivityIfNotPresent(android.content.ComponentName)
or their counterparts for other types of components.void
addResolveInfoForIntent
(Intent intent, List<ResolveInfo> info) Deprecated.see note onaddResolveInfoForIntent(Intent, ResolveInfo)
.void
addResolveInfoForIntentNoDefaults
(Intent intent, ResolveInfo info) Deprecated.see the note onaddResolveInfoForIntent(Intent, ResolveInfo)
.addServiceIfNotPresent
(ComponentName componentName) Makes sure that given service exists.void
addStringResource
(String packageName, int resId, String text) Adds a String resource withresId
corresponding topackageName
.void
addSystemAvailableFeature
(FeatureInfo featureInfo) void
Adds a value to be returned byPackageManager.getSystemSharedLibraryNames()
.void
clearIntentFilterForActivity
(ComponentName componentName) Clears intent filters for given activity.void
clearIntentFilterForProvider
(ComponentName componentName) Clears intent filters for given provider.void
clearIntentFilterForReceiver
(ComponentName componentName) Clears intent filters for given receiver.void
clearIntentFilterForService
(ComponentName componentName) Clears intent filters for given service.protected static void
clearPackagePreferredActivitiesInternal
(String packageName, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap) void
void
Clears the values returned byPackageManager.getSystemSharedLibraryNames()
.deleteModule
(String packageName) Deletes a module when given the module's package nameModuleInfo
be sure to give the correct name as this method does not ensure existence of the module before deletion.void
deletePackage
(String packageName) Uninstalls the package from the system in a way, that will allow its discovery throughPackageManager.MATCH_UNINSTALLED_PACKAGES
.protected void
deletePackage
(String packageName, IPackageDeleteObserver observer, int flags) void
Runs the callbacks pending from calls toPackageManager.deletePackage(String, IPackageDeleteObserver, int)
protected void
freeStorage
(long freeStorageSize, IntentSender pi) protected void
freeStorageAndNotify
(long freeStorageSize, IPackageDataObserver observer) protected PackageInfo
generatePackageInfo
(PackageParser.Package appPackage, int flags) int
getComponentEnabledSettingFlags
(ComponentName componentName) Return the flags set in call toApplicationPackageManager.setComponentEnabledSetting(ComponentName, int, int)
.Returns package names successfully deleted withPackageManager.deletePackage(String, IPackageDeleteObserver, int)
Note that like realPackageManager
the calling context must haveManifest.permission.DELETE_PACKAGES
permission set.int
Returns the last value provided tosetDistractingPackageRestrictions
forpkg
.getIntentFiltersForActivity
(ComponentName componentName) Get list of intent filters defined for given activity.getIntentFiltersForProvider
(ComponentName componentName) Get list of intent filters defined for given provider.getIntentFiltersForReceiver
(ComponentName componentName) Get list of intent filters defined for given receiver.getIntentFiltersForService
(ComponentName componentName) Get list of intent filters defined for given service.getInternalMutablePackageInfo
(String packageName) Testing API allowing to retrieve internal package representation.protected PackageInfo
getPackageArchiveInfo
(String archiveFilePath, int flags) getPackageSetting
(String packageName) Returns the currentShadowPackageManager.PackageSetting
ofpackageName
.protected String[]
getPackagesForUid
(int uid) int
getPersistentPreferredActivities
(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName) Method to retrieve persistent preferred activities as set byDevicePolicyManager.addPersistentPreferredActivity(android.content.ComponentName, android.content.IntentFilter, android.content.ComponentName)
.protected static int
getPreferredActivitiesInternal
(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName, SortedMap<ComponentName, List<IntentFilter>> preferredActivitiesMap) protected PackageInfo
getShadowPackageArchiveInfo
(String archiveFilePath, int flags) int
long
getVerificationExtendedTimeout
(int id) int
getVerificationResult
(int id) void
installModule
(Object moduleInfoObject) Installs a module with thePackageManager
as long as it is notnull
void
installPackage
(PackageInfo packageInfo) Installs a package with thePackageManager
.protected static <V> SortedMap<ComponentName,
V> mapForPackage
(SortedMap<ComponentName, V> input, String packageName) protected List<ResolveInfo>
queryBroadcastReceivers
(Intent intent, int flags, int userId) protected List<ResolveInfo>
queryBroadcastReceiversAsUser
(Intent intent, int flags, UserHandle userHandle) protected List<ResolveInfo>
queryOverriddenIntents
(Intent intent, int flags) removeActivity
(ComponentName componentName) Removes activity from the package manager.void
removePackage
(String packageName) removeProvider
(ComponentName componentName) Removes content provider from the package manager.removeReceiver
(ComponentName componentName) Removes broadcast receiver from the package manager.void
removeResolveInfosForIntent
(Intent intent, String packageName) Deprecated.see note onaddResolveInfoForIntent(Intent, ResolveInfo)
.removeService
(ComponentName componentName) Removes service from the package manager.static void
reset()
void
setApplicationIcon
(String packageName, Drawable drawable) void
setAutoRevokeWhitelisted
(boolean whitelisted) Set value to be returned byPackageManager.isAutoRevokeWhitelisted(java.lang.String)
.void
setCanRequestPackageInstalls
(boolean canRequestPackageInstalls) Sets if thePackageManager
is allowed to request package installs through package installer.void
setInstallSourceInfo
(String packageName, String initiatingPackage, String installerPackage) Adds install source information for a package.void
setNameForUid
(int uid, String name) void
setPackageArchiveInfo
(String archiveFilePath, PackageInfo packageInfo) void
setPackagesForCallingUid
(String... packagesForCallingUid) void
setPackagesForUid
(int uid, String... packagesForCallingUid) void
setResolveInfosForIntent
(Intent intent, List<ResolveInfo> info) Deprecated.see the note onaddResolveInfoForIntent(Intent, ResolveInfo)
.void
setSafeMode
(boolean safeMode) Set value to be returned byPackageManager.isSafeMode()
.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.void
setShouldShowRequestPermissionRationale
(String permission, boolean show) void
setSystemFeature
(String name, boolean supported) void
setUnbadgedApplicationIcon
(String packageName, Drawable drawable) void
triggerInstallVerificationTimeout
(Application appContext, int id)
-
Field Details
-
resources
-
-
Constructor Details
-
ShadowPackageManager
public ShadowPackageManager()
-
-
Method Details
-
addActivityIfNotPresent
Makes sure that given activity exists. If the activity doesn't exist yet, it will be created withapplicationInfo
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
Makes sure that given service exists. If the service doesn't exist yet, it will be created withapplicationInfo
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
Makes sure that given receiver exists. If the receiver doesn't exist yet, it will be created withapplicationInfo
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
Makes sure that given provider exists. If the provider doesn't exist yet, it will be created withapplicationInfo
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
Adds or updates given activity in the system. If activity with the samePackageItemInfo.name
andComponentInfo#packageName
exists it will be updated. ItsComponentInfo.applicationInfo
is always set toApplicationInfo
already existing in the system, but if no application exists a new one will be created usingComponentInfo.applicationInfo
in this component. -
addOrUpdateService
Adds or updates given service in the system. If service with the samePackageItemInfo.name
andComponentInfo#packageName
exists it will be updated. ItsComponentInfo.applicationInfo
is always set toApplicationInfo
already existing in the system, but if no application exists a new one will be created usingComponentInfo.applicationInfo
in this component. -
addOrUpdateReceiver
Adds or updates given broadcast receiver in the system. If broadcast receiver with the samePackageItemInfo.name
andComponentInfo#packageName
exists it will be updated. ItsComponentInfo.applicationInfo
is always set toApplicationInfo
already existing in the system, but if no application exists a new one will be created usingComponentInfo.applicationInfo
in this component. -
addOrUpdateProvider
Adds or updates given content provider in the system. If content provider with the samePackageItemInfo.name
andComponentInfo#packageName
exists it will be updated. ItsComponentInfo.applicationInfo
is always set toApplicationInfo
already existing in the system, but if no application exists a new one will be created usingComponentInfo.applicationInfo
in this component. -
removeActivity
Removes activity from the package manager.- Returns:
- the removed component or
null
if no such component existed.
-
removeService
Removes service from the package manager.- Returns:
- the removed component or
null
if no such component existed.
-
removeProvider
Removes content provider from the package manager.- Returns:
- the removed component or
null
if no such component existed.
-
removeReceiver
Removes broadcast receiver from the package manager.- Returns:
- the removed component or
null
if no such component existed.
-
setResolveInfosForIntent
Deprecated.see the note onaddResolveInfoForIntent(Intent, ResolveInfo)
.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 haveResolveInfo.isDefault
field set totrue
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 tofalse
. -
addResolveInfoForIntent
Deprecated.see note onaddResolveInfoForIntent(Intent, ResolveInfo)
. -
addResolveInfoForIntent
Deprecated.useaddIntentFilterForComponent(android.content.ComponentName, android.content.IntentFilter, java.util.Map<android.content.ComponentName, java.util.List<android.content.IntentFilter>>)
instead and if the component doesn't exist add it using any ofinstallPackage(android.content.pm.PackageInfo)
,addOrUpdateActivity(android.content.pm.ActivityInfo)
,addActivityIfNotPresent(android.content.ComponentName)
or their counterparts for other types of components.Adds extra resolve info for an intent. Note that this resolve info will haveResolveInfo.isDefault
field set totrue
to allow its resolution for implicit intents. If this is not what you want, then please useaddResolveInfoForIntentNoDefaults(android.content.Intent, android.content.pm.ResolveInfo)
instead. -
addResolveInfoForIntentNoDefaults
Deprecated.see the note onaddResolveInfoForIntent(Intent, ResolveInfo)
.Adds theinfo
asResolveInfo
for the intent but without applying any default values. In particular it will not make theResolveInfo.isDefault
fieldtrue
, that means that this resolve info will not resolve forIntent.resolveActivity(android.content.pm.PackageManager)
andContext.startActivity(android.content.Intent)
. -
removeResolveInfosForIntent
Deprecated.see note onaddResolveInfoForIntent(Intent, ResolveInfo)
.RemovesResolveInfo
s registered usingaddResolveInfoForIntent(android.content.Intent, java.util.List<android.content.pm.ResolveInfo>)
. -
addActivityIcon
-
addActivityIcon
-
setApplicationIcon
-
setUnbadgedApplicationIcon
-
getComponentEnabledSettingFlags
Return the flags set in call toApplicationPackageManager.setComponentEnabledSetting(ComponentName, int, int)
.- Parameters:
componentName
- The component name.- Returns:
- The flags.
-
installModule
Installs a module with thePackageManager
as long as it is notnull
In order to create ModuleInfo objects in a valid state please use
ModuleInfoBuilder
. -
deleteModule
Deletes a module when given the module's package nameModuleInfo
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 ofModuleInfo.getPackageName()
.- Returns:
- deleted module of
null
if no module with this name exists.
-
installPackage
Installs a package with thePackageManager
. In order to create PackageInfo objects in a valid state please useandroidx.test.core.content.pm.PackageInfoBuilder
. This method automatically simulates instalation of a package in the system, so it adds a flagApplicationInfo.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, useaddPackageNoDefaults(android.content.pm.PackageInfo)
instead. -
setInstallSourceInfo
public void setInstallSourceInfo(String packageName, String initiatingPackage, String installerPackage) Adds install source information for a package. -
addPackageNoDefaults
Adds a package to thePackageManager
, 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
Installs a package with its stats with thePackageManager
.This method doesn't add any defaults to the
packageInfo
parameters. You should make sure it is valid (seeinstallPackage(PackageInfo)
). -
addPackage
Deprecated.UseinstallPackage(PackageInfo)
instead. -
addPackage
@Deprecated @InlineMe(replacement="this.installPackage(packageInfo)") public final void addPackage(PackageInfo packageInfo) Deprecated.UseinstallPackage(android.content.pm.PackageInfo)
instead. -
getInternalMutablePackageInfo
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 usingPackageManager.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
-
addChangedPackage
AddspackageName
to the list of changed packages for the particularsequenceNumber
.- Parameters:
sequenceNumber
- has to be >= 0packageName
- name of the package that was changed
-
addPermissionGroupInfo
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 manifestPackageParser.PermissionGroup
s added through this method have precedence over those specified with the same name by one of the aforementioned methods. -
removePackage
-
setSystemFeature
-
addDrawableResolution
-
setNameForUid
-
setPackagesForCallingUid
-
setPackagesForUid
-
getPackagesForUid
-
setPackageArchiveInfo
-
getVerificationResult
public int getVerificationResult(int id) -
getVerificationExtendedTimeout
public long getVerificationExtendedTimeout(int id) -
getVerificationCodeAtTimeoutExtension
public int getVerificationCodeAtTimeoutExtension(int id) -
triggerInstallVerificationTimeout
-
setShouldShowRequestPermissionRationale
-
addSystemAvailableFeature
-
clearSystemAvailableFeatures
public void clearSystemAvailableFeatures() -
addCurrentToCannonicalName
Deprecated. -
addCanonicalName
Adds a canonical package name for a package.This will be reflected when calling
PackageManager.currentToCanonicalPackageNames(String[])
orPackageManager.canonicalToCurrentPackageNames(String[])
(String[])}. -
setCanRequestPackageInstalls
public void setCanRequestPackageInstalls(boolean canRequestPackageInstalls) Sets if thePackageManager
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(minSdk=17) protected List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) -
getPackageArchiveInfo
-
getShadowPackageArchiveInfo
-
freeStorageAndNotify
@Implementation protected void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer) -
freeStorage
-
deletePackage
Uninstalls the package from the system in a way, that will allow its discovery throughPackageManager.MATCH_UNINSTALLED_PACKAGES
. -
deletePackage
@Implementation protected void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) -
doPendingUninstallCallbacks
public void doPendingUninstallCallbacks()Runs the callbacks pending from calls toPackageManager.deletePackage(String, IPackageDeleteObserver, int)
-
getDeletedPackages
Returns package names successfully deleted withPackageManager.deletePackage(String, IPackageDeleteObserver, int)
Note that like realPackageManager
the calling context must haveManifest.permission.DELETE_PACKAGES
permission set. -
queryOverriddenIntents
-
addPackageInternal
Internal use only.- Parameters:
appPackage
-
-
generatePackageInfo
-
getIntentFiltersForActivity
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
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
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
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
Add intent filter for given activity.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
addIntentFilterForService
Add intent filter for given service.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
addIntentFilterForReceiver
Add intent filter for given receiver.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
addIntentFilterForProvider
Add intent filter for given provider.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
clearIntentFilterForActivity
Clears intent filters for given activity.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
clearIntentFilterForService
Clears intent filters for given service.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
clearIntentFilterForReceiver
Clears intent filters for given receiver.- Throws:
IllegalArgumentException
- if component with given name doesn't exist.
-
clearIntentFilterForProvider
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 byDevicePolicyManager.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(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
Returns the currentShadowPackageManager.PackageSetting
ofpackageName
. IfpackageName
is not present in thisShadowPackageManager
, 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 byPackageManager.isSafeMode()
. -
getDistractingPackageRestrictions
Returns the last value provided tosetDistractingPackageRestrictions
forpkg
. Defaults toPackageManager.RESTRICTION_NONE
ifsetDistractingPackageRestrictions
has not been called forpkg
. -
addStringResource
Adds a String resource withresId
corresponding topackageName
. This is retrieved in shadow implementation ofPackageManager.getText(String, int, ApplicationInfo)
. -
setAutoRevokeWhitelisted
public void setAutoRevokeWhitelisted(boolean whitelisted) Set value to be returned byPackageManager.isAutoRevokeWhitelisted(java.lang.String)
. -
reset
-
installPackage(android.content.pm.PackageInfo)
instead.