SystemUI startup process
Hi, everyone, I’m a banana-loving monkey. I took advantage of the New Year’s Day holiday at home to organize the contents of the SystmeUI startup process recorded locally.
Those who are familiar with android development should be familiar with SystemUI, which has the same system interface as the name. When the system starts, the first UI to start is SystemUI. Now we go through the process from Code;
The startup of system services naturally starts from SystemServer:
path: com\android\server\SystemServer.java
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {//代码从Main开始看
new SystemServer().run();//启动服务--> run中查看
}
Then, we look at the run method
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(); start various services
/**
* 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 starts to start 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
Do two analyses on the above code:
What did windowManager.onSystemUiStarted() perform?
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 }
Enter SystemUIService to see what is executed?
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);
}
Read various services from the configuration file, check the configuration file:
<!-- SystemUI Services: The classes of the stuff to start. -->
<string-array name="config_systemUIServiceComponents" translatable="false">
<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.biometrics.BiometricDialogImpl</item>
<item>com.android.systemui.SliceBroadcastRelayHandler</item>
<item>com.android.systemui.SizeCompatModeActivityController</item>
<item>com.android.systemui.statusbar.notification.InstantAppNotifier</item>
<item>com.android.systemui.theme.ThemeOverlayController</item>
</string-array>
Then enter startServicesIfNeeded to view:
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 is the various modules in the configuration file. All modules inherit SystemUI uniformly.
Simply draw a picture for easy memory:
Some parts have not been fully written yet, so please stream the code and add it later. Thank you. Thank you
for the referenced information: SystemUI Tutorial
Code的搬运工V.1
Add a StatusBar startup process
- In the above startup process, there is SystemBars in the config configuration file that starts StatusBar. Let me take a look
- path: com\android\systemui\SystemBars.java
@Override
public void start() {
if (DEBUG) Log.d(TAG, "start");
createStatusBarFromConfig();
}
In the createStatusBarFromConfig method
mStatusBar.mContext = mContext;
mStatusBar.mComponents = mComponents;
mStatusBar.start();
This will enter the start method
path of StatusBar : com\android\systemui\statusbar\phone\StatusBar.java
在start方法中
createAndAddWindows();
public void createAndAddWindows() {
addStatusBarWindow();
}
private void addStatusBarWindow() {
makeStatusBarView();
mStatusBarWindowManager = Dependency.get(StatusBarWindowManager.class);
mRemoteInputController = new RemoteInputController(mHeadsUpManager);
mStatusBarWindowManager.add(mStatusBarWindow, getStatusBarHeight());
}
In the makeStatusBarView method, the entire statusbar is instantiated.
Code的搬运工V2.0