SystemUI启动流程
- 
frameworks/base/services/java/com/android/server/SystemServer.java public final class SystemServer {
 private static final TimingsTraceLog BOOT_TIMINGS_TRACE_LOG;
 private SystemServiceManager mSystemServiceManager;
 
 public static void main(String[] args) {
 new SystemServer().run();
 }
 private void run() {
 ...
 // Start services.
 try {
 startBootstrapServices();
 }catch(){...}
 }
 
 private void startBootstrapServices() {
 ...
 // TODO: Might need to move after migration to WM.
 ActivityTaskManagerService atm =
 mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
 mActivityManagerService = ActivityManagerService.Lifecycle.startService(
 mSystemServiceManager, atm);
 mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
 mActivityManagerService.setInstaller(installer);
 mWindowManagerGlobalLock = atm.getGlobalLock();
 traceEnd();
 }
 
 }
 mActivityManagerService.systemReady(() -> {
 traceBeginAndSlog("StartSystemUI");
 try {
 startSystemUi(context, windowManagerF);
 } catch (Throwable e) {
 reportWtf("starting System UI", e);
 }
 traceEnd();
 },BOOT_TIMINGS_TRACE_LOG);
 
 
 //正式启动SystemUi 入口:com.android.systemui.SystemUIService.java
 private static 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();
 }
 
- 
启动SystemUIService: com.android.systemui.SystemUIService.java public class SystemUIService extends Service {
 
 @Override
 public void onCreate() {
 super.onCreate();
 ((SystemUIApplication)getApplication()).startServicesIfNeeded();
 }
 }
 
- 
com.android.systemui.SystemUIApplication.java public class SystemUIApplication extends Application implements SysUiServiceProvider {
 private SystemUI[] mServices;
 
 @Override
 public void onCreate() {
 super.onCreate();
 SystemUIFactory.createFromConfig(this);
 }
 public void startServicesIfNeeded() { //系统服务(SystemUiservice启动时执行该方法
 String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
 startServicesIfNeeded(names);
 }
 private void startServicesIfNeeded(String[] services) {
 mServices = new SystemUI[services.length];
 final int N = services.length;
 //反射机制,获取SysUI对象,对象列表所在数组:R.array.config_systemUIServiceComponents
 
 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);
 Object o = cls.newInstance();
 if (o instanceof SystemUI.Injector) {
 o = ((SystemUI.Injector) o).apply(this);
 }
 mServices[i] = (SystemUI) o;//Dependency$DependencyCreator+NotificationChannels.java
 }catch(){..}
 mServices[i].mContext = this;
 mServices[i].mComponents = mComponents;
 mServices[i].start();//反射结果.start() //SysUI组件extends SystemUI ,通过start()方法启动
 
 }
 //添加插件:OverLayPlugin.java
 Dependency.get(PluginManager.class).addPluginListener(..,OverlayPlugin.class, true)
 }
 }
- 
SystemUI子类,入口:start() 1. NotificationChannels.java 
 public class NotificationChannels extends SystemUI {
 public Context mContext;
 
 @Override
 public void start() {
 createAll(mContext);
 }
 }
 2.KeyguardViewMediator.java
 public class KeyguardViewMediator extends SystemUI {
 @Override
 public void start() {
 synchronized (this) {
 setupLocked();
 }
 putComponent(KeyguardViewMediator.class, this);
 }
 }
 .....
 
- 
通过反射获取的SystemUI子类:xml文件中的数组 <!-- SystemUI Services: The classes of the stuff to start. --> 
 <string-array name="config_systemUIServiceComponents" translatable="false">
 <item>com.android.systemui.Dependency$DependencyCreator</item> //依赖
 <item>com.android.systemui.util.NotificationChannels</item> //创建SystemUI的通知Channel
 <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> //定期检查SystemUI堆内存并报告
 <item>com.android.systemui.LatencyTester</item> // 在DEBUGGABLE版本运行,用于测试系统中的延迟
 <item>com.android.systemui.globalactions.GlobalActionsComponent</item> // 关机菜单
 <item>com.android.systemui.ScreenDecorations</item> //手机屏幕屏切圆角,模拟刘海屏
 <item>com.android.systemui.biometrics.BiometricDialogImpl</item>
 <item>com.android.systemui.SliceBroadcastRelayHandler</item>
 <item>com.android.systemui.SizeCompatModeActivityController</item>
 <item>com.android.systemui.statusbar.notification.InstantAppNotifier</item> //显示Instant Apps(用户设备不需要安装的应用)的通知
 <item>com.android.systemui.theme.ThemeOverlayController</item>
 
 </string-array>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/287662.html
