From c691d3ccf4eb29376225bcbeb35bc532a3071ed1 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Tue, 28 Jul 2015 17:11:29 -0700 Subject: [PATCH] BatteryStats: Use proper power calculator when energy data is received We check for the presence of energy data when determining whether to use the WiFiPowerCalculator or WiFiPowerEstimator. Since we can receive this data later, we need to switch to the WiFiPowerCalculator if we weren't using it before. We can't ask the hardware if it supports energy data because that would involve a call into WiFiManagerService, which can cause a deadlock if we are holding the BatteryStatsService lock while using this class. Bug:22776010 Change-Id: Id685d487c56595eab1d382f49da9417a423bb517 --- .../internal/os/BatteryStatsHelper.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/core/java/com/android/internal/os/BatteryStatsHelper.java b/core/java/com/android/internal/os/BatteryStatsHelper.java index 9caf78a6b3a3a..f178c8cf7ece7 100644 --- a/core/java/com/android/internal/os/BatteryStatsHelper.java +++ b/core/java/com/android/internal/os/BatteryStatsHelper.java @@ -126,6 +126,9 @@ public final class BatteryStatsHelper { PowerCalculator mCameraPowerCalculator; PowerCalculator mFlashlightPowerCalculator; + boolean mHasWifiPowerReporting = false; + boolean mHasBluetoothPowerReporting = false; + public static boolean checkWifiOnly(Context context) { ConnectivityManager cm = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE); @@ -349,21 +352,23 @@ public final class BatteryStatsHelper { } mMobileRadioPowerCalculator.reset(mStats); - if (mWifiPowerCalculator == null) { - if (checkHasWifiPowerReporting(mStats, mPowerProfile)) { - mWifiPowerCalculator = new WifiPowerCalculator(mPowerProfile); - } else { - mWifiPowerCalculator = new WifiPowerEstimator(mPowerProfile); - } + // checkHasWifiPowerReporting can change if we get energy data at a later point, so + // always check this field. + final boolean hasWifiPowerReporting = checkHasWifiPowerReporting(mStats, mPowerProfile); + if (mWifiPowerCalculator == null || hasWifiPowerReporting != mHasWifiPowerReporting) { + mWifiPowerCalculator = hasWifiPowerReporting ? + new WifiPowerCalculator(mPowerProfile) : + new WifiPowerEstimator(mPowerProfile); + mHasWifiPowerReporting = hasWifiPowerReporting; } mWifiPowerCalculator.reset(); - if (mBluetoothPowerCalculator == null) { - if (checkHasBluetoothPowerReporting(mStats, mPowerProfile)) { - mBluetoothPowerCalculator = new BluetoothPowerCalculator(mPowerProfile); - } else { - mBluetoothPowerCalculator = new BluetoothPowerCalculator(mPowerProfile); - } + final boolean hasBluetoothPowerReporting = checkHasBluetoothPowerReporting(mStats, + mPowerProfile); + if (mBluetoothPowerCalculator == null || + hasBluetoothPowerReporting != mHasBluetoothPowerReporting) { + mBluetoothPowerCalculator = new BluetoothPowerCalculator(mPowerProfile); + mHasBluetoothPowerReporting = hasBluetoothPowerReporting; } mBluetoothPowerCalculator.reset();