Merge "Add dock defender battery tips" into tm-qpr-dev am: f218f350c4

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/20489769

Change-Id: I5944ee42fe0e6da8a3e3a815ec3cebbabb79b155
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Wesley Wang
2022-11-30 17:21:36 +00:00
committed by Automerger Merge Worker
23 changed files with 853 additions and 63 deletions

View File

@@ -98,6 +98,7 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
intentFilter.addAction(BatteryUtils.BYPASS_DOCK_DEFENDER_ACTION);
final Intent intent = mContext.registerReceiver(this, intentFilter);
updateBatteryStatus(intent, true /* forceUpdate */);
@@ -132,6 +133,8 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
mBatteryHealth = batteryHealth;
} else if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) {
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_SAVER);
} else if (BatteryUtils.BYPASS_DOCK_DEFENDER_ACTION.equals(intent.getAction())) {
mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_STATUS);
}
}
}

View File

@@ -42,6 +42,8 @@ import com.android.settingslib.fuelgauge.EstimateKt;
import com.android.settingslib.utils.PowerUtil;
import com.android.settingslib.utils.StringUtil;
import java.text.NumberFormat;
public class BatteryInfo {
private static final String TAG = "BatteryInfo";
@@ -49,6 +51,7 @@ public class BatteryInfo {
public CharSequence remainingLabel;
public int batteryLevel;
public int batteryStatus;
public int pluggedStatus;
public boolean discharging = true;
public boolean isOverheated;
public long remainingTimeUs = 0;
@@ -253,7 +256,8 @@ public class BatteryInfo {
info.mBatteryUsageStats = batteryUsageStats;
info.batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
info.batteryPercentString = Utils.formatPercentage(info.batteryLevel);
info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
info.pluggedStatus = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
info.mCharging = info.pluggedStatus != 0;
info.averageTimeToDischarge = estimate.getAverageDischargeTime();
info.isOverheated = batteryBroadcast.getIntExtra(
BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN)
@@ -280,25 +284,37 @@ public class BatteryInfo {
BatteryManager.BATTERY_STATUS_UNKNOWN);
info.discharging = false;
info.suggestionLabel = null;
if (info.isOverheated && status != BatteryManager.BATTERY_STATUS_FULL) {
int dockDefenderMode = BatteryUtils.getCurrentDockDefenderMode(context, info);
if ((info.isOverheated && status != BatteryManager.BATTERY_STATUS_FULL
&& dockDefenderMode == BatteryUtils.DockDefenderMode.DISABLED)
|| dockDefenderMode == BatteryUtils.DockDefenderMode.ACTIVE) {
// Battery defender active, battery charging paused
info.remainingLabel = null;
int chargingLimitedResId = R.string.power_charging_limited;
info.chargeLabel =
context.getString(chargingLimitedResId, info.batteryPercentString);
} else if (chargeTimeMs > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
info.chargeLabel = context.getString(chargingLimitedResId, info.batteryPercentString);
} else if ((chargeTimeMs > 0 && status != BatteryManager.BATTERY_STATUS_FULL
&& dockDefenderMode == BatteryUtils.DockDefenderMode.DISABLED)
|| dockDefenderMode == BatteryUtils.DockDefenderMode.TEMPORARILY_BYPASSED) {
// Battery is charging to full
info.remainingTimeUs = PowerUtil.convertMsToUs(chargeTimeMs);
final CharSequence timeString = StringUtil.formatElapsedTime(
context,
PowerUtil.convertUsToMs(info.remainingTimeUs),
false /* withSeconds */,
final CharSequence timeString = StringUtil.formatElapsedTime(context,
(double) PowerUtil.convertUsToMs(info.remainingTimeUs), false /* withSeconds */,
true /* collapseTimeUnit */);
int resId = R.string.power_charging_duration;
info.remainingLabel = context.getString(
R.string.power_remaining_charging_duration_only, timeString);
info.remainingLabel = context.getString(R.string.power_remaining_charging_duration_only,
timeString);
info.chargeLabel = context.getString(resId, info.batteryPercentString, timeString);
} else if (dockDefenderMode == BatteryUtils.DockDefenderMode.FUTURE_BYPASS) {
// Dock defender will be triggered in the future, charging will be paused at 90%.
final int extraValue = context.getResources().getInteger(
R.integer.config_battery_extra_tip_value);
final String extraPercentage = NumberFormat.getPercentInstance().format(
extraValue * 0.01f);
info.chargeLabel = context.getString(R.string.power_charging_future_paused,
info.batteryPercentString, extraPercentage);
} else {
final String chargeStatusLabel =
Utils.getBatteryStatus(context, batteryBroadcast, compactStatus);
final String chargeStatusLabel = Utils.getBatteryStatus(context, batteryBroadcast,
compactStatus);
info.remainingLabel = null;
info.chargeLabel = info.batteryLevel == 100 ? info.batteryPercentString :
resources.getString(R.string.power_charging, info.batteryPercentString,

View File

@@ -24,6 +24,7 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.BatteryConsumer;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
@@ -33,6 +34,7 @@ import android.os.Process;
import android.os.SystemClock;
import android.os.UidBatteryConsumer;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.IntDef;
@@ -72,6 +74,11 @@ public class BatteryUtils {
/** Special UID for aggregated other users. */
public static final long UID_OTHER_USERS = Long.MIN_VALUE;
/** Flag to check if the dock defender mode has been temporarily bypassed */
public static final String SETTINGS_GLOBAL_DOCK_DEFENDER_BYPASS = "dock_defender_bypass";
public static final String BYPASS_DOCK_DEFENDER_ACTION = "battery.dock.defender.bypass";
@Retention(RetentionPolicy.SOURCE)
@IntDef({StatusType.SCREEN_USAGE,
StatusType.FOREGROUND,
@@ -85,6 +92,18 @@ public class BatteryUtils {
int ALL = 3;
}
@Retention(RetentionPolicy.SOURCE)
@IntDef({DockDefenderMode.FUTURE_BYPASS,
DockDefenderMode.ACTIVE,
DockDefenderMode.TEMPORARILY_BYPASSED,
DockDefenderMode.DISABLED})
public @interface DockDefenderMode {
int FUTURE_BYPASS = 0;
int ACTIVE = 1;
int TEMPORARILY_BYPASSED = 2;
int DISABLED = 3;
}
private static final String TAG = "BatteryUtils";
private static BatteryUtils sInstance;
@@ -570,4 +589,21 @@ public class BatteryUtils {
return -1L;
}
/** Gets the current dock defender mode */
public static int getCurrentDockDefenderMode(Context context, BatteryInfo batteryInfo) {
if (batteryInfo.pluggedStatus == BatteryManager.BATTERY_PLUGGED_DOCK) {
if (Settings.Global.getInt(context.getContentResolver(),
SETTINGS_GLOBAL_DOCK_DEFENDER_BYPASS, 0) == 1) {
return DockDefenderMode.TEMPORARILY_BYPASSED;
} else if (batteryInfo.isOverheated && FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.isExtraDefend()) {
return DockDefenderMode.ACTIVE;
} else if (!batteryInfo.isOverheated) {
return DockDefenderMode.FUTURE_BYPASS;
}
}
return DockDefenderMode.DISABLED;
}
}

View File

@@ -147,7 +147,7 @@ public interface PowerUsageFeatureProvider {
/**
* Gets a intent for one time bypass charge limited to resume charging.
*/
Intent getResumeChargeIntent();
Intent getResumeChargeIntent(boolean isDockDefender);
/**
* Returns battery history data with corresponding timestamp key.

View File

@@ -156,7 +156,7 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
}
@Override
public Intent getResumeChargeIntent() {
public Intent getResumeChargeIntent(boolean isDockDefender) {
return null;
}

View File

@@ -24,6 +24,7 @@ import androidx.annotation.VisibleForTesting;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.detectors.BatteryDefenderDetector;
import com.android.settings.fuelgauge.batterytip.detectors.DockDefenderDetector;
import com.android.settings.fuelgauge.batterytip.detectors.EarlyWarningDetector;
import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
@@ -74,6 +75,7 @@ public class BatteryTipLoader extends AsyncLoaderCompat<List<BatteryTip>> {
tips.add(new EarlyWarningDetector(policy, context).detect());
tips.add(new BatteryDefenderDetector(
batteryInfo, context.getApplicationContext()).detect());
tips.add(new DockDefenderDetector(batteryInfo, context.getApplicationContext()).detect());
Collections.sort(tips);
return tips;
}

View File

@@ -37,11 +37,10 @@ public class BatteryDefenderDetector implements BatteryTipDetector {
@Override
public BatteryTip detect() {
if (mBatteryInfo.isOverheated) {
final boolean extraDefend = FeatureFactory.getFactory(mContext)
.getPowerUsageFeatureProvider(mContext)
.isExtraDefend();
return new BatteryDefenderTip(BatteryTip.StateType.NEW, extraDefend);
if (mBatteryInfo.isOverheated && !FeatureFactory.getFactory(mContext)
.getPowerUsageFeatureProvider(mContext)
.isExtraDefend()) {
return new BatteryDefenderTip(BatteryTip.StateType.NEW);
}
return new BatteryDefenderTip(BatteryTip.StateType.INVISIBLE);
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batterytip.detectors;
import android.content.Context;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.DockDefenderTip;
/**
* Detect whether the dock defender mode is enabled.
*/
public class DockDefenderDetector implements BatteryTipDetector {
private final BatteryInfo mBatteryInfo;
private final Context mContext;
public DockDefenderDetector(BatteryInfo batteryInfo, Context context) {
mBatteryInfo = batteryInfo;
mContext = context;
}
@Override
public BatteryTip detect() {
int mode = BatteryUtils.getCurrentDockDefenderMode(mContext, mBatteryInfo);
return new DockDefenderTip(
mode != BatteryUtils.DockDefenderMode.DISABLED
? BatteryTip.StateType.NEW
: BatteryTip.StateType.INVISIBLE,
mode);
}
}

View File

@@ -32,24 +32,15 @@ import com.android.settings.widget.CardPreference;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import java.text.NumberFormat;
/**
* Tip to show current battery is overheated
*/
public class BatteryDefenderTip extends BatteryTip {
private static final String TAG = "BatteryDefenderTip";
private boolean mExtraDefend = false;
public BatteryDefenderTip(@StateType int state) {
this(state, false);
}
public BatteryDefenderTip(@StateType int state, boolean extraDefend) {
super(TipType.BATTERY_DEFENDER, state, true /* showDialog */);
mExtraDefend = extraDefend;
mShowDialog = false;
super(TipType.BATTERY_DEFENDER, state, false /* showDialog */);
}
private BatteryDefenderTip(Parcel in) {
@@ -63,14 +54,6 @@ public class BatteryDefenderTip extends BatteryTip {
@Override
public CharSequence getSummary(Context context) {
if (mExtraDefend) {
final int extraValue = context.getResources()
.getInteger(R.integer.config_battery_extra_tip_value);
final String extraPercentage = NumberFormat.getPercentInstance()
.format(extraValue * 0.01f);
return context.getString(
R.string.battery_tip_limited_temporarily_extra_summary, extraPercentage);
}
return context.getString(R.string.battery_tip_limited_temporarily_summary);
}
@@ -131,7 +114,7 @@ public class BatteryDefenderTip extends BatteryTip {
final Intent intent =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.getResumeChargeIntent();
.getResumeChargeIntent(false);
if (intent != null) {
context.sendBroadcast(intent);
}

View File

@@ -58,7 +58,8 @@ public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable {
TipType.REDUCED_BATTERY,
TipType.LOW_BATTERY,
TipType.REMOVE_APP_RESTRICTION,
TipType.BATTERY_DEFENDER})
TipType.BATTERY_DEFENDER,
TipType.DOCK_DEFENDER})
public @interface TipType {
int SMART_BATTERY_MANAGER = 0;
int APP_RESTRICTION = 1;
@@ -69,6 +70,7 @@ public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable {
int SUMMARY = 6;
int REMOVE_APP_RESTRICTION = 7;
int BATTERY_DEFENDER = 8;
int DOCK_DEFENDER = 9;
}
@VisibleForTesting
@@ -78,12 +80,13 @@ public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable {
TIP_ORDER.append(TipType.BATTERY_SAVER, 0);
TIP_ORDER.append(TipType.LOW_BATTERY, 1);
TIP_ORDER.append(TipType.BATTERY_DEFENDER, 2);
TIP_ORDER.append(TipType.APP_RESTRICTION, 3);
TIP_ORDER.append(TipType.HIGH_DEVICE_USAGE, 4);
TIP_ORDER.append(TipType.SUMMARY, 5);
TIP_ORDER.append(TipType.SMART_BATTERY_MANAGER, 6);
TIP_ORDER.append(TipType.REDUCED_BATTERY, 7);
TIP_ORDER.append(TipType.REMOVE_APP_RESTRICTION, 8);
TIP_ORDER.append(TipType.DOCK_DEFENDER, 3);
TIP_ORDER.append(TipType.APP_RESTRICTION, 4);
TIP_ORDER.append(TipType.HIGH_DEVICE_USAGE, 5);
TIP_ORDER.append(TipType.SUMMARY, 6);
TIP_ORDER.append(TipType.SMART_BATTERY_MANAGER, 7);
TIP_ORDER.append(TipType.REDUCED_BATTERY, 8);
TIP_ORDER.append(TipType.REMOVE_APP_RESTRICTION, 9);
}
private static final String KEY_PREFIX = "key_battery_tip";

View File

@@ -0,0 +1,192 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batterytip.tips;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.util.Log;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.BatteryUtils.DockDefenderMode;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.CardPreference;
import com.android.settingslib.HelpUtils;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import java.text.NumberFormat;
/**
* Tip to show dock defender status
*/
public class DockDefenderTip extends BatteryTip {
private static final String TAG = "DockDefenderTip";
private int mMode;
public DockDefenderTip(@StateType int state, @DockDefenderMode int mode) {
super(TipType.DOCK_DEFENDER, state, false);
mMode = mode;
}
private DockDefenderTip(Parcel in) {
super(in);
}
public int getMode() {
return mMode;
}
@Override
public CharSequence getTitle(Context context) {
switch (mMode) {
case DockDefenderMode.FUTURE_BYPASS:
return context.getString(R.string.battery_tip_dock_defender_future_bypass_title,
getExtraPercentage(context));
case DockDefenderMode.ACTIVE:
return context.getString(R.string.battery_tip_dock_defender_active_title);
case DockDefenderMode.TEMPORARILY_BYPASSED:
return context.getString(
R.string.battery_tip_dock_defender_temporarily_bypassed_title);
default:
return null;
}
}
@Override
public CharSequence getSummary(Context context) {
switch (mMode) {
case DockDefenderMode.FUTURE_BYPASS:
return context.getString(R.string.battery_tip_dock_defender_future_bypass_summary,
getExtraPercentage(context));
case DockDefenderMode.ACTIVE:
return context.getString(R.string.battery_tip_dock_defender_active_summary,
getExtraPercentage(context));
case DockDefenderMode.TEMPORARILY_BYPASSED:
return context.getString(
R.string.battery_tip_dock_defender_temporarily_bypassed_summary,
getExtraPercentage(context));
default:
return null;
}
}
@Override
public int getIconId() {
return R.drawable.ic_battery_status_protected_24dp;
}
@Override
public void updateState(BatteryTip tip) {
mState = tip.mState;
if (tip instanceof DockDefenderTip) {
mMode = ((DockDefenderTip) tip).mMode;
}
}
@Override
public void log(Context context, MetricsFeatureProvider metricsFeatureProvider) {
metricsFeatureProvider.action(context, SettingsEnums.ACTION_DOCK_DEFENDER_TIP,
mState);
}
@Override
public void updatePreference(Preference preference) {
super.updatePreference(preference);
final Context context = preference.getContext();
CardPreference cardPreference = castToCardPreferenceSafely(preference);
if (cardPreference == null) {
Log.e(TAG, "cast Preference to CardPreference failed");
return;
}
cardPreference.setSelectable(false);
switch (mMode) {
case DockDefenderMode.FUTURE_BYPASS:
case DockDefenderMode.ACTIVE:
cardPreference.setPrimaryButtonText(
context.getString(R.string.battery_tip_charge_to_full_button));
cardPreference.setPrimaryButtonClickListener(unused -> {
resumeCharging(context);
mMode = DockDefenderMode.TEMPORARILY_BYPASSED;
context.sendBroadcast(new Intent().setAction(
BatteryUtils.BYPASS_DOCK_DEFENDER_ACTION).setPackage(
context.getPackageName()).addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_FOREGROUND));
updatePreference(preference);
});
cardPreference.setPrimaryButtonVisible(true);
break;
case DockDefenderMode.TEMPORARILY_BYPASSED:
cardPreference.setPrimaryButtonVisible(false);
break;
default:
cardPreference.setVisible(false);
return;
}
cardPreference.setSecondaryButtonText(context.getString(R.string.learn_more));
//TODO: update helper string
cardPreference.setSecondaryButtonClickListener(
button -> button.startActivityForResult(
HelpUtils.getHelpIntent(
context,
context.getString(R.string.help_url_battery_defender),
/* backupContext */ ""), /* requestCode */ 0));
cardPreference.setSecondaryButtonVisible(true);
cardPreference.setSecondaryButtonContentDescription(context.getString(
R.string.battery_tip_limited_temporarily_sec_button_content_description));
}
private CardPreference castToCardPreferenceSafely(Preference preference) {
return preference instanceof CardPreference ? (CardPreference) preference : null;
}
private void resumeCharging(Context context) {
final Intent intent =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.getResumeChargeIntent(true);
if (intent != null) {
context.sendBroadcast(intent);
}
Log.i(TAG, "send resume charging broadcast intent=" + intent);
}
private String getExtraPercentage(Context context) {
final int extraValue = context.getResources()
.getInteger(R.integer.config_battery_extra_tip_value);
return NumberFormat.getPercentInstance()
.format(extraValue * 0.01f);
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new DockDefenderTip(in);
}
public BatteryTip[] newArray(int size) {
return new DockDefenderTip[size];
}
};
}