Android 省电模式

下拉状态栏和设置开启省电模式调用的方法
vendor/freeme/packages/apps/FreemeSystemUI/src/com/android/systemui/qs/tiles/BatterySaverTile.java
@Override
protected void handleClick() {
if (getState().state == Tile.STATE_UNAVAILABLE) {
return;
}
mBatteryController.setPowerSaveMode(!mPowerSave);
}

vendor/freeme/packages/apps/FreemeSystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
@Override
public void setPowerSaveMode(boolean powerSave) {
BatterySaverUtils.setPowerSaveMode(mContext, powerSave, /*needFirstTimeWarning*/ true);
}

frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatterySaverUtils.java
/**
* Enable / disable battery saver by user request.
* - If it's the first time and needFirstTimeWarning, show the first time dialog.
* - If it's 4th time through 8th time, show the schedule suggestion notification.
*
* @param enable true to disable battery saver.
*
* @return true if the request succeeded.
*/
public static synchronized boolean setPowerSaveMode(Context context,
boolean enable, boolean needFirstTimeWarning) {
if (DEBUG) {
Log.d(TAG, "Battery saver turning " + (enable ? "ON" : "OFF"));
}
final ContentResolver cr = context.getContentResolver();
final Bundle confirmationExtras = new Bundle(1);
confirmationExtras.putBoolean(EXTRA_CONFIRM_TEXT_ONLY, false);
if (enable && needFirstTimeWarning
&& maybeShowBatterySaverConfirmation(context, confirmationExtras)) {
return false;
}
if (enable && !needFirstTimeWarning) {
setBatterySaverConfirmationAcknowledged(context);
}
if (context.getSystemService(PowerManager.class).setPowerSaveModeEnabled(enable)) {
if (enable) {
final int count =
Secure.getInt(cr, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 0) + 1;
Secure.putInt(cr, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, count);
final Parameters parameters = new Parameters(context);
if ((count >= parameters.startNth)
&& (count <= parameters.endNth)
&& Global.getInt(cr, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) == 0
&& Secure.getInt(cr,
Secure.SUPPRESS_AUTO_BATTERY_SAVER_SUGGESTION, 0) == 0) {
showAutoBatterySaverSuggestion(context, confirmationExtras);
}
}
return true;
}
return false;
}

真正核心的地方是
context.getSystemService(PowerManager.class).setPowerSaveModeEnabled(enable)
应用可以使用此方法打开或关闭省电模式,后续代码分析参考
Android R默认打开省电模式就会开启深色主题,为了延长电池续航时间,省电模式会执行以下操作:开启深色主题;闭或限制后台活动、部分视觉效果和其他功能,例如“Ok Google”。
新需求需要修改开启省电模式,不改变系统主题,用户可以自己选择,根据参考内容,这里涉及到一个省电策略,如果不需要每次开启省电模式切换到深色主题,只需要将enableNightMode默认策略改成false即可。
frameworks/base/services/core/java/com/android/server/power/batterysaver/BatterySaverPolicy.java
private static final Policy DEFAULT_FULL_POLICY = new Policy(
0.5f, /* adjustBrightnessFactor */
true, /* advertiseIsEnabled */
true, /* deferFullBackup */
true, /* deferKeyValueBackup */
false, /* disableAnimation */
true, /* disableAod */
true, /* disableLaunchBoost */
true, /* disableOptionalSensors */
true, /* disableSoundTrigger */
true, /* disableVibration */
false, /* enableAdjustBrightness */
false, /* enableDataSaver */
true, /* enableFirewall */
true, /* enableNightMode */
true, /* enableQuickDoze */
new ArrayMap<>(), /* filesForInteractive */
new ArrayMap<>(), /* filesForNoninteractive */
true, /* forceAllAppsStandby */
true, /* forceBackgroundCheck */
PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF /* locationMode */
);

转自 https://blog.csdn.net/weixin_44008788/article/details/128429906

你可能感兴趣的:(安卓系统开发,android,java,开发语言)