From ad10b9efa0237699081d15c931ecf26820460c16 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 4 Mar 2015 12:13:06 -0800 Subject: Address NewApi warning in device/battery. This moves the Lollipop-specific code in BatteryStatusManager into a separate method and annotates it with @TargetApi(LOLLIPOP) so appease Android lint. BUG=411461 Review URL: https://codereview.chromium.org/975963003 Cr-Commit-Position: refs/heads/master@{#319115} --- build/android/lint/suppressions.xml | 1 - .../device/battery/BatteryStatusManager.java | 58 +++++++++++++--------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/build/android/lint/suppressions.xml b/build/android/lint/suppressions.xml index 11efa06..5f3ad43 100644 --- a/build/android/lint/suppressions.xml +++ b/build/android/lint/suppressions.xml @@ -85,7 +85,6 @@ Still reading? - diff --git a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java index b94c7f0..87fd280 100644 --- a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java +++ b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java @@ -4,6 +4,7 @@ package org.chromium.device.battery; +import android.annotation.TargetApi; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -133,35 +134,44 @@ class BatteryStatusManager { double chargingTimeSeconds = (charging && batteryFull) ? 0 : Double.POSITIVE_INFINITY; double dischargingTimeSeconds = Double.POSITIVE_INFINITY; - if (mLollipopBatteryManager != null) { - // On Lollipop we can provide a better estimate for chargingTime and dischargingTime. - double remainingCapacityRatio = mLollipopBatteryManager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CAPACITY) / 100.0; - double batteryCapacityMicroAh = mLollipopBatteryManager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); - double averageCurrentMicroA = mLollipopBatteryManager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE); - - if (charging) { - if (chargingTimeSeconds == Double.POSITIVE_INFINITY && averageCurrentMicroA > 0) { - double chargeFromEmptyHours = batteryCapacityMicroAh / averageCurrentMicroA; - chargingTimeSeconds = - Math.ceil((1 - remainingCapacityRatio) * chargeFromEmptyHours * 3600.0); - } - } else { - if (averageCurrentMicroA < 0) { - double dischargeFromFullHours = batteryCapacityMicroAh / -averageCurrentMicroA; - dischargingTimeSeconds = - Math.floor(remainingCapacityRatio * dischargeFromFullHours * 3600.0); - } - } - } - BatteryStatus batteryStatus = new BatteryStatus(); batteryStatus.charging = charging; batteryStatus.chargingTime = chargingTimeSeconds; batteryStatus.dischargingTime = dischargingTimeSeconds; batteryStatus.level = level; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + updateBatteryStatusForLollipop(batteryStatus); + } + mCallback.onBatteryStatusChanged(batteryStatus); } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private void updateBatteryStatusForLollipop(BatteryStatus batteryStatus) { + assert mLollipopBatteryManager != null; + + // On Lollipop we can provide a better estimate for chargingTime and dischargingTime. + double remainingCapacityRatio = mLollipopBatteryManager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CAPACITY) / 100.0; + double batteryCapacityMicroAh = mLollipopBatteryManager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); + double averageCurrentMicroA = mLollipopBatteryManager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE); + + if (batteryStatus.charging) { + if (batteryStatus.chargingTime == Double.POSITIVE_INFINITY + && averageCurrentMicroA > 0) { + double chargeFromEmptyHours = batteryCapacityMicroAh / averageCurrentMicroA; + batteryStatus.chargingTime = + Math.ceil((1 - remainingCapacityRatio) * chargeFromEmptyHours * 3600.0); + } + } else { + if (averageCurrentMicroA < 0) { + double dischargeFromFullHours = batteryCapacityMicroAh / -averageCurrentMicroA; + batteryStatus.dischargingTime = + Math.floor(remainingCapacityRatio * dischargeFromFullHours * 3600.0); + } + } + } } -- cgit v1.1