Show time remaining / to charge.

Rework the battery graph to include the time remaining or
time to charge.

Change-Id: Ib26b761cb10e01f5f3aa4189db10d44b8ce62f89
diff --git a/res/layout/preference_batteryhistory.xml b/res/layout/preference_batteryhistory.xml
index 2655615..c9403c6 100644
--- a/res/layout/preference_batteryhistory.xml
+++ b/res/layout/preference_batteryhistory.xml
@@ -16,13 +16,15 @@
 
 <com.android.settings.fuelgauge.BatteryHistoryChart
         xmlns:android="http://schemas.android.com/apk/res/android"
+        xmlns:app="http://schemas.android.com/apk/res/com.android.settings"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:minHeight="128dp"
     android:gravity="center_vertical"
     android:id="@+android:id/battery_history_chart"
     android:paddingEnd="?android:attr/scrollbarSize"
     android:textAppearance="?android:attr/textAppearanceMedium"
+    app:headerAppearance="?android:attr/textAppearanceMedium"
     android:shadowRadius="4"
     android:shadowColor="?android:attr/colorBackground"
     android:shadowDx="2"
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 5bca063..3ad0375 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -41,6 +41,8 @@
         <attr name="android:shadowDy" />
         <!-- Radius of the shadow. -->
         <attr name="android:shadowRadius" />
+        <!-- Text color, typeface, size, and style of header. -->
+        <attr name="headerAppearance" format="reference" />
     </declare-styleable>
 
     <declare-styleable name="PercentageBarChart">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 070a10e..6a632ce 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3279,6 +3279,27 @@
     <!-- Used to show an amount of time in the form "m minutes, s seconds" in BatteryHistory -->
     <string name="battery_history_minutes_no_seconds"><xliff:g id="minutes">%1$d</xliff:g>m</string>
 
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "d days" in BatteryHistory -->
+    <string name="battery_history_days_only"><xliff:g id="days">%1$d</xliff:g>d</string>
+
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "d days, h hours" in BatteryHistory -->
+    <string name="battery_history_days_and_hours"><xliff:g id="days">%1$d</xliff:g>d
+        <xliff:g id="hours">%2$d</xliff:g>h</string>
+
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "h hours" in BatteryHistory -->
+    <string name="battery_history_hours_only"><xliff:g id="hours">%1$d</xliff:g>h</string>
+
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "h hours, m minutes" in BatteryHistory -->
+    <string name="battery_history_hours_and_minutes"><xliff:g id="hours">%1$d</xliff:g>h
+        <xliff:g id="minutes">%2$d</xliff:g>m</string>
+
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "m minutes" in BatteryHistory -->
+    <string name="battery_history_minutes_only"><xliff:g id="minutes">%1$d</xliff:g>m</string>
+
+    <!-- [CHAR LIMIT=20] Used to show an amount of time in the form "m minutes, s seconds" in BatteryHistory -->
+    <string name="battery_history_minutes_and_seconds"><xliff:g id="minutes">%1$d</xliff:g>m
+        <xliff:g id="seconds">%2$d</xliff:g>s</string>
+
     <!-- XXX remove? Strings used for displaying usage statistics -->
     <string name="usage_stats_label">Usage statistics</string>
 
@@ -3562,8 +3583,13 @@
     <string name="power_usage_summary">What has been using the battery</string>
     <!-- Message to show when battery usage data is not available [CHAR LIMIT=30] -->
     <string name="power_usage_not_available">Battery usage data isn\'t available.</string>
