Merge "Update Anomaly detection framework" into oc-dr1-dev

This commit is contained in:
TreeHugger Robot
2017-07-07 02:13:37 +00:00
committed by Android (Google) Code Review
20 changed files with 457 additions and 75 deletions

View File

@@ -15,10 +15,13 @@
*/
package com.android.settings.fuelgauge;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.BatteryStats;
import android.os.Bundle;
import android.os.Build;
import android.os.SystemClock;
import android.os.UserManager;
import android.support.annotation.IntDef;
@@ -47,6 +50,7 @@ import java.util.List;
*/
public class BatteryUtils {
public static final int UID_NULL = -1;
public static final int SDK_NULL = -1;
@Retention(RetentionPolicy.SOURCE)
@IntDef({StatusType.FOREGROUND,
@@ -66,6 +70,7 @@ public class BatteryUtils {
private static BatteryUtils sInstance;
private PackageManager mPackageManager;
private AppOpsManager mAppOpsManager;
@VisibleForTesting
PowerUsageFeatureProvider mPowerUsageFeatureProvider;
@@ -79,6 +84,7 @@ public class BatteryUtils {
@VisibleForTesting
BatteryUtils(Context context) {
mPackageManager = context.getPackageManager();
mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
context).getPowerUsageFeatureProvider(context);
}
@@ -263,6 +269,37 @@ public class BatteryUtils {
return ArrayUtils.isEmpty(packageNames) ? null : packageNames[0];
}
/**
* Find the targetSdkVersion for package with name {@code packageName}
*
* @return the targetSdkVersion, or {@link #SDK_NULL} if {@code packageName} doesn't exist
*/
public int getTargetSdkVersion(final String packageName) {
try {
ApplicationInfo info = mPackageManager.getApplicationInfo(packageName,
PackageManager.GET_META_DATA);
return info.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Cannot find package: " + packageName, e);
}
return SDK_NULL;
}
/**
* Check whether background restriction is enabled
*/
public boolean isBackgroundRestrictionEnabled(final int targetSdkVersion, final int uid,
final String packageName) {
if (targetSdkVersion >= Build.VERSION_CODES.O) {
return true;
}
final int mode = mAppOpsManager
.checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, uid, packageName);
return mode == AppOpsManager.MODE_IGNORED || mode == AppOpsManager.MODE_ERRORED;
}
/**
* Sort the {@code usageList} based on {@link BatterySipper#totalPowerMah}
*/
@@ -337,7 +374,7 @@ public class BatteryUtils {
}
private boolean isDataCorrupted() {
return mPackageManager == null;
return mPackageManager == null || mAppOpsManager == null;
}
@VisibleForTesting

View File

@@ -46,16 +46,18 @@ public class Anomaly implements Parcelable {
@Retention(RetentionPolicy.SOURCE)
@IntDef({AnomalyActionType.FORCE_STOP,
AnomalyActionType.BACKGROUND_CHECK,
AnomalyActionType.LOCATION_CHECK})
AnomalyActionType.LOCATION_CHECK,
AnomalyActionType.STOP_AND_BACKGROUND_CHECK})
public @interface AnomalyActionType {
int FORCE_STOP = 0;
int BACKGROUND_CHECK = 1;
int LOCATION_CHECK = 2;
int STOP_AND_BACKGROUND_CHECK = 3;
}
@AnomalyType
public static final int[] ANOMALY_TYPE_LIST =
{AnomalyType.WAKE_LOCK,
public static final int[] ANOMALY_TYPE_LIST = {
AnomalyType.WAKE_LOCK,
AnomalyType.WAKEUP_ALARM,
AnomalyType.BLUETOOTH_SCAN};
@@ -64,7 +66,14 @@ public class Anomaly implements Parcelable {
*/
public final int type;
public final int uid;
public final int targetSdkVersion;
public final long wakelockTimeMs;
/**
* {@code true} if background restriction is enabled
*
* @see android.app.AppOpsManager.OP_RUN_IN_BACKGROUND
*/
public final boolean backgroundRestrictionEnabled;
/**
* Display name of this anomaly, usually it is the app name
*/
@@ -77,6 +86,8 @@ public class Anomaly implements Parcelable {
displayName = builder.mDisplayName;
packageName = builder.mPackageName;
wakelockTimeMs = builder.mWakeLockTimeMs;
targetSdkVersion = builder.mTargetSdkVersion;
backgroundRestrictionEnabled = builder.mBgRestrictionEnabled;
}
private Anomaly(Parcel in) {
@@ -85,6 +96,8 @@ public class Anomaly implements Parcelable {
displayName = in.readCharSequence();
packageName = in.readString();
wakelockTimeMs = in.readLong();
targetSdkVersion = in.readInt();
backgroundRestrictionEnabled = in.readBoolean();
}
@Override
@@ -99,6 +112,8 @@ public class Anomaly implements Parcelable {
dest.writeCharSequence(displayName);
dest.writeString(packageName);
dest.writeLong(wakelockTimeMs);
dest.writeInt(targetSdkVersion);
dest.writeBoolean(backgroundRestrictionEnabled);
}
@Override
@@ -115,12 +130,15 @@ public class Anomaly implements Parcelable {
&& uid == other.uid
&& wakelockTimeMs == other.wakelockTimeMs
&& TextUtils.equals(displayName, other.displayName)
&& TextUtils.equals(packageName, other.packageName);
&& TextUtils.equals(packageName, other.packageName)
&& targetSdkVersion == other.targetSdkVersion
&& backgroundRestrictionEnabled == other.backgroundRestrictionEnabled;
}
@Override
public int hashCode() {
return Objects.hash(type, uid, displayName, packageName, wakelockTimeMs);
return Objects.hash(type, uid, displayName, packageName, wakelockTimeMs, targetSdkVersion,
backgroundRestrictionEnabled);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@@ -137,9 +155,11 @@ public class Anomaly implements Parcelable {
@AnomalyType
private int mType;
private int mUid;
private int mTargetSdkVersion;
private CharSequence mDisplayName;
private String mPackageName;
private long mWakeLockTimeMs;
private boolean mBgRestrictionEnabled;
public Builder setType(@AnomalyType int type) {
mType = type;
@@ -166,6 +186,16 @@ public class Anomaly implements Parcelable {
return this;
}
public Builder setTargetSdkVersion(int targetSdkVersion) {
mTargetSdkVersion = targetSdkVersion;
return this;
}
public Builder setBackgroundRestrictionEnabled(boolean bgRestrictionEnabled) {
mBgRestrictionEnabled = bgRestrictionEnabled;
return this;
}
public Anomaly build() {
return new Anomaly(this);
}

View File

@@ -90,7 +90,7 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
return;
}
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly.type);
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);
anomalyAction.handlePositiveAction(mAnomaly, metricsKey);
@@ -103,16 +103,18 @@ public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
mAnomaly = bundle.getParcelable(ARG_ANOMALY);
final Context context = getContext();
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly.type);
final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
switch (anomalyAction.getActionType()) {
case Anomaly.AnomalyActionType.FORCE_STOP:
return new AlertDialog.Builder(context)
.setTitle(R.string.dialog_stop_title)
.setMessage(getString(R.string.dialog_stop_message, mAnomaly.displayName))
.setMessage(getString(mAnomaly.type == Anomaly.AnomalyType.WAKE_LOCK
? R.string.dialog_stop_message
: R.string.dialog_stop_message_wakeup_alarm, mAnomaly.displayName))
.setPositiveButton(R.string.dialog_stop_ok, this)
.setNegativeButton(R.string.dlg_cancel, null)
.create();
case Anomaly.AnomalyActionType.BACKGROUND_CHECK:
case Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK:
return new AlertDialog.Builder(context)
.setTitle(R.string.dialog_background_check_title)
.setMessage(getString(R.string.dialog_background_check_message,

View File

@@ -17,12 +17,13 @@
package com.android.settings.fuelgauge.anomaly;
import android.content.Context;
import android.os.Build;
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.action.LocationCheckAction;
import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction;
import com.android.settings.fuelgauge.anomaly.checker.AnomalyDetector;
import com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector;
import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
@@ -49,16 +50,22 @@ public class AnomalyUtils {
/**
* Return the corresponding {@link AnomalyAction} according to
* {@link com.android.settings.fuelgauge.anomaly.Anomaly.AnomalyType}
* {@link com.android.settings.fuelgauge.anomaly.Anomaly}
*
* @return corresponding {@link AnomalyAction}, or null if cannot find it.
*/
public AnomalyAction getAnomalyAction(@Anomaly.AnomalyType int anomalyType) {
switch (anomalyType) {
public AnomalyAction getAnomalyAction(Anomaly anomaly) {
switch (anomaly.type) {
case Anomaly.AnomalyType.WAKE_LOCK:
return new ForceStopAction(mContext);
case Anomaly.AnomalyType.WAKEUP_ALARM:
return new BackgroundCheckAction(mContext);
if (anomaly.targetSdkVersion >= Build.VERSION_CODES.O
|| (anomaly.targetSdkVersion < Build.VERSION_CODES.O
&& anomaly.backgroundRestrictionEnabled)) {
return new ForceStopAction(mContext);
} else {
return new StopAndBackgroundCheckAction(mContext);
}
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
return new LocationCheckAction(mContext);
default:

View File

@@ -18,8 +18,11 @@ package com.android.settings.fuelgauge.anomaly.action;
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Build;
import android.support.annotation.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.anomaly.Anomaly;
/**
@@ -28,6 +31,8 @@ import com.android.settings.fuelgauge.anomaly.Anomaly;
public class BackgroundCheckAction extends AnomalyAction {
private AppOpsManager mAppOpsManager;
@VisibleForTesting
BatteryUtils mBatteryUtils;
public BackgroundCheckAction(Context context) {
super(context);
@@ -38,17 +43,17 @@ public class BackgroundCheckAction extends AnomalyAction {
@Override
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
super.handlePositiveAction(anomaly, contextMetricsKey);
mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, anomaly.uid, anomaly.packageName,
AppOpsManager.MODE_IGNORED);
if (anomaly.targetSdkVersion < Build.VERSION_CODES.O) {
mAppOpsManager.setMode(AppOpsManager.OP_RUN_IN_BACKGROUND, anomaly.uid,
anomaly.packageName,
AppOpsManager.MODE_IGNORED);
}
}
@Override
public boolean isActionActive(Anomaly anomaly) {
final int mode = mAppOpsManager
.checkOpNoThrow(AppOpsManager.OP_RUN_IN_BACKGROUND, anomaly.uid,
anomaly.packageName);
return mode != AppOpsManager.MODE_IGNORED && mode != AppOpsManager.MODE_ERRORED;
return !mBatteryUtils.isBackgroundRestrictionEnabled(anomaly.targetSdkVersion, anomaly.uid,
anomaly.packageName);
}
@Override

View File

@@ -0,0 +1,63 @@
/*
* 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.content.Context;
import android.support.annotation.VisibleForTesting;
import com.android.settings.fuelgauge.anomaly.Anomaly;
/**
* Force stop and background check action for anomaly app, this action will
* 1. Force stop the app
* 2. Turn on background check
*/
public class StopAndBackgroundCheckAction extends AnomalyAction {
@VisibleForTesting
ForceStopAction mForceStopAction;
@VisibleForTesting
BackgroundCheckAction mBackgroundCheckAction;
public StopAndBackgroundCheckAction(Context context) {
this(context, new ForceStopAction(context), new BackgroundCheckAction(context));
}
@VisibleForTesting
StopAndBackgroundCheckAction(Context context, ForceStopAction forceStopAction,
BackgroundCheckAction backgroundCheckAction) {
super(context);
mForceStopAction = forceStopAction;
mBackgroundCheckAction = backgroundCheckAction;
}
@Override
public void handlePositiveAction(Anomaly anomaly, int metricsKey) {
mForceStopAction.handlePositiveAction(anomaly, metricsKey);
mBackgroundCheckAction.handlePositiveAction(anomaly, metricsKey);
}
@Override
public boolean isActionActive(Anomaly anomaly) {
return mForceStopAction.isActionActive(anomaly)
&& mBackgroundCheckAction.isActionActive(anomaly);
}
@Override
public int getActionType() {
return Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK;
}
}

View File

@@ -42,24 +42,21 @@ public class BluetoothScanAnomalyDetector implements AnomalyDetector {
private static final String TAG = "BluetoothScanAnomalyDetector";
@VisibleForTesting
BatteryUtils mBatteryUtils;
@VisibleForTesting
AnomalyAction mAnomalyAction;
private long mBluetoothScanningThreshold;
private Context mContext;
private AnomalyUtils mAnomalyUtils;
public BluetoothScanAnomalyDetector(Context context) {
this(context, new AnomalyDetectionPolicy(context),
AnomalyUtils.getInstance(context).getAnomalyAction(
Anomaly.AnomalyType.BLUETOOTH_SCAN));
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
}
@VisibleForTesting
BluetoothScanAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
AnomalyAction anomalyAction) {
AnomalyUtils anomalyUtils) {
mContext = context;
mBatteryUtils = BatteryUtils.getInstance(context);
mAnomalyAction = anomalyAction;
mBluetoothScanningThreshold = policy.bluetoothScanThreshold;
mAnomalyUtils = anomalyUtils;
}
@Override
@@ -98,7 +95,7 @@ public class BluetoothScanAnomalyDetector implements AnomalyDetector {
.setPackageName(packageName)
.build();
if (mAnomalyAction.isActionActive(anomaly)) {
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
anomalies.add(anomaly);
}
}

View File

@@ -29,7 +29,6 @@ import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy;
import com.android.settings.fuelgauge.anomaly.AnomalyUtils;
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
import java.util.ArrayList;
import java.util.List;
@@ -39,27 +38,25 @@ import java.util.List;
*/
public class WakeLockAnomalyDetector implements AnomalyDetector {
private static final String TAG = "WakeLockAnomalyChecker";
private PackageManager mPackageManager;
private Context mContext;
@VisibleForTesting
BatteryUtils mBatteryUtils;
@VisibleForTesting
long mWakeLockThresholdMs;
@VisibleForTesting
AnomalyAction mAnomalyAction;
private PackageManager mPackageManager;
private Context mContext;
private AnomalyUtils mAnomalyUtils;
public WakeLockAnomalyDetector(Context context) {
this(context, new AnomalyDetectionPolicy(context));
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
}
@VisibleForTesting
WakeLockAnomalyDetector(Context context, AnomalyDetectionPolicy policy) {
WakeLockAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
AnomalyUtils anomalyUtils) {
mContext = context;
mPackageManager = context.getPackageManager();
mBatteryUtils = BatteryUtils.getInstance(context);
mAnomalyAction = AnomalyUtils.getInstance(context).getAnomalyAction(
Anomaly.AnomalyType.WAKE_LOCK);
mAnomalyUtils = anomalyUtils;
mWakeLockThresholdMs = policy.wakeLockThreshold;
}
@@ -94,15 +91,20 @@ public class WakeLockAnomalyDetector implements AnomalyDetector {
final String packageName = mBatteryUtils.getPackageName(uid.getUid());
final CharSequence displayName = Utils.getApplicationLabel(mContext,
packageName);
final int targetSdkVersion = mBatteryUtils.getTargetSdkVersion(packageName);
Anomaly anomaly = new Anomaly.Builder()
.setUid(uid.getUid())
.setType(Anomaly.AnomalyType.WAKE_LOCK)
.setDisplayName(displayName)
.setPackageName(packageName)
.setTargetSdkVersion(targetSdkVersion)
.setBackgroundRestrictionEnabled(
mBatteryUtils.isBackgroundRestrictionEnabled(targetSdkVersion,
uid.getUid(), packageName))
.build();
if (mAnomalyAction.isActionActive(anomaly)) {
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
anomalies.add(anomaly);
}
}

View File

@@ -41,21 +41,20 @@ public class WakeupAlarmAnomalyDetector implements AnomalyDetector {
private static final String TAG = "WakeupAlarmAnomalyDetector";
@VisibleForTesting
BatteryUtils mBatteryUtils;
@VisibleForTesting
AnomalyAction mAnomalyAction;
private long mWakeupAlarmThreshold;
private Context mContext;
private AnomalyUtils mAnomalyUtils;
public WakeupAlarmAnomalyDetector(Context context) {
this(context, new AnomalyDetectionPolicy(context));
this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context));
}
@VisibleForTesting
WakeupAlarmAnomalyDetector(Context context, AnomalyDetectionPolicy policy) {
WakeupAlarmAnomalyDetector(Context context, AnomalyDetectionPolicy policy,
AnomalyUtils anomalyUtils) {
mContext = context;
mBatteryUtils = BatteryUtils.getInstance(context);
mAnomalyAction = AnomalyUtils.getInstance(context).getAnomalyAction(
Anomaly.AnomalyType.WAKEUP_ALARM);
mAnomalyUtils = anomalyUtils;
mWakeupAlarmThreshold = policy.wakeupAlarmThreshold;
}
@@ -98,7 +97,7 @@ public class WakeupAlarmAnomalyDetector implements AnomalyDetector {
.setPackageName(packageName)
.build();
if (mAnomalyAction.isActionActive(anomaly)) {
if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
anomalies.add(anomaly);
}
}