OSDN Git Service

Add background activity action
authorjackqdyulei <jackqdyulei@google.com>
Fri, 19 May 2017 21:42:24 +0000 (14:42 -0700)
committerjackqdyulei <jackqdyulei@google.com>
Wed, 24 May 2017 17:28:28 +0000 (10:28 -0700)
Background activity action is designed for wakeup alarm anomaly.
This cl also change the parameter in anomaly action interface
from "packageName" to "anomaly"

Bug: 36921529
Test: RunSettingsRoboTests
Change-Id: Ibde69f351f81043641f228f0e74deaa2e230c08a

src/com/android/settings/fuelgauge/anomaly/Anomaly.java
src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java
src/com/android/settings/fuelgauge/anomaly/AnomalyUtils.java
src/com/android/settings/fuelgauge/anomaly/action/AnomalyAction.java
src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckAction.java [new file with mode: 0644]
src/com/android/settings/fuelgauge/anomaly/action/ForceStopAction.java
tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyUtilsTest.java
tests/robotests/src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckActionTest.java [new file with mode: 0644]
tests/robotests/src/com/android/settings/fuelgauge/anomaly/action/ForceStopActionTest.java

index 90fc852..37b52fc 100644 (file)
@@ -42,9 +42,11 @@ public class Anomaly implements Parcelable {
     }
 
     @Retention(RetentionPolicy.SOURCE)
-    @IntDef({AnomalyActionType.FORCE_STOP})
+    @IntDef({AnomalyActionType.FORCE_STOP,
+            AnomalyActionType.BACKGROUND_CHECK})
     public @interface AnomalyActionType {
         int FORCE_STOP = 0;
+        int BACKGROUND_CHECK = 1;
     }
 
     @AnomalyType
index e05eace..ef15d51 100644 (file)
@@ -87,7 +87,7 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
         final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly.type);
         final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);
 
-        anomalyAction.handlePositiveAction(mAnomaly.packageName, metricsKey);
+        anomalyAction.handlePositiveAction(mAnomaly, metricsKey);
         lsn.onAnomalyHandled(mAnomaly);
     }
 
index e55d485..d226899 100644 (file)
@@ -20,9 +20,11 @@ import android.content.Context;
 import android.support.annotation.VisibleForTesting;
 
 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
+import com.android.settings.fuelgauge.anomaly.action.BackgroundCheckAction;
 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
 import com.android.settings.fuelgauge.anomaly.checker.AnomalyDetector;
 import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
+import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector;
 
 /**
  * Utility class for anomaly detection
@@ -53,6 +55,8 @@ public class AnomalyUtils {
         switch (anomalyType) {
             case Anomaly.AnomalyType.WAKE_LOCK:
                 return new ForceStopAction(mContext);
+            case Anomaly.AnomalyType.WAKEUP_ALARM:
+                return new BackgroundCheckAction(mContext);
             default:
                 return null;
         }
@@ -68,6 +72,8 @@ public class AnomalyUtils {
         switch (anomalyType) {
             case Anomaly.AnomalyType.WAKE_LOCK:
                 return new WakeLockAnomalyDetector(mContext);
+            case Anomaly.AnomalyType.WAKEUP_ALARM:
+                return new WakeupAlarmAnomalyDetector(mContext);
             default:
                 return null;
         }
index 6b8ff67..4d4b136 100644 (file)
 
 package com.android.settings.fuelgauge.anomaly.action;
 
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+
 /**
  * Interface for anomaly action, which is triggered if we need to handle the anomaly
  */
 public interface AnomalyAction {
     /**
      * handle the action when user clicks positive button
-     * @param packageName about the app that we need to handle
+     * @param Anomaly about the app that we need to handle
      * @param metricsKey key for the page that invokes the action
      *
      * @see com.android.internal.logging.nano.MetricsProto
      */
-    void handlePositiveAction(String packageName, int metricsKey);
+    void handlePositiveAction(Anomaly Anomaly, int metricsKey);
     int getActionType();
 }
diff --git a/src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckAction.java b/src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckAction.java
new file mode 100644 (file)
index 0000000..8c7e827
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2017 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.anomaly.action;
+
+import android.app.AppOpsManager;
+import android.content.Context;
+
+import com.android.settings.core.instrumentation.MetricsFeatureProvider;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.overlay.FeatureFactory;
+
+/**
+ * Background check action for anomaly app, which means to stop app running in the background
+ */
+public class BackgroundCheckAction implements AnomalyAction {
+
+    private Context mContext;
+    private MetricsFeatureProvider mMetricsFeatureProvider;
+    private AppOpsManager mAppOpsManager;
+
+    public BackgroundCheckAction(Context context) {
+        mContext = context;
+        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+        mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
+    }
+
+    @Override
+    public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
+        // TODO(b/37681923): add metric log here if possible
+        mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, anomaly.uid, anomaly.packageName,
+                AppOpsManager.MODE_IGNORED);
+    }
+
+    @Override
+    public int getActionType() {
+        return Anomaly.AnomalyActionType.BACKGROUND_CHECK;
+    }
+}
index 40f6839..c124c9e 100644 (file)
@@ -42,7 +42,8 @@ public class ForceStopAction implements AnomalyAction {
     }
 
     @Override
