From 85ee4a3cdf8bb0f238fb9a71ce10cfcbe08e124a Mon Sep 17 00:00:00 2001 From: Evan Laird Date: Wed, 6 Mar 2019 18:09:20 -0500 Subject: [PATCH] Always show time remaining estimate string in QS QS will always show the time remaining estimate string, unless charging or the estimate is unavailable Test: visual Fixes: 127519046 Change-Id: Iaa268ce631d3196967748b7f562d26e19ddeb465 --- .../android/settingslib/utils/PowerUtil.java | 17 +++----- .../android/systemui/BatteryMeterView.java | 14 +++++-- .../systemui/qs/QuickStatusBarHeader.java | 41 ++----------------- .../statusbar/policy/BatteryController.java | 4 +- .../policy/BatteryControllerImpl.java | 4 +- 5 files changed, 25 insertions(+), 55 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java index 43c97df24a58f..52d7e2cedeb73 100644 --- a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +++ b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java @@ -195,19 +195,12 @@ public class PowerUtil { } private static String getRegularTimeRemainingShortString(Context context, long drainTimeMs) { - // Get the time of day we think device will die rounded to the nearest 15 min. - final long roundedTimeOfDayMs = - roundTimeToNearestThreshold( - System.currentTimeMillis() + drainTimeMs, - FIFTEEN_MINUTES_MILLIS); + // Get the time remaining rounded to the nearest 15 min + final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, FIFTEEN_MINUTES_MILLIS); + CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs, + false /* withSeconds */); - // convert the time to a properly formatted string. - String skeleton = android.text.format.DateFormat.getTimeFormatString(context); - DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton); - Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs)); - CharSequence timeString = fmt.format(date); - - return context.getString(R.string.power_discharge_by_only_short, timeString); + return context.getString(R.string.power_remaining_duration_only_short, timeString); } public static long convertUsToMs(long timeUs) { diff --git a/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java b/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java index 592b6039d69d3..39a584272585a 100644 --- a/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java +++ b/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java @@ -355,15 +355,23 @@ public class BatteryMeterView extends LinearLayout implements if (mBatteryPercentView != null) { if (mShowPercentMode == MODE_ESTIMATE && !mCharging) { mBatteryController.getEstimatedTimeRemainingString((String estimate) -> { - mBatteryPercentView.setText(estimate); + if (estimate != null) { + mBatteryPercentView.setText(estimate); + } else { + setPercentTextAtCurrentLevel(); + } }); } else { - mBatteryPercentView.setText( - NumberFormat.getPercentInstance().format(mLevel / 100f)); + setPercentTextAtCurrentLevel(); } } } + private void setPercentTextAtCurrentLevel() { + mBatteryPercentView.setText( + NumberFormat.getPercentInstance().format(mLevel / 100f)); + } + private void updateShowPercent() { final boolean showing = mBatteryPercentView != null; final boolean systemSetting = 0 != Settings.System diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java index 6a8c19ad0e770..08cf606f4273c 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java @@ -15,7 +15,6 @@ package com.android.systemui.qs; import static android.app.StatusBarManager.DISABLE2_QUICK_SETTINGS; -import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT; @@ -31,15 +30,12 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.content.res.Resources; -import android.database.ContentObserver; import android.graphics.Color; import android.graphics.Rect; import android.media.AudioManager; -import android.net.Uri; import android.os.Handler; import android.os.Looper; import android.provider.AlarmClock; -import android.provider.Settings; import android.service.notification.ZenModeConfig; import android.text.format.DateUtils; import android.util.AttributeSet; @@ -75,7 +71,6 @@ import com.android.systemui.statusbar.phone.StatusBarIconController; import com.android.systemui.statusbar.phone.StatusBarIconController.TintedIconManager; import com.android.systemui.statusbar.phone.StatusIconContainer; import com.android.systemui.statusbar.phone.SystemUIDialog; -import com.android.systemui.statusbar.policy.BatteryController; import com.android.systemui.statusbar.policy.Clock; import com.android.systemui.statusbar.policy.DateView; import com.android.systemui.statusbar.policy.NextAlarmController; @@ -107,7 +102,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements public static final int MAX_TOOLTIP_SHOWN_COUNT = 2; private final Handler mHandler = new Handler(); - private final BatteryController mBatteryController; private final NextAlarmController mAlarmController; private final ZenModeController mZenController; private final StatusBarIconController mStatusBarIconController; @@ -162,9 +156,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements }; private boolean mHasTopCutout = false; - private final PercentSettingObserver mPercentSettingObserver = - new PercentSettingObserver(new Handler(mContext.getMainLooper())); - /** * Runnable for automatically fading out the long press tooltip (as if it were animating away). */ @@ -181,12 +172,11 @@ public class QuickStatusBarHeader extends RelativeLayout implements @Inject public QuickStatusBarHeader(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs, NextAlarmController nextAlarmController, ZenModeController zenModeController, - BatteryController batteryController, StatusBarIconController statusBarIconController, + StatusBarIconController statusBarIconController, ActivityStarter activityStarter, PrivacyItemController privacyItemController) { super(context, attrs); mAlarmController = nextAlarmController; mZenController = zenModeController; - mBatteryController = batteryController; mStatusBarIconController = statusBarIconController; mActivityStarter = activityStarter; mPrivacyItemController = privacyItemController; @@ -241,7 +231,9 @@ public class QuickStatusBarHeader extends RelativeLayout implements mBatteryRemainingIcon = findViewById(R.id.batteryRemainingIcon); // Don't need to worry about tuner settings for this icon mBatteryRemainingIcon.setIgnoreTunerUpdates(true); - updateShowPercent(); + // QS will always show the estimate, and BatteryMeterView handles the case where + // it's unavailable or charging + mBatteryRemainingIcon.setPercentShowMode(BatteryMeterView.MODE_ESTIMATE); } private List getIgnoredIconSlots() { @@ -480,9 +472,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements super.onAttachedToWindow(); mStatusBarIconController.addIconGroup(mIconManager); requestApplyInsets(); - mContext.getContentResolver().registerContentObserver( - Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mPercentSettingObserver, - ActivityManager.getCurrentUser()); } @Override @@ -521,7 +510,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements public void onDetachedFromWindow() { setListening(false); mStatusBarIconController.removeIconGroup(mIconManager); - mContext.getContentResolver().unregisterContentObserver(mPercentSettingObserver); super.onDetachedFromWindow(); } @@ -743,25 +731,4 @@ public class QuickStatusBarHeader extends RelativeLayout implements lp.rightMargin = sideMargins; } } - - private void updateShowPercent() { - final boolean systemSetting = 0 != Settings.System - .getIntForUser(getContext().getContentResolver(), - SHOW_BATTERY_PERCENT, 0, ActivityManager.getCurrentUser()); - - mBatteryRemainingIcon.setPercentShowMode(systemSetting - ? BatteryMeterView.MODE_ESTIMATE : BatteryMeterView.MODE_ON); - } - - private final class PercentSettingObserver extends ContentObserver { - PercentSettingObserver(Handler handler) { - super(handler); - } - - @Override - public void onChange(boolean selfChange, Uri uri) { - super.onChange(selfChange, uri); - updateShowPercent(); - } - } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java index 5e94152e7eab5..111cdd2cc32a6 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java @@ -16,6 +16,8 @@ package com.android.systemui.statusbar.policy; +import android.annotation.Nullable; + import com.android.systemui.DemoMode; import com.android.systemui.Dumpable; import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; @@ -78,6 +80,6 @@ public interface BatteryController extends DemoMode, Dumpable, * The callback * @param estimate the estimate */ - void onBatteryRemainingEstimateRetrieved(String estimate); + void onBatteryRemainingEstimateRetrieved(@Nullable String estimate); } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java index 3fa3e1a6e6d4c..273fa55ee9d8b 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java @@ -237,10 +237,10 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC mFetchingEstimate = true; Dependency.get(Dependency.BG_HANDLER).post(() -> { - mEstimate = mEstimates.getEstimate(); + // Only fetch the estimate if they are enabled + mEstimate = mEstimates.isHybridNotificationEnabled() ? mEstimates.getEstimate() : null; mLastEstimateTimestamp = System.currentTimeMillis(); mFetchingEstimate = false; - Dependency.get(Dependency.MAIN_HANDLER).post(this::notifyEstimateFetchCallbacks); }); }