Add impl for force stop action

Also refactor the AnomalyAction so it could take source id as
a parameter, which represents the fragment that invokes it.

Bug: 36924669
Test: RunSettingsRoboTests
Change-Id: Ib53865f92e1a6f1e9dcc1480c0c74fbcfb0226f4
This commit is contained in:
jackqdyulei
2017-05-01 16:32:50 -07:00
parent 5a24c1b84c
commit 66242d0e3d
11 changed files with 161 additions and 18 deletions

View File

@@ -28,6 +28,7 @@ import android.support.v7.preference.PreferenceGroup;
import android.util.IconDrawableFactory;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.PreferenceController;
@@ -92,7 +93,8 @@ public class PowerUsageAnomalyDetails extends DashboardFragment implements
AnomalyPreference anomalyPreference = (AnomalyPreference) preference;
final Anomaly anomaly = anomalyPreference.getAnomaly();
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly);
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
MetricsProto.MetricsEvent.FUELGAUGE_ANOMALY_DETAIL);
dialogFragment.setTargetFragment(this, REQUEST_ANOMALY_ACTION);
dialogFragment.show(getFragmentManager(), TAG);

View File

@@ -169,7 +169,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
KEY_TIME_SINCE_LAST_FULL_CHARGE);
mFooterPreferenceMixin.createFooterPreference().setTitle(R.string.battery_footer_summary);
mAnomalySummaryPreferenceController = new AnomalySummaryPreferenceController(
(SettingsActivity) getActivity(), this);
(SettingsActivity) getActivity(), this, MetricsEvent.FUELGAUGE_POWER_USAGE_SUMMARY);
mBatteryUtils = BatteryUtils.getInstance(getContext());
mAnomalySparseArray = new SparseArray<>();

View File

@@ -34,9 +34,11 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
DialogInterface.OnClickListener {
private static final String ARG_ANOMALY = "anomaly";
private static final String ARG_METRICS_KEY = "metrics_key";
@VisibleForTesting
Anomaly mAnomaly;
private AnomalyUtils mAnomalyUtils;
/**
* Listener to give the control back to target fragment
@@ -52,16 +54,23 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
void onAnomalyHandled(Anomaly anomaly);
}
public static AnomalyDialogFragment newInstance(Anomaly anomaly) {
public static AnomalyDialogFragment newInstance(Anomaly anomaly, int metricsKey) {
AnomalyDialogFragment dialogFragment = new AnomalyDialogFragment();
Bundle args = new Bundle(1);
Bundle args = new Bundle(2);
args.putParcelable(ARG_ANOMALY, anomaly);
args.putInt(ARG_METRICS_KEY, metricsKey);
dialogFragment.setArguments(args);
return dialogFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAnomalyUtils = AnomalyUtils.getInstance(getContext());
}
@Override
public int getMetricsCategory() {
// TODO(b/37681923): add anomaly metric id
@@ -75,8 +84,10 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
return;
}
final AnomalyAction anomalyAction = AnomalyUtils.getAnomalyAction(mAnomaly.type);
anomalyAction.handlePositiveAction(mAnomaly.packageName);
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly.type);
final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);
anomalyAction.handlePositiveAction(mAnomaly.packageName, metricsKey);
lsn.onAnomalyHandled(mAnomaly);
}
@@ -86,7 +97,7 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
mAnomaly = bundle.getParcelable(ARG_ANOMALY);
final Context context = getContext();
final AnomalyAction anomalyAction = AnomalyUtils.getAnomalyAction(mAnomaly.type);
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly.type);
switch (anomalyAction.getActionType()) {
case Anomaly.AnomalyActionType.FORCE_STOP:
return new AlertDialog.Builder(context)

View File

@@ -42,11 +42,19 @@ public class AnomalySummaryPreferenceController {
List<Anomaly> mAnomalies;
private SettingsActivity mSettingsActivity;
/**
* Metrics key about fragment that create this controller
*
* @see com.android.internal.logging.nano.MetricsProto.MetricsEvent
*/
private int mMetricsKey;
public AnomalySummaryPreferenceController(SettingsActivity activity,
PreferenceFragment fragment) {
PreferenceFragment fragment, int metricsKey) {
mFragment = fragment;
mSettingsActivity = activity;
mAnomalyPreference = mFragment.getPreferenceScreen().findPreference(ANOMALY_KEY);
mMetricsKey = metricsKey;
hideHighUsagePreference();
}
@@ -54,7 +62,8 @@ public class AnomalySummaryPreferenceController {
if (mAnomalies != null && ANOMALY_KEY.equals(preference.getKey())) {
if (mAnomalies.size() == 1) {
final Anomaly anomaly = mAnomalies.get(0);
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly);
AnomalyDialogFragment dialogFragment = AnomalyDialogFragment.newInstance(anomaly,
mMetricsKey);
dialogFragment.setTargetFragment(mFragment, REQUEST_ANOMALY_ACTION);
dialogFragment.show(mFragment.getFragmentManager(), TAG);
} else {

View File

@@ -16,23 +16,41 @@
package com.android.settings.fuelgauge.anomaly;
import android.content.Context;
import android.support.annotation.VisibleForTesting;
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
/**
* Utitily class for anomaly detection
* Utility class for anomaly detection
*/
public class AnomalyUtils {
private Context mContext;
private static AnomalyUtils sInstance;
@VisibleForTesting
AnomalyUtils(Context context) {
mContext = context.getApplicationContext();
}
public static AnomalyUtils getInstance(Context context) {
if (sInstance == null) {
sInstance = new AnomalyUtils(context);
}
return sInstance;
}
/**
* Return the corresponding {@link AnomalyAction} according to {@link AnomalyType}
* Return the corresponding {@link AnomalyAction} according to
* {@link com.android.settings.fuelgauge.anomaly.Anomaly.AnomalyType}
*
* @return corresponding {@link AnomalyAction}, or null if cannot find it.
*/
public static final AnomalyAction getAnomalyAction(int anomalyType) {
public final AnomalyAction getAnomalyAction(@Anomaly.AnomalyType int anomalyType) {
switch (anomalyType) {
case Anomaly.AnomalyType.WAKE_LOCK:
return new ForceStopAction();
return new ForceStopAction(mContext);
default:
return null;
}

View File

@@ -20,6 +20,13 @@ package com.android.settings.fuelgauge.anomaly.action;
* Interface for anomaly action, which is triggered if we need to handle the anomaly
*/
public interface AnomalyAction {
void handlePositiveAction(String packageName);
/**
* handle the action when user clicks positive button
* @param packageName 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);
int getActionType();
}

View File

@@ -16,15 +16,39 @@
package com.android.settings.fuelgauge.anomaly.action;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Pair;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.overlay.FeatureFactory;
/**
* Force stop action for anomaly app, which means to stop the app which causes anomaly
*/
public class ForceStopAction implements AnomalyAction {
private Context mContext;
private MetricsFeatureProvider mMetricsFeatureProvider;
private ActivityManager mActivityManager;
public ForceStopAction(Context context) {
mContext = context;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
mActivityManager = (ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE);
}
@Override
public void handlePositiveAction(String packageName) {
public void handlePositiveAction(String packageName, int metricsKey) {
// force stop the package
mMetricsFeatureProvider.action(mContext,
MetricsProto.MetricsEvent.ACTION_APP_FORCE_STOP, packageName,
Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, metricsKey));
mActivityManager.forceStopPackage(packageName);
}
@Override