一 system_server启动PMS
Android的所有Java服务都是通过system_server进程启动的,并且驻留在system_server进程中。SystemServer进程在启动时,通过创建一个ServerThread线程启动所有服务。
1.1 startBootstrapServices()
system_server的startBootstrapServices()函数会启动一些引导服务,比如:
- ActivityManagerService
- PowerManagerService
- DisplayManagerService
- SensorService
其中PackageManagerService就在这里启动。
private void startBootstrapServices() { //启动installer服务 Installer installer = mSystemServiceManager.startService(Installer.class); // We need the default display before we can initialize the package manager. mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); //处于加密状态则仅仅解析核心应用 // Only run "core" apps if we're encrypting the device. String cryptState = SystemProperties.get("vold.decrypt"); if (ENCRYPTING_STATE.equals(cryptState)) { Slog.w(TAG, "Detected encryption in progress - only parsing core apps"); mOnlyCore = true; } else if (ENCRYPTED_STATE.equals(cryptState)) { Slog.w(TAG, "Device encrypted - only parsing core apps"); mOnlyCore = true; } // 创建PMS对象 - 启动入口 traceBeginAndSlog("StartPackageManagerService"); mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); // 是否首次启动 mFirstBoot = mPackageManagerService.isFirstBoot(); // 获取PackageManager mPackageManager = mSystemContext.getPackageManager(); Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); }
1.2 startOtherService()
system_server的startOtherService()方法会启动其他服务,在这里也会对PMS做一些处理
private void startOtherServices() { ...... if (!mOnlyCore) { ........ try { //将调用performDexOpt:Performs dexopt on the set of packages mPackageManagerService.updatePackagesIfNeeded(); }....... ........ try { //执行Fstrim,执行磁盘维护操作 //可能类似于TRIM技术,将标记为删除的文件,彻底从硬盘上移除 //而不是等到写入时再移除,目的是提高写入时效率 mPackageManagerService.performFstrimIfNeeded(); }......... ....... try { mPackageManagerService.systemReady(); }........ ....... } }
PMS启动后将参与一些系统优化的工作,然后调用SystemReady方法通知系统服务进入就绪状态
在system_server进程启动过程,涉及PMS服务的主要几个处理:
- PMS.main()
- PMS.performDexOpt()
- PMS.systemReady()
二 PMS.main的入口
public static final PackageManagerService main(Context context, Installer installer,boolean factoryTest, boolean onlyCore) { PackageManagerService m = new PackageManagerService(context, installer,factoryTest, onlyCore); ServiceManager.addService("package", m); return m; }
在这个main入口中,直接实例化一个PMS服务,并将PMS服务放入ServiceManager中,便于管理,那么重点就是实例化PMS
代码比较多,选择一些重点
new PackageManagerService(context, installer, factoryTest, onlyCore);
public PackageManagerService(Context context, Installer installer, boolean factoryTest, boolean onlyCore) { EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_PMS_START, SystemClock.uptimeMillis()); if (mSdkVersion <= 0) { Slog.w(TAG, "**** ro.build.version.sdk not set!"); } mContext = context; mFactoryTest = factoryTest; mOnlyCore = onlyCore; mLazyDexOpt = "eng".equals(SystemProperties.get("ro.build.type")); // displayMetrics是一个描述界面显示,尺寸,分辨率,密度的类 mMetrics = new DisplayMetrics(); // Settings是Android的全局管理者,用于协助PMS保存所有的安装包信息 mSettings = new Settings(context); mSettings.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); mSettings.addSharedUserLPw("android.uid.log", LOG_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID, ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PRIVILEGED); ....... mInstaller = installer; // 获取默认的显示信息,保存到mMetrics getDefaultDisplayMetrics(context, mMetrics); // 获取系统配置信息 SystemConfi