From 0be7598cb8c8cf6808b336204632615c5f26298f Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Wed, 23 Apr 2014 17:14:15 -0700 Subject: Show time remaining / to charge. Rework the battery graph to include the time remaining or time to charge. Change-Id: Ib26b761cb10e01f5f3aa4189db10d44b8ce62f89 --- res/layout/preference_batteryhistory.xml | 4 +- res/values/attrs.xml | 2 + res/values/strings.xml | 30 ++- res/xml/power_usage_summary.xml | 5 - .../settings/fuelgauge/BatteryHistoryChart.java | 292 +++++++++++++-------- .../settings/fuelgauge/BatteryHistoryDetail.java | 6 +- .../fuelgauge/BatteryHistoryPreference.java | 9 +- .../settings/fuelgauge/PowerUsageSummary.java | 19 +- src/com/android/settings/fuelgauge/Utils.java | 41 +++ 9 files changed, 270 insertions(+), 138 deletions(-) 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 @@ + + 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 @@ %1$dm + + %1$dd + + + %1$dd + %2$dh + + + %1$dh + + + %1$dh + %2$dm + + + %1$dm + + + %1$dm + %2$ds + Usage statistics @@ -3562,8 +3583,13 @@ What has been using the battery Battery usage data isn\'t available. - - %1$s - %2$s + + %1$s + - %2$s + + %1$s remaining + + %1$s to charge Battery use since unplugged 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 @@ - 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 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { int mTextAscent; int mTextDescent; + int mHeaderTextAscent; + int mHeaderTextDescent; int mDurationStringWidth; - int mTotalDurationStringWidth; + int mChargeLabelStringWidth; + int mChargeDurationStringWidth; + int mDrainStringWidth; boolean mLargeMode; @@ -174,7 +182,96 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { 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; - - 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; - } - } + 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); - appearance.recycle(); - } - int shadowcolor = 0; float dx=0, dy=0, r=0; @@ -269,80 +328,47 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { 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 @@ public class BatteryHistoryChart extends View { // 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 @@ public class BatteryHistoryChart extends View { } 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 @@ public class BatteryHistoryChart extends View { 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 @@ import com.android.settings.R; 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 @@ public class BatteryHistoryDetail extends Fragment { parcel.setDataPosition(0); mStats = com.android.internal.os.BatteryStatsImpl.CREATOR .createFromParcel(parcel); + mBatteryBroadcast = getArguments().getParcelable(EXTRA_BROADCAST); } @Override @@ -47,7 +51,7 @@ public class BatteryHistoryDetail extends Fragment { 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 @@ import com.android.settings.R; */ 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 @@ public class BatteryHistoryPreference extends Preference { 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.BroadcastReceiver; 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 @@ public class PowerUsageSummary extends PreferenceFragment { 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 @@ public class PowerUsageSummary extends PreferenceFragment { private PreferenceGroup mAppListGroup; private String mBatteryLevel; private String mBatteryStatus; - private Preference mBatteryStatusPref; private int mStatsType = BatteryStats.STATS_SINCE_CHARGED; @@ -94,7 +93,7 @@ public class PowerUsageSummary extends PreferenceFragment { 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 @@ public class PowerUsageSummary extends PreferenceFragment { addPreferencesFromResource(R.xml.power_usage_summary); mAppListGroup = (PreferenceGroup) findPreference(KEY_APP_LIST); - mBatteryStatusPref = mAppListGroup.findPreference(KEY_BATTERY_STATUS); setHasOptionsMenu(true); } @@ -141,6 +139,8 @@ public class PowerUsageSummary extends PreferenceFragment { 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 @@ public class PowerUsageSummary extends PreferenceFragment { 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 @@ public class PowerUsageSummary extends PreferenceFragment { return; } final int dischargeAmount = mStatsHelper.getStats().getDischargeAmount(mStatsType); - mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.myUserId()); List usageList = mStatsHelper.getUsageList(); for (int i=0; i= 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); + } + } } -- cgit v1.1