-    <!-- Display the battery level and status [CHAR_LIMIT=30] -->
-    <string name="power_usage_level_and_status"><xliff:g id="level">%1$s</xliff:g> - <xliff:g id="status">%2$s</xliff:g></string>
+    <!-- Display the battery level and status [CHAR_LIMIT=60] -->
+    <string name="power_usage_level_and_status"><xliff:g id="level">%1$s</xliff:g>
+            - <xliff:g id="status">%2$s</xliff:g></string>
+    <!-- Display time remaining until battery is discharged [CHAR_LIMIT=60] -->
+    <string name="power_discharge_remaining"><xliff:g id="remain">%1$s</xliff:g> remaining</string>
+    <!-- Display time remaining until battery is charged [CHAR_LIMIT=60] -->
+    <string name="power_charge_remaining"><xliff:g id="until_charged">%1$s</xliff:g> to charge</string>
     <!-- Battery usage since unplugged -->
     <string name="battery_since_unplugged">Battery use since unplugged</string>
     <!-- Battery usage since user reset the stats -->
diff --git a/res/xml/power_usage_summary.xml b/res/xml/power_usage_summary.xml
index f6ee594..44e40bd 100644
--- a/res/xml/power_usage_summary.xml
+++ b/res/xml/power_usage_summary.xml
@@ -17,9 +17,4 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
         android:title="@string/power_usage_summary_title"
         android:key="app_list">
-    <Preference
-        style="?android:attr/preferenceInformationStyle"
-        android:key="battery_status"
-        android:persistent="false"
-    />
 </PreferenceScreen>
diff --git a/src/com/android/settings/fuelgauge/BatteryHistoryChart.java b/src/com/android/settings/fuelgauge/BatteryHistoryChart.java
index 44274a6..58471a3 100644
--- a/src/com/android/settings/fuelgauge/BatteryHistoryChart.java
+++ b/src/com/android/settings/fuelgauge/BatteryHistoryChart.java
@@ -16,6 +16,8 @@
 
 package com.android.settings.fuelgauge;
 
+import android.content.Intent;
+import android.os.BatteryManager;
 import com.android.settings.R;
 
 import android.content.Context;
@@ -120,7 +122,8 @@
     final Paint mCpuRunningPaint = new Paint();
     final ChartData mPhoneSignalChart = new ChartData();
     final TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
-    
+    final TextPaint mHeaderTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
+
     final Path mBatLevelPath = new Path();
     final Path mBatGoodPath = new Path();
     final Path mBatWarnPath = new Path();
@@ -131,12 +134,13 @@
     final Path mWifiRunningPath = new Path();
     final Path mCpuRunningPath = new Path();
     
-    int mFontSize;
-    
     BatteryStats mStats;
+    Intent mBatteryBroadcast;
     long mStatsPeriod;
     String mDurationString;
-    String mTotalDurationString;
+    String mChargeLabelString;
+    String mChargeDurationString;
+    String mDrainString;
     String mChargingLabel;
     String mScreenOnLabel;
     String mGpsOnLabel;
@@ -146,8 +150,12 @@
     
     int mTextAscent;
     int mTextDescent;
+    int mHeaderTextAscent;
+    int mHeaderTextDescent;
     int mDurationStringWidth;
-    int mTotalDurationStringWidth;
+    int mChargeLabelStringWidth;
+    int mChargeDurationStringWidth;
+    int mDrainStringWidth;
 
     boolean mLargeMode;
 
@@ -174,7 +182,96 @@
     boolean mHaveWifi;
     boolean mHaveGps;
     boolean mHavePhoneSignal;
