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; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 25 | import android.text.TextUtils; |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 26 | |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 27 | import androidx.annotation.Nullable; |
| 28 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 29 | import com.android.settingslib.R; |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 30 | |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 31 | import java.time.Instant; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 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 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 84 | /** |
| 85 | * Method to produce a shortened string describing the remaining battery. Suitable for Quick |
| 86 | * Settings and other areas where space is constrained. |
| 87 | * |
| 88 | * @param context context to fetch descriptions from |
| 89 | * @param drainTimeMs The estimated time remaining before the phone dies in milliseconds. |
| 90 | * |
| 91 | * @return a properly formatted and localized short string describing how much time remains |
| 92 | * before the battery runs out. |
| 93 | */ |
| 94 | @Nullable |
| 95 | public static String getBatteryRemainingShortStringFormatted( |
| 96 | Context context, long drainTimeMs) { |
| 97 | if (drainTimeMs <= 0) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | if (drainTimeMs <= ONE_DAY_MILLIS) { |
| 102 | return getRegularTimeRemainingShortString(context, drainTimeMs); |
| 103 | } else { |
| 104 | return getMoreThanOneDayShortString(context, drainTimeMs); |
| 105 | } |
| 106 | } |
| 107 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 108 | private static String getShutdownImminentString(Context context, String percentageString) { |
| 109 | return TextUtils.isEmpty(percentageString) |
| 110 | ? context.getString(R.string.power_remaining_duration_only_shutdown_imminent) |
| 111 | : context.getString( |
| 112 | R.string.power_remaining_duration_shutdown_imminent, |
| 113 | percentageString); |
| 114 | } |
| 115 | |
| 116 | private static String getUnderFifteenString(Context context, CharSequence timeString, |
| 117 | String percentageString) { |
| 118 | return TextUtils.isEmpty(percentageString) |
| 119 | ? context.getString(R.string.power_remaining_less_than_duration_only, timeString) |
| 120 | : context.getString( |
| 121 | R.string.power_remaining_less_than_duration, |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 122 | timeString, |
| 123 | percentageString); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 124 | |
| 125 | } |
| 126 | |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 127 | private static String getMoreThanOneDayString(Context context, long drainTimeMs, |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 128 | String percentageString, boolean basedOnUsage) { |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 129 | final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 130 | CharSequence timeString = StringUtil.formatElapsedTime(context, |
| 131 | roundedTimeMs, |
| 132 | false /* withSeconds */); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 133 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 134 | if (TextUtils.isEmpty(percentageString)) { |
| 135 | int id = basedOnUsage |
| 136 | ? R.string.power_remaining_duration_only_enhanced |
| 137 | : R.string.power_remaining_duration_only; |
| 138 | return context.getString(id, timeString); |
| 139 | } else { |
| 140 | int id = basedOnUsage |
| 141 | ? R.string.power_discharging_duration_enhanced |
| 142 | : R.string.power_discharging_duration; |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 143 | return context.getString(id, timeString, percentageString); |
| 144 | } |
| 145 | } |
| 146 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 147 | private static String getMoreThanOneDayShortString(Context context, long drainTimeMs) { |
| 148 | final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); |
| 149 | CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs, |
| 150 | false /* withSeconds */); |
| 151 | |
| 152 | return context.getString(R.string.power_remaining_duration_only_short, timeString); |
| 153 | } |
| 154 | |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 155 | private static String getMoreThanTwoDaysString(Context context, String percentageString) { |
| 156 | final Locale currentLocale = context.getResources().getConfiguration().getLocales().get(0); |
| 157 | final MeasureFormat frmt = MeasureFormat.getInstance(currentLocale, FormatWidth.SHORT); |
| 158 | |
| 159 | final Measure daysMeasure = new Measure(2, MeasureUnit.DAY); |
| 160 | |
| 161 | return TextUtils.isEmpty(percentageString) |
| 162 | ? context.getString(R.string.power_remaining_only_more_than_subtext, |
| 163 | frmt.formatMeasures(daysMeasure)) |
| 164 | : context.getString( |
| 165 | R.string.power_remaining_more_than_subtext, |
| 166 | frmt.formatMeasures(daysMeasure), |
| 167 | percentageString); |
| 168 | } |
| 169 | |
| 170 | private static String getRegularTimeRemainingString(Context context, long drainTimeMs, |
| 171 | String percentageString, boolean basedOnUsage) { |
| 172 | // Get the time of day we think device will die rounded to the nearest 15 min. |
| 173 | final long roundedTimeOfDayMs = |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 174 | roundTimeToNearestThreshold( |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 175 | System.currentTimeMillis() + drainTimeMs, |
| 176 | FIFTEEN_MINUTES_MILLIS); |
| 177 | |
| 178 | // convert the time to a properly formatted string. |
Salvador Martinez | b9e8cfa | 2018-04-05 11:05:40 -0700 | [diff] [blame] | 179 | String skeleton = android.text.format.DateFormat.getTimeFormatString(context); |
| 180 | DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton); |
Salvador Martinez | eec8736 | 2018-02-27 12:57:49 -0800 | [diff] [blame] | 181 | Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs)); |
| 182 | CharSequence timeString = fmt.format(date); |
| 183 | |
| 184 | if (TextUtils.isEmpty(percentageString)) { |
| 185 | int id = basedOnUsage |
| 186 | ? R.string.power_discharge_by_only_enhanced |
| 187 | : R.string.power_discharge_by_only; |
| 188 | return context.getString(id, timeString); |
| 189 | } else { |
| 190 | int id = basedOnUsage |
| 191 | ? R.string.power_discharge_by_enhanced |
| 192 | : R.string.power_discharge_by; |
| 193 | return context.getString(id, timeString, percentageString); |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 197 | private static String getRegularTimeRemainingShortString(Context context, long drainTimeMs) { |
| 198 | // Get the time of day we think device will die rounded to the nearest 15 min. |
| 199 | final long roundedTimeOfDayMs = |
| 200 | roundTimeToNearestThreshold( |
| 201 | System.currentTimeMillis() + drainTimeMs, |
| 202 | FIFTEEN_MINUTES_MILLIS); |
| 203 | |
| 204 | // convert the time to a properly formatted string. |
| 205 | String skeleton = android.text.format.DateFormat.getTimeFormatString(context); |
| 206 | DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton); |
| 207 | Date date = Date.from(Instant.ofEpochMilli(roundedTimeOfDayMs)); |
| 208 | CharSequence timeString = fmt.format(date); |
| 209 | |
| 210 | return context.getString(R.string.power_discharge_by_only_short, timeString); |
| 211 | } |
| 212 | |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 213 | public static long convertUsToMs(long timeUs) { |
| 214 | return timeUs / 1000; |
| 215 | } |
| 216 | |
| 217 | public static long convertMsToUs(long timeMs) { |
| 218 | return timeMs * 1000; |
| 219 | } |
| 220 | |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Rounds a time to the nearest multiple of the provided threshold. Note: This function takes |
| 223 | * the absolute value of the inputs since it is only meant to be used for times, not general |
| 224 | * purpose rounding. |
| 225 | * |
| 226 | * ex: roundTimeToNearestThreshold(41, 24) = 48 |
| 227 | * @param drainTime The amount to round |
| 228 | * @param threshold The value to round to a multiple of |
| 229 | * @return The rounded value as a long |
| 230 | */ |
| 231 | public static long roundTimeToNearestThreshold(long drainTime, long threshold) { |
| 232 | long time = Math.abs(drainTime); |
| 233 | long multiple = Math.abs(threshold); |
| 234 | final long remainder = time % multiple; |
| 235 | if (remainder < multiple / 2) { |
| 236 | return time - remainder; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 237 | } else { |
Salvador Martinez | 2ee2b0a | 2018-04-13 13:34:46 -0700 | [diff] [blame] | 238 | return time - remainder + multiple; |
Salvador Martinez | eb9ab29 | 2018-01-19 17:50:24 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |