Android 9.0 SystemUI 启动流程

基于 AOSP 9.0 分析。

开机流程

开机大致会经历如下几个过程:

  1. uboot 在引导 os 启动,然后加载 kernel;
  2. 当 kernel 加载完成后,进入 init 进程,fork 出 zygote,然后由 zygote 去启动 SystemServer;
  3. SystemServer 会启动系统运行所需的众多核心服务和普通服务,以及初始化和加载一些应用;
  4. 之后就进入到锁屏或者 Launcher,开机过程就基本结束了。

SystemUI 启动就是从 SystemServer 开始的。

序列图

SystemServer#main

SystemServer 名为系统服务进程,负责启动 Android 系统的关键服务,其入口是 SystemServer.main():

public static void main(String[] args) {
    new SystemServer().run();
}
复制代码

可以看到 main() 中生成了 SystemServer 对象并执行了 run 方法:

private void run() {
    //省略部分代码
    // Start services.
    try {
        traceBeginAndSlog("StartServices");
        startBootstrapServices();
        startCoreServices();
        startOtherServices();
        SystemServerInitThreadPool.shutdown();
    } catch (Throwable ex) {
        Slog.e("System", "******************************************");
        Slog.e("System", "************ Failure starting system services", ex);
        throw ex;
    } finally {
        traceEnd();
    }
   //省略部分代码
}
复制代码

查看 startOtherServices():

private void startOtherServices() {
    //省略部分代码
    traceBeginAndSlog("StartSystemUI");
    try {
        startSystemUi(context, windowManagerF);
    } catch (Throwable e) {
        reportWtf("starting System UI", e);
    }
    traceEnd();
    //省略部分代码
}

static final void startSystemUi(Context context, WindowManagerService windowManager) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.android.systemui",
                "com.android.systemui.SystemUIService"));
    intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
    //Slog.d(TAG, "Starting service: " + intent);
    context.startServiceAsUser(intent, UserHandle.SYSTEM);
    windowManager.onSystemUiStarted();
}
复制代码

通过 intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.SystemUIService"))启动了服务 SystemUIService,进入 SystemUIService。

SystemUIService#onCreate

public class SystemUIService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
        //省略其他代码
复制代码

onCreate 方法中获得 SystemUIApplication 对象并调用其 startServicesIfNeeded 方法。

SystemUIApplication#startServicesIfNeeded

public void startServicesIfNeeded() {
    String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
    startServicesIfNeeded(names);
}
复制代码

其中 config_systemUIServiceComponents 值在 AOSP/frameworks/base/packages/SystemUI/res/values/config.xml 里:

<string-array name="config_systemUIServiceComponents" translatable="false">
    <!-- 对其他模块提供组件支持 -->
    <item>com.android.systemui.Dependency</item>
    <!-- NotificationChannel -->
    <item>com.android.systemui.util.NotificationChannels</item>
    <item>com.android.systemui.statusbar.CommandQueue$CommandQueueStart</item>
    <!-- 锁屏模块 -->
    <item>com.android.systemui.keyguard.KeyguardViewMediator</item>
    <!-- 最近应用 -->
    <item>com.android.systemui.recents.Recents</item>
    <!-- 全局音量控制 -->
    <item>com.android.systemui.volume.VolumeUI</item>
    <!-- 分屏功能调节器 -->
    <item>com.android.systemui.stackdivider.Divider</item>
    <!-- 系统状态栏,包括 StatusBar、NavigationBar、快捷菜单等等 -->
    <item>com.android.systemui.SystemBars</item>
    <!-- Storage 存储通知 -->
    <item>com.android.systemui.usb.StorageNotification</item>
    <!-- 电量管理相关 -->
    <item>com.android.systemui.power.PowerUI</item>
    <!-- 铃声播放 -->
    <item>com.android.systemui.media.RingtonePlayer</item>
    <!-- 键盘相关 -->
    <item>com.android.systemui.keyboard.KeyboardUI</item>
    <!-- 画中画界面 -->
    <item>com.android.systemui.pip.PipUI</item>
    <!-- 快捷键调度程序 -->
    <item>com.android.systemui.shortcut.ShortcutKeyDispatcher</item>
    <item>@string/config_systemUIVendorServiceComponent</item>
    <item>com.android.systemui.util.leak.GarbageMonitor$Service</item>
    <item>com.android.systemui.LatencyTester</item>
    <item>com.android.systemui.globalactions.GlobalActionsComponent</item>
    <!-- 截屏界面 -->
    <item>com.android.systemui.ScreenDecorations</item>
    <!-- 指纹相关 -->
    <item>com.android.systemui.fingerprint.FingerprintDialogImpl</item>
    <item>com.android.systemui.SliceBroadcastRelayHandler</item>
</string-array>
复制代码

SystemUIApplication#startServicesIfNeeded

继续看 startServicesIfNeeded 方法:

private void startServicesIfNeeded(String[] services) {
    if (mServicesStarted) {
        return;
    }
    mServices = new SystemUI[services.length];
    if (!mBootCompleted) {
        // check to see if maybe it was already completed long before we began
        // see ActivityManagerService.finishBooting()
        if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
            mBootCompleted = true;
            if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
        }
    }
    Log.v(TAG, "Starting SystemUI services for user " +
            Process.myUserHandle().getIdentifier() + ".");
    TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming",
            Trace.TRACE_TAG_APP);
    log.traceBegin("StartServices");
    final int N = services.length;
    for (int i = 0; i < N; i++) {
        String clsName = services[i];
        if (DEBUG) Log.d(TAG, "loading: " + clsName);
        log.traceBegin("StartServices" + clsName);
        long ti = System.currentTimeMillis();
        Class cls;
        try {
            cls = Class.forName(clsName);
            mServices[i] = (SystemUI) cls.newInstance();
        } catch(ClassNotFoundException ex){
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        }
        mServices[i].mContext = this;
        mServices[i].mComponents = mComponents;
        if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
        mServices[i].start();
        log.traceEnd();
        //省略其他代码
    }
}
复制代码

可以看到 startServicesIfNeeded() 循环 start 了config_systemUIServiceComponents 里的 Service,这里的 Service 都是继承了 SystemUI 类的子服务,后续将一一分析这些 Service。这样,SystemUI 启动流程分析完毕。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值