Add AnomalyConfigReceiver
This receiver receives the following intent:
1. android.app.action.STATSD_STARTED
2. android.intent.action.BOOT_COMPLETED
Also it does:
1. Check whether to upload/update config(future cl)
2. Send PendingIntent to StatsManager
Bug: 72385333
Test: Will add robo test once robo framework is updated(b/73172999)
Change-Id: Iff7240663ecc1e080581683743b3027093945566
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 026b7ea..c039041 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3299,6 +3299,13 @@
<receiver android:name=".fuelgauge.batterytip.AnomalyDetectionReceiver"
android:exported="false" />
+ <receiver android:name=".fuelgauge.batterytip.AnomalyConfigReceiver">
+ <intent-filter>
+ <action android:name="android.app.action.STATSD_STARTED"/>
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
+ </intent-filter>
+ </receiver>
+
<!-- This is the longest AndroidManifest.xml ever. -->
</application>
</manifest>
diff --git a/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java b/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java
new file mode 100644
index 0000000..9d6f78d
--- /dev/null
+++ b/src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2018 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.batterytip;
+
+import android.app.PendingIntent;
+import android.app.StatsManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Process;
+import android.os.StatsDimensionsValue;
+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
+ * prepare info for {@link StatsManager}
+ */
+public class AnomalyConfigReceiver extends BroadcastReceiver {
+ private static final String TAG = "AnomalyConfigReceiver";
+ private static final int REQUEST_CODE = 0;
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (StatsManager.ACTION_STATSD_STARTED.equals(intent.getAction())
+ || 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);
+ final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE,
+ extraIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+
+ uploadPendingIntent(statsManager, pendingIntent);
+ }
+ }
+
+ @VisibleForTesting
+ void uploadPendingIntent(StatsManager statsManager, PendingIntent pendingIntent) {
+ Log.i(TAG, "Upload PendingIntent to StatsManager. configKey: "
+ + StatsManagerConfig.ANOMALY_CONFIG_KEY + " subId: "
+ + StatsManagerConfig.SUBSCRIBER_ID);
+ statsManager.setBroadcastSubscriber(StatsManagerConfig.ANOMALY_CONFIG_KEY,
+ StatsManagerConfig.SUBSCRIBER_ID, pendingIntent);
+ }
+}
diff --git a/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java b/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java
index 2f0a6e4..3b5e97d 100644
--- a/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java
+++ b/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java
@@ -25,4 +25,9 @@
* This value is used in {@link android.app.StatsManager#addConfiguration(long, byte[])}
*/
public static final long ANOMALY_CONFIG_KEY = 1;
+
+ /**
+ * The key that represents subscriber, which is settings app.
+ */
+ public static final long SUBSCRIBER_ID = 1;
}