PackageManagerCompat

public interface PackageManagerCompat

A helper for accessing features in PackageManager in a backwards compatible fashion. Construct this by using getPackageManagerCompat(Context).

This helper exposes methods otherwise only available on some API levels. For other methods that exist on all API levels (e.g. getPackagesForUid(int)) it will augment the results with data about instant apps as well as installed apps.

Public Method Summary

abstract ApplicationInfo
getApplicationInfo(String packageName, int flags)
Retrieve all of the information we know about a particular package/application.
abstract CharSequence
getApplicationLabel(ApplicationInfo info)
Return the label to use for this application.
abstract String
getInstallerPackageName(String packageName)
Retrieve the package name of the application that installed a package.
abstract byte[]
getInstantAppCookie()
Gets the instant application cookie for this app.
abstract int
getInstantAppCookieMaxSize()
Gets the maximum size in bytes of the cookie data an instant app can store on the device.
abstract PackageInfo
getPackageInfo(String packageName, int flags)
Retrieve overall information about an application package that is installed on the system or a running instant app.
abstract String[]
getPackagesForUid(int uid)
Retrieve the names of all packages that are associated with a particular user id.
abstract boolean
isInstantApp()
Returns true if this application is an instant app.
abstract boolean
isInstantApp(String packageName)
Returns true if the provided package is that of a running instant app that has whitelisted the calling application.
abstract boolean
setInstantAppCookie(byte[] cookie)
Sets the instant application cookie for the calling app.

Public Methods

public abstract ApplicationInfo getApplicationInfo (String packageName, int flags)

Retrieve all of the information we know about a particular package/application.

Information about installed apps will only be returned if requested when constructing the implementation of this interface; information about instant apps will only be returned if they are running and have whitelisted the calling application.

Parameters
packageName the full name (i.e. com.google.apps.contacts) of an application
flags additional option flags. Supported flags are defined in getApplicationInfo(String, int); instant apps will return data for the flag GET_META_DATA.
Returns
  • ApplicationInfo object containing information about the package.
Throws
PackageManager.NameNotFoundException if a package with the given name is not installed (or was not requested) and is not a package name of a running instant app

public abstract CharSequence getApplicationLabel (ApplicationInfo info)

Return the label to use for this application.

Information about installed apps will only be returned if requested when constructing the implementation of this interface; information about instant apps will only be returned if they are running and have whitelisted the calling application.

Parameters
info The application to get the label of.
Returns
  • Returns the label associated with this application, or null if it could not be found for any reason.

public abstract String getInstallerPackageName (String packageName)

Retrieve the package name of the application that installed a package. This identifies which market the package came from.

Parameters
packageName The name of the package to query

public abstract byte[] getInstantAppCookie ()

Gets the instant application cookie for this app. Non instant apps and apps that were instant but were upgraded to normal apps can still access this API. For instant apps this cookie is cached for some time after uninstall while for normal apps the cookie is deleted after the app is uninstalled. The cookie is always present while the app is installed.

This can be called by instant apps or installed apps.

Returns
  • The cookie, or null if no cookie was previously-set.

public abstract int getInstantAppCookieMaxSize ()

Gets the maximum size in bytes of the cookie data an instant app can store on the device.

This can be called by instant apps or installed apps.

Returns
  • The max cookie size in bytes.

public abstract PackageInfo getPackageInfo (String packageName, int flags)

Retrieve overall information about an application package that is installed on the system or a running instant app.

Information about installed apps will only be returned if requested when constructing the implementation of this interface; information about instant apps will only be returned if they are running and have whitelisted the calling application.

Parameters
packageName the full name (i.e. com.google.apps.contacts) of the desired package
flags additional option flags. Supported flags are defined in getPackageInfo(String, int); instant apps will return data for the flags GET_ACTIVITIES, GET_CONFIGURATIONS, GET_META_DATA, GET_PERMISSIONS, and GET_SIGNATURES
Returns
  • PackageInfo object containing information about the package.
Throws
PackageManager.NameNotFoundException if a package with the given name is not installed (or was not requested) and is not a package name of a running instant app

public abstract String[] getPackagesForUid (int uid)

Retrieve the names of all packages that are associated with a particular user id.

Information about installed apps will only be returned if requested when constructing the implementation of this interface; information about instant apps will only be returned if they are running and have whitelisted the calling application.

Parameters
uid the user id for which you would like to retrieve the associated packages
Returns
  • an array of one or more packages assigned to the user id, or null if there are no known packages with the given id

public abstract boolean isInstantApp ()

Returns true if this application is an instant app.

Returns
  • true if this application is an instant app, false otherwise.

public abstract boolean isInstantApp (String packageName)

Returns true if the provided package is that of a running instant app that has whitelisted the calling application.

Parameters
packageName the package you want to check for being an instant app.
Returns
  • true if the package is that of a running instant app, false otherwise.

public abstract boolean setInstantAppCookie (byte[] cookie)

Sets the instant application cookie for the calling app. Non instant apps and apps that were instant but were upgraded to normal apps can still access this API. For instant apps this cookie is cached for some time after uninstall while for normal apps the cookie is deleted after the app is uninstalled. The cookie is always present while the app is installed. The cookie size is limited by getInstantAppCookieMaxSize(). If the provided cookie size is over the limit this method returns false. Passing null or an empty array clears the cookie.

Parameters
cookie The cookie data.
Returns
  • True if the cookie was set. False if cookie is too large or I/O fails.