基于Android源码的SystemUI分析

1 SystemUI是什么

SystemUI是一个系统的apk,它主要包括几个部分, status bar 、navigation bar,以及将status bar拉下来之后呈现在用户眼前的,quick settings, notification list等等。

官方文档定义的system bar(这里可以认为system bar 就是systemUI)如下:
The systembars are screen areas dedicated to the display of notifications,communication of device status, and device navigation. Typically the systembars (which consist of the status and navigation bars, as shown in figure 1)are displayed concurrently with your app. Apps that display immersive content,such as movies or images, can temporarily dim the system bar icons for a lessdistracting experience, or temporarily hide the bars for a fully immersiveexperience.

意译一下:

Systembars 是用于显示notification、设备的各种状态、以及设备导航的一块屏幕区域。并且,system bar会和你的app同时显示。当需要显示全屏内容(沉浸式内容,比如播放电影、图片),apps可以暂时置灰或隐藏system bar。



1.1 Status Bar

图中“1”表示的就是Status Bar, 它在左侧显示等待用户查看的notification,在右侧显示时间、电量、信号强度等。
用户下拉status bar会显示notification的详细信息。

1.2 Navigation Bar

图中“2”表示的是Navigation Bar。navigation bar只会出现在在那些没有传统的硬件键盘的设备上。它控制设备的导航,包括back、home和recents三个按键。同时也显示基于android2.3或更早版本开发的App的菜单选项。

1.3 Notifications

Notification是一种简短的信息,用户可以在任意时刻从status bar访问它。他们提供了更新、提醒,或者那些不足以打断用户的消息。下拉Status bar就可以看到notification的列表。点击一个notification会打开与其相关联的的app。

Notification可以被打开和折叠,用户可以看到更多的信息和相关操作。Notification推荐的布局是两行。当它折叠起来时,notification有一行标题和一行消息。如果有必要,可以添加更多行内容。

向右或向左滑动notification,会将其从notification 列表里删除。
如图:


2. SystemUI 何时出生

SystemUIservice 的启动过程如图


1> SystemServer 启动 SystemUI service

首先明确一点,SystemUI作为系统的应用,是被SystemServer start的。

SystemServer的源文件(git 路径:frameworks/base/services/java/com/android/server/SystemServer.java)包含两个类, SystemServer 和ServerThread。SystemServer中包含main方法,并在其中调用ServerThread 定义的“initAndLoop()”方法进行整体的初始化工作。
1176        // This used to be its own separate thread, but now it is
1177        // just the loop we run on the main thread.
1178        ServerThread thr = new ServerThread();
1179        thr.initAndLoop();

在initAndLoop中,会调用startSystemUI,封装好Intent,最终start Service。
1097    static final void startSystemUi(Context context) {
1098        Intent intent = new Intent();
1099        intent.setComponent(new ComponentName("com.android.systemui",
1100                    "com.android.systemui.SystemUIService"));
1101        //Slog.d(TAG, "Starting service: " + intent);
1102        context.startServiceAsUser(intent, UserHandle.OWNER);
1103    }
1104}

2> SystemUI service 启动 SystemUI各个组件

SystemUI service被Start之后,“onCreate”方法被调用。
在onCreate中,会应用java 反射机制,获取各个组件的对象,并循环将其start。
50    public void onCreate() {
51        HashMap<Class<?>, Object> components = new HashMap<Class<?>, Object>();
52        final int N = SERVICES.length;
53        for (int i=0; i<N; i++) {
54            Class<?> cl = SERVICES[i];
55            Log.d(TAG, "loading: " + cl);
56            try {
57                mServices[i] = (SystemUI)cl.newInstance();
58            } catch (IllegalAccessException ex) {
59                throw new RuntimeException(ex);
60            } catch (InstantiationException ex) {
61                throw new RuntimeException(ex);
62            }
63            mServices[i].mContext = this;
64            mServices[i].mComponents = components;
65            Log.d(TAG, "running: " + mServices[i]);
66            mServices[i].start();
67        }
68    }

其中的SERVICES则是对应组件的Class 数组

35 private final Class<?>[] SERVICES = new Class[] {
36            com.android.systemui.recent.Recents.class,
37            com.android.systemui.statusbar.SystemBars.class,
38            com.android.systemui.usb.StorageNotification.class,
39            com.android.systemui.power.PowerUI.class,
40            com.android.systemui.media.RingtonePlayer.class,
41            com.android.systemui.settings.SettingsUI.class,
42        };

我们可以看到,其中包括Recents、SystemBars、StorageNotification、PowerUI、RingtonePlayer和SettingsUI


3. SystemUI组件的类层次

上文可以看到SystemUI包含了以下组件:Recents、SystemBars、StorageNotification、PowerUI、RingtonePlayer和SettingsUI
它们的类层次如下:



SystemUI.java是一个抽象类,其中声明了start方法。下面的类都继承自SystemUI.java
之后的文章争取对它们一一进行分析讲解。

本文到此结束,欢迎大家一起交流 :)


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值