代码改变世界

SystemUI分析

2017-06-21 13:48  cascle  阅读(1163)  评论(0编辑  收藏  举报

  SystemUI是安卓的一个系统APP,负责的内容有系统通知栏,状态栏,最近应用程序,锁屏,壁纸,屏保,系统对话框,截屏,录屏等功能。

       Apk的路径位于/system/priv-app,源码code位于frameworks/base/packages/SystemUI

       1.Android.mk

       2.AndroidManifest.xml配置文件表明了APP要求的权限,特征以及四大组件。

       3.初始化流程

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相关的类图关系如下:
从SystemUI继承了很多的Panel,这些Panel有我们很熟悉的,比如Recents(近期任务栏),VolumeUI(音量条),SystemBars(状态栏)等。
 
 
回到SystemUIApplication里的startService函数:
[java] view plain copy
 
  1. /** 
  2.  * Makes sure that all the SystemUI services are running. If they are already running, this is a 
  3.  * no-op. This is needed to conditinally start all the services, as we only need to have it in 
  4.  * the main process. 
  5.  * 
  6.  * <p>This method must only be called from the main thread.</p> 
  7.  */  
  8. public void startServicesIfNeeded() {  
  9.     if (mServicesStarted) {  
  10.         return;  
  11.     }  
  12.   
  13.     if (!mBootCompleted) {  
  14.         // check to see if maybe it was already completed long before we began  
  15.         // see ActivityManagerService.finishBooting()  
  16.         if ("1".equals(SystemProperties.get("sys.boot_completed"))) {  
  17.             mBootCompleted = true;  
  18.             if (DEBUG) Log.v(TAG, "BOOT_COMPLETED was already sent");  
  19.         }  
  20.     }  
  21.   
  22.     Log.v(TAG, "Starting SystemUI services.");  
  23.     final int N = SERVICES.length;  
  24.     for (int i=0; i<N; i++) {  
  25.         Class<?> cl = SERVICES[i];  
  26.         if (DEBUG) Log.d(TAG, "loading: " + cl);  
  27.         try {  
  28.             mServices[i] = (SystemUI)cl.newInstance();  
  29.         } catch (IllegalAccessException ex) {  
  30.             throw new RuntimeException(ex);  
  31.         } catch (InstantiationException ex) {  
  32.             throw new RuntimeException(ex);  
  33.         }  
  34.         mServices[i].mContext = this;  
  35.         mServices[i].mComponents = mComponents;  
  36.         if (DEBUG) Log.d(TAG, "running: " + mServices[i]);  
  37.         mServices[i].start();  
  38.   
  39.         if (mBootCompleted) {  
  40.             mServices[i].onBootCompleted();  
  41.         }  
  42.     }  
  43.     mServicesStarted = true;  
  44. }  
这个函数主要是实例化以及启动SystemUI Services(这里的Service并非是真正的service),这样通过SystemUIService的启动,SystemUI核心的services也启动了。
 
 
在SystemUIApplication类的onCreate里面,会注册开机完成广播,并将开机完成事件,给到SystemUI Services.
[java] view plain copy
 
  1. @Override  
  2.   public void onCreate() {  
  3.       super.onCreate();  
  4.       // Set the application theme that is inherited by all services. Note that setting the  
  5.       // application theme in the manifest does only work for activities. Keep this in sync with  
  6.       // the theme set there.  
  7.       setTheme(R.style.systemui_theme);  
  8.   
  9.       IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);  
  10.       filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);  
  11.       registerReceiver(new BroadcastReceiver() {  
  12.           @Override  
  13.           public void onReceive(Context context, Intent intent) {  
  14.               if (mBootCompleted) return;  
  15.   
  16.               if (DEBUG) Log.v(TAG, "BOOT_COMPLETED received");  
  17.               unregisterReceiver(this);  
  18.               mBootCompleted = true;  
  19.               if (mServicesStarted) {  
  20.                   final int N = mServices.length;  
  21.                   for (int i = 0; i < N; i++) {  
  22.                       mServices[i].onBootCompleted();  
  23.                   }  
  24.               }  
  25.           }  
  26.       }, filter);  
  27.   }  

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