OSDN Git Service

Add AnomalyConfigReceiver
authorjackqdyulei <jackqdyulei@google.com>
Thu, 1 Feb 2018 04:24:47 +0000 (20:24 -0800)
committerjackqdyulei <jackqdyulei@google.com>
Wed, 21 Feb 2018 20:34:08 +0000 (12:34 -0800)
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

AndroidManifest.xml
src/com/android/settings/fuelgauge/batterytip/AnomalyConfigReceiver.java [new file with mode: 0644]
src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java

index 026b7ea..c039041 100644 (file)
         <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 (file)
index 0000000..9d6f78d
--- /dev/null
@@ -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);
+    }
+}
index 2f0a6e4..3b5e97d 100644 (file)
@@ -25,4 +25,9 @@ public class StatsManagerConfig {
      * 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;
 }