publicvoidonCreate(){super.onCreate();// Set the application theme that is inherited by all services. Note that setting the
// application theme in the manifest does only work for activities. Keep this in sync with
// the theme set there.
// 设置主题
setTheme(R.style.Theme_SystemUI);SystemUIFactory.createFromConfig(this);if(Process.myUserHandle().equals(UserHandle.SYSTEM)){// SYSTEM 用户
// 监听开机广播
IntentFilterbootCompletedFilter=newIntentFilter(Intent.ACTION_BOOT_COMPLETED);// 设置优先级
bootCompletedFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);// 注册开机广播接收器
registerReceiver(newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextcontext,Intentintent){if(mBootCompleted)return;// 移除本接收器
unregisterReceiver(this);mBootCompleted=true;if(mServicesStarted){finalintN=mServices.length;for(inti=0;i<N;i++){mServices[i].onBootCompleted();}}}},bootCompletedFilter);// 监听语言改变并更新 SystemUi 的通知渠道
IntentFilterlocaleChangedFilter=newIntentFilter(Intent.ACTION_LOCALE_CHANGED);registerReceiver(newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextcontext,Intentintent){if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){if(!mBootCompleted)return;// Update names of SystemUi notification channels
NotificationChannels.createAll(context);}}},localeChangedFilter);}else{// We don't need to startServices for sub-process that is doing some tasks.
// (screenshots, sweetsweetdesserts or tuner ..)
StringprocessName=ActivityThread.currentProcessName();ApplicationInfoinfo=getApplicationInfo();if(processName!=null&&processName.startsWith(info.processName+":")){return;}// For a secondary user, boot-completed will never be called because it has already
// been broadcasted on startup for the primary SystemUI process. Instead, for
// components which require the SystemUI component to be initialized per-user, we
// start those components now for the current non-system user.
// 第二用户启动的服务
startSecondaryUserServicesIfNeeded();}}
publicvoidstartServicesIfNeeded(){// 获取 SystemUI 所有服务类名,后面看看这个数组
String[]names=getResources().getStringArray(R.array.config_systemUIServiceComponents);startServicesIfNeeded(names);}privatevoidstartServicesIfNeeded(String[]services){if(mServicesStarted){return;}mServices=newSystemUI[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;}}finalintN=services.length;for(inti=0;i<N;i++){StringclsName=services[i];longti=System.currentTimeMillis();Classcls;try{// 通过反射获取服务对象
cls=Class.forName(clsName);mServices[i]=(SystemUI)cls.newInstance();}catch(ClassNotFoundExceptionex){thrownewRuntimeException(ex);}catch(IllegalAccessExceptionex){thrownewRuntimeException(ex);}catch(InstantiationExceptionex){thrownewRuntimeException(ex);}// 填充对象信息并启动服务
mServices[i].mContext=this;mServices[i].mComponents=mComponents;mServices[i].start();// 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();}}// 插件相关
Dependency.get(PluginManager.class).addPluginListener(newPluginListener<OverlayPlugin>(){privateArraySet<OverlayPlugin>mOverlays;@OverridepublicvoidonPluginConnected(OverlayPluginplugin,ContextpluginContext){StatusBarstatusBar=getComponent(StatusBar.class);if(statusBar!=null){plugin.setup(statusBar.getStatusBarWindow(),statusBar.getNavigationBarView());}// Lazy init.
if(mOverlays==null)mOverlays=newArraySet<>();if(plugin.holdStatusBarOpen()){mOverlays.add(plugin);Dependency.get(StatusBarWindowManager.class).setStateListener(b->mOverlays.forEach(o->o.setCollapseDesired(b)));Dependency.get(StatusBarWindowManager.class).setForcePluginOpen(mOverlays.size()!=0);}}@OverridepublicvoidonPluginDisconnected(OverlayPluginplugin){mOverlays.remove(plugin);Dependency.get(StatusBarWindowManager.class).setForcePluginOpen(mOverlays.size()!=0);}},OverlayPlugin.class,true/* Allow multiple plugins */);mServicesStarted=true;}