Android SystemUI加载流程

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()方法,进而对启动配置中的每个模块。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值