Update Anomaly detection framework

This cl make it could have multiple actions for each anomaly type.

This includes:
1. Add field "targetSdkVersion", "bgRestrictionEnabled" in Anomaly
2. Add new action which includes "force stop" and "background check"
3. Add "sdkVersion" and "bgRestrictionEnabled" method in BatteryUtils
4. Update BackgroundCheckAction to check sdkVersion first
5. Update serveral tests and add new test

Bug: 63037765
Test: RunSettingsRoboTests
Change-Id: Iea7a131ee57d501f7737ae4a3ba672d948d05cd8
This commit is contained in:
jackqdyulei
2017-06-29 14:20:46 -07:00
parent 8218511fb7
commit bfc2b11b93
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