PowerUI启动流程梳理
PowerUI是SystemUI控制电量提醒的模块,包括低电量提醒、危急电量关机提醒、高温关机提醒、省电模式等功能,如果收到监听的时间则用Notification 在systemUI显示通知。
二、代码控制流程
启动初始化
SystemUI 启动时会加载众多功能模块其中就包含 PowerUI ,其会直接调用 PowerUI.start()
- public void start() {
- // 获取 PowerManager 用于获取电池相关信息
- mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
- // 获取自启动以来的毫秒数
- mScreenOffTime = mPowerManager.isScreenOn() ? -1 : SystemClock.elapsedRealtime();
- mWarnings = Dependency.get(WarningsUI.class);
- // 和混合通知相关,相关功能平台默认是关闭的
- mEnhancedEstimates = Dependency.get(EnhancedEstimates.class);
- mLastConfiguration.setTo(mContext.getResources().getConfiguration());
-
- ContentObserver obs = new ContentObserver(mHandler) {
- @Override
- public void onChange(boolean selfChange) {
- updateBatteryWarningLevels();
- }
- };
- // 监测低电量阀值的改变
- final ContentResolver resolver = mContext.getContentResolver();
- resolver.registerContentObserver(Settings.Global.getUriFor(
- Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL),
- false, obs, UserHandle.USER_ALL);
- // 更新电量警告
- updateBatteryWarningLevels();
-
- // 监听相关广播
- mReceiver.init();
- // 如果上次因过热而关机则告知用户
- showWarnOnThermalShutdown();
-
- //注册观察者以配置 mEnableSkinTemperatureWarning 并在设置更改时执行温度过过热事件侦听器的注册
- resolver.registerContentObserver(
- Settings.Global.getUriFor(Settings.Global.SHOW_TEMPERATURE_WARNING),
- false /*notifyForDescendants*/,
- new ContentObserver(mHandler) {
- @Override
- public void onChange(boolean selfChange) {
- doSkinThermalEventListenerRegistration();
- }
- });
- //注册观察者以配置 mEnableUsbTemperatureAlarm 并在设置更改时执行 USB 过热事件监听器的注册
- resolver.registerContentObserver(
- Settings.Global.getUriFor(Settings.Global.SHOW_USB_TEMPERATURE_ALARM),
- false /*notifyForDescendants*/,
- new ContentObserver(mHandler) {
- @Override
- public void onChange(boolean selfChange) {
- doUsbThermalEventListenerRegistration();
- }
- });
- // 初始化温度阀值高温警告
- initThermalEventListeners();
- mCommandQueue.addCallback(this);
- }
|
低电量警告
updateBatteryWarningLevels
- void updateBatteryWarningLevels() {
- // 获取系统配置的危急电量值,默认:5
- int critLevel = mContext.getResources().getInteger(
- com.android.internal.R.integer.config_criticalBatteryWarningLevel);
- // 获取系统配置的低电量值,默认:15
- int warnLevel = mContext.getResources().getInteger(
- com.android.internal.R.integer.config_lowBatteryWarningLevel);
-
- if (warnLevel < critLevel) {
- warnLevel = critLevel;
- }
-
- mLowBatteryReminderLevels[0] = warnLevel;
- mLowBatteryReminderLevels[1] = critLevel;
- // 获取系统配置的关闭低电量警告状态的电量值,默认:20(15+5)
- mLowBatteryAlertCloseLevel = mLowBatteryReminderLevels[0]
- + mContext.getResources().getInteger(
- com.android.internal.R.integer.config_lowBatteryCloseWarningBump);
- }
|
电池相关广播监听
- final class Receiver extends BroadcastReceiver {
-
- private boolean mHasReceivedBattery = false;
-
- public void init() {
- // Register for Intent broadcasts for...
- IntentFilter filter = new IntentFilter();
- // 省电模式改变
- filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
- // 电池状态改变
- filter.addAction(Intent.ACTION_BATTERY_CHANGED);
- // 开关屏
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- filter.addAction(Intent.ACTION_SCREEN_ON);
- // 切换用户
- filter.addAction(Intent.ACTION_USER_SWITCHED);
- mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mHandler);
- //强制获取初始值。在获取信息的 API 之前依赖 Sticky 行为
- if (!mHasReceivedBattery) {
- // Get initial state
- Intent intent = mContext.registerReceiver(
- null,
- new IntentFilter(Intent.ACTION_BATTERY_CHANGED)
- );
- if (intent != null) {
- onReceive(mContext, intent);
- }
- }
- }
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) {
- ThreadUtils.postOnBackgroundThread(() -> {
- if (mPowerManager.isPowerSaveMode()) {
- / 开启省电模式则关闭低电量警告
- mWarnings.dismissLowBatteryWarning();
- }
- });
- } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
- ……
- mLastBatteryStateSnapshot = mCurrentBatteryStateSnapshot;
-
- final boolean plugged = mPlugType != 0;
- // 根据当前电量与危急电量、低电量、关闭低电量警告电量比较判断当前所处状态
- final boolean oldPlugged = oldPlugType != 0;
-
- int oldBucket = findBatteryLevelBucket(oldBatteryLevel);
- int bucket = findBatteryLevelBucket(mBatteryLevel);
-
- // Show the correct version of low battery warning if needed
- if (mLastShowWarningTask != null) {
- mLastShowWarningTask.cancel(true);
- if (DEBUG) {
- Slog.d(TAG, "cancelled task");
- }
- }
- mLastShowWarningTask = ThreadUtils.postOnBackgroundThread(() -> { // 判断是否要显示低电量警告
- maybeShowBatteryWarningV2(
- plugged, bucket);
- });
-
- } else if ……
- }
- }
|
高温警告
- private void initThermalEventListeners() {
- doSkinThermalEventListenerRegistration();
- doUsbThermalEventListenerRegistration();
- }
|
synchronized void doSkinThermalEventListenerRegistration() {
final boolean oldEnableSkinTemperatureWarning = mEnableSkinTemperatureWarning;
boolean ret = false;
// 是否开启高温警告,默认关闭
mEnableSkinTemperatureWarning = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.SHOW_TEMPERATURE_WARNING, mContext.getResources().getInteger(R.integer.config_showTemperatureWarning)) != 0;
if (mEnableSkinTemperatureWarning != oldEnableSkinTemperatureWarning) {
try {
if (mSkinThermalEventListener == null) {
mSkinThermalEventListener = new SkinThermalEventListener();
}
if (mThermalService == null) {
mThermalService = IThermalService.Stub.asInterface(
ServiceManager.getService(Context.THERMAL_SERVICE));
}
if (mEnableSkinTemperatureWarning) {
ret = mThermalService.registerThermalEventListenerWithType(
mSkinThermalEventListener, Temperature.TYPE_SKIN);
} else {
ret = mThermalService.unregisterThermalEventListener(mSkinThermalEventListener);
}
} catch (RemoteException e) {
Slog.e(TAG, "Exception while (un)registering skin thermal event listener.", e);
}
if (!ret) {
mEnableSkinTemperatureWarning = !mEnableSkinTemperatureWarning;
Slog.e(TAG, "Failed to register or unregister skin thermal event listener.");
}
}
} |
- final class SkinThermalEventListener extends IThermalEventListener.Stub {
- @Override public void notifyThrottling(Temperature temp) {
- int status = temp.getStatus();
-
- if (status >= Temperature.THROTTLING_EMERGENCY) {
- if (!mStatusBarLazy.get().isDeviceInVrMode()) {
- mWarnings.showHighTemperatureWarning();
- Slog.d(TAG, "SkinThermalEventListener: notifyThrottling was called "
- + ", current skin status = " + status
- + ", temperature = " + temp.getValue());
- }
- } else {
- mWarnings.dismissHighTemperatureWarning();
- }
- }
- }
|