Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib.utils; |
| 18 | |
| 19 | import android.content.Context; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 20 | import android.icu.text.DateFormat; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 21 | import android.icu.text.MeasureFormat; |
| 22 | import android.icu.text.MeasureFormat.FormatWidth; |
| 23 | import android.icu.util.Measure; |
| 24 | import android.icu.util.MeasureUnit; |
| 25 | import android.support.annotation.Nullable; |
| 26 | import android.text.TextUtils; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 27 | import com.android.internal.annotations.VisibleForTesting; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 28 | import com.android.settingslib.R; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 29 | import java.time.Clock; |
| 30 | import java.time.Instant; |
| 31 | import java.util.Calendar; |
| 32 | import java.util.Date; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 33 | import java.util.Locale; |
| 34 | import java.util.concurrent.TimeUnit; |
| 35 | |
| 36 | /** Utility class for keeping power related strings consistent**/ |
| 37 | public class PowerUtil { |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 38 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 39 | private static final long SEVEN_MINUTES_MILLIS = TimeUnit.MINUTES.toMillis(7); |
| 40 | private static final long FIFTEEN_MINUTES_MILLIS = TimeUnit.MINUTES.toMillis(15); |
| 41 | private static final long ONE_DAY_MILLIS = TimeUnit.DAYS.toMillis(1); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 42 | private static final long TWO_DAYS_MILLIS = TimeUnit.DAYS.toMillis(2); |
| 43 | private static final long ONE_HOUR_MILLIS = TimeUnit.HOURS.toMillis(1); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * This method produces the text used in various places throughout the system to describe the |
| 47 | * remaining battery life of the phone in a consistent manner. |
| 48 | * |
| 49 | * @param context |
| 50 | * @param drainTimeMs The estimated time remaining before the phone dies in milliseconds. |
| 51 | * @param percentageString An optional percentage of battery remaining string. |
| 52 | * @param basedOnUsage Whether this estimate is based on usage or simple extrapolation. |
| 53 | * @return a properly formatted and localized string describing how much time remains |
| 54 | * before the battery runs out. |
| 55 | */ |
| 56 | public static String getBatteryRemainingStringFormatted(Context context, long drainTimeMs, |
| 57 | @Nullable String percentageString, boolean basedOnUsage) { |
| 58 | if (drainTimeMs > 0) { |
| 59 | if (drainTimeMs <= SEVEN_MINUTES_MILLIS) { |
| 60 | // show a imminent shutdown warning if less than 7 minutes remain |
| 61 | return getShutdownImminentString(context, percentageString); |
| 62 | } else if (drainTimeMs <= FIFTEEN_MINUTES_MILLIS) { |
| 63 | // show a less than 15 min remaining warning if appropriate |
| 64 | CharSequence timeString = StringUtil.formatElapsedTime(context, |
| 65 | FIFTEEN_MINUTES_MILLIS, |
| 66 | false /* withSeconds */); |
| 67 | return getUnderFifteenString(context, timeString, percentageString); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 68 | } else if (drainTimeMs >= TWO_DAYS_MILLIS) { |
| 69 | // just say more than two day if over 48 hours |
| 70 | return getMoreThanTwoDaysString(context, percentageString); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 71 | } else if (drainTimeMs >= ONE_DAY_MILLIS) { |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 72 | // show remaining days & hours if more than a day |
| 73 | return getMoreThanOneDayString(context, drainTimeMs, |
| 74 | percentageString, basedOnUsage); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 75 | } else { |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 76 | // show the time of day we think you'll run out |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 77 | return getRegularTimeRemainingString(context, drainTimeMs, |
| 78 | percentageString, basedOnUsage); |
| 79 | } |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | private static String getShutdownImminentString(Context context, String percentageString) { |
| 85 | return TextUtils.isEmpty(percentageString) |
| 86 | ? context.getString(R.string.power_remaining_duration_only_shutdown_imminent) |
| 87 | : context.getString( |
| 88 | R.string.power_remaining_duration_shutdown_imminent, |
| 89 | percentageString); |
| 90 | } |
| 91 | |
| 92 | private static String getUnderFifteenString(Context context, CharSequence timeString, |
| 93 | String percentageString) { |
| 94 | return TextUtils.isEmpty(percentageString) |
| 95 | ? context.getString(R.string.power_remaining_less_than_duration_only, timeString) |
| 96 | : context.getString( |
| 97 | R.string.power_remaining_less_than_duration, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 98 | timeString, |
| 99 | percentageString); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 100 | |
| 101 | } |
| 102 | |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 103 | private static String getMoreThanOneDayString(Context context, long drainTimeMs, |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 104 | String percentageString, boolean basedOnUsage) { |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 105 | final long roundedTimeMs = roundToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 106 | CharSequence timeString = StringUtil.formatElapsedTime(context, |
| 107 | roundedTimeMs, |
| 108 | false /* withSeconds */); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 109 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 110 | if (TextUtils.isEmpty(percentageString)) { |
| 111 | int id = basedOnUsage |
| 112 | ? R.string.power_remaining_duration_only_enhanced |
| 113 | : R.string.power_remaining_duration_only; |
| 114 | return context.getString(id, timeString); |
| 115 | } else { |
| 116 | int id = basedOnUsage |
| 117 | ? R.string.power_discharging_duration_enhanced |
| 118 | : R.string.power_discharging_duration; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 119 | return context.getString(id, timeString, percentageString); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private static String getMoreThanTwoDaysString(Context context, String percentageString) { |
| 124 | final Locale currentLocale = context.getResources().getConfiguration().getLocales().get(0); |
| 125 | final MeasureFormat frmt = MeasureFormat.getInstance(currentLocale, FormatWidth.SHORT); |
| 126 | |
| 127 | final Measure daysMeasure = new Measure(2, MeasureUnit.DAY); |
| 128 | |
| 129 | return TextUtils.isEmpty(percentageString) |
| 130 | ? context.getString(R.string.power_remaining_only_more_than_subtext, |
| 131 | frmt.formatMeasures(daysMeasure)) |
| 132 | : context.getString( |
| 133 | R.string.power_remaining_more_than_subtext, |
| 134 | frmt.formatMeasures(daysMeasure), |
| 135 | percentageString); |
| 136 | } |
| 137 | |
| 138 | private static String getRegularTimeRemainingString(Context context, long drainTimeMs, |
| 139 | String percentageString, boolean basedOnUsage) { |
| 140 | // Get the time of day we think device will die rounded to the nearest 15 min. |
| 141 | final long roundedTimeOfDayMs = |
| 142 | roundToNearestThreshold( |
| 143 | System.currentTimeMillis() + drainTimeMs, |
| 144 | FIFTEEN_MINUTES_MILLIS); |
| 145 | |
| 146 | // convert the time to a properly formatted string. |
Salvador Martinez | b9e8cfa | 2018-04-05 11:05:40 -0700 | [diff] [blame] | 147 | String skeleton = android.text.format.DateFormat.getTimeFormatString(context); |
| 148 | DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 149 | Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs)); |
| 150 | CharSequence timeString = fmt.format(date); |
| 151 | |
| 152 | if (TextUtils.isEmpty(percentageString)) { |
| 153 | int id = basedOnUsage |
| 154 | ? R.string.power_discharge_by_only_enhanced |
| 155 | : R.string.power_discharge_by_only; |
| 156 | return context.getString(id, timeString); |
| 157 | } else { |
| 158 | int id = basedOnUsage |
| 159 | ? R.string.power_discharge_by_enhanced |
| 160 | : R.string.power_discharge_by; |
| 161 | return context.getString(id, timeString, percentageString); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | public static long convertUsToMs(long timeUs) { |
| 166 | return timeUs / 1000; |
| 167 | } |
| 168 | |
| 169 | public static long convertMsToUs(long timeMs) { |
| 170 | return timeMs * 1000; |
| 171 | } |
| 172 | |
| 173 | private static long roundToNearestThreshold(long drainTime, long threshold) { |
| 174 | final long remainder = drainTime % threshold; |
| 175 | if (remainder < threshold / 2) { |
| 176 | return drainTime - remainder; |
| 177 | } else { |
| 178 | return drainTime - remainder + threshold; |
| 179 | } |
| 180 | } |
| 181 | } |