1 /* 2 * Copyright (C) 2017 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 17 package com.android.settings.fuelgauge.anomaly.checker; 18 19 import android.content.Context; 20 import android.os.BatteryStats; 21 import android.os.SystemClock; 22 import android.support.annotation.VisibleForTesting; 23 import android.text.format.DateUtils; 24 import android.util.ArrayMap; 25 26 import com.android.internal.os.BatterySipper; 27 import com.android.internal.os.BatteryStatsHelper; 28 import com.android.settings.Utils; 29 import com.android.settings.fuelgauge.BatteryUtils; 30 import com.android.settings.fuelgauge.anomaly.Anomaly; 31 import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy; 32 import com.android.settings.fuelgauge.anomaly.AnomalyUtils; 33 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Check whether apps have unoptimized bluetooth scanning in the background 40 */ 41 public class BluetoothScanAnomalyDetector implements AnomalyDetector { 42 private static final String TAG = "BluetoothScanAnomalyDetector"; 43 @VisibleForTesting 44 BatteryUtils mBatteryUtils; 45 private long mBluetoothScanningThreshold; 46 private Context mContext; 47 private AnomalyUtils mAnomalyUtils; 48 49 public BluetoothScanAnomalyDetector(Context context) { 50 this(context, new AnomalyDetectionPolicy(context), AnomalyUtils.getInstance(context)); 51 } 52 53 @VisibleForTesting 54 BluetoothScanAnomalyDetector(Context context, AnomalyDetectionPolicy policy, 55 AnomalyUtils anomalyUtils) { 56 mContext = context; 57 mBatteryUtils = BatteryUtils.getInstance(context); 58 mBluetoothScanningThreshold = policy.bluetoothScanThreshold; 59 mAnomalyUtils = anomalyUtils; 60 } 61 62 @Override 63 public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper) { 64 // Detect all apps if targetPackageName is null 65 return detectAnomalies(batteryStatsHelper, null /* targetPackageName */); 66 } 67 68 @Override 69 public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper, 70 String targetPackageName) { 71 final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList(); 72 final List<Anomaly> anomalies = new ArrayList<>(); 73 final int targetUid = mBatteryUtils.getPackageUid(targetPackageName); 74 final long elapsedRealtimeMs = SystemClock.elapsedRealtime(); 75 76 for (int i = 0, size = batterySippers.size(); i < size; i++) { 77 final BatterySipper sipper = batterySippers.get(i); 78 final BatteryStats.Uid uid = sipper.uidObj; 79 if (uid == null 80 || mBatteryUtils.shouldHideSipper(sipper) 81 || (targetUid != BatteryUtils.UID_NULL && targetUid != uid.getUid())) { 82 continue; 83 } 84 85 final long bluetoothTimeMs = getBluetoothUnoptimizedBgTimeMs(uid, elapsedRealtimeMs); 86 if (bluetoothTimeMs > mBluetoothScanningThreshold) { 87 final String packageName = mBatteryUtils.getPackageName(uid.getUid()); 88 final CharSequence displayName = Utils.getApplicationLabel(mContext, 89 packageName); 90 91 Anomaly anomaly = new Anomaly.Builder() 92 .setUid(uid.getUid()) 93 .setType(Anomaly.AnomalyType.BLUETOOTH_SCAN) 94 .setDisplayName(displayName) 95 .setPackageName(packageName) 96 .setBluetoothScanningTimeMs(bluetoothTimeMs) 97 .build(); 98 99 if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) { 100 anomalies.add(anomaly); 101 } 102 } 103 } 104 105 return anomalies; 106 } 107 108 @VisibleForTesting 109 public long getBluetoothUnoptimizedBgTimeMs(BatteryStats.Uid uid, long elapsedRealtimeMs) { 110 BatteryStats.Timer timer = uid.getBluetoothUnoptimizedScanBackgroundTimer(); 111 112 return timer != null ? timer.getTotalDurationMsLocked(elapsedRealtimeMs) : 0; 113 } 114 115 } 116