-    
+
+    static class TextAttrs {
+        ColorStateList textColor = null;
+        int textSize = 15;
+        int typefaceIndex = -1;
+        int styleIndex = -1;
+
+        void retrieve(Context context, TypedArray from, int index) {
+            TypedArray appearance = null;
+            int ap = from.getResourceId(index, -1);
+            if (ap != -1) {
+                appearance = context.obtainStyledAttributes(ap,
+                                    com.android.internal.R.styleable.TextAppearance);
+            }
+            if (appearance != null) {
+                int n = appearance.getIndexCount();
+                for (int i = 0; i < n; i++) {
+                    int attr = appearance.getIndex(i);
+
+                    switch (attr) {
+                    case com.android.internal.R.styleable.TextAppearance_textColor:
+                        textColor = appearance.getColorStateList(attr);
+                        break;
+
+                    case com.android.internal.R.styleable.TextAppearance_textSize:
+                        textSize = appearance.getDimensionPixelSize(attr, textSize);
+                        break;
+
+                    case com.android.internal.R.styleable.TextAppearance_typeface:
+                        typefaceIndex = appearance.getInt(attr, -1);
+                        break;
+
+                    case com.android.internal.R.styleable.TextAppearance_textStyle:
+                        styleIndex = appearance.getInt(attr, -1);
+                        break;
+                    }
+                }
+
+                appearance.recycle();
+            }
+        }
+
+        void apply(Context context, TextPaint paint) {
+            paint.density = context.getResources().getDisplayMetrics().density;
+            paint.setCompatibilityScaling(
+                    context.getResources().getCompatibilityInfo().applicationScale);
+
+            paint.setColor(textColor.getDefaultColor());
+            paint.setTextSize(textSize);
+
+            Typeface tf = null;
+            switch (typefaceIndex) {
+                case SANS:
+                    tf = Typeface.SANS_SERIF;
+                    break;
+
+                case SERIF:
+                    tf = Typeface.SERIF;
+                    break;
+
+                case MONOSPACE:
+                    tf = Typeface.MONOSPACE;
+                    break;
+            }
+
+            setTypeface(paint, tf, styleIndex);
+        }
+
+        public void setTypeface(TextPaint paint, Typeface tf, int style) {
+            if (style > 0) {
+                if (tf == null) {
+                    tf = Typeface.defaultFromStyle(style);
+                } else {
+                    tf = Typeface.create(tf, style);
+                }
+
+                paint.setTypeface(tf);
+                // now compute what (if any) algorithmic styling is needed
+                int typefaceStyle = tf != null ? tf.getStyle() : 0;
+                int need = style & ~typefaceStyle;
+                paint.setFakeBoldText((need & Typeface.BOLD) != 0);
+                paint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
+            } else {
+                paint.setFakeBoldText(false);
+                paint.setTextSkewX(0);
+                paint.setTypeface(tf);
+            }
+        }
+    }
+
     public BatteryHistoryChart(Context context, AttributeSet attrs) {
         super(context, attrs);
         
@@ -197,53 +294,15 @@
                 0xff80a000, 0xff409000, 0xff008000
         });
         
-        mTextPaint.density = getResources().getDisplayMetrics().density;
-        mTextPaint.setCompatibilityScaling(
-                getResources().getCompatibilityInfo().applicationScale);
-        
         TypedArray a =
             context.obtainStyledAttributes(
                 attrs, R.styleable.BatteryHistoryChart, 0, 0);
-        
-        ColorStateList textColor = null;
-        int textSize = 15;
-        int typefaceIndex = -1;
-        int styleIndex = -1;
-        
-        TypedArray appearance = null;
-        int ap = a.getResourceId(R.styleable.BatteryHistoryChart_android_textAppearance, -1);
-        if (ap != -1) {
-            appearance = context.obtainStyledAttributes(ap,
-                                com.android.internal.R.styleable.
-                                TextAppearance);
-        }
-        if (appearance != null) {
-            int n = appearance.getIndexCount();
-            for (int i = 0; i < n; i++) {
-                int attr = appearance.getIndex(i);
 
-                switch (attr) {
-                case com.android.internal.R.styleable.TextAppearance_textColor:
-                    textColor = appearance.getColorStateList(attr);
-                    break;
+        final TextAttrs mainTextAttrs = new TextAttrs();
+        final TextAttrs headTextAttrs = new TextAttrs();
+        mainTextAttrs.retrieve(context, a, R.styleable.BatteryHistoryChart_android_textAppearance);
+        headTextAttrs.retrieve(context, a, R.styleable.BatteryHistoryChart_headerAppearance);
 
-                case com.android.internal.R.styleable.TextAppearance_textSize:
-                    textSize = appearance.getDimensionPixelSize(attr, textSize);
-                    break;
-
-                case com.android.internal.R.styleable.TextAppearance_typeface:
-                    typefaceIndex = appearance.getInt(attr, -1);
-                    break;
-
-                case com.android.internal.R.styleable.TextAppearance_textStyle:
-                    styleIndex = appearance.getInt(attr, -1);
-                    break;
-                }
-            }
-
-            appearance.recycle();
-        }
-        
         int shadowcolor = 0;
         float dx=0, dy=0, r=0;
         
