From 2148988cae4bb95df09efbcb41325c0d2f304a61 Mon Sep 17 00:00:00 2001 From: mxyyiyi Date: Wed, 26 Jul 2023 15:21:08 +0800 Subject: [PATCH] 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 --- .../battery_tips_all_rounded_bg_ripple.xml | 21 +++ res/layout/battery_tips_card.xml | 82 +++++----- res/values/arrays.xml | 21 +++ res/values/strings.xml | 17 +- .../PowerUsageFeatureProviderImpl.java | 2 +- .../BatteryChartPreferenceController.java | 8 +- .../BatteryTipsCardPreference.java | 87 +++++++--- .../batteryusage/BatteryTipsController.java | 116 +++++++++++--- .../PowerUsageFeatureProviderImplTest.java | 4 +- .../BatteryTipsCardPreferenceTest.java | 3 + .../BatteryTipsControllerTest.java | 149 ++++++++++++++++++ .../settings/testutils/BatteryTestUtils.java | 36 +++++ 12 files changed, 434 insertions(+), 112 deletions(-) create mode 100644 res/drawable/battery_tips_all_rounded_bg_ripple.xml create mode 100644 tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java diff --git a/res/drawable/battery_tips_all_rounded_bg_ripple.xml b/res/drawable/battery_tips_all_rounded_bg_ripple.xml new file mode 100644 index 00000000000..31805704b3b --- /dev/null +++ b/res/drawable/battery_tips_all_rounded_bg_ripple.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/res/layout/battery_tips_card.xml b/res/layout/battery_tips_card.xml index d2edb51c0ab..2fa5bf2ae4c 100644 --- a/res/layout/battery_tips_card.xml +++ b/res/layout/battery_tips_card.xml @@ -12,36 +12,16 @@ android:id="@+id/tips_card" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="@drawable/battery_tips_all_rounded_bg" + android:background="@drawable/battery_tips_all_rounded_bg_ripple" android:orientation="vertical" android:padding="24dp"> - - - - - - - - + android:layout_gravity="center_vertical|start" + android:src="@drawable/ic_battery_tips_lightbulb" /> - + android:gravity="end"> - + + + + + + + adaptive_brightness + screen_timeout + + + + Turn on adaptive brightness to extend battery life + Reduce screen timeout to extend battery life + + + + @string/battery_tips_card_action_button + @string/battery_tips_card_action_button + + + + @string/battery_tips_card_dismiss_button + @string/battery_tips_card_dismiss_button + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 040a91ed7cb..04901fb05dd 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9651,23 +9651,14 @@ Set battery usage for apps - Optimize + View Settings + + + Got it Is this message helpful? - - Turn on adaptive brightness to extend battery life - - - It will help reduce your daily battery drain by 10% - - - Reduce screen timeout to extend battery life - - - It will help reduce your daily battery drain by 10% - Unrestricted diff --git a/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImpl.java b/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImpl.java index 9b5bb5e47c6..ed9751834ce 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImpl.java +++ b/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImpl.java @@ -82,7 +82,7 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider @Override public boolean isBatteryTipsFeedbackEnabled() { - return true; + return false; } @Override diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java index b7e1885ce52..3231e55ccee 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java +++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryChartPreferenceController.java @@ -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; diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreference.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreference.java index 661d7c8aca3..f59f2e2c73d 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreference.java +++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreference.java @@ -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); } } diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java index bcedd4f5794..991a8f66eb7 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java +++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java @@ -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 getInfo(PowerAnomalyEvent powerAnomalyEvent, + Function warningBannerInfoSupplier, + Function 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 warningBannerInfoSupplier, + Function 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); + } } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImplTest.java index c9591a51b08..bf4e8938f88 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImplTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageFeatureProviderImplTest.java @@ -73,8 +73,8 @@ public class PowerUsageFeatureProviderImplTest { } @Test - public void testIsBatteryTipsFeedbackEnabled_returnTrue() { - assertThat(mPowerFeatureProvider.isBatteryTipsFeedbackEnabled()).isTrue(); + public void testIsBatteryTipsFeedbackEnabled_returnFalse() { + assertThat(mPowerFeatureProvider.isBatteryTipsFeedbackEnabled()).isFalse(); } @Test public void testGetBatteryUsageListConsumePowerThreshold_return0() { diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreferenceTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreferenceTest.java index 6f9a47416ae..bc787f1af0a 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsCardPreferenceTest.java @@ -35,11 +35,14 @@ public final class BatteryTipsCardPreferenceTest { private Context mContext; private BatteryTipsCardPreference mBatteryTipsCardPreference; + private BatteryTipsController mBatteryTipsController; @Before public void setUp() { mContext = spy(RuntimeEnvironment.application); mBatteryTipsCardPreference = new BatteryTipsCardPreference(mContext, /*attrs=*/ null); + mBatteryTipsController = new BatteryTipsController(mContext); + mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference; } @Test diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java new file mode 100644 index 00000000000..0c9a0b03a83 --- /dev/null +++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2023 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.batteryusage; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.res.Resources; +import android.os.LocaleList; + +import com.android.settings.fuelgauge.PowerUsageFeatureProvider; +import com.android.settings.testutils.BatteryTestUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +import java.util.Locale; +import java.util.TimeZone; + +@RunWith(RobolectricTestRunner.class) +public final class BatteryTipsControllerTest { + + private Context mContext; + private BatteryTipsController mBatteryTipsController; + + @Mock + private BatteryTipsCardPreference mBatteryTipsCardPreference; + + @Mock + private PowerUsageFeatureProvider mPowerUsageFeatureProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + Locale.setDefault(new Locale("en_US")); + org.robolectric.shadows.ShadowSettings.set24HourTimeFormat(false); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + mContext = spy(RuntimeEnvironment.application); + final Resources resources = spy(mContext.getResources()); + resources.getConfiguration().setLocales(new LocaleList(new Locale("en_US"))); + doReturn(resources).when(mContext).getResources(); + mBatteryTipsController = new BatteryTipsController(mContext); + mBatteryTipsController.mCardPreference = mBatteryTipsCardPreference; + mBatteryTipsController.mPowerUsageFeatureProvider = mPowerUsageFeatureProvider; + } + + @Test + public void parsePowerAnomalyKey_preDefinedKeys_returnTrue() { + final String[] keys = {"adaptive_brightness", "screen_timeout"}; + for (int index = 0; index < keys.length; index++) { + assertThat(mBatteryTipsController.getPowerAnomalyEventIndex(keys[index])) + .isEqualTo(index); + } + } + + @Test + public void parsePowerAnomalyKey_unknownKey_returnTrue() { + final String key = "unknown_key_for_test"; + assertThat(mBatteryTipsController.getPowerAnomalyEventIndex(key)).isEqualTo(-1); + } + + @Test + public void handleBatteryTipsCardUpdated_null_hidePreference() { + mBatteryTipsController.handleBatteryTipsCardUpdated(/* powerAnomalyEvents= */ null); + + verify(mBatteryTipsCardPreference).setVisible(false); + } + + @Test + public void handleBatteryTipsCardUpdated_adaptiveBrightnessAnomaly_showAnomaly() { + PowerAnomalyEvent event = BatteryTestUtils.createAdaptiveBrightnessAnomalyEvent(); + when(mPowerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true); + + mBatteryTipsController.handleBatteryTipsCardUpdated(event); + + // Check pre-defined string + verify(mBatteryTipsCardPreference).setTitle( + "Turn on adaptive brightness to extend battery life"); + verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings"); + verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it"); + // Check proto info + verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo( + "com.android.settings.display.AutoBrightnessSettings", + 1381); + verify(mBatteryTipsCardPreference).setVisible(true); + } + + @Test + public void handleBatteryTipsCardUpdated_screenTimeoutAnomaly_showAnomaly() { + PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent(); + when(mPowerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true); + + mBatteryTipsController.handleBatteryTipsCardUpdated(event); + + verify(mBatteryTipsCardPreference).setTitle("Reduce screen timeout to extend battery life"); + verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings"); + verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it"); + verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo( + "com.android.settings.display.ScreenTimeoutSettings", + 1852); + verify(mBatteryTipsCardPreference).setVisible(true); + } + @Test + public void handleBatteryTipsCardUpdated_screenTimeoutAnomalyHasTitle_showAnomaly() { + PowerAnomalyEvent event = BatteryTestUtils.createScreenTimeoutAnomalyEvent(); + String testTitle = "TestTitle"; + event = event.toBuilder() + .setWarningBannerInfo( + event.getWarningBannerInfo().toBuilder() + .setTitleString(testTitle) + .build()) + .build(); + when(mPowerUsageFeatureProvider.isBatteryTipsEnabled()).thenReturn(true); + + mBatteryTipsController.handleBatteryTipsCardUpdated(event); + + verify(mBatteryTipsCardPreference).setTitle(testTitle); + verify(mBatteryTipsCardPreference).setMainButtonLabel("View Settings"); + verify(mBatteryTipsCardPreference).setDismissButtonLabel("Got it"); + verify(mBatteryTipsCardPreference).setMainButtonLauncherInfo( + "com.android.settings.display.ScreenTimeoutSettings", + 1852); + verify(mBatteryTipsCardPreference).setVisible(true); + } +} diff --git a/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java b/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java index c54b750cb55..e99b17f5084 100644 --- a/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java +++ b/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java @@ -18,6 +18,7 @@ package com.android.settings.testutils; import static org.mockito.Mockito.when; +import android.app.settings.SettingsEnums; import android.content.Context; import android.content.Intent; import android.hardware.usb.UsbManager; @@ -25,11 +26,17 @@ import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; import android.os.BatteryManager; import android.os.UserManager; + import androidx.room.Room; +import com.android.settings.display.AutoBrightnessSettings; +import com.android.settings.display.ScreenTimeoutSettings; import com.android.settings.fuelgauge.batteryusage.BatteryInformation; import com.android.settings.fuelgauge.batteryusage.ConvertUtils; import com.android.settings.fuelgauge.batteryusage.DeviceBatteryState; +import com.android.settings.fuelgauge.batteryusage.PowerAnomalyEvent; +import com.android.settings.fuelgauge.batteryusage.PowerAnomalyEventList; +import com.android.settings.fuelgauge.batteryusage.WarningBannerInfo; import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao; import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity; import com.android.settings.fuelgauge.batteryusage.db.BatteryState; @@ -193,4 +200,33 @@ public class BatteryTestUtils { when(mockUsbPortStatus.getComplianceWarnings()) .thenReturn(new int[]{UsbPortStatus.COMPLIANCE_WARNING_OTHER}); } + + /** Create an empty power anomaly event list proto. */ + public static PowerAnomalyEventList createEmptyPowerAnomalyEventList() { + return PowerAnomalyEventList.getDefaultInstance(); + } + + /** Create a power anomaly event proto of adaptive brightness. */ + public static PowerAnomalyEvent createAdaptiveBrightnessAnomalyEvent() { + return PowerAnomalyEvent.newBuilder() + .setType("settings banner") + .setKey("adaptive_brightness") + .setWarningBannerInfo(WarningBannerInfo.newBuilder() + .setMainButtonDestination(AutoBrightnessSettings.class.getName()) + .setMainButtonSourceMetricsCategory(SettingsEnums.SETTINGS_AUTO_BRIGHTNESS) + .build()) + .build(); + } + + /** Create a power anomaly event proto of screen timeout. */ + public static PowerAnomalyEvent createScreenTimeoutAnomalyEvent() { + return PowerAnomalyEvent.newBuilder() + .setType("settings banner") + .setKey("screen_timeout") + .setWarningBannerInfo(WarningBannerInfo.newBuilder() + .setMainButtonDestination(ScreenTimeoutSettings.class.getName()) + .setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT) + .build()) + .build(); + } }