在分析PackageManagerService中main()之前,先分析Settings类.这个类用来保存和PackageManagerService相关的一些设置.先来看看Settings类的方法,代码如下
Settings(Object lock) {
this(Environment.getDataDirectory(), lock);
}
Settings(File dataDir, Object lock) {
mLock = lock;
mRuntimePermissionsPersistence = new RuntimePermissionPersistence(mLock);
mSystemDir = new File(dataDir, "system");
mSystemDir.mkdirs();
FileUtils.setPermissions(mSystemDir.toString(),
FileUtils.S_IRWXU|FileUtils.S_IRWXG
|FileUtils.S_IROTH|FileUtils.S_IXOTH,
-1, -1);
mSettingsFilename = new File(mSystemDir, "packages.xml");
mBackupSettingsFilename = new File(mSystemDir, "packages-backup.xml");
mPackageListFilename = new File(mSystemDir, "packages.list");
FileUtils.setPermissions(mPackageListFilename, 0640, SYSTEM_UID, PACKAGE_INFO_GID);
// Deprecated: Needed for migration
mStoppedPackagesFilename = new File(mSystemDir, "packages-stopped.xml");
mBackupStoppedPackagesFilename = new File(mSystemDir, "packages-stopped-backup.xml");
}
以上的方法主要作用是创建了5个位于data/system/目录下的File对象.
1)packages.xml: 记录系统中所有安装的应用信息,包括基本信息,签名和权限.
2)packagers-backup.xml:是packages.xml文件的备份.
3)packagers-stopped.xml:记录系统中被强制停止运行的应用信息.系统在强制停止某个应用时,会将应用的信息记录到改文件中.
4)packagers-stopped-backup.xml:是packagers-stopped.xml备份.
5)packages.list:保存普通应用的数据目录和uid等信息.
分析完Settings.java的一些基本信息,我们来分析下PackageManagerService.java的初始化基本过程
Step 1 :
这个函数定义在frameworks/base/services/java/com/android/server/SystemServer.java文件中
/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored
* and organized.
*/
private void startOtherServices() {
....
try {
mPackageManagerService.performBootDexOpt();
} catch (Throwable e) {
reportWtf("performing boot dexopt", e);
}
...
try {
mPackageManagerService.systemReady();
} catch (Throwable e) {
reportWtf("making Package Manager Service ready", e);
}
}
private void startBootstrapServices() {
....
// Start the package manager.
Slog.i(TAG, "Package Manager");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
mFirstBoot = mPackageManagerService.isFirstBoot();
mPackageManager = mSystemContext.getPackageManager();
...
startSensorService();
}
通过以上代码可以看出,SystemServer对PackagerManagerService的初始化主要通过上面的代码来完成的,接下来我们来分析下PackagerManagerService的main初始化过程.
Step 2:
这个文件定义在frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java中
public static PackageManagerService main(Context context, Installer installer,
boolean factoryTest, boolean onlyCore) {
PackageManagerService m;
if (FeatureConfig.INTEL_FEATURE_PARALLEL_SCAN) {
int taskNum = SystemProperties.getInt("persist.pms.tasknum",
Runtime.getRuntime().availableProcessors()*2);
m = new PackageManagerService(context, installer,
factoryTest, onlyCore, taskNum);
} else {
m = new PackageManagerService(context, installer,
factoryTest, onlyCore);
}
ServiceManager.addService("package", m);
return m;
}
通过以上代码看出,main的方法比较简单,主要是创建了PackageManagerService对象并在ServiceManager注册.