@@ -269,80 +328,47 @@
                     break;
 
                 case R.styleable.BatteryHistoryChart_android_textColor:
-                    textColor = a.getColorStateList(attr);
+                    mainTextAttrs.textColor = a.getColorStateList(attr);
+                    headTextAttrs.textColor = a.getColorStateList(attr);
                     break;
 
                 case R.styleable.BatteryHistoryChart_android_textSize:
-                    textSize = a.getDimensionPixelSize(attr, textSize);
+                    mainTextAttrs.textSize = a.getDimensionPixelSize(attr, mainTextAttrs.textSize);
+                    headTextAttrs.textSize = a.getDimensionPixelSize(attr, headTextAttrs.textSize);
                     break;
 
                 case R.styleable.BatteryHistoryChart_android_typeface:
-                    typefaceIndex = a.getInt(attr, typefaceIndex);
+                    mainTextAttrs.typefaceIndex = a.getInt(attr, mainTextAttrs.typefaceIndex);
+                    headTextAttrs.typefaceIndex = a.getInt(attr, headTextAttrs.typefaceIndex);
                     break;
 
                 case R.styleable.BatteryHistoryChart_android_textStyle:
-                    styleIndex = a.getInt(attr, styleIndex);
+                    mainTextAttrs.styleIndex = a.getInt(attr, mainTextAttrs.styleIndex);
+                    headTextAttrs.styleIndex = a.getInt(attr, headTextAttrs.styleIndex);
                     break;
             }
         }
         
         a.recycle();
         
-        mTextPaint.setColor(textColor.getDefaultColor());
-        mTextPaint.setTextSize(textSize);
-        
-        Typeface tf = null;
-        switch (typefaceIndex) {
-            case SANS:
-                tf = Typeface.SANS_SERIF;
-                break;
+        mainTextAttrs.apply(context, mTextPaint);
+        headTextAttrs.apply(context, mHeaderTextPaint);
 
-            case SERIF:
-                tf = Typeface.SERIF;
-                break;
-
-            case MONOSPACE:
-                tf = Typeface.MONOSPACE;
-                break;
-        }
-        
-        setTypeface(tf, styleIndex);
-        
         if (shadowcolor != 0) {
             mTextPaint.setShadowLayer(r, dx, dy, shadowcolor);
+            mHeaderTextPaint.setShadowLayer(r, dx, dy, shadowcolor);
         }
     }
     
