android 6.0 SystemUI源码分析(2)-SystemUI启动流程

1.SystemUI启动

SystemUI是核心系统应用,需要开机启动,启动SystemUI进程,是通过启动SystemUIService来实现的。

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

SystemServer启动后,会在SystemServer Main Thread启动ActivityManagerService,当ActivityManagerService  systemReady后,会去启动SystemUIService。

 mActivityManagerService.systemReady(new Runnable() {

            @Override

            public void run() {

           …            try {

                    startSystemUi(context);

                } catch (Throwable e) {

                    reportWtf(“starting System UI”, e);

                } 由如上可以看出,startSystemUi不是在SystemServer Main thread,而是在ActivityManagerService Thread。

 static final void startSystemUi(Context context) {

        Intent intent = new Intent();

        intent.setComponent(new ComponentName(“com.android.systemui”,

                    “com.android.systemui.SystemUIService”));

        //Slog.d(TAG, “Starting service: ” + intent);

        context.startServiceAsUser(intent, UserHandle.OWNER);

    } 通过startServiceAsUser,SystemUIService就启动了,即SystemUI进程开机启动。

2.SystemUI Services启动

SystemServer启动SystemUIService后,会走到SystemUIService的onCreate函数。 public class SystemUIService extends Service {

    @Override

    public void onCreate() {

        super.onCreate();

        ((SystemUIApplication) getApplication()).startServicesIfNeeded();

    }

SystemUIService就是一个普通的Service,在onCreate里面,会调用SystemUIApplication的services

/**

 * Application class for SystemUI.

 */

public class SystemUIApplication extends Application {

    private static final String TAG = “SystemUIService”;

    private static final boolean DEBUG = false;

    /**

     * The classes of the stuff to start.

     */

    private final Class<?>[] SERVICES = new Class[] {

            com.android.systemui.tuner.TunerService.class,

            com.android.systemui.keyguard.KeyguardViewMediator.class,

            com.android.systemui.recents.Recents.class,

            com.android.systemui.volume.VolumeUI.class,

            com.android.systemui.statusbar.SystemBars.class,

            com.android.systemui.usb.StorageNotification.class,

            com.android.systemui.power.PowerUI.class,

            com.android.systemui.media.RingtonePlayer.class,

    };

SystemUIApplication是一个Application实现,重写Application相关函数。

SystemUIApplication定义了很多System Panel,这里叫做SERVICES,但是并非是真正的service.

SystemUI应用定义了一个抽象的SystemUI类,根据Java抽象化的特征,可以使开发更加灵活。

SystemUI相关的类图关系如下:
《android 6.0 SystemUI源码分析(2)-SystemUI启动流程》

从SystemUI继承了很多的Panel,这些Panel有我们很熟悉的,比如Recents(近期任务栏),VolumeUI(音量条),SystemBars(状态栏)等。

回到SystemUIApplication里的startService函数:

    /**
     * Makes sure that all the SystemUI services are running. If they are already running, this is a
     * no-op. This is needed to conditinally start all the services, as we only need to have it in
     * the main process.
     *
     * <p>This method must only be called from the main thread.</p>
     */
    public void startServicesIfNeeded() {
        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.");
        final int N = SERVICES.length;
        for (int i=0; i<N; i++) {
            Class<?> cl = SERVICES[i];
            if (DEBUG) Log.d(TAG, "loading: " + cl);
            try {
                mServices[i] = (SystemUI)cl.newInstance();
            } 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();

            if (mBootCompleted) {
                mServices[i].onBootCompleted();
            }
        }
        mServicesStarted = true;
    }

这个函数主要是实例化以及启动SystemUI Services(这里的Service并非是真正的service),这样通过SystemUIService的启动,SystemUI核心的services也启动了。

在SystemUIApplication类的onCreate里面,会注册开机完成广播,并将开机完成事件,给到SystemUI Services.

  @Override
    public void onCreate() {
        super.onCreate();
        // Set the application theme that is inherited by all services. Note that setting the
        // application theme in the manifest does only work for activities. Keep this in sync with
        // the theme set there.
        setTheme(R.style.systemui_theme);

        IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (mBootCompleted) return;

                if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");
                unregisterReceiver(this);
                mBootCompleted = true;
                if (mServicesStarted) {
                    final int N = mServices.length;
                    for (int i = 0; i < N; i++) {
                        mServices[i].onBootCompleted();
                    }
                }
            }
        }, filter);
    }

SystemUI Services启动后,根据各Services的功能,SystemUI开始各司其职的正常工作起来。

    原文作者:Android源码分析
    原文地址: https://blog.csdn.net/zhudaozhuan/article/details/50817180
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