App anomaly tips on PowerUsage App list
Screenshots: [in bg - banner] https://screenshot.googleplex.com/MzLC6LfX93TkkYf [in bg - hints] https://screenshot.googleplex.com/9JLXNsRiVG8arAU [in fg - banner] https://screenshot.googleplex.com/9oYbwUkeeLbQX2t [in fg - hints] https://screenshot.googleplex.com/53DTTUCUnf8rsoE [apps anomaly highlight hint + settings anomaly banner] https://screenshot.googleplex.com/8NdS2VMrSzwv2DM Bug: 291689643 Bug: 291689623 Bug: 284893240 Test: manual Change-Id: Ic02db49cb3794ef134759d9dcec5f5ef32454a95 Merged-In: Ic02db49cb3794ef134759d9dcec5f5ef32454a95 Merged-In: I7015cdf5a96d518febb160934d780ae84fe14427
This commit is contained in:
@@ -18,21 +18,15 @@ package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Controls the update for battery tips card */
|
||||
public class BatteryTipsController extends BasePreferenceController {
|
||||
|
||||
@@ -59,6 +53,10 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
|
||||
@VisibleForTesting
|
||||
BatteryTipsCardPreference mCardPreference;
|
||||
@VisibleForTesting
|
||||
AnomalyEventWrapper mAnomalyEventWrapper = null;
|
||||
@VisibleForTesting
|
||||
Boolean mIsAcceptable = false;
|
||||
|
||||
public BatteryTipsController(Context context) {
|
||||
super(context, ROOT_PREFERENCE_KEY);
|
||||
@@ -85,132 +83,56 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
mOnAnomalyRejectListener = listener;
|
||||
}
|
||||
|
||||
private <T> T getInfo(PowerAnomalyEvent powerAnomalyEvent,
|
||||
Function<WarningBannerInfo, T> warningBannerInfoSupplier,
|
||||
Function<WarningItemInfo, T> warningItemInfoSupplier) {
|
||||
if (warningBannerInfoSupplier != null && powerAnomalyEvent.hasWarningBannerInfo()) {
|
||||
return warningBannerInfoSupplier.apply(powerAnomalyEvent.getWarningBannerInfo());
|
||||
} else if (warningItemInfoSupplier != null && powerAnomalyEvent.hasWarningItemInfo()) {
|
||||
return warningItemInfoSupplier.apply(powerAnomalyEvent.getWarningItemInfo());
|
||||
void acceptTipsCard() {
|
||||
if (mAnomalyEventWrapper == null || !mIsAcceptable) {
|
||||
return;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getStringFromResource(int resourceId, int resourceIndex) {
|
||||
if (resourceId < 0) {
|
||||
return null;
|
||||
// For anomaly events with same record key, dismissed until next time full charged.
|
||||
final String dismissRecordKey = mAnomalyEventWrapper.getDismissRecordKey();
|
||||
if (!TextUtils.isEmpty(dismissRecordKey)) {
|
||||
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, dismissRecordKey);
|
||||
}
|
||||
final String[] stringArray = mContext.getResources().getStringArray(resourceId);
|
||||
return (resourceIndex >= 0 && resourceIndex < stringArray.length)
|
||||
? stringArray[resourceIndex] : null;
|
||||
mCardPreference.setVisible(false);
|
||||
mMetricsFeatureProvider.action(
|
||||
mContext, SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT,
|
||||
mAnomalyEventWrapper.getEventId());
|
||||
}
|
||||
|
||||
private int getResourceId(int resourceId, int resourceIndex, String defType) {
|
||||
final String key = getStringFromResource(resourceId, resourceIndex);
|
||||
return TextUtils.isEmpty(key) ? 0
|
||||
: mContext.getResources().getIdentifier(key, defType, mContext.getPackageName());
|
||||
}
|
||||
|
||||
private String getString(PowerAnomalyEvent powerAnomalyEvent,
|
||||
Function<WarningBannerInfo, String> warningBannerInfoSupplier,
|
||||
Function<WarningItemInfo, String> warningItemInfoSupplier,
|
||||
int resourceId, int resourceIndex) {
|
||||
String string =
|
||||
getInfo(powerAnomalyEvent, warningBannerInfoSupplier, warningItemInfoSupplier);
|
||||
return (!TextUtils.isEmpty(string) || resourceId < 0) ? string
|
||||
: getStringFromResource(resourceId, resourceIndex);
|
||||
}
|
||||
|
||||
/** Generate a key string of current anomaly to record as dismissed in sharedPreferences. */
|
||||
public static String getDismissRecordKey(PowerAnomalyEvent event) {
|
||||
if (!event.hasKey()) {
|
||||
return null;
|
||||
}
|
||||
switch (event.getKey()){
|
||||
case KEY_APP:
|
||||
return event.hasWarningItemInfo()
|
||||
&& event.getWarningItemInfo().hasDismissRecordKey()
|
||||
? event.getWarningItemInfo().getDismissRecordKey() : null;
|
||||
default:
|
||||
return event.getKey().name();
|
||||
}
|
||||
}
|
||||
|
||||
void handleBatteryTipsCardUpdated(PowerAnomalyEvent powerAnomalyEvent) {
|
||||
if (powerAnomalyEvent == null) {
|
||||
void handleBatteryTipsCardUpdated(
|
||||
AnomalyEventWrapper anomalyEventWrapper, boolean isAcceptable) {
|
||||
mAnomalyEventWrapper = anomalyEventWrapper;
|
||||
mIsAcceptable = isAcceptable;
|
||||
if (mAnomalyEventWrapper == null) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get card icon and color styles
|
||||
final int cardStyleId = powerAnomalyEvent.getType().getNumber();
|
||||
final int iconResId = getResourceId(
|
||||
R.array.battery_tips_card_icons, cardStyleId, "drawable");
|
||||
final int colorResId = getResourceId(
|
||||
R.array.battery_tips_card_colors, cardStyleId, "color");
|
||||
|
||||
// Get card preference strings and navigate fragment info
|
||||
final String eventId = powerAnomalyEvent.hasEventId()
|
||||
? powerAnomalyEvent.getEventId() : null;
|
||||
final PowerAnomalyKey powerAnomalyKey = powerAnomalyEvent.hasKey()
|
||||
? powerAnomalyEvent.getKey() : null;
|
||||
final int resourceIndex = powerAnomalyKey != null ? powerAnomalyKey.getNumber() : -1;
|
||||
final String eventId = mAnomalyEventWrapper.getEventId();
|
||||
|
||||
final String titleString = getString(powerAnomalyEvent, WarningBannerInfo::getTitleString,
|
||||
WarningItemInfo::getTitleString, R.array.power_anomaly_titles, resourceIndex);
|
||||
if (titleString.isEmpty()) {
|
||||
// Update card & buttons preference
|
||||
if (!mAnomalyEventWrapper.updateTipsCardPreference(mCardPreference)) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
final String mainBtnString = getString(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonString, WarningItemInfo::getMainButtonString,
|
||||
R.array.power_anomaly_main_btn_strings, resourceIndex);
|
||||
final String dismissBtnString = getString(powerAnomalyEvent,
|
||||
WarningBannerInfo::getCancelButtonString, WarningItemInfo::getCancelButtonString,
|
||||
R.array.power_anomaly_dismiss_btn_strings, resourceIndex);
|
||||
|
||||
final String destinationClassName = getInfo(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonDestination, null);
|
||||
final Integer sourceMetricsCategory = getInfo(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonSourceMetricsCategory, null);
|
||||
final String preferenceHighlightKey = getInfo(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonSourceHighlightKey, null);
|
||||
|
||||
// Update card preference and main button fragment launcher
|
||||
mCardPreference.setTitle(titleString);
|
||||
mCardPreference.setIconResourceId(iconResId);
|
||||
mCardPreference.setMainButtonStrokeColorResourceId(colorResId);
|
||||
mCardPreference.setMainButtonLabel(mainBtnString);
|
||||
mCardPreference.setDismissButtonLabel(dismissBtnString);
|
||||
|
||||
// Set battery tips card listener
|
||||
mCardPreference.setOnConfirmListener(() -> {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyConfirmListener != null) {
|
||||
mOnAnomalyConfirmListener.onAnomalyConfirm();
|
||||
} else if (!TextUtils.isEmpty(destinationClassName)) {
|
||||
// Navigate to sub setting page
|
||||
Bundle arguments = Bundle.EMPTY;
|
||||
if (!TextUtils.isEmpty(preferenceHighlightKey)) {
|
||||
arguments = new Bundle(1);
|
||||
arguments.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY,
|
||||
preferenceHighlightKey);
|
||||
}
|
||||
new SubSettingLauncher(mContext)
|
||||
.setDestination(destinationClassName)
|
||||
.setSourceMetricsCategory(sourceMetricsCategory)
|
||||
.setArguments(arguments)
|
||||
.launch();
|
||||
} else if (mAnomalyEventWrapper.launchSubSetting()) {
|
||||
mMetricsFeatureProvider.action(
|
||||
mContext, SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT, eventId);
|
||||
}
|
||||
mMetricsFeatureProvider.action(
|
||||
mContext, SettingsEnums.ACTION_BATTERY_TIPS_CARD_ACCEPT, eventId);
|
||||
});
|
||||
mCardPreference.setOnRejectListener(() -> {
|
||||
mCardPreference.setVisible(false);
|
||||
if (mOnAnomalyRejectListener != null) {
|
||||
mOnAnomalyRejectListener.onAnomalyReject();
|
||||
}
|
||||
// For anomaly events with same record key, dismissed until next time full charged.
|
||||
final String dismissRecordKey = getDismissRecordKey(powerAnomalyEvent);
|
||||
final String dismissRecordKey = mAnomalyEventWrapper.getDismissRecordKey();
|
||||
if (!TextUtils.isEmpty(dismissRecordKey)) {
|
||||
DatabaseUtils.setDismissedPowerAnomalyKeys(mContext, dismissRecordKey);
|
||||
}
|
||||
|
Reference in New Issue
Block a user