-    public void setTypeface(Typeface tf, int style) {
-        if (style > 0) {
-            if (tf == null) {
-                tf = Typeface.defaultFromStyle(style);
-            } else {
-                tf = Typeface.create(tf, style);
-            }
-
-            mTextPaint.setTypeface(tf);
-            // now compute what (if any) algorithmic styling is needed
-            int typefaceStyle = tf != null ? tf.getStyle() : 0;
-            int need = style & ~typefaceStyle;
-            mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
-            mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
-        } else {
-            mTextPaint.setFakeBoldText(false);
-            mTextPaint.setTextSkewX(0);
-            mTextPaint.setTypeface(tf);
-        }
-    }
-    
-    void setStats(BatteryStats stats) {
+    void setStats(BatteryStats stats, Intent broadcast) {
         mStats = stats;
-        
-        long uSecTime = mStats.computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000,
+        mBatteryBroadcast = broadcast;
+
+        final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
+
+        long uSecTime = mStats.computeBatteryRealtime(elapsedRealtimeUs,
                 BatteryStats.STATS_SINCE_CHARGED);
         mStatsPeriod = uSecTime;
-        String durationString = Utils.formatElapsedTime(getContext(), mStatsPeriod / 1000, true);
-        mDurationString = getContext().getString(R.string.battery_stats_on_battery,
-                durationString);
         mChargingLabel = getContext().getString(R.string.battery_stats_charging_label);
         mScreenOnLabel = getContext().getString(R.string.battery_stats_screen_on_label);
         mGpsOnLabel = getContext().getString(R.string.battery_stats_gps_on_label);
@@ -382,16 +408,42 @@
             mHavePhoneSignal = true;
         }
         if (mHistEnd <= mHistStart) mHistEnd = mHistStart+1;
-        mTotalDurationString = Utils.formatElapsedTime(getContext(), mHistEnd - mHistStart, true);
+
+        //String durationString = Utils.formatElapsedTime(getContext(), mStatsPeriod / 1000, true);
+        //mDurationString = getContext().getString(R.string.battery_stats_on_battery,
+        //        durationString);
+        mDurationString = Utils.formatElapsedTime(getContext(), mHistEnd - mHistStart, true);
+        mDrainString = com.android.settings.Utils.getBatteryPercentage(mBatteryBroadcast);
+        mChargeLabelString = com.android.settings.Utils.getBatteryStatus(getResources(),
+                mBatteryBroadcast);
+        final long drainTime = mStats.computeBatteryTimeRemaining(elapsedRealtimeUs);
+        final long chargeTime = mStats.computeChargeTimeRemaining(elapsedRealtimeUs);
+        final int status = mBatteryBroadcast.getIntExtra(BatteryManager.EXTRA_STATUS,
+                BatteryManager.BATTERY_STATUS_UNKNOWN);
+        if (drainTime > 0) {
+            String timeString = Utils.formatShortElapsedTime(getContext(),drainTime / 1000);
+            mChargeDurationString = getContext().getResources().getString(
+                    R.string.power_discharge_remaining, timeString);
+        } else if (chargeTime > 0 && status != BatteryManager.BATTERY_STATUS_FULL) {
+            String timeString = Utils.formatShortElapsedTime(getContext(), chargeTime / 1000);
+            mChargeDurationString = getContext().getResources().getString(
+                    R.string.power_charge_remaining, timeString);
+        } else {
+            mChargeDurationString = "";
+        }
     }
 
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         mDurationStringWidth = (int)mTextPaint.measureText(mDurationString);
-        mTotalDurationStringWidth = (int)mTextPaint.measureText(mTotalDurationString);
+        mDrainStringWidth = (int)mHeaderTextPaint.measureText(mDrainString);
+        mChargeLabelStringWidth = (int)mHeaderTextPaint.measureText(mChargeLabelString);
+        mChargeDurationStringWidth = (int)mHeaderTextPaint.measureText(mChargeDurationString);
         mTextAscent = (int)mTextPaint.ascent();
         mTextDescent = (int)mTextPaint.descent();
+        mHeaderTextAscent = (int)mHeaderTextPaint.ascent();
+        mHeaderTextDescent = (int)mHeaderTextPaint.descent();
     }
 
     void finishPaths(int w, int h, int levelh, int startX, int y, Path curLevelPath,
@@ -434,9 +486,10 @@
         super.onSizeChanged(w, h, oldw, oldh);
         
         int textHeight = mTextDescent - mTextAscent;
+        int headerTextHeight = mHeaderTextDescent - mHeaderTextAscent;
         mThinLineWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                 2, getResources().getDisplayMetrics());
