Merge "Update power_anomaly_event proto fields" into main
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 906d4ee..552ca3f 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -1409,12 +1409,7 @@
     <integer-array name="network_mode_3g_deprecated_carrier_id" translatable="false">
     </integer-array>
 
-    <!-- The following 4 arrays are for power anomaly tips card. Please keep them the same size. -->
-    <string-array name="power_anomaly_keys" translatable="false">
-        <item>adaptive_brightness</item>
-        <item>screen_timeout</item>
-    </string-array>
-
+    <!-- The following 3 arrays are for power anomaly tips card. Please keep them the same size. -->
     <string-array name="power_anomaly_titles">
         <item>Turn on adaptive brightness to extend battery life</item>
         <item>Reduce screen timeout to extend battery life</item>
diff --git a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java
index 91fe59c..b8cfea6 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsController.java
@@ -36,8 +36,6 @@
     private static final String ROOT_PREFERENCE_KEY = "battery_tips_category";
     private static final String CARD_PREFERENCE_KEY = "battery_tips_card";
 
-    private final String[] mPowerAnomalyKeys;
-
     @VisibleForTesting
     BatteryTipsCardPreference mCardPreference;
     @VisibleForTesting
@@ -47,7 +45,6 @@
         super(context, ROOT_PREFERENCE_KEY);
         mPowerUsageFeatureProvider = FeatureFactory.getFeatureFactory()
             .getPowerUsageFeatureProvider();
-        mPowerAnomalyKeys = context.getResources().getStringArray(R.array.power_anomaly_keys);
     }
 
     private boolean isTipsCardVisible() {
@@ -66,16 +63,6 @@
         mCardPreference = screen.findPreference(CARD_PREFERENCE_KEY);
     }
 
-    @VisibleForTesting
-    int getPowerAnomalyEventIndex(String powerAnomalyKey) {
-        for (int index = 0; index < mPowerAnomalyKeys.length; index++) {
-            if (mPowerAnomalyKeys[index].equals(powerAnomalyKey)) {
-                return index;
-            }
-        }
-        return -1;
-    }
-
     private <T> T getInfo(PowerAnomalyEvent powerAnomalyEvent,
                           Function<WarningBannerInfo, T> warningBannerInfoSupplier,
                           Function<WarningItemInfo, T> warningItemInfoSupplier) {
@@ -98,8 +85,9 @@
             return string;
         }
 
-        if (resourceIndex >= 0) {
-            string = mContext.getResources().getStringArray(resourceId)[resourceIndex];
+        String[] stringArray = mContext.getResources().getStringArray(resourceId);
+        if (resourceIndex >= 0 && resourceIndex < stringArray.length) {
+            string = stringArray[resourceIndex];
         }
 
         return string;
@@ -117,10 +105,11 @@
         }
 
         // Get card preference strings and navigate fragment info
