状态栏电池图标更新总流程如下(以更新电池电量为例):
BatteryControllerImpl.onReceive -> fireBatteryLevelChanged -> BatteryMeterViewController.onBatteryLevelChanged
->BatteryMeterView.onBatteryLevelChanged -> ThemedBatteryDrawable.setBatteryLevel -> invalidateSelf
首先由BatteryControllerImpl接收广播(主要有ACTION_BATTERY_CHANGED、ACTION_POWER_SAVE_MODE_CHANGED等)去更新电池相关信息,如
电量、充电速度、充电状态、省电模式等信息。然后将接收到的信息通过回调callback更新至电池图标上,即BatterMeterView中。最后BatteryMeterView将
接收到的信息更新至电池图标对应的drawable(ThemedBatteryDrawable),后者更新信息后进行刷新,从而达到更新电池图标的目标。
BatteryControllerImpl回调callback将充电信息更新至BatteryMeterViewController时,后者实际上只使用了level和pluggedIn这两个参数。充电信息更新至
BatteryMeterView中时,可以看到是否处于充电状态取决于pluggedIn。所以可能会出现哪怕是已经充满了电、或者只是插入了otg,图标也会显示正在充电的
情况。
SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
402 protected void fireBatteryLevelChanged() {
403 synchronized (mChangeCallbacks) {
404 final int N = mChangeCallbacks.size();
405 for (int i = 0; i < N; i++) {
更新充电参数:mLevel, mPluggedIn, mCharging
406 mChangeCallbacks.get(i).onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
407 }
408 }
409 }
SystemUI/src/com/android/systemui/battery/BatteryMeterViewController.java
71 private final BatteryController.BatteryStateChangeCallback mBatteryStateChangeCallback =
72 new BatteryController.BatteryStateChangeCallback() {
73 @Override
74 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
将level和pluggedIn更新至BatteryMeterView
75 mView.onBatteryLevelChanged(level, pluggedIn);
76 }
SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
195 void onBatteryLevelChanged(int level, boolean pluggedIn) {
charging取决于pluggedIn
196 mDrawable.setCharging(pluggedIn);
197 mDrawable.setChargingSpeed(mChargingSpeed); // UNISOC: Modify for bug 1838475
198 mDrawable.setBatteryLevel(level);
如果状态栏电池图标信息显示有误,比如电量、充电状态等,可按更新流程进行排查。
3.1 排查BatteryControllerImpl是否有接收到对应广播去更新充电信息
SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
198 @Override
199 public void onReceive(final Context context, Intent intent) {
200 final String action = intent.getAction();
201 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
根据广播更新充电信息
202 if (mTestMode && !intent.getBooleanExtra(“testmode”, false)) return;
203 mHasReceivedBattery = true;
204 mLevel = (int)(100f
205 * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
206 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
207 mPluggedIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
208 mPluggedInWireless = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)
209 == BatteryManager.BATTERY_PLUGGED_WIRELESS;
210
211 final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
212 BatteryManager.BATTERY_STATUS_UNKNOWN);
213 mCharged = status == BatteryManager.BATTERY_STATUS_FULL;
214 mCharging = mCharged || status == BatteryManager.BATTERY_STATUS_CHARGING;
…
230 final BatteryStatus batteryStatus = new BatteryStatus(intent);
231 int chargingSpeed = batteryStatus.getChargingSpeed(context);
3.2 排查BatteryMeterViewController中实现的对应callback是否有被回调
SystemUI/src/com/android/systemui/battery/BatteryMeterViewController.java
71 private final BatteryController.BatteryStateChangeCallback mBatteryStateChangeCallback =
72 new BatteryController.BatteryStateChangeCallback() {
73 @Override
74 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
更新电量、充电状态
75 mView.onBatteryLevelChanged(level, pluggedIn);
76 }
77
78 /* UNISOC: Modify for bug 1838475 @{ */
79 @Override
80 public void onChargingSpeedChanged(int chargingSpeed) {
更新充电速度
81 mView.onChargingSpeedChanged(chargingSpeed);
82 }
83 /* @} */
84
85 @Override
86 public void onPowerSaveChanged(boolean isPowerSave) {
更新省电模式
87 mView.onPowerSaveChanged(isPowerSave);
88 }
89
90 @Override
91 public void onBatteryUnknownStateChanged(boolean isUnknown) {
更新电池unKnown状态
92 mView.onBatteryUnknownStateChanged(isUnknown);
93 }
94 };
3.3 排查电池信息是否有更新至BatteryMeterView,并提交更新至drawable中
SystemUI/src/com/android/systemui/battery/BatteryMeterView.java
195 void onBatteryLevelChanged(int level, boolean pluggedIn) {
drawable中更新充电状态
196 mDrawable.setCharging(pluggedIn);
drawable中更新充电速度
197 mDrawable.setChargingSpeed(mChargingSpeed); // UNISOC: Modify for bug 1838475
drawable中更新充电电量
198 mDrawable.setBatteryLevel(level);
199 mCharging = pluggedIn;
200 mLevel = level;
更新电量百分比
201 updatePercentText();
202 }