Merge "Snap for 4726689 from 9d66948caaf607cca2d5631b80de813e241953a8 to oreo-mr1-cts-release" into oreo-mr1-cts-release
diff --git a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
index 3b83fa5..160b52f 100644
--- a/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/AdvancedPowerUsageDetail.java
@@ -45,6 +45,7 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.enterprise.DevicePolicyManagerWrapper;
 import com.android.settings.enterprise.DevicePolicyManagerWrapperImpl;
+import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
 import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment;
 import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
@@ -348,6 +349,9 @@
 
     @Override
     public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
+        final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(getContext());
+        anomalyUtils.logAnomalies(mMetricsFeatureProvider, data,
+                MetricsEvent.FUELGAUGE_POWER_USAGE_DETAIL);
         mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
     }
 
diff --git a/src/com/android/settings/fuelgauge/PowerUsageAnomalyDetails.java b/src/com/android/settings/fuelgauge/PowerUsageAnomalyDetails.java
index bf4c2af..0d73511 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageAnomalyDetails.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageAnomalyDetails.java
@@ -125,8 +125,7 @@
 
     @Override
     public int getMetricsCategory() {
-        //TODO(b/37681923): add correct metrics category
-        return 0;
+        return MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL;
     }
 
     void refreshUi() {
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index ee6ebb2..3ff40ad 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -68,6 +68,7 @@
 import com.android.settings.fuelgauge.anomaly.AnomalyDialogFragment.AnomalyDialogListener;
 import com.android.settings.fuelgauge.anomaly.AnomalyLoader;
 import com.android.settings.fuelgauge.anomaly.AnomalySummaryPreferenceController;
+import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.core.AbstractPreferenceController;
@@ -151,6 +152,10 @@
 
                 @Override
                 public void onLoadFinished(Loader<List<Anomaly>> loader, List<Anomaly> data) {
+                    final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(getContext());
+                    anomalyUtils.logAnomalies(mMetricsFeatureProvider, data,
+                            MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY);
+
                     // show high usage preference if possible
                     mAnomalySummaryPreferenceController.updateAnomalySummaryPreference(data);
 
diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java
index 69d03b9..538f5b1 100644
--- a/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java
+++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java
@@ -23,6 +23,7 @@
 import android.os.Bundle;
 import android.support.annotation.VisibleForTesting;
 
+import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
@@ -79,8 +80,7 @@
 
     @Override
     public int getMetricsCategory() {
-        // TODO(b/37681923): add anomaly metric id
-        return 0;
+        return MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY;
     }
 
     @Override
@@ -100,9 +100,13 @@
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         final Bundle bundle = getArguments();
-        mAnomaly = bundle.getParcelable(ARG_ANOMALY);
-
         final Context context = getContext();
+        final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(context);
+
+        mAnomaly = bundle.getParcelable(ARG_ANOMALY);
+        anomalyUtils.logAnomaly(mMetricsFeatureProvider, mAnomaly,
+                MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY);
+
         final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
         switch (anomalyAction.getActionType()) {
             case Anomaly.AnomalyActionType.FORCE_STOP:
diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyUtils.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyUtils.java
index 491a3b4..39d51dc0 100644
--- a/src/com/android/settings/fuelgauge/anomaly/AnomalyUtils.java
+++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyUtils.java
@@ -19,8 +19,12 @@
 import android.content.Context;
 import android.os.Build;
 import android.support.annotation.VisibleForTesting;
+import android.util.Pair;
+import android.util.SparseIntArray;
 
+import com.android.internal.logging.nano.MetricsProto;
 import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
 import com.android.settings.fuelgauge.anomaly.action.LocationCheckAction;
@@ -40,6 +44,17 @@
     private Context mContext;
     private static AnomalyUtils sInstance;
 
+    private static final SparseIntArray mMetricArray;
+    static {
+        mMetricArray = new SparseIntArray();
+        mMetricArray.append(Anomaly.AnomalyType.WAKE_LOCK,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK);
+        mMetricArray.append(Anomaly.AnomalyType.WAKEUP_ALARM,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKEUP_ALARM);
+        mMetricArray.append(Anomaly.AnomalyType.BLUETOOTH_SCAN,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_UNOPTIMIZED_BT);
+    }
+
     @VisibleForTesting
     AnomalyUtils(Context context) {
         mContext = context.getApplicationContext();
@@ -118,4 +133,39 @@
         return anomalies;
     }
 
+    /**
+     * Log the list of {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
+     * anomaly type, package name, field_context, field_action_type
+     *
+     * @param provider  provider to do the logging
+     * @param anomalies contains the data to log
+     * @param contextId which page invoke this logging
+     * @see #logAnomaly(MetricsFeatureProvider, Anomaly, int)
+     */
+    public void logAnomalies(MetricsFeatureProvider provider, List<Anomaly> anomalies,
+            int contextId) {
+        for (int i = 0, size = anomalies.size(); i < size; i++) {
+            logAnomaly(provider, anomalies.get(i), contextId);
+        }
+    }
+
+    /**
+     * Log the {@link Anomaly} using {@link MetricsFeatureProvider}, which contains
+     * anomaly type, package name, field_context, field_action_type
+     *
+     * @param provider  provider to do the logging
+     * @param anomaly   contains the data to log
+     * @param contextId which page invoke this logging
+     * @see #logAnomalies(MetricsFeatureProvider, List, int)
+     */
+    public void logAnomaly(MetricsFeatureProvider provider, Anomaly anomaly, int contextId) {
+        provider.action(
+                mContext,
+                mMetricArray.get(anomaly.type, MetricsProto.MetricsEvent.VIEW_UNKNOWN),
+                anomaly.packageName,
+                Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, contextId),
+                Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
+                        getAnomalyAction(anomaly).getActionType()));
+    }
+
 }
