【Battery】慢速和快速充电的显示逻辑
【Battery】慢速和快速充电的显示逻辑
- 一、需求描述
- 二、需求分析
- 三、解决方案
- 四、Battery 扩展
一、需求描述
基于 Android 14平台,锁屏界面显示为慢速充电,慢速
会造成用户体验不好,需要修改为充电,并重新配置快速充电的阈值。
二、需求分析
SystemUI 锁屏界面上显示充电表示的逻辑:src/com/android/systemui/statusbar/KeyguardIndicationController.java
if (mPowerPluggedInWired) {switch (mChargingSpeed) {case BatteryStatus.CHARGING_FAST:chargingId = hasChargingTime? R.string.keyguard_indication_charging_time_fast: R.string.keyguard_plugged_in_charging_fast;break;case BatteryStatus.CHARGING_SLOWLY:chargingId = hasChargingTime? R.string.keyguard_indication_charging_time_slowly: R.string.keyguard_plugged_in_charging_slowly;break;default:chargingId = hasChargingTime? R.string.keyguard_indication_charging_time: R.string.keyguard_plugged_in;break;}
}
主要通过系统电池状态 BatteryStatus.CHARGING_FAST 和 BatteryStatus.CHARGING_SLOWLY 来区分快速、慢速和正常充电的。mChargingSpeed
的赋值是 KeyguardUpdateMonitor 的回调来更新状态 onRefreshBatteryInfo
/*** KeyguardUpdateMonitor only sends "interesting" battery updates* {@link KeyguardUpdateMonitor#isBatteryUpdateInteresting}.* Therefore, make sure to always check plugged in state along with any charging status* change, or else we could end up with stale state.*/
@Override
public void onRefreshBatteryInfo(BatteryStatus status) {boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STATUS_CHARGING|| status.isCharged();boolean wasPluggedIn = mPowerPluggedIn;mPowerPluggedInWired = status.isPluggedInWired() && isChargingOrFull;mPowerPluggedInWireless = status.isPluggedInWireless() && isChargingOrFull;mPowerPluggedInDock = status.isPluggedInDock() && isChargingOrFull;mPowerPluggedIn = status.isPluggedIn() && isChargingOrFull;mPowerCharged = status.isCharged();mChargingWattage = status.maxChargingWattage;mChargingSpeed = status.getChargingSpeed(mContext);mBatteryLevel = status.level;mBatteryPresent = status.present;mBatteryDefender = status.isBatteryDefender();// when the battery is overheated, device doesn't charge so only guard on pluggedIn:mEnableBatteryDefender = mBatteryDefender && status.isPluggedIn();mIncompatibleCharger = status.incompatibleCharger.orElse(false);try {mChargingTimeRemaining = mPowerPluggedIn? mBatteryInfo.computeChargeTimeRemaining() : -1;} catch (RemoteException e) {mKeyguardLogger.log(TAG, ERROR, "Error calling IBatteryStats", e);mChargingTimeRemaining = -1;}mKeyguardLogger.logRefreshBatteryInfo(isChargingOrFull, mPowerPluggedIn, mBatteryLevel,mBatteryDefender);updateDeviceEntryIndication(!wasPluggedIn && mPowerPluggedInWired);
}
再通过 SettingLib
: src/com/android/settingslib/fuelgauge/BatteryStatus.java,找到判断 CHARGING_FAST 或 CHARGING_SLOWLY 或 CHARGING_REGULAR 的状态判断逻辑
public final int getChargingSpeed(Context context) {final int slowThreshold = context.getResources().getInteger(R.integer.config_chargingSlowlyThreshold);// <integer name="config_chargingFastThreshold_unisoc">15000000</integer>// 这里 Sprd 做过修改final int fastThreshold =UniSettingsLibComponentFactory.getInstance().getChargingFastThreshold(context);return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :maxChargingWattage > fastThreshold ? CHARGING_FAST :CHARGING_REGULAR;
}
config_chargingSlowlyThreshold
的定义如下,config_chargingFastThreshold
暂未使用而是使用 unisoc 修改的 config
<!-- Threshold in micro watts below which a charger is rated as "slow"; 1A @ 5V -->
<integer name="config_chargingSlowlyThreshold">5000000</integer><!-- Threshold in micro watts above which a charger is rated as "fast"; 1.5A @ 5V -->
<integer name="config_chargingFastThreshold">7500000</integer>
上诉的 maxChargingWattage
的赋值如下,是通过 batteryChangedIntent
private BatteryStatus(Intent batteryChangedIntent, Optional<Boolean> incompatibleCharger) {status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);plugged = batteryChangedIntent.getIntExtra(EXTRA_PLUGGED, 0);level = getBatteryLevel(batteryChangedIntent);chargingStatus = batteryChangedIntent.getIntExtra(EXTRA_CHARGING_STATUS,CHARGING_POLICY_DEFAULT);present = batteryChangedIntent.getBooleanExtra(EXTRA_PRESENT, true);this.incompatibleCharger = incompatibleCharger;final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT,-1);int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);if (maxChargingMicroVolt <= 0) {maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;}if (maxChargingMicroAmp > 0) {// Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor// to maintain precision equally on both factors.maxChargingWattage = (maxChargingMicroAmp / 1000)* (maxChargingMicroVolt / 1000);} else {maxChargingWattage = -1;}}
三、解决方案
为了修改锁屏界面上的显示慢速充电或者快速充电的阈值,直接调整 2 个config 的值即可!如果不想显示为慢速充电,将 config_chargingSlowlyThreshold 修改为不为 0 的极小值即可,如1。
<integer name="config_chargingSlowlyThreshold">1</integer>
<integer name="config_chargingFastThreshold_unisoc">15000000</integer>
除了锁屏界面,设置 - 电池界面也有这样的显示,逻辑相同。
四、Battery 扩展
frameworks/base/services/core/java/com/android/server/BatteryService.java
private void dumpInternal(FileDescriptor fd, PrintWriter pw, String[] args) {synchronized (mLock) {if (args == null || args.length == 0 || "-a".equals(args[0])) {pw.println("Current Battery Service state:");if (mUpdatesStopped) {pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");}pw.println(" AC powered: " + mHealthInfo.chargerAcOnline);pw.println(" USB powered: " + mHealthInfo.chargerUsbOnline);pw.println(" Wireless powered: " + mHealthInfo.chargerWirelessOnline);pw.println(" Dock powered: " + mHealthInfo.chargerDockOnline);pw.println(" Max charging current: " + mHealthInfo.maxChargingCurrentMicroamps);pw.println(" Max charging voltage: " + mHealthInfo.maxChargingVoltageMicrovolts);pw.println(" Charge counter: " + mHealthInfo.batteryChargeCounterUah);pw.println(" status: " + mHealthInfo.batteryStatus);pw.println(" health: " + mHealthInfo.batteryHealth);pw.println(" present: " + mHealthInfo.batteryPresent);pw.println(" level: " + mHealthInfo.batteryLevel);pw.println(" scale: " + BATTERY_SCALE);pw.println(" voltage: " + mHealthInfo.batteryVoltageMillivolts);pw.println(" temperature: " + mHealthInfo.batteryTemperatureTenthsCelsius);pw.println(" technology: " + mHealthInfo.batteryTechnology);} else {Shell shell = new Shell();shell.exec(mBinderService, null, fd, null, args, null, new ResultReceiver(null));}}
}
可以通过 ADB 命令 dump BatteryService 的相关信息:
C:\Users\Administrator>adb shell dumpsys battery
Current Battery Service state:AC powered: falseUSB powered: trueWireless powered: falseDock powered: falseMax charging current: 500000Max charging voltage: 5000000Charge counter: 1status: 2health: 2present: truelevel: 95scale: 100voltage: 4313temperature: 325technology: Li-ion
电池状态信息参数解析:
参数 | 数值 | 说明 |
---|---|---|
AC powered | false | 设备未通过交流电源(如墙式充电器)充电。 |
USB powered | true | 设备正在通过 USB 连接充电。 |
Wireless powered | false | 设备未通过无线方式充电。 |
Dock powered | false | 设备未通过底座充电。 |
Max charging current | 500000 | 当前配置的最大充电电流为 500,000 微安 (µA),即 500 毫安 (mA)。 |
Max charging voltage | 5000000 | 当前配置的最大充电电压为 5,000,000 微伏 (µV),即 5 伏 (V)。 |
Charge counter | 1029715 | 电池的电荷计数器,单位通常为微安时 (µAh),用于跟踪电池的充放电周期。 |
status | 2 | 电池状态。2 表示电池正处于充电状态。其他常见状态包括:3 (未充电)、4 (不充电)、5 (已充满)。 |
health | 2 | 电池健康状态。2 表示电池健康状况良好。 |
present | true | 表示电池已安装在设备中。 |
level | 64 | 当前电池电量百分比为 64%。 |
scale | 100 | 电池电量的满量程为 100%。 |
voltage | 4020 | 当前电池的电压为 4020 毫伏 (mV),即 4.02 伏 (V)。 |
temperature | 266 | 电池的温度为 266,单位是十分之一摄氏度。因此实际温度为 26.6 摄氏度。 |
technology | Li-ion | 电池技术类型为锂离子电池。 |
这个命令只能查看当前的电压
,没有显示当前的电流
,可以尝试直接读取系统文件来获取电流值,使用以下命令查看相关文件是否存在并包含数据:
adb shell cat /sys/class/power_supply/battery/current_now
如果这个路径不存在,可以尝试列出电源供应目录下的内容,寻找可能的文件:
adb shell ls -la /sys/class/power_supply/
adb shell ls -la /sys/class/power_supply/*/current_now
找到的正确文件可能会返回一个以微安(µA)为单位的数值,负值表示充电,正值表示放电。