-        if (h > (textHeight*6)) {
+        if (h > (textHeight*12)) {
             mLargeMode = true;
             if (h > (textHeight*15)) {
                 // Plenty of room for the chart.
@@ -445,7 +498,7 @@
                 // Compress lines to make more room for chart.
                 mLineWidth = textHeight/3;
             }
-            mLevelTop = textHeight + mLineWidth;
+            mLevelTop = headerTextHeight*2 + mLineWidth;
             mScreenOnPaint.setARGB(255, 32, 64, 255);
             mGpsOnPaint.setARGB(255, 32, 64, 255);
             mWifiRunningPaint.setARGB(255, 32, 64, 255);
@@ -453,7 +506,7 @@
         } else {
             mLargeMode = false;
             mLineWidth = mThinLineWidth;
-            mLevelTop = 0;
+            mLevelTop = headerTextHeight*2 + mLineWidth;
             mScreenOnPaint.setARGB(255, 0, 0, 255);
             mGpsOnPaint.setARGB(255, 0, 0, 255);
             mWifiRunningPaint.setARGB(255, 0, 0, 255);
@@ -659,22 +712,35 @@
         final int height = getHeight();
         final boolean layoutRtl = isLayoutRtl();
         final int textStartX = layoutRtl ? width : 0;
-        mTextPaint.setTextAlign(layoutRtl ? Paint.Align.RIGHT : Paint.Align.LEFT);
+        final int textEndX = layoutRtl ? 0 : width;
+        final Paint.Align textAlignLeft = layoutRtl ? Paint.Align.RIGHT : Paint.Align.LEFT;
+        final Paint.Align textAlignRight = layoutRtl ? Paint.Align.LEFT : Paint.Align.RIGHT;
+        mTextPaint.setTextAlign(textAlignLeft);
 
         canvas.drawPath(mBatLevelPath, mBatteryBackgroundPaint);
+        int durationHalfWidth = mDurationStringWidth / 2;
+        if (layoutRtl) durationHalfWidth = -durationHalfWidth;
         if (mLargeMode) {
-            int durationHalfWidth = mTotalDurationStringWidth / 2;
-            if (layoutRtl) durationHalfWidth = -durationHalfWidth;
-            canvas.drawText(mDurationString, textStartX, -mTextAscent + (mLineWidth / 2),
-                    mTextPaint);
-            canvas.drawText(mTotalDurationString, (width / 2) - durationHalfWidth,
+            canvas.drawText(mDurationString, (width / 2) - durationHalfWidth,
                     mLevelBottom - mTextAscent + mThinLineWidth, mTextPaint);
         } else {
-            int durationHalfWidth = mDurationStringWidth / 2;
-            if (layoutRtl) durationHalfWidth = -durationHalfWidth;
             canvas.drawText(mDurationString, (width / 2) - durationHalfWidth,
-                    (height / 2) - ((mTextDescent - mTextAscent) / 2) - mTextAscent, mTextPaint);
+                    mLevelTop + ((height-mLevelTop) / 2) - ((mTextDescent - mTextAscent) / 2)
+                            - mTextAscent, mTextPaint);
         }
+
+        int headerTop = mLevelTop/2 + (mHeaderTextDescent-mHeaderTextAscent)/2;
+        mHeaderTextPaint.setTextAlign(textAlignLeft);
+        canvas.drawText(mChargeLabelString, textStartX, headerTop, mHeaderTextPaint);
+        durationHalfWidth = mChargeDurationStringWidth / 2;
+        if (layoutRtl) durationHalfWidth = -durationHalfWidth;
+        int headerCenter = ((width-mChargeDurationStringWidth-mDrainStringWidth)/2)
+                + (layoutRtl ? mDrainStringWidth : mChargeLabelStringWidth);
+        canvas.drawText(mChargeDurationString, headerCenter - durationHalfWidth, headerTop,
+                mHeaderTextPaint);
+        mHeaderTextPaint.setTextAlign(textAlignRight);
+        canvas.drawText(mDrainString, textEndX, headerTop, mHeaderTextPaint);
+
         if (!mBatGoodPath.isEmpty()) {
             canvas.drawPath(mBatGoodPath, mBatteryGoodPaint);
         }
diff --git a/src/com/android/settings/fuelgauge/BatteryHistoryDetail.java b/src/com/android/settings/fuelgauge/BatteryHistoryDetail.java
index ad6fb30..0cbb9b7 100644
--- a/src/com/android/settings/fuelgauge/BatteryHistoryDetail.java
+++ b/src/com/android/settings/fuelgauge/BatteryHistoryDetail.java
@@ -17,6 +17,7 @@
 package com.android.settings.fuelgauge;
 
 import android.app.Fragment;
+import android.content.Intent;
 import android.os.Bundle;
 import android.os.Parcel;
 import android.view.LayoutInflater;
@@ -28,8 +29,10 @@
 
 public class BatteryHistoryDetail extends Fragment {
     public static final String EXTRA_STATS = "stats";
+    public static final String EXTRA_BROADCAST = "broadcast";
 
     private BatteryStatsImpl mStats;
+    private Intent mBatteryBroadcast;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -40,6 +43,7 @@
         parcel.setDataPosition(0);
         mStats = com.android.internal.os.BatteryStatsImpl.CREATOR
                 .createFromParcel(parcel);
+        mBatteryBroadcast = getArguments().getParcelable(EXTRA_BROADCAST);
     }
     
     @Override
@@ -47,7 +51,7 @@
         View view = inflater.inflate(R.layout.preference_batteryhistory, null);
         BatteryHistoryChart chart = (BatteryHistoryChart)view.findViewById(
                 R.id.battery_history_chart);
-        chart.setStats(mStats);
+        chart.setStats(mStats, mBatteryBroadcast);
         return view;
     }
 }
