Improve battery tips cards.
- Updated Card UI - Set thumb-up/down feedback preference to be GONE - Add basic button action [Screenshots] https://screenshot.googleplex.com/925MEzRyUCxZCi8 Test: manual Change-Id: I356648d0fcec5dd1ea724297187ecafb5be55fd8 Merged-In: I356648d0fcec5dd1ea724297187ecafb5be55fd8 Bug: 291689623
This commit is contained in:
@@ -82,7 +82,7 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
|
||||
|
||||
@Override
|
||||
public boolean isBatteryTipsFeedbackEnabled() {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -105,10 +105,9 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
public interface OnBatteryTipsUpdatedListener {
|
||||
/**
|
||||
* The callback function for the battery tips card is updated.
|
||||
* @param title the title of the battery tip card
|
||||
* @param summary the summary of the battery tip card
|
||||
* @param powerAnomalyEvent the power anomaly event with highest score
|
||||
*/
|
||||
void onBatteryTipsUpdated(String title, String summary);
|
||||
void onBatteryTipsUpdated(PowerAnomalyEvent powerAnomalyEvent);
|
||||
}
|
||||
|
||||
|
||||
@@ -365,7 +364,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
|
||||
slotUsageData, getSlotInformation(), isBatteryUsageMapNullOrEmpty());
|
||||
|
||||
if (mOnBatteryTipsUpdatedListener != null) {
|
||||
mOnBatteryTipsUpdatedListener.onBatteryTipsUpdated(null, null);
|
||||
// TODO: replace with a selected powerAnomalyEvent with highest score
|
||||
mOnBatteryTipsUpdatedListener.onBatteryTipsUpdated(null);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
@@ -17,15 +17,19 @@
|
||||
package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
@@ -40,12 +44,14 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
|
||||
|
||||
private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
|
||||
private MaterialButton mActionButton;
|
||||
private ImageButton mDismissButton;
|
||||
private ImageButton mThumbUpButton;
|
||||
private ImageButton mThumbDownButton;
|
||||
private CharSequence mTitle;
|
||||
private CharSequence mSummary;
|
||||
@VisibleForTesting
|
||||
CharSequence mMainButtonLabel;
|
||||
@VisibleForTesting
|
||||
CharSequence mDismissButtonLabel;
|
||||
@VisibleForTesting
|
||||
String mDestinationComponentName;
|
||||
@VisibleForTesting
|
||||
int mSourceMetricsCategory;
|
||||
|
||||
public BatteryTipsCardPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
@@ -55,34 +61,65 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
|
||||
.getPowerUsageFeatureProvider(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(CharSequence title) {
|
||||
mTitle = title;
|
||||
notifyChanged();
|
||||
/**
|
||||
* Update the label of main button in tips card.
|
||||
*/
|
||||
public void setMainButtonLabel(CharSequence label) {
|
||||
if (!TextUtils.equals(mMainButtonLabel, label)) {
|
||||
mMainButtonLabel = label;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSummary(CharSequence summary) {
|
||||
mSummary = summary;
|
||||
notifyChanged();
|
||||
/**
|
||||
* Update the label of dismiss button in tips card.
|
||||
*/
|
||||
public void setDismissButtonLabel(CharSequence label) {
|
||||
if (!TextUtils.equals(mDismissButtonLabel, label)) {
|
||||
mDismissButtonLabel = label;
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the info of target fragment launched by main button.
|
||||
*/
|
||||
public void setMainButtonLauncherInfo(final String destinationClassName,
|
||||
final Integer sourceMetricsCategory) {
|
||||
mDestinationComponentName = destinationClassName;
|
||||
mSourceMetricsCategory = sourceMetricsCategory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
// TODO: replace with the settings anomaly obtained from detectSettingsAnomaly();
|
||||
final int viewId = view.getId();
|
||||
if (viewId == R.id.main_button || viewId == R.id.tips_card) {
|
||||
if (TextUtils.isEmpty(mDestinationComponentName)) {
|
||||
return;
|
||||
}
|
||||
new SubSettingLauncher(getContext())
|
||||
.setDestination(mDestinationComponentName)
|
||||
.setSourceMetricsCategory(mSourceMetricsCategory)
|
||||
.launch();
|
||||
} else if (viewId == R.id.dismiss_button) {
|
||||
setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(view);
|
||||
|
||||
((TextView) view.findViewById(R.id.title)).setText(mTitle);
|
||||
((TextView) view.findViewById(R.id.summary)).setText(mSummary);
|
||||
((TextView) view.findViewById(R.id.title)).setText(getTitle());
|
||||
|
||||
mActionButton = (MaterialButton) view.findViewById(R.id.action_button);
|
||||
mActionButton.setOnClickListener(this);
|
||||
mDismissButton = (ImageButton) view.findViewById(R.id.dismiss_button);
|
||||
mDismissButton.setOnClickListener(this);
|
||||
LinearLayout tipsCard = (LinearLayout) view.findViewById(R.id.tips_card);
|
||||
tipsCard.setOnClickListener(this);
|
||||
MaterialButton mainButton = (MaterialButton) view.findViewById(R.id.main_button);
|
||||
mainButton.setOnClickListener(this);
|
||||
mainButton.setText(mMainButtonLabel);
|
||||
MaterialButton dismissButton = (MaterialButton) view.findViewById(R.id.dismiss_button);
|
||||
dismissButton.setOnClickListener(this);
|
||||
dismissButton.setText(mDismissButtonLabel);
|
||||
|
||||
if (!mPowerUsageFeatureProvider.isBatteryTipsFeedbackEnabled()) {
|
||||
return;
|
||||
@@ -91,9 +128,9 @@ public class BatteryTipsCardPreference extends Preference implements View.OnClic
|
||||
.setBackgroundResource(R.drawable.battery_tips_half_rounded_top_bg);
|
||||
view.findViewById(R.id.feedback_card).setVisibility(View.VISIBLE);
|
||||
|
||||
mThumbUpButton = (ImageButton) view.findViewById(R.id.thumb_up);
|
||||
mThumbUpButton.setOnClickListener(this);
|
||||
mThumbDownButton = (ImageButton) view.findViewById(R.id.thumb_down);
|
||||
mThumbDownButton.setOnClickListener(this);
|
||||
ImageButton thumbUpButton = (ImageButton) view.findViewById(R.id.thumb_up);
|
||||
thumbUpButton.setOnClickListener(this);
|
||||
ImageButton thumbDownButton = (ImageButton) view.findViewById(R.id.thumb_down);
|
||||
thumbDownButton.setOnClickListener(this);
|
||||
}
|
||||
}
|
||||
|
@@ -17,13 +17,18 @@
|
||||
package com.android.settings.fuelgauge.batteryusage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Controls the update for battery tips card */
|
||||
public class BatteryTipsController extends BasePreferenceController {
|
||||
|
||||
@@ -31,15 +36,23 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
private static final String ROOT_PREFERENCE_KEY = "battery_tips_category";
|
||||
private static final String CARD_PREFERENCE_KEY = "battery_tips_card";
|
||||
|
||||
private final PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
private final String[] mPowerAnomalyKeys;
|
||||
|
||||
private Context mPrefContext;
|
||||
private BatteryTipsCardPreference mCardPreference;
|
||||
@VisibleForTesting
|
||||
BatteryTipsCardPreference mCardPreference;
|
||||
@VisibleForTesting
|
||||
PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
|
||||
public BatteryTipsController(Context context) {
|
||||
super(context, ROOT_PREFERENCE_KEY);
|
||||
mPowerUsageFeatureProvider = FeatureFactory.getFactory(context)
|
||||
.getPowerUsageFeatureProvider(context);
|
||||
mPowerAnomalyKeys = context.getResources().getStringArray(R.array.power_anomaly_keys);
|
||||
}
|
||||
|
||||
private boolean isTipsCardVisible() {
|
||||
// TODO: compared with the timestamp of last user dismiss action in sharedPreference.
|
||||
return mPowerUsageFeatureProvider.isBatteryTipsEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,28 +63,89 @@ public class BatteryTipsController extends BasePreferenceController {
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPrefContext = screen.getContext();
|
||||
mCardPreference = screen.findPreference(CARD_PREFERENCE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the card visibility and contents.
|
||||
* @param title a string not extend 2 lines.
|
||||
* @param summary a string not extend 10 lines.
|
||||
*/
|
||||
// TODO: replace parameters with SettingsAnomaly Data Proto
|
||||
public void handleBatteryTipsCardUpdated(String title, String summary) {
|
||||
if (!mPowerUsageFeatureProvider.isBatteryTipsEnabled()) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
@VisibleForTesting
|
||||
int getPowerAnomalyEventIndex(String powerAnomalyKey) {
|
||||
for (int index = 0; index < mPowerAnomalyKeys.length; index++) {
|
||||
if (mPowerAnomalyKeys[index].equals(powerAnomalyKey)) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
if (title == null || summary == null) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
mCardPreference.setTitle(title);
|
||||
mCardPreference.setSummary(summary);
|
||||
mCardPreference.setVisible(true);
|
||||
return -1;
|
||||
}
|
||||
|
||||
private <T> T getInfo(PowerAnomalyEvent powerAnomalyEvent,
|
||||
Function<WarningBannerInfo, T> warningBannerInfoSupplier,
|
||||
Function<WarningItemInfo, T> warningItemInfoSupplier) {
|
||||
if (powerAnomalyEvent.hasWarningBannerInfo() && warningBannerInfoSupplier != null) {
|
||||
return warningBannerInfoSupplier.apply(powerAnomalyEvent.getWarningBannerInfo());
|
||||
} else if (powerAnomalyEvent.hasWarningItemInfo() && warningItemInfoSupplier != null) {
|
||||
return warningItemInfoSupplier.apply(powerAnomalyEvent.getWarningItemInfo());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getString(PowerAnomalyEvent powerAnomalyEvent,
|
||||
Function<WarningBannerInfo, String> warningBannerInfoSupplier,
|
||||
Function<WarningItemInfo, String> warningItemInfoSupplier,
|
||||
int resourceId, int resourceIndex) {
|
||||
String string =
|
||||
getInfo(powerAnomalyEvent, warningBannerInfoSupplier, warningItemInfoSupplier);
|
||||
|
||||
if (!TextUtils.isEmpty(string) || resourceId < 0) {
|
||||
return string;
|
||||
}
|
||||
|
||||
if (resourceIndex >= 0) {
|
||||
string = mContext.getResources().getStringArray(resourceId)[resourceIndex];
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void handleBatteryTipsCardUpdated(PowerAnomalyEvent powerAnomalyEvent) {
|
||||
if (!isTipsCardVisible()) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
if (powerAnomalyEvent == null) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get card preference strings and navigate fragment info
|
||||
final int index = getPowerAnomalyEventIndex(powerAnomalyEvent.getKey());
|
||||
|
||||
String titleString = getString(powerAnomalyEvent, WarningBannerInfo::getTitleString,
|
||||
WarningItemInfo::getTitleString, R.array.power_anomaly_titles, index);
|
||||
if (titleString.isEmpty()) {
|
||||
mCardPreference.setVisible(false);
|
||||
return;
|
||||
}
|
||||
|
||||
String mainBtnString = getString(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonString, WarningItemInfo::getMainButtonString,
|
||||
R.array.power_anomaly_main_btn_strings, index);
|
||||
String dismissBtnString = getString(powerAnomalyEvent,
|
||||
WarningBannerInfo::getCancelButtonString, WarningItemInfo::getCancelButtonString,
|
||||
R.array.power_anomaly_dismiss_btn_strings, index);
|
||||
|
||||
String destinationClassName = getString(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonDestination,
|
||||
WarningItemInfo::getMainButtonDestination,
|
||||
-1, -1);
|
||||
Integer sourceMetricsCategory = getInfo(powerAnomalyEvent,
|
||||
WarningBannerInfo::getMainButtonSourceMetricsCategory,
|
||||
WarningItemInfo::getMainButtonSourceMetricsCategory);
|
||||
|
||||
// Updated card preference and main button fragment launcher
|
||||
mCardPreference.setTitle(titleString);
|
||||
mCardPreference.setMainButtonLabel(mainBtnString);
|
||||
mCardPreference.setDismissButtonLabel(dismissBtnString);
|
||||
mCardPreference.setMainButtonLauncherInfo(destinationClassName, sourceMetricsCategory);
|
||||
mCardPreference.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user