}
@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
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);
}
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
switch (anomalyType) {
case Anomaly.AnomalyType.WAKE_LOCK:
return new ForceStopAction(mContext);
+ case Anomaly.AnomalyType.WAKEUP_ALARM:
+ return new BackgroundCheckAction(mContext);
default:
return null;
}
switch (anomalyType) {
case Anomaly.AnomalyType.WAKE_LOCK:
return new WakeLockAnomalyDetector(mContext);
+ case Anomaly.AnomalyType.WAKEUP_ALARM:
+ return new WakeupAlarmAnomalyDetector(mContext);
default:
return null;
}
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();
}
--- /dev/null
+/*
+ * 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;
+ }
+}
}
@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,
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;
}
@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);
+ }
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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;
private Context mContext;
@Mock
private ActivityManager mActivityManager;
+ private Anomaly mAnomaly;
private ForceStopAction mForceStopAction;
@Before
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);
}