SystemUI分析(一)

SystemUI分析(一)

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();

}

}

}

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里配置的,我们看看都有哪些组件

com.android.systemui.Dependency$DependencyCreator//使用dagger的依赖注入

com.android.systemui.util.NotificationChannels//通知面板信息

com.android.systemui.statusbar.CommandQueue$CommandQueueStart//命令队列,各个服务里通过调用putComponent把组件的包名信息传入进来,处理完消息后再回调

com.android.systemui.keyguard.KeyguardViewMediator//管理锁屏的状态

com.android.systemui.recents.Recents//最近使用的应用

com.android.systemui.volume.VolumeUI//声音相关的ui

com.android.systemui.stackdivider.Divider//显示在分屏模式下两个应用程序之间的分隔符的拖动手柄

com.android.systemui.SystemBars//状态栏

com.android.systemui.usb.StorageNotification//USB状态和通知管理

com.android.systemui.power.PowerUI//电源管理,如低电量发出通知等

com.android.systemui.media.RingtonePlayer//播放铃声

com.android.systemui.keyboard.KeyboardUI//键盘管理UI

com.android.systemui.pip.PipUI//画中画ui显示

com.android.systemui.shortcut.ShortcutKeyDispatcher//快捷方式和分发

@string/config_systemUIVendorServiceComponent//由用户自实现的组件

com.android.systemui.util.leak.GarbageMonitor$Service//监听是否堆内存泄露

com.android.systemui.LatencyTester//延迟测试组件

com.android.systemui.globalactions.GlobalActionsComponent//全局的对话框显示,如长按电源键显示关机对话框

com.android.systemui.ScreenDecorations//绘制屏幕的装饰,如圆角等

com.android.systemui.biometrics.BiometricDialogImpl//生物识别

com.android.systemui.SliceBroadcastRelayHandler//切片广播延迟策略

com.android.systemui.SizeCompatModeActivityController//界面兼容模式控制

com.android.systemui.statusbar.notification.InstantAppNotifier//应用通知实例

com.android.systemui.theme.ThemeOverlayController//主题替换

相关推荐

第六期:是什么让你坚持IAR,放弃了KEIL
2025 年永劫无间双截棍连招全攻略:从入门到精通的实战技巧
det365娱乐场所官方网

2025 年永劫无间双截棍连招全攻略:从入门到精通的实战技巧

📅 08-05 👁️ 9511
安装出错(出错码=5):拒绝访问
det365娱乐场所官方网

安装出错(出错码=5):拒绝访问

📅 09-08 👁️ 1583