Add code to handle anomaly config update
Store a local version number in Settings. If new version comes,
upload config to StatsManager.
Bug: 72385333
Test: Will add robo test once robo framework is updated(b/73172999)
Change-Id: I03f5ee6f6e013746397d39b8d2b52ce587825b11
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java
index 9d6f78d..f658627 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java
@@ -21,14 +21,12 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
-import android.os.Process;
-import android.os.StatsDimensionsValue;
+import android.content.SharedPreferences;
+import android.provider.Settings;
+import android.util.Base64;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
-import com.android.settings.fuelgauge.BatteryUtils;
-
-import java.util.List;
/**
* Receive broadcast when {@link StatsManager} restart, then check the anomaly config and
@@ -37,6 +35,9 @@
public class AnomalyConfigReceiver extends BroadcastReceiver {
private static final String TAG = "AnomalyConfigReceiver";
private static final int REQUEST_CODE = 0;
+ private static final String PREF_DB = "anomaly_pref";
+ private static final String KEY_ANOMALY_CONFIG_VERSION = "anomaly_config_version";
+ private static final int DEFAULT_VERSION = 0;
@Override
public void onReceive(Context context, Intent intent) {
@@ -44,9 +45,11 @@
|| Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
final StatsManager statsManager = context.getSystemService(StatsManager.class);
- //TODO(b/72385333): Check whether to update the config
- final Intent extraIntent = new Intent();
- extraIntent.setClass(context, AnomalyDetectionReceiver.class);
+ // Check whether to update the config
+ checkAnomalyConfig(context, statsManager);
+
+ // Upload PendingIntent to StatsManager
+ final Intent extraIntent = new Intent(context, AnomalyDetectionReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE,
extraIntent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -62,4 +65,30 @@
statsManager.setBroadcastSubscriber(StatsManagerConfig.ANOMALY_CONFIG_KEY,
StatsManagerConfig.SUBSCRIBER_ID, pendingIntent);
}
+
+ private void checkAnomalyConfig(Context context, StatsManager statsManager) {
+ final SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_DB,
+ Context.MODE_PRIVATE);
+ final int currentVersion = sharedPreferences.getInt(KEY_ANOMALY_CONFIG_VERSION,
+ DEFAULT_VERSION);
+ final int newVersion = Settings.Global.getInt(context.getContentResolver(),
+ Settings.Global.ANOMALY_CONFIG_VERSION, DEFAULT_VERSION);
+ Log.i(TAG, "CurrentVersion: " + currentVersion + " new version: " + newVersion);
+
+ if (newVersion > currentVersion) {
+ final byte[] config = Base64.decode(
+ Settings.Global.getString(context.getContentResolver(),
+ Settings.Global.ANOMALY_CONFIG), Base64.DEFAULT);
+ if (statsManager.addConfiguration(StatsManagerConfig.ANOMALY_CONFIG_KEY, config)) {
+ Log.i(TAG, "Upload the anomaly config. configKey: "
+ + StatsManagerConfig.ANOMALY_CONFIG_KEY);
+ SharedPreferences.Editor editor = sharedPreferences.edit();
+ editor.putInt(KEY_ANOMALY_CONFIG_VERSION, newVersion);
+ editor.apply();
+ } else {
+ Log.i(TAG, "Upload the anomaly config failed. configKey: "
+ + StatsManagerConfig.ANOMALY_CONFIG_KEY);
+ }
+ }
+ }
}
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
index 10ef726..834a355 100644
--- a/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyDetectionReceiver.java
@@ -54,6 +54,7 @@
// The Example of intentDimsValue is: 35:{1:{1:{1:10013|}|}|}
StatsDimensionsValue intentDimsValue =
intent.getParcelableExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE);
+ Log.i(TAG, "Extra stats value: " + intentDimsValue.toString());
List<StatsDimensionsValue> intentTuple = intentDimsValue.getTupleValueList();
if (!intentTuple.isEmpty()) {