Android SystemUI是一个APK,是一个核心应用,源码位于framework/base/packages/目录下,它是一个持久化的进程,为系统提供一套UI交互组件,在开机时通过SystemServer启动。
SystemUI功能庞大,包含状态栏、导航栏、下拉状态栏、电源管理、声音管理、通知栏等功能,下面将分为多给小节来对SystemUI进行分析,一起进入SystemUI的学习。
SystemUIApplication.java
public class SystemUIApplication extends Application implements SysUiServiceProvider {
...........
@Override
public void onCreate() {
super.onCreate();
.........
SystemUIFactory.createFromConfig(this);//通过反射得到SystemUIFactory的实例,然后初始化
if (Process.myUserHandle().equals(UserHandle.SYSTEM)) {
IntentFilter bootCompletedFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);//注册开完成广播
bootCompletedFilter.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;//mServices是SystemUI每个组件的服务列表
for (int i = 0; i < N; i++) {
mServices[i].onBootCompleted();//调用组件服务的开机完成
}
}
}
}, bootCompletedFilter);
...........
} else {//没有系统权限的的用户
............
startSecondaryUserServicesIfNeeded();
}
}
}
SystemUIService.java
public class SystemUIService extends Service {
@Override
public void onCreate() {
super.onCreate();
((SystemUIApplication) getApplication()).startServicesIfNeeded();
.............
}
}
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);
Object o = cls.newInstance();
if (o instanceof SystemUI.Injector) {
o = ((SystemUI.Injector) o).apply(this);
}
mServices[i] = (SystemUI) o;//反射得到服务组件的实例
} 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;
if (DEBUG) Log.d(TAG, "running: " + mServices[i]);
mServices[i].start();//启动服务组件
log.traceEnd();
// Warn if initialization of component takes too long
ti = System.currentTimeMillis() - ti;
if (ti > 1000) {
Log.w(TAG, "Initialization of " + cls.getName() + " took " + ti + " ms");
}
if (mBootCompleted) {
mServices[i].onBootCompleted();//开机完成,组件做一些开机完成的工作
}
}
...........
}
每个服务组件都是继承自SystemUI的抽象类,并且实现SysUiServiceProvicer,服务组件是通过config_systemUIServiceComponents得到的,这个数组是在config.xml里配置的,我们看看都有哪些组件
<!-- SystemUI Services: The classes of the stuff to start. -->
<string-array name="config_systemUIServiceComponents" translatable="false">
<item>com.android.systemui.Dependency$DependencyCreator</item>//使用dagger的依赖注入
<item>com.android.systemui.util.NotificationChannels</item>//通知面板信息
<item>com.android.systemui.statusbar.CommandQueue$CommandQueueStart</item>//命令队列,各个服务里通过调用putComponent把组件的包名信息传入进来,处理完消息后再回调
<item>com.android.systemui.keyguard.KeyguardViewMediator</item>//管理锁屏的状态
<item>com.android.systemui.recents.Recents</item>//最近使用的应用
<item>com.android.systemui.volume.VolumeUI</item>//声音相关的ui
<item>com.android.systemui.stackdivider.Divider</item>//显示在分屏模式下两个应用程序之间的分隔符的拖动手柄
<item>com.android.systemui.SystemBars</item>//状态栏
<item>com.android.systemui.usb.StorageNotification</item>//USB状态和通知管理
<item>com.android.systemui.power.PowerUI</item>//电源管理,如低电量发出通知等
<item>com.android.systemui.media.RingtonePlayer</item>//播放铃声
<item>com.android.systemui.keyboard.KeyboardUI</item>//键盘管理UI
<item>com.android.systemui.pip.PipUI</item>//画中画ui显示
<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.biometrics.BiometricDialogImpl</item>//生物识别
<item>com.android.systemui.SliceBroadcastRelayHandler</item>//切片广播延迟策略
<item>com.android.systemui.SizeCompatModeActivityController</item>//界面兼容模式控制
<item>com.android.systemui.statusbar.notification.InstantAppNotifier</item>//应用通知实例
<item>com.android.systemui.theme.ThemeOverlayController</item>//主题替换
</string-array>