diff --git a/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java b/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java
index 4579db7..bdea852 100644
--- a/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java
+++ b/src/com/android/settings/fuelgauge/BatteryHistoryPreference.java
@@ -17,6 +17,7 @@
 package com.android.settings.fuelgauge;
 
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.drawable.Drawable;
 import android.os.BatteryStats;
 import android.preference.Preference;
@@ -33,12 +34,14 @@
  */
 public class BatteryHistoryPreference extends Preference {
 
-    private BatteryStats mStats;
+    final private BatteryStats mStats;
+    final private Intent mBatteryBroadcast;
 
-    public BatteryHistoryPreference(Context context, BatteryStats stats) {
+    public BatteryHistoryPreference(Context context, BatteryStats stats, Intent batteryBroadcast) {
         super(context);
         setLayoutResource(R.layout.preference_batteryhistory);
         mStats = stats;
+        mBatteryBroadcast = batteryBroadcast;
     }
 
     BatteryStats getStats() {
@@ -51,6 +54,6 @@
 
         BatteryHistoryChart chart = (BatteryHistoryChart)view.findViewById(
                 R.id.battery_history_chart);
-        chart.setStats(mStats);
+        chart.setStats(mStats, mBatteryBroadcast);
     }
 }
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 0f7eff2..87e5d76 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -21,6 +21,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.os.BatteryManager;
 import android.os.BatteryStats;
 import android.os.Bundle;
 import android.os.Handler;
@@ -57,7 +58,6 @@
     private static final String TAG = "PowerUsageSummary";
 
     private static final String KEY_APP_LIST = "app_list";
-    private static final String KEY_BATTERY_STATUS = "battery_status";
 
     private static final int MENU_STATS_TYPE = Menu.FIRST;
     private static final int MENU_STATS_REFRESH = Menu.FIRST + 1;
@@ -68,7 +68,6 @@
     private PreferenceGroup mAppListGroup;
     private String mBatteryLevel;
     private String mBatteryStatus;
-    private Preference mBatteryStatusPref;
 
     private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
 
@@ -94,7 +93,7 @@
     public void onAttach(Activity activity) {
         super.onAttach(activity);
         mUm = (UserManager) activity.getSystemService(Context.USER_SERVICE);
-        mStatsHelper = new BatteryStatsHelper(activity);
+        mStatsHelper = new BatteryStatsHelper(activity, true);
     }
 
     @Override
@@ -104,7 +103,6 @@
 
         addPreferencesFromResource(R.xml.power_usage_summary);
         mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST);
-        mBatteryStatusPref = mAppListGroup.findPreference(KEY_BATTERY_STATUS);
         setHasOptionsMenu(true);
     }
 
