ActivityManagerService安装SettingsProvider的过程详解—Android 12(三)

先介绍了SystemServer.startBootstrapServices()中AMS和ATMS启动相关业务,ActivityManagerService和ActivityTaskManagerService启动详解

,接着介绍了startCoreServices()中AMS设置UsageStatsManagerInternal相关业务ActivityManagerService和ActivityTaskManagerService详解—Android 12(二),本章介绍startOtherServices()中与AMS和ATMS相关的业务。


目录

1. startOtherServices()中installSystemProviders()过程

1.1 AMS.getContentProviderHelper()

1.2 ContentProviderHelper.installSystemProviders()

1.3 ActivityThread.installSystemProviders()


安装SettingsProvider的过程如下图所示:

1. startOtherServices()中installSystemProviders()过程

// /frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer implements Dumpable {
    private void startOtherServices(TimingsTraceAndSlog t) {
        //1.加载SettingsProvider到systemserver进程中
        t.traceBegin("InstallSystemProviders");
        mActivityManagerService.getContentProviderHelper().installSystemProviders();
        // Now that SettingsProvider is ready, reactivate SQLiteCompatibilityWalFlags
        SQLiteCompatibilityWalFlags.reset();
        t.traceEnd();   
        //2.启动WMS, 注册WMS到systemserver进程中
        wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
                    new PhoneWindowManager(),    mActivityManagerService.mActivityTaskManager);
        ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
                    DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
        //注册IMS到systemserver进程
        ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
                    /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
        t.traceEnd();
        //3. 设置AMS的WM
        t.traceBegin("SetWindowManagerService");
        mActivityManagerService.setWindowManager(wm);
        t.traceEnd();
        //WM.onInitReady
        t.traceBegin("WindowManagerServiceOnInitReady");
        wm.onInitReady();
        t.traceEnd();
        
        t.traceBegin("MakePowerManagerServiceReady");
        try {
            //PowerMS启动完成
            mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
        } catch (Throwable e) {
            reportWtf("making Power Manager Service ready", e);
        }
        t.traceEnd();
        //4.AMS.systemReady()
        mActivityManagerService.systemReady(() -> {
            Slog.i(TAG, "Making services ready");
            t.traceBegin("StartActivityManagerReadyPhase");
            mSystemServiceManager.startBootPhase(t, SystemService.PHASE_ACTIVITY_MANAGER_READY);
            t.traceEnd();
            t.traceBegin("StartObservingNativeCrashes");
            try {
                mActivityManagerService.startObservingNativeCrashes();
            } catch (Throwable e) {
                reportWtf("observing native crashes", e);
            }
            t.traceEnd();

            t.traceBegin("RegisterAppOpsPolicy");
            try {
                mActivityManagerService.setAppOpsPolicy(new AppOpsPolicy(mSystemContext));
            } catch (Throwable e) {
                reportWtf("registering app ops policy", e);
            }
            t.traceEnd();
            ...
        }     
    }
}

1.1 AMS.getContentProviderHelper()

// /frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java    
public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback, ActivityManagerGlobalLock {
    final ContentProviderHelper mCpHelper;
    public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
        ...        
        mCpHelper = new ContentProviderHelper(this, false);
    }

    public ContentProviderHelper getContentProviderHelper() {
        return mCpHelper;
    }
}

1.2 ContentProviderHelper.installSystemProviders()

ContentProviderHelper是为ams处理content provider服务的,它的代码地址为:ContentProviderHelper.java - OpenGrok cross reference for /frameworks/base/services/core/java/com/android/server/am/ContentProviderHelper.java

 该方法的主要逻辑:

  1. 获取AMS中process="system"且UID=“SYSTEM_UID”的ProcessRecord。
  2. 从PKMS中查找符合上述信息的Providers,即SettingsProvider,并把这些providers保存到AMS和ProcessRecord中对应的数据结构中。
  3. 接着把SettingsProvider安装到系统的主进程中,即systemserver进程。
///frameworks/base/services/core/java/com/android/server/am/ContentProviderHelper.java    
public class ContentProviderHelper {
    private final ActivityManagerService mService;    
    ContentProviderHelper(ActivityManagerService service, boolean createProviderMap) {
        mService = service;
        mProviderMap = createProviderMap ? new ProviderMap(mService) : null;
    }
    public final void installSystemProviders() {
        List<ProviderInfo> providers;
        synchronized (mService) {
            //1.从AMS的processList中找到进程为"system"且uid="SYSTEM_UID"的ProcessRecord
            ProcessRecord app = mService.mProcessList
                    .getProcessNamesLOSP().get("system", SYSTEM_UID);
            //从PKMS查询满足条件的ProviderInfo,保存到AMS和ProcessRecord中对应的数据结构中
            providers = generateApplicationProvidersLocked(app);
            if (providers != null) {
                for (int i = providers.size() - 1; i >= 0; i--) {
                    ProviderInfo pi = providers.get(i);
                    if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                        //去除providers中非系统APK(没有设置SYSTEM_UID的apk)的Provider
                        Slog.w(TAG, "Not installing system proc provider " + pi.name
                                + ": not system .apk");
                        providers.remove(i);
                    }
                }
            }
        }

        if (providers != null) {
            //为AMS的mSystemThread(它是systemserver的主线程)安装SystemProviders,
            //因此是为systemserver安装系统providers.
            mService.mSystemThread.installSystemProviders(providers);
        }
        synchronized (this) {
            mSystemProvidersInstalled = true;
        }

        mService.mConstants.start(mService.mContext.getContentResolver());
        mService.mCoreSettingsObserver = new CoreSettingsObserver(mService);
        mService.mActivityTaskManager.installSystemProviders();
        new DevelopmentSettingsObserver(); // init to observe developer settings enable/disable
        SettingsToPropertiesMapper.start(mService.mContext.getContentResolver());
        mService.mOomAdjuster.initSettings();

        // Now that the settings provider is published we can consider sending in a rescue party.
        RescueParty.onSettingsProviderPublished(mService.mContext);
    }

上述代码的1中查找process="system"且有“SYSTEM_UID”权限,得到的Providers就是SettingsProvider,因为它的AndroidManifest.xml中就表明了如下所示:

 从上述文件可以看到SettingsProvider中只有一个provider组件,之后会安装该provider。

 SettingsProvider的Android.bp和代码地址如下:AndroidManifest.xml - OpenGrok cross reference for /frameworks/base/packages/SettingsProvider/AndroidManifest.xml

1.3 ActivityThread.installSystemProviders()

该方法是实际上调用自身的installContentProvides()来实现,参数providers是SettinsProvider的AndroidManifest.xml中声明的一个provider。

// /frameworks/base/core/java/android/app/ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler
        implements ActivityThreadInternal {
    @UnsupportedAppUsage
    public final void installSystemProviders(List<ProviderInfo> providers) {
        if (providers != null) {
            //该providers中是SettingProviders中的provider,调用install content provider的方法
            installContentProviders(mInitialApplication, providers);
        }
    }

    @UnsupportedAppUsage
    private void installContentProviders(Context context, List<ProviderInfo> providers) {
        final ArrayList<ContentProviderHolder> results = new ArrayList<>();

        for (ProviderInfo cpi : providers) {
            ...//遍历安装provider   
            ContentProviderHolder cph = installProvider(context, null, cpi,
                    false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
            if (cph != null) {
                cph.noReleaseNeeded = true;
                results.add(cph);
            }
        }

        try {
            //向AMS发布这个SettingsProvider,之后settings数据库相关操作均由它管理
            ActivityManager.getService().publishContentProviders(
                getApplicationThread(), results);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值