Add background activity action
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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -16,17 +16,19 @@
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user