diff --git a/src/com/android/settings/fuelgauge/anomaly/action/AnomalyAction.java b/src/com/android/settings/fuelgauge/anomaly/action/AnomalyAction.java
index 87212e7..3ee89d1 100644
--- a/src/com/android/settings/fuelgauge/anomaly/action/AnomalyAction.java
+++ b/src/com/android/settings/fuelgauge/anomaly/action/AnomalyAction.java
@@ -58,5 +58,6 @@
      */
     public abstract boolean isActionActive(Anomaly anomaly);
 
+    @Anomaly.AnomalyActionType
     public abstract int getActionType();
 }
diff --git a/src/com/android/settings/fuelgauge/anomaly/action/StopAndBackgroundCheckAction.java b/src/com/android/settings/fuelgauge/anomaly/action/StopAndBackgroundCheckAction.java
index dba221a..2c799ee 100644
--- a/src/com/android/settings/fuelgauge/anomaly/action/StopAndBackgroundCheckAction.java
+++ b/src/com/android/settings/fuelgauge/anomaly/action/StopAndBackgroundCheckAction.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.support.annotation.VisibleForTesting;
 
+import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
 
 /**
@@ -34,6 +35,7 @@
 
     public StopAndBackgroundCheckAction(Context context) {
         this(context, new ForceStopAction(context), new BackgroundCheckAction(context));
+        mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_STOP_AND_BACKGROUND_CHECK;
     }
 
     @VisibleForTesting
@@ -46,6 +48,7 @@
 
     @Override
     public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
+        super.handlePositiveAction(anomaly, metricsKey);
         mForceStopAction.handlePositiveAction(anomaly, metricsKey);
         mBackgroundCheckAction.handlePositiveAction(anomaly, metricsKey);
     }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragmentTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragmentTest.java
index 1111804..51a82c8 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragmentTest.java
@@ -32,6 +32,7 @@
 
 import com.android.settings.R;
 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
+import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 
@@ -68,7 +69,7 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
-        mContext = RuntimeEnvironment.application;
+        mContext = spy(RuntimeEnvironment.application);
         mWakeLockAnomaly = new Anomaly.Builder()
                 .setType(Anomaly.AnomalyType.WAKE_LOCK)
                 .setUid(UID)
@@ -94,6 +95,7 @@
                 .setPackageName(PACKAGE_NAME)
                 .setDisplayName(DISPLAY_NAME)
                 .build();
+        FakeFeatureFactory.setupForTest(mContext);
     }
 
     @Test
@@ -166,29 +168,4 @@
         assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
                 mContext.getString(R.string.dlg_cancel));
     }
-
-    @Test
-    public void testOnCreateDialog_bluetoothAnomaly_fireLocationCheckDialog() {
-        mAnomalyDialogFragment = spy(AnomalyDialogFragment.newInstance(mBluetoothAnomaly,
-                0 /* metricskey */));
-        mAnomalyDialogFragment.mAnomalyUtils = mAnomalyUtils;
-        doReturn(mAnomalyAction).when(mAnomalyUtils).getAnomalyAction(any());
-        doNothing().when(mAnomalyDialogFragment).initAnomalyUtils();
-        doReturn(Anomaly.AnomalyActionType.LOCATION_CHECK).when(mAnomalyAction).getActionType();
-
-        FragmentTestUtil.startFragment(mAnomalyDialogFragment);
-
-        final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
-        ShadowAlertDialog shadowDialog = shadowOf(dialog);
-
-        assertThat(shadowDialog.getMessage()).isEqualTo(
-                mContext.getString(R.string.dialog_location_message,
-                        mWakeLockAnomaly.displayName));
-        assertThat(shadowDialog.getTitle()).isEqualTo(
-                mContext.getString(R.string.dialog_location_title));
-        assertThat(dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText()).isEqualTo(
-                mContext.getString(R.string.dialog_location_ok));
-        assertThat(dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText()).isEqualTo(
-                mContext.getString(R.string.dlg_cancel));
-    }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
index 8cd5a06..38391c9 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
@@ -18,8 +18,13 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import android.os.Build;
+import static org.mockito.Mockito.verify;
 
