blob: 43c97df24a58fa00b21ad2a23598dbed25705663 [file] [log] [blame]
Salvador Martinezeb9ab292018-01-19 17:50:24 -08001/*
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
17package com.android.settingslib.utils;
18
19import android.content.Context;
Salvador Martinezeec87362018-02-27 12:57:49 -080020import android.icu.text.DateFormat;
Salvador Martinezeb9ab292018-01-19 17:50:24 -080021import android.icu.text.MeasureFormat;
22import android.icu.text.MeasureFormat.FormatWidth;
23import android.icu.util.Measure;
24import android.icu.util.MeasureUnit;
Salvador Martinezeb9ab292018-01-19 17:50:24 -080025import android.text.TextUtils;
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -070026
Fan Zhangf7802ea2018-08-28 15:15:19 -070027import androidx.annotation.Nullable;
28
Salvador Martinezeb9ab292018-01-19 17:50:24 -080029import com.android.settingslib.R;
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -070030
Salvador Martinezeec87362018-02-27 12:57:49 -080031import java.time.Instant;
Salvador Martinezeec87362018-02-27 12:57:49 -080032import java.util.Date;
Salvador Martinezeb9ab292018-01-19 17:50:24 -080033import java.util.Locale;
34import java.util.concurrent.TimeUnit;
35
36/** Utility class for keeping power related strings consistent**/
37public class PowerUtil {
Salvador Martinezeec87362018-02-27 12:57:49 -080038
Salvador Martinezeb9ab292018-01-19 17:50:24 -080039 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 Martinezeec87362018-02-27 12:57:49 -080042 private static final long TWO_DAYS_MILLIS = TimeUnit.DAYS.toMillis(2);
43 private static final long ONE_HOUR_MILLIS = TimeUnit.HOURS.toMillis(1);
Salvador Martinezeb9ab292018-01-19 17:50:24 -080044
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 Martinezeec87362018-02-27 12:57:49 -080068 } else if (drainTimeMs >= TWO_DAYS_MILLIS) {
69 // just say more than two day if over 48 hours
70 return getMoreThanTwoDaysString(context, percentageString);
Salvador Martinezeb9ab292018-01-19 17:50:24 -080071 } else if (drainTimeMs >= ONE_DAY_MILLIS) {
Salvador Martinezeec87362018-02-27 12:57:49 -080072 // show remaining days & hours if more than a day
73 return getMoreThanOneDayString(context, drainTimeMs,
74 percentageString, basedOnUsage);
Salvador Martinezeb9ab292018-01-19 17:50:24 -080075 } else {
Salvador Martinezeec87362018-02-27 12:57:49 -080076 // show the time of day we think you'll run out
Salvador Martinezeb9ab292018-01-19 17:50:24 -080077 return getRegularTimeRemainingString(context, drainTimeMs,
78 percentageString, basedOnUsage);
79 }
80 }
81 return null;
82 }
83
Evan Laird4bf21df2018-10-22 14:24:32 -040084 /**
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 Martinezeb9ab292018-01-19 17:50:24 -0800108 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 Martinezeec87362018-02-27 12:57:49 -0800122 timeString,
123 percentageString);
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800124
125 }
126
Salvador Martinezeec87362018-02-27 12:57:49 -0800127 private static String getMoreThanOneDayString(Context context, long drainTimeMs,
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800128 String percentageString, boolean basedOnUsage) {
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -0700129 final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS);
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800130 CharSequence timeString = StringUtil.formatElapsedTime(context,
131 roundedTimeMs,
132 false /* withSeconds */);
Salvador Martinezeec87362018-02-27 12:57:49 -0800133
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800134 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 Martinezeec87362018-02-27 12:57:49 -0800143 return context.getString(id, timeString, percentageString);
144 }
145 }
146
Evan Laird4bf21df2018-10-22 14:24:32 -0400147 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 Martinezeec87362018-02-27 12:57:49 -0800155 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 Martinez2ee2b0a2018-04-13 13:34:46 -0700174 roundTimeToNearestThreshold(
Salvador Martinezeec87362018-02-27 12:57:49 -0800175 System.currentTimeMillis() + drainTimeMs,
176 FIFTEEN_MINUTES_MILLIS);
177
178 // convert the time to a properly formatted string.
Salvador Martinezb9e8cfa2018-04-05 11:05:40 -0700179 String skeleton = android.text.format.DateFormat.getTimeFormatString(context);
180 DateFormat fmt = DateFormat.getInstanceForSkeleton(skeleton);
Salvador Martinezeec87362018-02-27 12:57:49 -0800181 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 Martinezeb9ab292018-01-19 17:50:24 -0800194 }
195 }
196
Evan Laird4bf21df2018-10-22 14:24:32 -0400197 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 Martinezeb9ab292018-01-19 17:50:24 -0800213 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 Martinez2ee2b0a2018-04-13 13:34:46 -0700221 /**
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 Martinezeb9ab292018-01-19 17:50:24 -0800237 } else {
Salvador Martinez2ee2b0a2018-04-13 13:34:46 -0700238 return time - remainder + multiple;
Salvador Martinezeb9ab292018-01-19 17:50:24 -0800239 }
240 }
241}