Loading packages/SettingsLib/res/values/strings.xml +2 −0 Original line number Diff line number Diff line Loading @@ -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> Loading packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +42 −14 Original line number Diff line number Diff line Loading @@ -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); } } Loading Loading @@ -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) { Loading @@ -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 Loading @@ -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); Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java +19 −0 Original line number Diff line number Diff line Loading @@ -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%)') Loading Loading @@ -175,6 +176,24 @@ public class PowerUtilTest { assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); } @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 Loading Loading
packages/SettingsLib/res/values/strings.xml +2 −0 Original line number Diff line number Diff line Loading @@ -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> Loading
packages/SettingsLib/src/com/android/settingslib/utils/PowerUtil.java +42 −14 Original line number Diff line number Diff line Loading @@ -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); } } Loading Loading @@ -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) { Loading @@ -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 Loading @@ -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); Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/PowerUtilTest.java +19 −0 Original line number Diff line number Diff line Loading @@ -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%)') Loading Loading @@ -175,6 +176,24 @@ public class PowerUtilTest { assertThat(info2).isEqualTo("More than 2 days remaining (10%)"); } @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 Loading