@@ -141,6 +139,8 @@
             byte[] histData = hist.marshall();
             Bundle args = new Bundle();
             args.putByteArray(BatteryHistoryDetail.EXTRA_STATS, histData);
+            args.putParcelable(BatteryHistoryDetail.EXTRA_BROADCAST,
+                    mStatsHelper.getBatteryBroadcast());
             SettingsActivity sa = (SettingsActivity) getActivity();
             sa.startPreferencePanel(BatteryHistoryDetail.class.getName(), args,
                     R.string.history_details_title, null, null, 0);
@@ -220,16 +220,10 @@
         mAppListGroup.removeAll();
         mAppListGroup.setOrderingAsAdded(false);
 
-        mBatteryStatusPref.setOrder(-2);
-        if (mBatteryLevel != null && mBatteryStatus != null) {
-            String batterySummary = getActivity().getResources().getString(
-                    R.string.power_usage_level_and_status, mBatteryLevel, mBatteryStatus);
-            mBatteryStatusPref.setTitle(batterySummary);
-        }
-        mAppListGroup.addPreference(mBatteryStatusPref);
+        mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
 
         BatteryHistoryPreference hist = new BatteryHistoryPreference(
-                getActivity(), mStatsHelper.getStats());
+                getActivity(), mStatsHelper.getStats(), mStatsHelper.getBatteryBroadcast());
         hist.setOrder(-1);
         mAppListGroup.addPreference(hist);
 
@@ -239,7 +233,6 @@
             return;
         }
         final int dischargeAmount = mStatsHelper.getStats().getDischargeAmount(mStatsType);
-        mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId());
         List<BatterySipper> usageList = mStatsHelper.getUsageList();
         for (int i=0; i<usageList.size(); i++) {
             BatterySipper sipper = usageList.get(i);
diff --git a/src/com/android/settings/fuelgauge/Utils.java b/src/com/android/settings/fuelgauge/Utils.java
index 9a06c9f..c88b0d2 100644
--- a/src/com/android/settings/fuelgauge/Utils.java
+++ b/src/com/android/settings/fuelgauge/Utils.java
@@ -81,4 +81,45 @@
         }
         return sb.toString();
     }
+
+    /**
+     * Returns elapsed time for the given millis, in the following format:
+     * 2d 5h; will include at most two units, can go down to seconds precision.
+     * @param context the application context
+     * @param millis the elapsed time in milli seconds
+     * @return the formatted elapsed time
+     */
+    public static String formatShortElapsedTime(Context context, double millis) {
+        int seconds = (int) Math.floor(millis / 1000);
+
+        int days = 0, hours = 0, minutes = 0;
+        if (seconds >= SECONDS_PER_DAY) {
+            days = seconds / SECONDS_PER_DAY;
+            seconds -= days * SECONDS_PER_DAY;
+        }
+        if (seconds >= SECONDS_PER_HOUR) {
+            hours = seconds / SECONDS_PER_HOUR;
+            seconds -= hours * SECONDS_PER_HOUR;
+        }
+        if (seconds >= SECONDS_PER_MINUTE) {
+            minutes = seconds / SECONDS_PER_MINUTE;
+            seconds -= minutes * SECONDS_PER_MINUTE;
+        }
+        if (days >= 4) {
+            return context.getString(R.string.battery_history_days_only, days);
+        } else if (days > 0) {
+            return context.getString(R.string.battery_history_days_and_hours, days, hours);
+        } else if (hours >= 12) {
+            return context.getString(R.string.battery_history_hours_only, hours);
+        } else if (hours > 0) {
+            return context.getString(R.string.battery_history_hours_and_minutes, hours, minutes);
+        } else if (minutes >= 10) {
+            return context.getString(R.string.battery_history_minutes_only, minutes);
+        } else if (minutes > 0) {
+            return context.getString(R.string.battery_history_minutes_and_seconds, minutes,
+                    seconds);
+        } else {
+            return context.getString(R.string.battery_history_seconds, seconds);
+        }
+    }
 }