AnomalyDetectionReceiver.java revision 10b7c0c39d3d586b37dee5ea56f809041b3caf1f
1/* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.settings.fuelgauge.batterytip; 18 19import android.app.StatsManager; 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.os.StatsDimensionsValue; 24import android.support.annotation.VisibleForTesting; 25import android.util.Log; 26 27import com.android.settings.fuelgauge.BatteryUtils; 28 29import java.util.List; 30 31/** 32 * Receive the anomaly info from {@link StatsManager} 33 */ 34public class AnomalyDetectionReceiver extends BroadcastReceiver { 35 private static final String TAG = "SettingsAnomalyReceiver"; 36 37 @Override 38 public void onReceive(Context context, Intent intent) { 39 final BatteryDatabaseManager databaseManager = new BatteryDatabaseManager(context); 40 final BatteryUtils batteryUtils = BatteryUtils.getInstance(context); 41 final long configUid = intent.getLongExtra(StatsManager.EXTRA_STATS_CONFIG_UID, -1); 42 final long configKey = intent.getLongExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, -1); 43 final long subscriptionId = intent.getLongExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, 44 -1); 45 46 Log.i(TAG, "Anomaly intent received. configUid = " + configUid + " configKey = " 47 + configKey + " subscriptionId = " + subscriptionId); 48 saveAnomalyToDatabase(databaseManager, batteryUtils, intent); 49 } 50 51 @VisibleForTesting 52 void saveAnomalyToDatabase(BatteryDatabaseManager databaseManager, BatteryUtils batteryUtils 53 , Intent intent) { 54 // The Example of intentDimsValue is: 35:{1:{1:{1:10013|}|}|} 55 StatsDimensionsValue intentDimsValue = 56 intent.getParcelableExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE); 57 Log.i(TAG, "Extra stats value: " + intentDimsValue.toString()); 58 List<StatsDimensionsValue> intentTuple = intentDimsValue.getTupleValueList(); 59 60 if (!intentTuple.isEmpty()) { 61 try { 62 // TODO(b/72385333): find more robust way to extract the uid. 63 final StatsDimensionsValue intentTupleValue = intentTuple.get(0) 64 .getTupleValueList().get(0).getTupleValueList().get(0); 65 final int uid = intentTupleValue.getIntValue(); 66 // TODD(b/72385333): extract anomaly type 67 final int anomalyType = 0; 68 final String packageName = batteryUtils.getPackageName(uid); 69 final long timeMs = System.currentTimeMillis(); 70 databaseManager.insertAnomaly(packageName, anomalyType, timeMs); 71 } catch (NullPointerException | IndexOutOfBoundsException e) { 72 Log.e(TAG, "Parse stats dimensions value error.", e); 73 } 74 } 75 } 76} 77