开篇
核心源码
关键类 | 路径 |
---|---|
Context.java | frameworks/base/core/java/android/content/Context.java |
ContextImpl.java | frameworks/base/core/java/android/app/ContextImpl.java |
PackageManager.java | frameworks/base/core/java/android/content/pm/PackageManager.java |
ApplicationPackageManager.java | frameworks/base/core/java/android/app/ApplicationPackageManager.java |
ActivityThread.java | frameworks/base/core/java/android/app/ActivityThread.java |
PackageManager
简介
PackageManager 是一个抽象类:
/**
* Class for retrieving various kinds of information related to the application
* packages that are currently installed on the device.
*
* You can find this class through {@link Context#getPackageManager}.
*/
public abstract class PackageManager {
注释可以看出:PackageManager 这个类是检测当前已经安装在当前设备上的应用程序包的信息。你可以调用 Context 类的 getPackageManager() 方法来获取 PackageManager。
安装 APK
PackageManager 是一个实际上管理应用程序安装、卸载和升级的 API。当我们安装 APK 文件时,PackageManager 会解析 APK 包文件和显示确认信息。当我们点击 OK 按钮后,PackageManager 会调用一个叫 "InstallPackage" 的方法,这个方法有 4 个参数,也就是 uri、installFlags、observer、installPackagename。PackageManager 会启动一个叫 "package" 的 servcie 服务,现在所有模糊的东西会发生在这个 service 中。
实现功能
✨ 1、安装、卸载应用
✨ 2、查询 permission 相关信息
✨ 3、查询 Application 相关信息(application、activity、receiver、service、provider 及相应属性等)
✨ 4、查询已安装应用
✨ 5、增加、删除 permission
✨ 6、清除用户数据、缓存、代码等
抽象方法
getPackageInfo
/**
* Retrieve overall information about an application package that is
* installed on the system.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
* desired package.
* @param flags Additional option flags to modify the data returned.
* @return A PackageInfo object containing information about the package. If
* flag {@code MATCH_UNINSTALLED_PACKAGES} is set and if the package
* is not found in the list of installed applications, the package
* information is retrieved from the list of uninstalled
* applications (which includes installed applications as well as
* applications with data directory i.e. applications which had been
* deleted with {@code DONT_DELETE_DATA} flag set).
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 通过包名获取该包名对应的应用程序的 PackageInfo 对象,
* PackageInfo 类包含了从 AndroidManifest.xml 文件中收集的所有信息。
*/
public abstract PackageInfo getPackageInfo(String packageName, @PackageInfoFlags int flags)
throws NameNotFoundException;
getApplicationInfo
/**
* Retrieve all of the information we know about a particular
* package/application.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of an
* application.
* @param flags Additional option flags to modify the data returned.
* @return An {@link ApplicationInfo} containing information about the
* package. If flag {@code MATCH_UNINSTALLED_PACKAGES} is set and if
* the package is not found in the list of installed applications,
* the application information is retrieved from the list of
* uninstalled applications (which includes installed applications
* as well as applications with data directory i.e. applications
* which had been deleted with {@code DONT_DELETE_DATA} flag set).
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 根据包名返回其对应的 ApplicationInfo 信息。
*/
public abstract ApplicationInfo getApplicationInfo(String packageName,
@ApplicationInfoFlags int flags) throws NameNotFoundException;
getActivityInfo
/**
* Retrieve all of the information we know about a particular activity
* class.
*
* @param component The full component name (i.e.
* com.google.apps.contacts/com.google.apps.contacts.
* ContactsList) of an Activity class.
* @param flags Additional option flags to modify the data returned.
* @return An {@link ActivityInfo} containing information about the
* activity.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 检索出一个特定的 Activity 类的所有信息。
*/
public abstract ActivityInfo getActivityInfo(ComponentName component,
@ComponentInfoFlags int flags) throws NameNotFoundException;
getReceiverInfo
/**
* Retrieve all of the information we know about a particular receiver
* class.
*
* @param component The full component name (i.e.
* com.google.apps.calendar/com.google.apps.calendar.
* CalendarAlarm) of a Receiver class.
* @param flags Additional option flags to modify the data returned.
* @return An {@link ActivityInfo} containing information about the
* receiver.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 检索出一个特定的 Receiver 类的所有信息(这里主要指 ActivityInfo)。
*/
public abstract ActivityInfo getReceiverInfo(ComponentName component,
@ComponentInfoFlags int flags) throws NameNotFoundException;
getServiceInfo
/**
* Retrieve all of the information we know about a particular service class.
*
* @param component The full component name (i.e.
* com.google.apps.media/com.google.apps.media.
* BackgroundPlayback) of a Service class.
* @param flags Additional option flags to modify the data returned.
* @return A {@link ServiceInfo} object containing information about the
* service.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 检索出一个特定的 Service 类的所有信息(这里主要指 ServiceInfo)。
*/
public abstract ServiceInfo getServiceInfo(ComponentName component,
@ComponentInfoFlags int flags) throws NameNotFoundException;
getProviderInfo
/**
* Retrieve all of the information we know about a particular content
* provider class.
*
* @param component The full component name (i.e.
* com.google.providers.media/com.google.providers.media.
* MediaProvider) of a ContentProvider class.
* @param flags Additional option flags to modify the data returned.
* @return A {@link ProviderInfo} object containing information about the
* provider.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 检索出一个特定的 content provider 类的所有信息(这里主要指 ProviderInfo)。
*/
public abstract ProviderInfo getProviderInfo(ComponentName component,
@ComponentInfoFlags int flags) throws NameNotFoundException;
getInstalledPackages
/**
* Return a List of all packages that are installed for the current user.
*
* @param flags Additional option flags to modify the data returned.
* @return A List of PackageInfo objects, one for each installed package,
* containing information about the package. In the unlikely case
* there are no installed packages, an empty list is returned. If
* flag {@code MATCH_UNINSTALLED_PACKAGES} is set, the package
* information is retrieved from the list of uninstalled
* applications (which includes installed applications as well as
* applications with data directory i.e. applications which had been
* deleted with {@code DONT_DELETE_DATA} flag set).
*
* 获取设备上安装的所有软件包。
*/
public abstract List<PackageInfo> getInstalledPackages(@PackageInfoFlags int flags);
currentToCanonicalPackageNames
/**
* Map from the current package names in use on the device to whatever
* the current canonical name of that package is.
* @param names Array of current names to be mapped.
* @return Returns an array of the same size as the original, containing
* the canonical name for each package.
*
* 从设备上使用当前包名映射到该软件包名的当前规范名称,
* 如果修改包名会用到,没有修改过包名一般不会用到。
*/
public abstract String[] currentToCanonicalPackageNames(String[] names);
canonicalToCurrentPackageNames
/**
* Map from a packages canonical name to the current name in use on the device.
* @param names Array of new names to be mapped.
* @return Returns an array of the same size as the original, containing
* the current name for each package.
*
* 将软件包规范名称映射到设备上正在使用的当前名称,
* 我们发现:canonicalToCurrentPackageNames() 和 currentToCanonicalPackageNames() 方法是相反的两个方法
*/
public abstract String[] canonicalToCurrentPackageNames(String[] names);
getPermissionInfo
/**
* Retrieve all of the information we know about a particular permission.
*
* @param name The fully qualified name (i.e. com.google.permission.LOGIN)
* of the permission you are interested in.
* @param flags Additional option flags to modify the data returned.
* @return Returns a {@link PermissionInfo} containing information about the
* permission.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 检测出我们想要知道的所有关于权限的信息。
*/
public abstract PermissionInfo getPermissionInfo(String name, @PermissionInfoFlags int flags)
throws NameNotFoundException;
queryPermissionsByGroup
/**
* Query for all of the permissions associated with a particular group.
*
* @param group The fully qualified name (i.e. com.google.permission.LOGIN)
* of the permission group you are interested in. Use null to
* find all of the permissions not associated with a group.
* @param flags Additional option flags to modify the data returned.
* @return Returns a list of {@link PermissionInfo} containing information
* about all of the permissions in the given group.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* 查询与特定组相关的所有权限。
*/
public abstract List<PermissionInfo> queryPermissionsByGroup(String group,
@PermissionInfoFlags int flags) throws NameNotFoundException;
getAllPermissionGroups
/**
* Retrieve all of the known permission groups in the system.
*
* @param flags Additional option flags to modify the data returned.
* @return Returns a list of {@link PermissionGroupInfo} containing
* information about all of the known permission groups.
*
* 检索出系统中所有已知的权限。
*/
public abstract List<PermissionGroupInfo> getAllPermissionGroups(
@PermissionGroupInfoFlags int flags);
当然除了上面列举出来的方法以外,还有其他很多方法,我们不再一一列出来,如果后面分析遇到会再单独拿出来分析!
安装方法
接下来我们来看看 PackageManager 中关于安装的几个方法!
InstallPackage
/**
* packageURI:表示安装的路径,可以是 "file:" 或者 "content:" 的 URI
* observer: 一个回调的观察者,有了这个观察者,就可以在软件包安装完成后得到安装结果的通知。
* 如果安装完成会调用这个观察者 IPackageInstallObserver 的 packageInstalled(String,int)方法,observer这个入参不能为空。
* flags: 标志位参数
* nstallerPackageName:正在进行安装的安装包包名
*/
/**
* @deprecated replaced by {@link PackageInstaller}
* @hide
*/
@Deprecated
public abstract void installPackage( // 弃用
Uri packageURI,
IPackageInstallObserver observer,
@InstallFlags int flags,
String installerPackageName);
/**
* @deprecated replaced by {@link PackageInstaller}
* @hide
*/
@Deprecated
public abstract void installPackage( // 弃用
Uri packageURI,
PackageInstallObserver observer,
@InstallFlags int flags,
String installerPackageName);
从 8.1 开始,已经弃用了 installPackage 方法(9.0 的代码中已经去除 installPackage 代码了),而是使用 PackageInstaller 执行应用的安装、升级和删除操作。
/**
* Return interface that offers the ability to install, upgrade, and remove
* applications on the device.
*/
public abstract @NonNull PackageInstaller getPackageInstaller();
installExistingPackage
/**
* If there is already an application with the given package name installed
* on the system for other users, also install it for the calling user.
* @hide
*/
@SystemApi // 系统 API
public abstract int installExistingPackage(String packageName) throws NameNotFoundException;
通过注释可以看出这个方法的用途:如果系统上已经安装相同包名的应用程序,则重复重新安装。
具体实现类
我们知道 PackageManager 是一个抽象类,它里面很重要的方法都是抽象的,所以在具体执行的时候,肯定是它的实现子类,那么我们就来看下它的具体实现类。
前面我们讲解 PackageManager 类的时候,官网推荐获取 PackageManager 对象的方法是 Context 的 Context#getPackageManager() 方法,那我们来看下:
// frameworks/base/core/java/android/content/Context.java
/** Return PackageManager instance to find global package information. */
public abstract PackageManager getPackageManager();
我们知道 Context 也是一个抽象类,而它的 getPackageManager() 也是抽象方法,但 Context 的具体实现类是 ContextImpl,那我们就去 ContextImpl 里面看下:
/**
* Common implementation of Context API, which provides the base
* context object for Activity and other application components.
* @hide
*/
public class ContextImpl extends Context {
private PackageManager mPackageManager;
... ...
@Override
public PackageManager getPackageManager() {
// 判断 mPackageManager 是否为空,如果为空,则说明是第一次调用
if (mPackageManager != null) {
return mPackageManager;
}
// 调用 ActivityThread 的静态方法 getPackageManager() 获取一个 IPackageManager 对象
IPackageManager pm = ActivityThread.getPackageManager();
// 如果获取的 IPackageManager 对象不为空,则构造一个 ApplicationPackageManager 对象,ApplicationPackageManager 是 PackageManager 的子类
if (pm != null) {
// Doesn't matter if we make more than one instance.
return (mPackageManager = new ApplicationPackageManager(this, pm));
}
return null;
}
... ...
}
所以,在我们平时调用 Context 的 getPackageManager()方法后,其实返回的是 ApplicationPackageManager 这个类。
ApplicationPackageManager
简介
我们先来看下 ApplicationPackageManager 类的源码:
/** @hide */
public class ApplicationPackageManager extends PackageManager {
通过源码我们知道 ApplicationPackageManager 继承自PackageManager,而且 ApplicationPackageManager 类不是抽象的,所以 ApplicationPackageManager 必然实现了 PackageManager 的所有抽象方法。
构造函数
protected ApplicationPackageManager(ContextImpl context, IPackageManager pm) {
mContext = context;
mPM = pm;
}
InstallPackage
在讲解 PackageManager 的时候,我们提到过安装 APK 会调用 InstallPackage 方法(Android 8.1,9.0 已弃用),我们看下在 ApplicationPackageManager 中的具体实现:
@Override
public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
String installerPackageName) {
installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
installerPackageName, mContext.getUserId());
}
installCommon
// 源码来自:Android 8.1,9.0 已弃用
private void installCommon(Uri packageURI,
PackageInstallObserver observer, int flags, String installerPackageName,
int userId) {
// scheme 判断,如果非 "file" 则抛异常,因为只支持 file 格式的 URI
if (!"file".equals(packageURI.getScheme())) {
throw new UnsupportedOperationException("Only file:// URIs are supported");
}
// 获取相应的路径
final String originPath = packageURI.getPath();
try {
// 调用 installPackageAsUser 方法
mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
可以发现,public abstract void installPackage() 方法其内部本质是调用的 IPackageManager 的 installPackageAsUser() 方法。
IPackageManager
ActivityThread
我们先来看看如下代码(上面没有分析):
// 调用 ActivityThread 的静态方法 getPackageManager() 获取一个 IPackageManager 对象
IPackageManager pm = ActivityThread.getPackageManager();
跟踪 getPackageManager():
static volatile IPackageManager sPackageManager;
public static IPackageManager getPackageManager() {
// 判断 sPackageManager 是否为空,如果为空,则说明是的第一次调用,走第二步,如果不为空,则直接返回 sPackageManager
if (sPackageManager != null) {
return sPackageManager;
}
// 能走到第二步,说明这是第一次调用,则调用 ServiceManager 的 getService(String) 方法获取一个 IBinder 对象
IBinder b = ServiceManager.getService("package");
// 调用 IPackageManager.Stub.asInterface(IBinder) 获取一个 sPackageManager 对象
sPackageManager = IPackageManager.Stub.asInterface(b);
return sPackageManager;
}
IPM/PM/PMS 三者关联
APM/PMS/IPM
在上面分析 ContextImpl 的 getPackageManager() 方法里面,我们知道:
IPackageManager pm = ActivityThread.getPackageManager();
if (pm != null) {
// Doesn't matter if we make more than one instance.
return (mPackageManager = new ApplicationPackageManager(this, pm));
}
而在 ActivityThread 的静态方法 getPackageManager() 里面:
sPackageManager = IPackageManager.Stub.asInterface(b);
return sPackageManager;
所以在 ApplicationPackageManager 里面的 mPM 其实就是 IPackageManager.Stub 内部类 Proxy 对象。
那对应的 IPackageManager.Stub 是什么?其实就是 PackageManagerService.java 。
public class PackageManagerService extends IPackageManager.Stub implements PackageSender {
如下图所示:
小结
结合上面的知识,再结合 PackageManager、ApplicationPackageManager 和 PackageManagerService 总结如下:
✨ IPackageManager 负责通信:IPackageManager 接口类中定义了很多业务方法,但是由于安全等方面的考虑,Android 对外(即SDK)提供的仅仅是一个子集,该子集被封装在抽象类 PackageManager 中。客户端一般通过 Context 的 getPackageManager 函数返回一个类型为 PackageManager 的对象,该对象的实际类型是 PackageManager 的子类 ApplicationPackageManager 。ApplicationPackageManager 并没有直接参与 Binder 通信,而是通过 mPM 成员变量指向了一个 IPackageManager.Stub.Proxy 类型的对象。
✨ AIDL中 的 Binder 服务端是 PackageManagerService,因为 PackageManagerService 继承自 IPackageManager.Stub 。由于 IPackageManager.Stub 类从 Binder 派生,所以 PackageManagerService 将作为服务端参与 Binder 通信。
✨ AIDL中 的 Binder 客户端是 ApplicationPackageManager 中成员变量 mPM,因为mPM内部指向的是 IPackageManager.Stub.Proxy。
整体流程的 Binder 结构大致如下:
参考
01. https://www.jianshu.com/p/c56...
02. https://www.jianshu.com/p/a30...