Android P SystemUi启动流程

本文代码基于Android 9.0分析,个人理解能力有限,如有不对请多指教。
SystemUI是在开机过程中SystemServer启动的,SystemServer的startOtherServices()中

/*frameworks/base/services/java/com/android/server/SystemServer.java*/
private void startOtherServices() {
...
  try {
      startSystemUi(context, windowManagerF);
  } catch (Throwable e) {
      reportWtf("starting System UI", e);
  }
...
}

//startSystemUi方法
 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);
        context.startServiceAsUser(intent, UserHandle.SYSTEM);
        windowManager.onSystemUiStarted();
 }

可以看到该方法会启动systemUi的SystemUIService服务。windowManager.onSystemUiStarted()方法最终会通过绑定的方式启动SystemUi的KeyguardService服务。

SystemUIService继承Service,是一个标准的android服务,所以必须重载onCreate接口:

//frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
@Override
public void onCreate() {
   super.onCreate();
   ((SystemUIApplication) getApplication()).startServicesIfNeeded();
   ... //省略不重要代码
}

SystemUIService的onCreate方法中又调用了SystemUIApplication的startServicesIfNeeded方法。

//frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
public void startServicesIfNeeded() {
    String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
    startServicesIfNeeded(names);
}

先看下names里面存储的是什么。
config_systemUIServiceComponents:

<!-- frameworks/base/packages/SystemUI/res/values/config.xml --!>
<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>

config_systemUIServiceComponents是一系列包名加类名。这些类都继承自SystemUI类,SystemUI是一个抽象类,其中有一个抽象方法

public abstract void start();

读取到String数组names里面,然后调用带参数的startServicesIfNeeded方法:

private void startServicesIfNeeded(String[] services) {
     ...
     //创建SystemUI数组,长度为names的长度
     mServices = new SystemUI[services.length];
	...
    //开始遍历services数组,也就是之前的names数组
    final int N = services.length;
    for (int i = 0; i < N; i++) {
         String clsName = services[i];
         ...
         long ti = System.currentTimeMillis();
         Class cls;
         try {
              cls = Class.forName(clsName);
              //实例化取出的class
              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();
          ...
          if (mBootCompleted) {
              mServices[i].onBootCompleted();
          }
		  ...   
}

每一个系统ui元素(包括statusBar,PowerUI等)都必须继承SystemUI这个抽象类,并重载其中的start方法,这是一种比较灵活的编程方式,它允许我们在后期对系统UI元素进行轻松的扩展或者删除。具体每个UI元素的启动将在后续章节分析。

你可能感兴趣的:(android)