SystemUI启动相关代码
相关代码主要分为两个部分
一、Service部分
代码路径:framework/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
......
//创建消息Looper
Looper.prepareMainLooper();
......
// Initialize native services.
//加载动态库libandroid_servers.so,初始化native服
System.loadLibrary("android_servers");
......
// Initialize the system context.
//初始化系统context
createSystemContext();
......
// Create the system service manager.
//创建系统服务
mSystemServiceManager = new SystemServiceManager(mSystemContext);
......
// Start services.
//启动引导服务,如AMS等
startBootstrapServices(t);
//启动核心服务
startCoreServices(t);
//启动其他服务,WMS,SystemUI等
startOtherServices(t);
}
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
......
mActivityManagerService.systemReady(() -> {
t.traceBegin("StartSystemUI");
try {
//启动SystemUI
startSystemUi(context, windowManagerF);
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
t.traceEnd();
}
......
}
private static void startSystemUi(
Context context,
WindowManagerService windowManager) {
PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
Intent intent = new Intent();
/*
SystemUIComponent配置路径:
路径:frameworks/base/core/res/res/values/config.xml
<string name="config_systemUIServiceComponent"/>
值:com.android.systemui/com.android.systemui.SystemUIService
*/
intent.setComponent(pm.getSystemUiServiceComponent());
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.SYSTEM);
windowManager.onSystemUiStarted();
}
至此已完成启动SystemUIService
二、应用部分
上面我们已经确定了SystemUIService的启动流程,SystemUI中SystemUIService是整个系统UI比较重要的载体,下面我们看一下SystemUIService做了写什么。
/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
1、SystemUIService-> onCreate()
((SystemUIApplication) getApplication()).startServicesIfNeeded();
2、SystemUIApplication->startServicesIfNeeded()
String[] names =
SystemUIFactory.getInstance()
.getSystemUIServiceComponents(getResources());
startServicesIfNeeded(/* metricsPrefix= */ "StartServices", names);
2.1 names
代码路径:/frameworks/base/packages/SystemUI/res/values/config.xml
<string-array name="config_systemUIServiceComponents">
<item>com.android.systemui.util.NotificationChannels</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.statusbar.phone.StatusBar</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.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.AuthController</item>
<item>com.android.systemui.SliceBroadcastRelayHandler</item>
<item>com.android.systemui.statusbar.notification.InstantAppNotifier</item>
<item>com.android.systemui.theme.ThemeOverlayController</item>
<item>com.android.systemui.accessibility.WindowMagnification</item>
<item>com.android.systemui.accessibility.SystemActions</item>
<item>com.android.systemui.toast.ToastUI</item>
<item>com.android.systemui.wmshell.WMShell</item>
</string-array>
以上并非是Service,而是继承SystemUI的模块,可以理解为每个模块对应的Service的静态代理。
SystemUI是个抽象类,如下:
/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUI.java
public abstract class SystemUI implements Dumpable {
protected final Context mContext;
public SystemUI(Context context) {
mContext = context;
}
//加载后执行(SystemUIApplication.startServicesIfNeeded中调用)
public abstract void start();
//配置变化时调用(SystemUIApplication.onConfigurationChanged中调用)
protected void onConfigurationChanged(Configuration newConfig) { }
@Override
public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) { }
//加载后执行SystemUIApplication.startServicesIfNeeded中调用)
protected void onBootCompleted() { }
public static void overrideNotificationAppName(
Context context,
Notification.Builder n,
boolean system) {
final Bundle extras = new Bundle();
String appName = system
? context.getString(com.android.internal.R.string.notification_app_name_system)
: context.getString(com.android.internal.R.string.notification_app_name_settings);
extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, appName);
n.addExtras(extras);
}}
2.2 startServicesIfNeeded关键部分
private void startServicesIfNeeded(
String metricsPrefix,
String[] services) {
if (mServicesStarted) {
return;
}
mServices = new SystemUI[services.length];
// see ActivityManagerService.finishBooting()
if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
......
final int N = services.length;
for (int i = 0; i < N; i++) {
String clsName = services[i];
SystemUI obj = mComponentHelper.resolveSystemUI(clsName);
......
mServices[i] = obj;
mServices[i].start();
......
if (mBootCompleteCache.isBootComplete()) {
mServices[i].onBootCompleted();
}
......
}
主要用于加载2.1中names每个模块的start()和onBootCompleted()方法,进而对启动配置中的每个模块。