Refactor anomaly detection
This cl refactor it so: 1. AnomayLoader could create its own BatteryStatsHelper if needed. 2. AnomalyDetector could detect the anomaly with a target package name 3. Remove FeatureProvider in AnomalyLoader and use AnomalyDetectionPolicy instead With this cl, we could add anomaly detection to battery usage page more easily. Bug: 36924669 Test: RunSettingsRoboTests Change-Id: I1ee09701de8b5c8855f44e447813760285794c89
This commit is contained in:
@@ -17,11 +17,12 @@
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.utils.AsyncLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -33,16 +34,45 @@ import java.util.List;
|
||||
*/
|
||||
public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
|
||||
private String mPackageName;
|
||||
private UserManager mUserManager;
|
||||
@VisibleForTesting
|
||||
AnomalyUtils mAnomalyUtils;
|
||||
@VisibleForTesting
|
||||
AnomalyDetectionPolicy mPolicy;
|
||||
|
||||
/**
|
||||
* Create {@link AnomalyLoader} that runs anomaly check for all apps.
|
||||
*/
|
||||
public AnomalyLoader(Context context, BatteryStatsHelper batteryStatsHelper) {
|
||||
this(context, batteryStatsHelper, null, new AnomalyDetectionPolicy(context));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link AnomalyLoader} with {@code packageName}, so this loader will only
|
||||
* detect anomalies related to {@code packageName}, or check all apps if {@code packageName}
|
||||
* is {@code null}.
|
||||
*
|
||||
* This constructor will create {@link BatteryStatsHelper} in background thread.
|
||||
*
|
||||
* @param context
|
||||
* @param packageName if set, only finds anomalies for this package. If {@code null},
|
||||
* detects all anomalies of this type.
|
||||
*/
|
||||
public AnomalyLoader(Context context, String packageName) {
|
||||
this(context, null, packageName, new AnomalyDetectionPolicy(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
AnomalyLoader(Context context, BatteryStatsHelper batteryStatsHelper,
|
||||
String packageName, AnomalyDetectionPolicy policy) {
|
||||
super(context);
|
||||
mBatteryStatsHelper = batteryStatsHelper;
|
||||
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
|
||||
context).getPowerUsageFeatureProvider(context);
|
||||
mPackageName = packageName;
|
||||
mAnomalyUtils = AnomalyUtils.getInstance(context);
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
mPolicy = policy;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,11 +81,18 @@ public class AnomalyLoader extends AsyncLoader<List<Anomaly>> {
|
||||
|
||||
@Override
|
||||
public List<Anomaly> loadInBackground() {
|
||||
if (mBatteryStatsHelper == null) {
|
||||
mBatteryStatsHelper = new BatteryStatsHelper(getContext());
|
||||
mBatteryStatsHelper.create((Bundle) null);
|
||||
mBatteryStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED,
|
||||
mUserManager.getUserProfiles());
|
||||
}
|
||||
|
||||
final List<Anomaly> anomalies = new ArrayList<>();
|
||||
for (@Anomaly.AnomalyType int type : Anomaly.ANOMALY_TYPE_LIST) {
|
||||
if (mPowerUsageFeatureProvider.isAnomalyDetectorEnabled(type)) {
|
||||
if (mPolicy.isAnomalyDetectorEnabled(type)) {
|
||||
anomalies.addAll(mAnomalyUtils.getAnomalyDetector(type).detectAnomalies(
|
||||
mBatteryStatsHelper));
|
||||
mBatteryStatsHelper, mPackageName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user