-        final int index = getPowerAnomalyEventIndex(powerAnomalyEvent.getKey());
+        final int resourceIndex = powerAnomalyEvent.hasKey()
+                ? powerAnomalyEvent.getKey().getNumber() : -1;
 
         String titleString = getString(powerAnomalyEvent, WarningBannerInfo::getTitleString,
-                WarningItemInfo::getTitleString, R.array.power_anomaly_titles, index);
+                WarningItemInfo::getTitleString, R.array.power_anomaly_titles, resourceIndex);
         if (titleString.isEmpty()) {
             mCardPreference.setVisible(false);
             return;
@@ -128,10 +117,10 @@
 
         String mainBtnString = getString(powerAnomalyEvent,
                 WarningBannerInfo::getMainButtonString, WarningItemInfo::getMainButtonString,
-                R.array.power_anomaly_main_btn_strings, index);
+                R.array.power_anomaly_main_btn_strings, resourceIndex);
         String dismissBtnString = getString(powerAnomalyEvent,
                 WarningBannerInfo::getCancelButtonString, WarningItemInfo::getCancelButtonString,
-                R.array.power_anomaly_dismiss_btn_strings, index);
+                R.array.power_anomaly_dismiss_btn_strings, resourceIndex);
 
         String destinationClassName = getString(powerAnomalyEvent,
                 WarningBannerInfo::getMainButtonDestination,
diff --git a/src/com/android/settings/fuelgauge/protos/power_anomaly_event.proto b/src/com/android/settings/fuelgauge/protos/power_anomaly_event.proto
index 380c786..a5f475f 100644
--- a/src/com/android/settings/fuelgauge/protos/power_anomaly_event.proto
+++ b/src/com/android/settings/fuelgauge/protos/power_anomaly_event.proto
@@ -11,8 +11,8 @@
 message PowerAnomalyEvent {
   optional string event_id = 1;
   optional int64 timestamp = 2;
-  optional string type = 3;  // e.g. settings, apps
-  optional string key = 4;  // e.g. brightness, significant_increase
+  optional PowerAnomalyType type = 3;
+  optional PowerAnomalyKey key = 4;
   optional float score = 5;
   oneof info {
     WarningBannerInfo warning_banner_info = 6;
@@ -20,6 +20,25 @@
   }
 }
 
+// NOTE: Please DO NOT delete enum items or change enum values. Use [deprecated = true] instead.
+// The enum value will be used to decide the tips card style like icons and colors.
+//
+// Next id: 2
+enum PowerAnomalyType{
+  SETTINGS_BANNER = 0;
+  APPS_ITEM = 1;
+}
+
+// NOTE: Please DO NOT delete enum items or change enum values. Use [deprecated = true] instead.
+// The enum value will be used to decide pre-defined title and button labels.
+//
+// Next id: 3
+enum PowerAnomalyKey{
+  BRIGHTNESS = 0;
+  SCREEN_TIMEOUT = 1;
+  APP = 2;
+}
+
 message WarningBannerInfo {
   optional string title_string = 1;
   optional string description_string = 2;
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java
index 0c9a0b0..ffb200d 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batteryusage/BatteryTipsControllerTest.java
@@ -16,8 +16,6 @@
 
 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;
@@ -69,21 +67,6 @@
     }
 
     @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);
 
diff --git a/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java b/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java
index e99b17f..d1d768d 100644
--- a/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java
+++ b/tests/robotests/src/com/android/settings/testutils/BatteryTestUtils.java
@@ -36,6 +36,8 @@
 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.PowerAnomalyKey;
+import com.android.settings.fuelgauge.batteryusage.PowerAnomalyType;
 import com.android.settings.fuelgauge.batteryusage.WarningBannerInfo;
 import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventDao;
 import com.android.settings.fuelgauge.batteryusage.db.AppUsageEventEntity;
@@ -209,8 +211,8 @@
     /** Create a power anomaly event proto of adaptive brightness. */
     public static PowerAnomalyEvent createAdaptiveBrightnessAnomalyEvent() {
         return PowerAnomalyEvent.newBuilder()
-                .setType("settings banner")
-                .setKey("adaptive_brightness")
+                .setType(PowerAnomalyType.SETTINGS_BANNER)
+                .setKey(PowerAnomalyKey.BRIGHTNESS)
                 .setWarningBannerInfo(WarningBannerInfo.newBuilder()
                         .setMainButtonDestination(AutoBrightnessSettings.class.getName())
                         .setMainButtonSourceMetricsCategory(SettingsEnums.SETTINGS_AUTO_BRIGHTNESS)
@@ -221,8 +223,8 @@
     /** Create a power anomaly event proto of screen timeout. */
     public static PowerAnomalyEvent createScreenTimeoutAnomalyEvent() {
         return PowerAnomalyEvent.newBuilder()
-                .setType("settings banner")
-                .setKey("screen_timeout")
+                .setType(PowerAnomalyType.SETTINGS_BANNER)
+                .setKey(PowerAnomalyKey.SCREEN_TIMEOUT)
                 .setWarningBannerInfo(WarningBannerInfo.newBuilder()
                         .setMainButtonDestination(ScreenTimeoutSettings.class.getName())
                         .setMainButtonSourceMetricsCategory(SettingsEnums.SCREEN_TIMEOUT)