Merge "update "Last full charge" preference" into pi-dev
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9466624..32ab1cc 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5135,6 +5135,8 @@
     <string name="battery_detail_since_full_charge">Breakdown since last full charge</string>
     <!-- Title for usage time since last full charge. [CHAR LIMIT=60] -->
     <string name="battery_last_full_charge">Last full charge</string>
+    <!-- Title for usage time that full charge lasts. [CHAR LIMIT=60] -->
+    <string name="battery_full_charge_last">Full charge lasts about</string>
     <!-- Description for text in battery footer. [CHAR LIMIT=NONE] -->
     <string name="battery_footer_summary">Battery usage data is approximate and can change based on usage</string>
     <!-- Title for text that shows the amount of time an app has been running while in the foreground. [CHAR LIMIT=80] -->
diff --git a/src/com/android/settings/fuelgauge/BatteryUtils.java b/src/com/android/settings/fuelgauge/BatteryUtils.java
index 3a84026..2b79855 100644
--- a/src/com/android/settings/fuelgauge/BatteryUtils.java
+++ b/src/com/android/settings/fuelgauge/BatteryUtils.java
@@ -422,32 +422,24 @@
                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
         final long elapsedRealtimeUs = PowerUtil.convertMsToUs(
                 SystemClock.elapsedRealtime());
+        final BatteryStats stats = statsHelper.getStats();
         BatteryInfo batteryInfo;
 
-        // 0 means we are discharging, anything else means charging
-        final boolean discharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
-                == 0;
-        // Get enhanced prediction if available and discharging, otherwise use the old code
-        Estimate estimate = null;
-        if (discharging && mPowerUsageFeatureProvider != null &&
+        final Estimate estimate;
+        // Get enhanced prediction if available
+        if (mPowerUsageFeatureProvider != null &&
                 mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(mContext)) {
             estimate = mPowerUsageFeatureProvider.getEnhancedBatteryPrediction(mContext);
-        }
-        final BatteryStats stats = statsHelper.getStats();
-        BatteryUtils.logRuntime(tag, "BatteryInfoLoader post query", startTime);
-
-        if (estimate != null) {
-            batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
-                    estimate, elapsedRealtimeUs, false /* shortString */);
         } else {
             estimate = new Estimate(
                     PowerUtil.convertUsToMs(stats.computeBatteryTimeRemaining(elapsedRealtimeUs)),
-                    false,
-                    Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN
-            );
-            batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
-                    estimate, elapsedRealtimeUs, false /* shortString */);
+                    false /* isBasedOnUsage */,
+                    Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN);
         }
+
+        BatteryUtils.logRuntime(tag, "BatteryInfoLoader post query", startTime);
+        batteryInfo = BatteryInfo.getBatteryInfo(mContext, batteryBroadcast, stats,
+                estimate, elapsedRealtimeUs, false /* shortString */);
         BatteryUtils.logRuntime(tag, "BatteryInfoLoader.loadInBackground", startTime);
 
         return batteryInfo;
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index b64dc52..8256868 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -95,6 +95,8 @@
     BatteryUtils mBatteryUtils;
     @VisibleForTesting
     LayoutPreference mBatteryLayoutPref;
+    @VisibleForTesting
+    BatteryInfo mBatteryInfo;
 
     /**
      * SparseArray that maps uid to {@link Anomaly}, so we could find {@link Anomaly} by uid
@@ -118,6 +120,8 @@
                 @Override
                 public void onLoadFinished(Loader<BatteryInfo> loader, BatteryInfo batteryInfo) {
                     mBatteryHeaderPreferenceController.updateHeaderPreference(batteryInfo);
+                    mBatteryInfo = batteryInfo;
+                    updateLastFullChargePreference();
                 }
 
                 @Override
@@ -291,9 +295,7 @@
 
         // reload BatteryInfo and updateUI
         restartBatteryInfoLoader();
-        final long lastFullChargeTime = mBatteryUtils.calculateLastFullChargeTime(mStatsHelper,
-                System.currentTimeMillis());
-        updateLastFullChargePreference(lastFullChargeTime);
+        updateLastFullChargePreference();
         mScreenUsagePref.setSubtitle(StringUtil.formatElapsedTime(getContext(),
                 mBatteryUtils.calculateScreenUsageTime(mStatsHelper), false));
     }
@@ -314,10 +316,21 @@
     }
 
     @VisibleForTesting
-    void updateLastFullChargePreference(long timeMs) {
-        final CharSequence timeSequence = StringUtil.formatRelativeTime(getContext(), timeMs,
-                false);
-        mLastFullChargePref.setSubtitle(timeSequence);
+    void updateLastFullChargePreference() {
+        if (mBatteryInfo != null && mBatteryInfo.averageTimeToDischarge
+                != Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN) {
+            mLastFullChargePref.setTitle(R.string.battery_full_charge_last);
+            mLastFullChargePref.setSubtitle(
+                    StringUtil.formatElapsedTime(getContext(), mBatteryInfo.averageTimeToDischarge,
+                            false /* withSeconds */));
+        } else {
+            final long lastFullChargeTime = mBatteryUtils.calculateLastFullChargeTime(mStatsHelper,
+                    System.currentTimeMillis());
+            mLastFullChargePref.setTitle(R.string.battery_last_full_charge);
+            mLastFullChargePref.setSubtitle(
+                    StringUtil.formatRelativeTime(getContext(), lastFullChargeTime,
+                            false /* withSeconds */));
+        }
     }
 
     @VisibleForTesting
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
index 6176bef..24b789c 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -123,6 +123,8 @@
     private MenuInflater mMenuInflater;
     @Mock
     private MenuItem mAdvancedPageMenu;
+    @Mock
+    private BatteryInfo mBatteryInfo;
 
     private List<BatterySipper> mUsageList;
     private Context mRealContext;
@@ -185,12 +187,28 @@
     }
 
     @Test
-    public void testUpdateLastFullChargePreference_showCorrectSummary() {
+    public void testUpdateLastFullChargePreference_noAverageTime_showLastFullChargeSummary() {
+        mFragment.mBatteryInfo = null;
+        when(mFragment.getContext()).thenReturn(mRealContext);
+        doReturn(TIME_SINCE_LAST_FULL_CHARGE_MS).when(
+                mFragment.mBatteryUtils).calculateLastFullChargeTime(any(), anyLong());
+
+        mFragment.updateLastFullChargePreference();
+
+        assertThat(mLastFullChargePref.getTitle()).isEqualTo("Last full charge");
+        assertThat(mLastFullChargePref.getSubtitle()).isEqualTo("2 hr. ago");
+    }
+
+    @Test
+    public void testUpdateLastFullChargePreference_hasAverageTime_showFullChargeLastSummary() {
+        mFragment.mBatteryInfo = mBatteryInfo;
+        mBatteryInfo.averageTimeToDischarge = TIME_SINCE_LAST_FULL_CHARGE_MS;
         when(mFragment.getContext()).thenReturn(mRealContext);
 
-        mFragment.updateLastFullChargePreference(TIME_SINCE_LAST_FULL_CHARGE_MS);
+        mFragment.updateLastFullChargePreference();
 
-        assertThat(mLastFullChargePref.getSubtitle()).isEqualTo("2 hr. ago");
+        assertThat(mLastFullChargePref.getTitle()).isEqualTo("Full charge lasts about");
+        assertThat(mLastFullChargePref.getSubtitle().toString()).isEqualTo("2h");
     }
 
     @Test