SystemUI启动流程——基于Android P AOSP

Android系统启动流程:

  1. 按下电源,系统上电
  2. 从固定地址启动固化在ROM的BootLoader程序
  3. 启动Linux内核,加载init进程
  4. 启动init进程,fork出zygote进程
  5. 启动SystemServer,启动Binder线程池以及各种服务
  6. AMS启动Launcher, 然后你就看到该看到的了

SystemUI作为系统主要进程,就是在SystemServer启动的服务其中之一。
话不多说,read the fucking source code:

SystemServer.java

    public static void main(String[] args) {
        new 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();
        }
        ......
	}


    private void startOtherServices() {
    	......
        mActivityManagerService.systemReady(() -> {
            Slog.i(TAG, "Making services ready");
            traceBeginAndSlog("StartActivityManagerReadyPhase");
            ......
            try {
                startSystemUi(context, windowManagerF);
            } catch (Throwable e) {
                reportWtf("starting System UI", e);
            }
            ......
       }
    }


    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();
    }

总结下代码流程:SystemServermain()方法会调用SystemServer.run()方法,在run方法中,会启动bootStrapService,coreService,以及其他服务otherService,在startOtherServices()中,会注册AMS的systemReady回调,在回调中启动SystemUI,其实就是启动了SystemUIService。

SystemUIService.java

    @Override
    public void onCreate() {
        super.onCreate();
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
        .....
    }

SystemUIApplication.java

    @Override
    public void startServicesIfNeeded() {
        String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
        startServicesIfNeeded(names);
    }


	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();
            ......
        }
    }
    
    <string-array name="config_systemUIServiceComponents" translatable="false">
        <item>com.android.systemui.Dependencyitem>
        <item>com.android.systemui.util.NotificationChannelsitem>
        <item>com.android.systemui.statusbar.CommandQueue$CommandQueueStartitem>
        <item>com.android.systemui.keyguard.KeyguardViewMediatoritem>
        <item>com.android.systemui.recents.Recentsitem>
        <item>com.android.systemui.volume.VolumeUIitem>
        <item>com.android.systemui.stackdivider.Divideritem>
        <item>com.android.systemui.SystemBarsitem>
        <item>com.android.systemui.usb.StorageNotificationitem>
        <item>com.android.systemui.power.PowerUIitem>
        <item>com.android.systemui.media.RingtonePlayeritem>
        <item>com.android.systemui.keyboard.KeyboardUIitem>
        <item>com.android.systemui.pip.PipUIitem>
        <item>com.android.systemui.shortcut.ShortcutKeyDispatcheritem>
        <item>@string/config_systemUIVendorServiceComponentitem>
        <item>com.android.systemui.util.leak.GarbageMonitor$Serviceitem>
        <item>com.android.systemui.LatencyTesteritem>
        <item>com.android.systemui.globalactions.GlobalActionsComponentitem>
        <item>com.android.systemui.ScreenDecorationsitem>
        <item>com.android.systemui.fingerprint.FingerprintDialogImplitem>
        <item>com.android.systemui.SliceBroadcastRelayHandleritem>
    string-array>

    
    <string name="config_systemUIVendorServiceComponent" translatable="false">com.android.systemui.VendorServicesstring>

SystemUIService的onCreate()方法中,直接调用SystemUIApplication.startServicesIfNeeded()方法,最终会将所有服务一一加载并通过start()方法启动起来。

至此,SystemUI基本启动流程就结束了。

你可能感兴趣的:(Android)