-    public void handlePositiveAction(String packageName, int metricsKey) {
+    public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
+        final String packageName = anomaly.packageName;
         // force stop the package
         mMetricsFeatureProvider.action(mContext,
                 MetricsProto.MetricsEvent.ACTION_APP_FORCE_STOP, packageName,
index d8652e5..c246eee 100644 (file)
@@ -20,9 +20,11 @@ import static com.google.common.truth.Truth.assertThat;
 
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
+import com.android.settings.fuelgauge.anomaly.action.BackgroundCheckAction;
 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
 import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
 import com.android.settings.testutils.shadow.ShadowKeyValueListParserWrapperImpl;
+import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -48,8 +50,20 @@ public class AnomalyUtilsTest {
     }
 
     @Test
+    public void testGetAnomalyAction_typeWakeUpAlarm_returnBackgroundCheck() {
+        assertThat(mAnomalyUtils.getAnomalyAction(Anomaly.AnomalyType.WAKEUP_ALARM)).isInstanceOf(
+                BackgroundCheckAction.class);
+    }
+
+    @Test
     public void testGetAnomalyDetector_typeWakeLock_returnWakeLockDetector() {
         assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKE_LOCK)).isInstanceOf(
                 WakeLockAnomalyDetector.class);
     }
+
+    @Test
+    public void testGetAnomalyDetector_typeWakeUpAlarm_returnWakeUpAlarmDetector() {
+        assertThat(mAnomalyUtils.getAnomalyDetector(Anomaly.AnomalyType.WAKEUP_ALARM)).isInstanceOf(
+                WakeupAlarmAnomalyDetector.class);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckActionTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/action/BackgroundCheckActionTest.java
new file mode 100644 (file)
index 0000000..83d2e98
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 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.anomaly.action;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import android.app.ActivityManager;
+import android.app.AppOpsManager;
+import android.content.Context;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class BackgroundCheckActionTest {
+    private static final String PACKAGE_NAME = "com.android.app";
+    private static final int UID = 111;
+
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Context mContext;
+    @Mock
+    private AppOpsManager mAppOpsManagerr;
+    private Anomaly mAnomaly;
+    private BackgroundCheckAction mBackgroundCheckAction;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        FakeFeatureFactory.setupForTest(mContext);
+        doReturn(mAppOpsManagerr).when(mContext).getSystemService(Context.APP_OPS_SERVICE);
+
+        mAnomaly = new Anomaly.Builder()
+                .setUid(UID)
+                .setPackageName(PACKAGE_NAME)
+                .build();
+        mBackgroundCheckAction = new BackgroundCheckAction(mContext);
+    }
+
+    @Test
+    public void testHandlePositiveAction_forceStopPackage() {
+        mBackgroundCheckAction.handlePositiveAction(mAnomaly, 0 /* metricskey */);
+
+        verify(mAppOpsManagerr).setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, UID, PACKAGE_NAME,
+                AppOpsManager.MODE_IGNORED);
+    }
+}
index e4bcb1d..87c946a 100644 (file)
@@ -24,6 +24,7 @@ import android.content.Context;
 
 import com.android.settings.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
+import com.android.settings.fuelgauge.anomaly.Anomaly;
 import com.android.settings.testutils.FakeFeatureFactory;
 
 import org.junit.Before;
@@ -43,6 +44,7 @@ public class ForceStopActionTest {
     private Context mContext;
     @Mock
     private ActivityManager mActivityManager;
+    private Anomaly mAnomaly;
     private ForceStopAction mForceStopAction;
 
     @Before
@@ -52,12 +54,15 @@ public class ForceStopActionTest {
         FakeFeatureFactory.setupForTest(mContext);
         doReturn(mActivityManager).when(mContext).getSystemService(Context.ACTIVITY_SERVICE);
 
+        mAnomaly = new Anomaly.Builder()
+                .setPackageName(PACKAGE_NAME)
+                .build();
         mForceStopAction = new ForceStopAction(mContext);
     }
 
     @Test
     public void testHandlePositiveAction_forceStopPackage() {
-        mForceStopAction.handlePositiveAction(PACKAGE_NAME, 0 /* metricskey */);
+        mForceStopAction.handlePositiveAction(mAnomaly, 0 /* metricskey */);
 
         verify(mActivityManager).forceStopPackage(PACKAGE_NAME);
     }