+import android.os.Build;
+import android.util.Pair;
+
+import com.android.internal.logging.nano.MetricsProto;
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
 import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
@@ -31,18 +36,42 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows = {
         ShadowKeyValueListParserWrapperImpl.class})
 public class AnomalyUtilsTest {
+    private static final String PACKAGE_NAME_WAKEUP = "com.android.app1";
+    private static final String PACKAGE_NAME_WAKELOCK = "com.android.app2";
+    private static final int CONTEXT_ID = 55;
+
+    @Mock
+    private MetricsFeatureProvider mMetricsFeatureProvider;
     private AnomalyUtils mAnomalyUtils;
+    private Anomaly mWakeupAnomaly;
+    private Anomaly mWakeLockAnomaly;
 
     @Before
     public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
         mAnomalyUtils = new AnomalyUtils(RuntimeEnvironment.application);
+
+        mWakeLockAnomaly = new Anomaly.Builder()
+                .setType(Anomaly.AnomalyType.WAKE_LOCK)
+                .setPackageName(PACKAGE_NAME_WAKELOCK)
+                .build();
+        mWakeupAnomaly = new Anomaly.Builder()
+                .setType(Anomaly.AnomalyType.WAKEUP_ALARM)
+                .setPackageName(PACKAGE_NAME_WAKEUP)
+                .build();
     }
 
     @Test
@@ -97,4 +126,47 @@
         assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKEUP_ALARM)).isInstanceOf(
                 WakeupAlarmAnomalyDetector.class);
     }
+
+    @Test
+    public void testLogAnomaly() {
+        mAnomalyUtils.logAnomaly(mMetricsFeatureProvider, mWakeLockAnomaly, CONTEXT_ID);
+
+        verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK,
+                PACKAGE_NAME_WAKELOCK,
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_CONTEXT,
+                        CONTEXT_ID),
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
+                        Anomaly.AnomalyActionType.FORCE_STOP));
+    }
+
+    @Test
+    public void testLogAnomalies() {
+        final List<Anomaly> anomalies = new ArrayList<>();
+        anomalies.add(mWakeLockAnomaly);
+        anomalies.add(mWakeupAnomaly);
+
+        mAnomalyUtils.logAnomalies(mMetricsFeatureProvider, anomalies, CONTEXT_ID);
+
+        verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKELOCK,
+                PACKAGE_NAME_WAKELOCK,
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_CONTEXT,
+                        CONTEXT_ID),
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
+                        Anomaly.AnomalyActionType.FORCE_STOP));
+        verify(mMetricsFeatureProvider).action(RuntimeEnvironment.application,
+                MetricsProto.MetricsEvent.ANOMALY_TYPE_WAKEUP_ALARM,
+                PACKAGE_NAME_WAKEUP,
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_CONTEXT,
+                        CONTEXT_ID),
+                Pair.create(
+                        MetricsProto.MetricsEvent.FIELD_ANOMALY_ACTION_TYPE,
+                        Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK));
+    }
 }