diff options
3 files changed, 63 insertions, 14 deletions
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml index f4c7275a828a..c60e8c321ec5 100644 --- a/packages/SettingsLib/res/values/strings.xml +++ b/packages/SettingsLib/res/values/strings.xml @@ -950,6 +950,8 @@ <string name="power_discharge_by_only">Should last until about <xliff:g id="time">%1$s</xliff:g></string> <!-- [CHAR_LIMIT=100] Label for estimated time that phone will run out of battery --> <string name="power_discharge_by_only_short">Until <xliff:g id="time" example="12 PM">%1$s</xliff:g></string> + <!-- [CHAR_LIMIT=100] Extend the battery life past a certain time --> + <string name="power_suggestion_extend_battery">Extend battery life past <xliff:g id="time" example="12 PM">%1$s</xliff:g></string> <!-- [CHAR_LIMIT=60] label for estimated remaining duration of battery when under a certain amount --> <string name="power_remaining_less_than_duration_only">Less than <xliff:g id="threshold">%1$s</xliff:g> remaining</string> diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java index 52d7e2cedeb7..5600dd279b95 100644 --- a/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +++ b/packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java @@ -101,7 +101,29 @@ public class PowerUtil { if (drainTimeMs <= ONE_DAY_MILLIS) { return getRegularTimeRemainingShortString(context, drainTimeMs); } else { - return getMoreThanOneDayShortString(context, drainTimeMs); + return getMoreThanOneDayShortString(context, drainTimeMs, + R.string.power_remaining_duration_only_short); + } + } + + /** + * This method produces the text used in Settings battery tip to describe the effect after + * use the tip. + * + * @param context + * @param drainTimeMs The estimated time remaining before the phone dies in milliseconds. + * @return a properly formatted and localized string + */ + public static String getBatteryTipStringFormatted(Context context, long drainTimeMs) { + if (drainTimeMs <= 0) { + return null; + } + if (drainTimeMs <= ONE_DAY_MILLIS) { + return context.getString(R.string.power_suggestion_extend_battery, + getDateTimeStringFromMs(context, drainTimeMs)); + } else { + return getMoreThanOneDayShortString(context, drainTimeMs, + R.string.power_remaining_only_more_than_subtext); } } @@ -144,12 +166,13 @@ public class PowerUtil { } } - private static String getMoreThanOneDayShortString(Context context, long drainTimeMs) { + private static String getMoreThanOneDayShortString(Context context, long drainTimeMs, + int resId) { final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS); CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs, false /* withSeconds */); - return context.getString(R.string.power_remaining_duration_only_short, timeString); + return context.getString(resId, timeString); } private static String getMoreThanTwoDaysString(Context context, String percentageString) { @@ -169,17 +192,8 @@ public class PowerUtil { private static String getRegularTimeRemainingString(Context context, long drainTimeMs, String percentageString, boolean basedOnUsage) { - // 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); - // 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); + CharSequence timeString = getDateTimeStringFromMs(context, drainTimeMs); if (TextUtils.isEmpty(percentageString)) { int id = basedOnUsage @@ -194,6 +208,20 @@ public class PowerUtil { } } + private static CharSequence getDateTimeStringFromMs(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); + + // 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)); + return fmt.format(date); + } + private static String getRegularTimeRemainingShortString(Context context, long drainTimeMs) { // Get the time remaining rounded to the nearest 15 min final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, FIFTEEN_MINUTES_MILLIS); @@ -231,4 +259,4 @@ public class PowerUtil { return time - remainder + multiple; } } -} +}
\ No newline at end of file diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java index 7ef31df6ab26..61fdbd54ead1 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java @@ -42,6 +42,7 @@ public class PowerUtilTest { private static final long THIRTY_HOURS_MILLIS = Duration.ofHours(30).toMillis(); private static final String NORMAL_CASE_EXPECTED_PREFIX = "Should last until about"; private static final String ENHANCED_SUFFIX = " based on your usage"; + private static final String EXTEND_PREFIX = "Extend battery life past"; // matches a time (ex: '1:15 PM', '2 AM', '23:00') private static final String TIME_OF_DAY_REGEX = " (\\d)+:?(\\d)* ((AM)*)|((PM)*)"; // matches a percentage with parenthesis (ex: '(10%)') @@ -176,6 +177,24 @@ public class PowerUtilTest { } @Test + public void getBatteryTipStringFormatted_moreThanOneDay_usesCorrectString() { + String info = PowerUtil.getBatteryTipStringFormatted(mContext, + THREE_DAYS_MILLIS); + + assertThat(info).isEqualTo("More than 3 days remaining"); + } + + @Test + public void getBatteryTipStringFormatted_lessThanOneDay_usesCorrectString() { + String info = PowerUtil.getBatteryTipStringFormatted(mContext, + SEVENTEEN_MIN_MILLIS); + + // ex: Extend battery life past 1:15 PM + assertThat(info).containsMatch(Pattern.compile( + EXTEND_PREFIX + TIME_OF_DAY_REGEX)); + } + + @Test public void testRoundToNearestThreshold_roundsCorrectly() { // test some pretty normal values assertThat(PowerUtil.roundTimeToNearestThreshold(1200, 1000)).isEqualTo(1000); |