Android 8.0 SystemUI(一)

一、SystemUI介绍

1、什么是SystemUI

作为Android系统核心应用,SystemUI负责反馈系统及应用状态并与用户保持大量的交互。

2、SystemUI组成

SystemUI包含的功能非常丰富,主要模块有:

StatusBar:状态栏
Notification Panel:Quick settings 和 Notification
NavigationBar:导航栏(返回,Home,Recent)
Keyguard:锁屏界面 Recents:近期任务界面
VolumeUI:音量调节对话框(媒体音量、铃声音量、闹钟音量、通知音量)
Screenshot:截图界面
PowerUI:主要处理和Power相关的事件,比如省电模式切换、电池电量变化和开关屏事件等。
RingtonePlayer:铃声播放界面

二、SystemUI的启动过程

Android系统开机的过程,会创建ServerThread维护系统各种服务,当核心系统服务启动完成后ServerThread会通过调用ActivityManagerService.systemReady()方法通知AMS系统已经就绪。

/framework/base/services/java/com/android/server/SystemServer.java
    mActivityManagerService.systemReady(() -> {
    	//省略一些
    	traceBeginAndSlog("StartSystemUI");
    	try {
    			startSystemUi(context, windowManagerF);
    	} catch (Throwable e) {
    			reportWtf("starting System UI", e);
    	}
    	traceEnd();
	static final 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);
        windowManager.onSystemUiStarted();
    }

当核心的系统服务启动完毕后,ServerThread通过Context.startServiceAsUser()方法完成了SystemUIService的启动。
SystemUIService继承Service,首先看onCreate方法:

/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIService.java
    @Override
    public void onCreate() {
        super.onCreate();
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
        // For debugging RescueParty
        if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("debug.crash_sysui", false)) {
            throw new RuntimeException();
        }
    }

它调用SystemUIApplication的startServicesIfNeeded(),代码如下:

/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
    public void startServicesIfNeeded() {
    	startServicesIfNeeded(SERVICES);
    }

继续跟踪,启动个模块:

/frameworks/base/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
        private void startServicesIfNeeded(Class[] services) {
            if (mServicesStarted) {
                return;
            }
            if (!mBootCompleted) {
                // check to see if maybe it was already completed long before we began
                // see ActivityManagerService.finishBooting()
                if ("1".equals(SystemProperties.get("sys.boot_completed"))) {
                    mBootCompleted = true;
                    if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");
                }
            }
            Log.v(TAG, "Starting SystemUI services for user " +
                    Process.myUserHandle().getIdentifier() + ".");
            TimingsTraceLog log = new TimingsTraceLog("SystemUIBootTiming",
                    Trace.TRACE_TAG_APP);
            log.traceBegin("StartServices");
            final int N = services.length;
            for (int i = 0; i < N; i++) {
                Class cl = services[i];
                if (DEBUG) Log.d(TAG, "loading: " + cl);
                log.traceBegin("StartServices" + cl.getSimpleName());
                long ti = System.currentTimeMillis();
                try {
                    Object newService = SystemUIFactory.getInstance().createInstance(cl);
                    mServices[i] = (SystemUI) ((newService == null) ? cl.newInstance() : newService);
                } catch (IllegalAccessException ex) {
                    throw new RuntimeException(ex);
                } catch (InstantiationException ex) {
                    throw new RuntimeException(ex);
                }
                mServices[i].mContext = this;
                mServices[i].mComponents = mComponents;
                if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
                mServices[i].start();
                log.traceEnd();

除了onCreate()方法之外,SystemUIService没有其他有意义的代码了。显而易见,SystemUIService是一个容器。在其启动时,将会逐个实例化定义在SERVICIES列表中的继承自SystemUI抽象类的子服务。在调用了子服务的start()方法之后,SystemUIService便不再做任何其他的事情,任由各个子服务自行运行。而状态栏导航栏则是这些子服务中的一个。
对于SystemUI App启动,这里有一张总结了的图给大家参考,如下所示(图片来自https://blog.csdn.net/dfghhvbafbga/article/details/79973851):
Android 8.0 SystemUI(一)_第1张图片

你可能感兴趣的:(Android系统)