diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 026b7ea9da9..c039041ecb4 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3299,6 +3299,13 @@ + + + + + + + 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 00000000000..9d6f78d9e46 --- /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 2f0a6e49b55..3b5e97dd0f9 100644 --- a/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java +++ b/src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java @@ -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; }