自己看,如果不对,请指出!
framework/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
// setp 1 SystemServer启动
new SystemServer().run();
}
private void run() {
// step 2 这里启动很多服务,包括RuntimeService等等
startOtherServices();
}
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
// step 3 AMS对象调用systemReady(),这里开始启动Launcher流程
mActivityManagerService.systemReady(()->{
// step 4 启动SystemUI,网络管理服务等
// windowManagerF是WindowManagerServiced对象
startSystemUi(context, windowManagerF);
});
}
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);
// WindowManagerService回调SystemUI启动,内部是WindowManagerPolicy的回调
windowManager.onSystemUiStarted();
}
}
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java
该类是配置在SystemUI的AndroidManifest.xml的Application中的android:appComponentFactory节点
@Override
public Application instantiateApplication(ClassLoader cl, String className)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// step 5 当SystemUIService被SystemServer启动后,SystemUI程序会先进入这个回调函数,具体原理分析ContextImpl,AppComponentFactory
Application app = super.instantiateApplication(cl, className);
if (app instanceof SystemUIApplication) {
((SystemUIApplication) app).setContextAvailableCallback(
context -> {
// step 7 初始化SystemUIFactory
SystemUIFactory.createFromConfig(context);
SystemUIFactory.getInstance().getRootComponent().inject(
SystemUIAppComponentFactory.this);
}
);
}
return app;
}
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
public class SystemUIApplication extends Application implements SysUiServiceProvider {
@Override
public void onCreate() {
super.onCreate();
// step 6 回调通知给SystemUIAppComponentFactory
mContextAvailableCallback.onContextAvailable(this);
}
}
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
public class SystemUIFactory {
public static void createFromConfig(Context context) {
final String clsName = context.getString(R.string.config_systemUIFactoryComponent);
try {
Class<?> cls = null;
cls = context.getClassLoader().loadClass(clsName);
mFactory = (SystemUIFactory) cls.newInstance();
// step 8 反射回去SystemUIFactory静态对象,并初始化
mFactory.init(context);
} catch (Throwable t) {
}
}
private void init(Context context) {
mRootComponent = buildSystemUIRootComponent(context);
// step 9 初始化
Dependency.initDependencies(mRootComponent);
}
}
frameworks/base/packages/SystemUI/src/com/android/systemui/Dependency.java
public class Dependency {
public static void initDependencies(SystemUIRootComponent rootComponent) {
sDependency = new Dependency();
rootComponent.createDependency().createSystemUI(sDependency);
// step10 加载各种管理类,在getDependency时获取对象,通过get方法初始化
sDependency.start();
}
}
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
public class SystemUIService extends Service {
@Override
public void onCreate() {
super.onCreate();
// step 11 通过SystemUIApplication启动对应服务
((SystemUIApplication) getApplication()).startServicesIfNeeded();
}
}
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
public class SystemUIApplication extends Application implements SysUiServiceProvider {
public void startServicesIfNeeded() {
// 读取xml配置文件中定义的services,服务都继承于SystemUI模板
String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
// step 12
startServicesIfNeeded(/* metricsPrefix= */ "StartServices", names);
}
private void startServicesIfNeeded(String metricsPrefix, String[] services) {
// 根据xml配置初文件的服务列表,始化服务数组对象
mServices = new SystemUI[services.length];
final int N = services.length;
for (int i = 0; i < N; i++) {
String clsName = services[i];
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;
} catch(ClassNotFoundException ex){
throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
}
mServices[i].mContext = this;
mServices[i].mComponents = mComponents;
// 调用各服务的start
// step 13 其中包含SystemBar
mServices[i].start();
if (mBootCompleted) {
// 调用各服务的onBootCompleted
mServices[i].onBootCompleted();
}
}
}
frameworks/base/packages/SystemUI/res/values/config.xml
<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>
frameworks/base/packages/SystemUI/src/com/android/systemui/SystemBar.java
public class SystemBars extends SystemUI {
@Override
public void start() {
// step 14
createStatusBarFromConfig();
}
private void createStatusBarFromConfig() {
// 获取配置文件statubar的信息
final String clsName = mContext.getString(R.string.config_statusBarComponent);
Class<?> cls = null;
try {
// 加载StatuaBar.java
cls = mContext.getClassLoader().loadClass(clsName);
} catch (Throwable t) {
}
try {
// 反射获取mStatusBar对象
mStatusBar = (SystemUI) cls.newInstance();
} catch (Throwable t) {
}
mStatusBar.mContext = mContext;
mStatusBar.mComponents = mComponents;
if (mStatusBar instanceof StatusBar) {
SystemUIFactory.getInstance().getRootComponent()
.getStatusBarInjector()
.createStatusBar((StatusBar) mStatusBar);
}
// step 15
mStatusBar.start();
}
}
frameworks/base/packages/SystemUI/res/values/config.xml
<string name="config_statusBarComponent" translatable="false">com.android.systemui.statusbar.phone.StatusBar</string>
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
public class StatusBar extends SystemUI implements DemoMode,
ActivityStarter, OnUnlockMethodChangedListener,
OnHeadsUpChangedListener, CommandQueue.Callbacks, ZenModeController.Callback,
ColorExtractor.OnColorsChangedListener, ConfigurationListener,
StatusBarStateController.StateListener, ShadeController,
ActivityLaunchAnimator.Callback, AppOpsController.Callback {
@Override
public void start() {
// step 16 通过Dependency获取各种控制器
getDependencies();
RegisterStatusBarResult result = null;
try {
result = mBarService.registerStatusBar(mCommandQueue);
} catch (RemoteException ex) {
ex.rethrowFromSystemServer();
}
// step 17 创建状态栏视图
createAndAddWindows(result);
}
public void createAndAddWindows(@Nullable RegisterStatusBarResult result) {
// step 18
makeStatusBarView(result);
mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
mStatusBarWindowController.add(mStatusBarWindow, getStatusBarHeight());
}
protected void makeStatusBarView(@Nullable RegisterStatusBarResult result) {
final Context context = mContext;
// 更新display信息
updateDisplaySize(); // populates mDisplayMetrics
// 更新资源信息
updateResources();
// 更新主题信息
updateTheme();
// step 19 加载mStatusBar布局
inflateStatusBarWindow(context);
}
protected void inflateStatusBarWindow(Context context) {
mStatusBarWindow = (StatusBarWindowView) mInjectionInflater.injectable(
LayoutInflater.from(context)).inflate(R.layout.super_status_bar, null);
}
}