状态栏默认显示电量百分比
在Android P 的BatteryMeterView中有集成显示电量百分比相关代码,所以我们只需要将其显示控制条件默认打开就行。
在Settings的Battery的子页面中存在一个Battery percentage Switch选项,该选项就是用来控制电量百分比是否显示在状态栏的,不过默认是关闭的~
通过Battery percentage Switch选项,可以定位到android\packages\apps\Settings\res\xml\power_usage_summary.xml 文件,有如下代码:
<SwitchPreference
android:key="battery_percentage"
android:title="@string/battery_percentage"
android:summary="@string/battery_percentage_description" />
根据布局中的key值,可以查询到该SwitchPreference的控制器类android\packages\apps\Settings\src\com\android\settings\display\BatteryPercentagePreferenceController.java,如下:
public class BatteryPercentagePreferenceController extends AbstractPreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
private static final String KEY_BATTERY_PERCENTAGE = "battery_percentage";
public BatteryPercentagePreferenceController(Context context) {
super(context);
}
/**
*isAvailable返回值主要控制在Settings的Battery页面中的
*Battery percentage Switch选项是否显示出来
*
*返回false,Battery percentage选项将会被隐藏掉
*
*config_battery_percentage_setting_available默认值为true,
*在android\frameworks\base\core\res\res\values\config.xml中定义。
*/
@Override
public boolean isAvailable() {
return mContext.getResources()
.getBoolean(R.bool.config_battery_percentage_setting_available);
}
@Override
public String getPreferenceKey() {
return KEY_BATTERY_PERCENTAGE;
}
/**
*根据SHOW_BATTERY_PERCENT值,更改Switch显示状态
*在没有取到SHOW_BATTERY_PERCENT值时候,以0为默认值
* 1:显示,0:不显示
*/
@Override
public void updateState(Preference preference) {
int setting = Settings.System.getInt(mContext.getContentResolver(),
SHOW_BATTERY_PERCENT, 0);
((SwitchPreference) preference).setChecked(setting == 1);
}
/**
*根据Switch状态改变,从而更改SHOW_BATTERY_PERCENT这个系统值
*/
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean showPercentage = (Boolean) newValue;
Settings.System.putInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT,
showPercentage ? 1 : 0);
return true;
}
}
所以要想电量百分比默认显示,只需要找到SHOW_BATTERY_PERCENT默认值设置的地方,然后将其置为1,就默认显示电量百分比了。
一通查找,然后并没有发现SHOW_BATTERY_PERCENT赋初值的地方,所以需要自己手动添加一下:
在android/frameworks/base/packages/SettingsProvider/res/values/defaults.xml末尾添加一项:
<!-- Default to show battery percaent on statusbar -->
<integer name="def_show_battery_percent">1</integer>
然后该默认值写到系统数据库中去:
android/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
R.bool.def_notification_pulse);
loadUISoundEffectsSettings(stmt);
loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
R.integer.def_pointer_speed);
//为SHOW_BATTERY_PERCENT写入默认值
loadIntegerSetting(stmt, Settings.System.SHOW_BATTERY_PERCENT, R.integer.def_show_battery_percent);
/*
* IMPORTANT: Do not add any more upgrade steps here as the global,
* secure, and system settings are no longer stored in a database
* but are kept in memory and persisted to XML.
*
* See: SettingsProvider.UpgradeController#onUpgradeLocked
*/
} finally {
if (stmt != null) stmt.close();
}
}
将SettingsProvider单编,然后push到设备中,将设备恢复出厂设置一下,状态栏显示电量百分比即可生效了!