[深入SystemUI]-SystemUI_statusbar的啟動流程

baijimao發表於2019-04-26

本文流程基於Android 9.0

[深入SystemUI]-SystemUI_statusbar的啟動流程

1.啟動SystemUIService

在SystemServer中有一個方法startSystemUi,當系統啟動後,會執行到SystemServer的startSystemUi()方法,正是在這個方法中啟動了SystemUIService。

static final void startSystemUi(Context context, WindowManagerService windowManager) {
    Intent intent = new Intent();
    // 指定了systemui的包名"com.android.systemui",指定了ServiceUIService的類名"com.android.systemui.SystemUIService"
    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();
}
複製程式碼

2.在SystemUIService中啟動SystemUI中的服務

SystemUIService.java

@Override
public void onCreate() {
    super.onCreate();
    //在oncreate方法中呼叫SystemUIApplication的startServicesIfNeeded來啟動相關服務
    ((SystemUIApplication) getApplication()).startServicesIfNeeded();

    // code...
}
複製程式碼

SystemUIApplication

/**
 * Makes sure that all the SystemUI services are running. If they are already running, this is a
 * no-op. This is needed to conditinally start all the services, as we only need to have it in
 * the main process.
 * <p>This method must only be called from the main thread.</p>
 */

public void startServicesIfNeeded() {
    String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
    startServicesIfNeeded(names);
}
複製程式碼

注意,這裡的servicenames是從config檔案中讀取出來的,具體內容如下,其中就包含了SystemBars的路徑:

<!-- SystemUI Services: The classes of the stuff to start. -->
<string-array name="config_systemUIServiceComponents" translatable="false">
    <item>com.android.systemui.Dependency</item>
    <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>
    <item>com.android.systemui.SystemBars</item>
    <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>
複製程式碼

具體的啟動過程

private void startServicesIfNeeded(String[] services) {
    //這裡就是方法名IfNeeded的原因,首先會判斷boolean變數mServicesStarted是否為ture,為true,表示服務已經啟動,就不需要執行後面的啟動流程了
    if (mServicesStarted) {
        return;
    }
    //定義一個用於啟動SystemUI服務的陣列,並且這些要啟動的服務都是繼承自SystemUI的
    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]);
        //呼叫各個服務重寫了的start()方法
        mServices[i].start();
        log.traceEnd();

        // Warn if initialization of component takes too long
        ti = System.currentTimeMillis() - ti;
        if (ti > 1000) {
            Log.w(TAG, "Initialization of " + cls.getName() + " took " + ti + " ms");
        }
        if (mBootCompleted) {
            mServices[i].onBootCompleted();
        }
    }
    // code...

    //啟動過一次後,就將mServicesStarted置為true,避免重複啟動
    mServicesStarted = true;
}
複製程式碼

3.在SystemBars中啟動statusbar

關於SystemBars類的註釋是這樣的,“根據產品配置,使用程式內實現,確保一個status bar服務能夠一直執行”。

/**
 * Ensure a single status bar service implementation is running at all times, using the in-process
 * implementation according to the product config.
 */
複製程式碼

那麼status bar是怎樣啟動的呢?在SystemUIApplication中也是通過呼叫SystemBars的start()方法,在SystemBars中的start()方法中去啟動statusbar。

@Override
public void start() {
    if (DEBUG) Log.d(TAG, "start");
    createStatusBarFromConfig();
}
複製程式碼
private void createStatusBarFromConfig() {
    if (DEBUG) Log.d(TAG, "createStatusBarFromConfig");
    final String clsName = mContext.getString(R.string.config_statusBarComponent);
    if (clsName == null || clsName.length() == 0) {
        throw andLog("No status bar component configured", null);
    }
    Class<?> cls = null;
    try {
        cls = mContext.getClassLoader().loadClass(clsName);
    } catch (Throwable t) {
        throw andLog("Error loading status bar component: " + clsName, t);
    }
    try {
        mStatusBar = (SystemUI) cls.newInstance();
    } catch (Throwable t) {
        throw andLog("Error creating status bar component: " + clsName, t);
    }
    mStatusBar.mContext = mContext;
    mStatusBar.mComponents = mComponents;
    mStatusBar.start();
    if (DEBUG) Log.d(TAG, "started " + mStatusBar.getClass().getSimpleName());
}
複製程式碼

這裡的clsName和之前服務的names一樣,也是從config中讀取的,具體如下,是StatusBar的類路徑。

<!-- Component to be used as the status bar service.  Must implement the IStatusBar
 interface.  This name is in the ComponentName flattened format (package/class)  -->
<string name="config_statusBarComponent" translatable="false">com.android.systemui.statusbar.phone.StatusBar</string>
複製程式碼

於是乎,通過這樣一種方式,就啟動起來一個一直執行著的StatusBar服務了。

相關文章