hi,大家好,我是爱吃香蕉的猴子,趁元旦假期在家把之前本地记录的关于SystmeUI启动流程的内容,整理一下.
熟悉android 开发的都应该熟悉SystemUI, 和名字一样 系统的界面,当系统启动时,最先启动的Ui的则是SystemUI,现在我们从Code上来走一遍流程;
系统服务的启动,自然从SystemServer开始:
path: com\android\server\SystemServer.java
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {//代码从Main开始看
new SystemServer().run();//启动服务--> run中查看
}
然后,我们看一下run方法
private void run() {
...
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority(//为服务设置线程
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
Looper.getMainLooper().setSlowLogThresholdMs(
SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
// Initialize native services.
System.loadLibrary("android_servers");
// Start services.
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();//Start SystemUI
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
...
}
startOtherServices();启动各种服务
/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored
* and organized.
* 其中有待重构的东西
*/
private void startOtherServices() {
....
try {
startSystemUi(context, windowManagerF);
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
.....
}
startSystemUi开始启动SystemUI
static final void startSystemUi(Context context, WindowManagerService windowManager) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.systemui",
"com.android.systemui.SystemUIService"));//启动SystemUIService
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.SYSTEM);//set uid
windowManager.onSystemUiStarted();
}//开始启动SystemUI
针对上面的代码做两个分析:
windowManager.onSystemUiStarted()执行了什么?
path:services/core/java/com/android/server/wm/WindowManagerService.java
3274 /**
3275 ┊* Called when System UI has been started.
3276 ┊*/
3277 public void onSystemUiStarted() {
3278 ┊ mPolicy.onSystemUiStarted();//-->PhoneWindowManager
3279 }
path:services/core/java/com/android/server/policy/PhoneWindowManager.java
4837 @Override
4838 public void onSystemUiStarted() {
4839 ┊ bindKeyguard();
4840 }
4827 private void bindKeyguard() {
4828 ┊ synchronized (mLock) {
4829 ┊ ┊ if (mKeyguardBound) {
4830 ┊ ┊ ┊ return;
4831 ┊ ┊ }
4832 ┊ ┊ mKeyguardBound = true;
4833 ┊ }
4834 ┊ mKeyguardDelegate.bindService(mContext);//启动了keyguard模块,后面代码不再贴
4835 }
进入SystemUIService看看执行了什么?
path: src\main\java\com\systemui\SystemUIService.java
@Override
public void onCreate() {
super.onCreate();
((SystemUIApplication) getApplication()).startServicesIfNeeded();
....
}
path: app/src/main/java/com/systemui/SystemUIApplication.java
public void startServicesIfNeeded() {
String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
startServicesIfNeeded(/* metricsPrefix= */ "StartServices", names);
}
从配置文件中读取各类服务,查看一下配置文件:
- com.android.systemui.util.NotificationChannels
- com.android.systemui.statusbar.CommandQueue$CommandQueueStart
- com.android.systemui.keyguard.KeyguardViewMediator
- com.android.systemui.recents.Recents
- com.android.systemui.volume.VolumeUI
- com.android.systemui.stackdivider.Divider
- com.android.systemui.SystemBars
- com.android.systemui.usb.StorageNotification
- com.android.systemui.power.PowerUI
- com.android.systemui.media.RingtonePlayer
- com.android.systemui.keyboard.KeyboardUI
- com.android.systemui.pip.PipUI
- com.android.systemui.shortcut.ShortcutKeyDispatcher
- @string/config_systemUIVendorServiceComponent
- com.android.systemui.util.leak.GarbageMonitor$Service
- com.android.systemui.LatencyTester
- com.android.systemui.globalactions.GlobalActionsComponent
- com.android.systemui.ScreenDecorations
- com.android.systemui.biometrics.BiometricDialogImpl
- com.android.systemui.SliceBroadcastRelayHandler
- com.android.systemui.SizeCompatModeActivityController
- com.android.systemui.statusbar.notification.InstantAppNotifier
- com.android.systemui.theme.ThemeOverlayController
然后进入startServicesIfNeeded查看:
private void startServicesIfNeeded(String metricsPrefix, String[] services) {
...
String clsName = services[i];
if (DEBUG) Log.d(TAG, "loading: " + clsName);
log.traceBegin(metricsPrefix + 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;
...
}
mService就是配置文件中的各个模块。所有的模块都统一继承了SystemUI.
简单画个图,方便记忆:
有些地方还没有完全写明白,先把代码顺一遍,后续添加,谢谢
首先感谢参考了的资料:SystemUI教程
Code的搬运工V.1