SystemUI 7.0学习总结一-SystemUI的启动

最近在学习并接触SystemUI模块的工作,Keyguard做为引入库也纳入了SystemUI的大家庭,所以整体感觉SystemUI模块还是挺大的,为了日后的查阅还是要写一下笔记。笔记记录过程中参考了许多网友的文章,在本文最后的文章参考会一一罗列,在此非常感谢他们的分享。

SystemUI概览

SystemUI属于系统级的apk,位置在frameworks\base\packages\SystemUI,主要功能有:

状态栏信息显示,比如电池,wifi信号,3G/4G等icon显示

通知面板,比如系统消息,第三方应用消息

近期任务栏显示面板,比如长按近期任务快捷键,显示近期使用的应用

截图服务

壁纸服务

……

SystemUI的启动流程

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

SystemServer路径:/base/services/java/com/android/server/SystemServer.java

mActivityManagerService.systemReady(newRunnable() {@Overridepublicvoidrun() {                Slog.i(TAG,"Making services ready");                ......                Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,"StartSystemUI");try{                    startSystemUi(context);                }catch(Throwable e) {                    reportWtf("starting System UI", e);                }                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);                ......            }                    });

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

在这个方法里启动一个SystemUIService服务,

static final void startSystemUi(Context context) {        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);}

1

2

3

4

5

6

7

8

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

publicclassSystemUIServiceextendsService{@OverridepublicvoidonCreate() {super.onCreate();        ((SystemUIApplication) getApplication()).startServicesIfNeeded();    }    ......

1

2

3

4

5

6

7

8

在SystemUIService的onCreate方法中会调用SystemUIApplication的startServicesIfNeeded方法,这个方法会调用 startServicesIfNeeded(SERVICES)方法启动一系列服务(并不是真正的service,都继承自SystemUI)。

public class SystemUIApplication extends Application {    ....../**

    * 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,            Divider.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,com.android.systemui.keyboard.KeyboardUI.class,com.android.systemui.tv.pip.PipUI.class,com.android.systemui.shortcut.ShortcutKeyDispatcher.class,com.android.systemui.VendorServices.class};......    public void startServicesIfNeeded() {        startServicesIfNeeded(SERVICES);}}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

startServicesIfNeeded方法会遍历SERVICES 这个数组,依次调用service的start方法启动服务。

privatevoidstartServicesIfNeeded(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() +".");        finalintN = services.length;for(inti=0; i cl = services[i];if(DEBUG) Log.d(TAG,"loading: "+ cl);try{                Object newService = SystemUIFactory.getInstance().createInstance(cl);                mServices[i] = (SystemUI) ((newService ==null) ? cl.newInstance() : newService);            }catch(IllegalAccessException ex) {thrownewRuntimeException(ex);            }catch(InstantiationException ex) {thrownewRuntimeException(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;    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

看到这里,这么多文字可能不够直观,那就看看图吧,非常感谢参考文章中的分享。 

这里以SERVICES 中com.android.systemui.keyguard.KeyguardViewMediator.class为例, 

SystemUI 7.0学习总结一-SystemUI的启动_第1张图片

SystemUI Services启动后,根据各Services的功能,SystemUI的各个模块就开始正常工作起来了。

你可能感兴趣的:(SystemUI 7.0学习总结一-SystemUI的启动)