《SytemUI》启动流程

简介


Android系统中有3个非常重要的应用,分别是SystemUI,launcher,Setting

  1. Setting:显示需要用户知道的设置项目,以前让用户配置自己系统的设置
  2. launcher:显示所有的应用,展示应用入口
  3. SystemUI:显示重要的信息,比如信号,电量,供用户操作的导航栏

Android 的 SystemUI 其实就是 Android 的系统界面,它包括了界面上方的状态栏 status bar,下方的导航栏Navigation Bar,锁屏界面 Keyguard ,电源界面 PowerUI,近期任务界面 Recent Task 等等。对于用户而言,SystemUI 的改动是最能直观感受到的。因此,每个 Android 版本在 SystemUI 上都有比较大的改动。而对开发者而言,理解 Android SystemUI 对优化Android系统界面,改善用户体验十分重要。

SystemUI的启动


Android系统在启动系统服务器SystemService的时候,会启动各个服务器,而在启动这个服务器之后会启动launcher和SystemUI

frameworks/base/services/java/com/android/server/SystemServer.java

public final class SystemServer {
    private void run() {
        startBootstrapServices();
        startCoreServices();
        startOtherServices(); 
    }
    
    private void startOtherServices() {
        // We now tell the activity manager it is okay to run third party
        // code.  It will call back into us once it has gotten to the state
        // where third party code can really run (but before it has actually
        // started launching the initial applications), for us to complete our
        // initialization.
        mActivityManagerService.systemReady(() -> {
            startSystemUi(context, windowManagerF);
        }

    }
}

可以看出在系统在启动所有服务器之后,监听mActivityManagerService状态,如果“systemReady”后,启动SytemUI,AMS的systemReady先启动SystemUI之后启动launcher,我们这里只关注SystemUI


    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();
    }

可以看见启动的是SystemUI的服务,然后调用 windowManager.onSystemUiStarted();

windowManager.onSystemUiStarted();执行了下面的流程

  1. windowManagerSerivce#onSystemUiStarted
  2. PhoneWindoiwManager#onSystemUiStarted
  3. KeyguardServiceDelegate#KeyguardServiceDelegate
    public void bindService(Context context) {
        context.bindServiceAsUser(intent, mKeyguardConnection,//注1
                Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM)
    }

注1:这里的intent是SystemUI的keygaurdServUC儿,通过获取SystemUI的keyguard,控制SystemU的锁屏。

SystemService启动SystemUIService


SystemUIService类十分简短,下面基本就是他全部的代码,

public class SystemUIService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        ((SystemUIApplication) getApplication()).startServicesIfNeeded();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

他只是调用SystemUIApplication的startServicesIfNeeded方法

public class SystemUIApplication extends Application implements SysUiServiceProvider {

    public void startServicesIfNeeded() {
        String[] names = getResources().getStringArray(R.array.config_systemUIServiceComponents);
        startServicesIfNeeded(names);
    }
    
    private void startServicesIfNeeded(String[] services) {
    if (mServicesStarted) {
        return;
    }
    mServices = new SystemUI[services.length];

    final int N = services.length;
    for (int i = 0; i < N; i++) {
        String clsName = services[i];
        if (DEBUG) Log.d(TAG, "loading: " + clsName);
        log.traceBegin("StartServices" + clsName);
        long ti = System.currentTimeMillis();
           Class cls;
        try {
            cls = Class.forName(clsName);
            mServices[i] = (SystemUI) cls.newInstance();//new it
        } 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;
    }

}

可以看到他通过反射实例化config_systemUIServiceComponents数组对应的“service”,然后调用start方法。
config_systemUIServiceComponents有哪些呢?

    <string-array name="config_systemUIServiceComponents" translatable="false">
        <item>com.android.systemui.Dependency</item>
        <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.fingerprint.FingerprintDialogImpl</item>
        <item>com.android.systemui.SliceBroadcastRelayHandler</item>
    </string-array>

这些类继承SystemUI,SystemUI类又是干什么的呢?
这个应用主要界面都是显示在Android屏幕部分区域,这些界面没有通过Activity控制,SystemUI类具有管理的功能,如在屏幕旋转会调用onConfigurationChanged方法,各个界面就可以做出相应的显示,调用onstar启动相应的模块。

启动SystemBars


各个模块比较多,已SystemBars为例进行分析。在startServicesIfNeeded方法中会实例化这个模块,然后调用其start方法。

SystemBars类(去除不重要的代码,以及log)

public class SystemBars extends SystemUI {
    @Override
    public void start() {
        createStatusBarFromConfig();
    }

    private void createStatusBarFromConfig() {
        //
        final String clsName =com.android.systemui.statusbar.phone.StatusBar mContext.getString(R.string.config_statusBarComponent);
        Class<?> cls = null;
        cls = mContext.getClassLoader().loadClass(clsName);
        mStatusBar = (SystemUI) cls.newInstance();
        mStatusBar.mContext = mContext;
        mStatusBar.mComponents = mComponents;
        mStatusBar.start();
    }
}

SystemBars实例化了R.string.config_statusBarComponent对应的com.android.systemui.statusbar.phone.StatusBar这个类,并调用其start方法。

public class StatusBar extends SystemUI{
    @Override
    public void start() {
        createAndAddWindows();
    }
    public void createAndAddWindows() {
        addStatusBarWindow();
    }
    private void addStatusBarWindow() {
        makeStatusBarView();
        mStatusBarWindowManager = Dependency.get(StatusBarWindowManager.class);
        mRemoteInputManager.setUpWithPresenter(this, mEntryManager, this,
                new RemoteInputController.Delegate() {
                    public void setRemoteInputActive(NotificationData.Entry entry,
                            boolean remoteInputActive) {
                        mHeadsUpManager.setRemoteInputActive(entry, remoteInputActive);
                        entry.row.notifyHeightChanged(true /* needsAnimation */);
                        updateFooter();
                    }
                    public void lockScrollTo(NotificationData.Entry entry) {
                        mStackScroller.lockScrollTo(entry.row);
                    }
                    public void requestDisallowLongPressAndDismiss() {
                        mStackScroller.requestDisallowLongPress();
                        mStackScroller.requestDisallowDismiss();
                    }
                });
        mRemoteInputManager.getController().addCallback(mStatusBarWindowManager);
        mStatusBarWindowManager.add(mStatusBarWindow, getStatusBarHeight());
    }
    
    protected void inflateStatusBarWindow(Context context) {
        mStatusBarWindow = (StatusBarWindowView) View.inflate(context,
                R.layout.super_status_bar, null);
    }

}

最后通过inflateStatusBarWindow获取view,然后mStatusBarWindowManager添加这个view,并显示出来。

SystemBars加载各个视图


SystemBars加载基本全部SystemUI的界面显示,由于布局太多,查找起来十分麻烦,我从其他人那里copy过来,链接:++https://www.jianshu.com/p/2e0f403e5299++

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章