Update StringUtil#formatElapsedTime method (1/3)

- Add new flag to control the output format, make it present top 2
 time unit

Bug: 183689347
Test: make RunSettingsLibRoboTests
Change-Id: Ib2db11088ee0bb20ca8bf883dfb53c0c7d28afbf
This commit is contained in:
Wesley.CW Wang
2021-04-01 19:05:39 +08:00
committed by Wesley Wang
parent 02841faaf6
commit e3b41cf669
4 changed files with 69 additions and 16 deletions

View File

@@ -41,6 +41,7 @@ public class PowerUtil {
private static final long ONE_DAY_MILLIS = TimeUnit.DAYS.toMillis(1);
private static final long TWO_DAYS_MILLIS = TimeUnit.DAYS.toMillis(2);
private static final long ONE_HOUR_MILLIS = TimeUnit.HOURS.toMillis(1);
private static final long ONE_MIN_MILLIS = TimeUnit.MINUTES.toMillis(1);
/**
* This method produces the text used in various places throughout the system to describe the
@@ -63,7 +64,7 @@ public class PowerUtil {
// show a less than 15 min remaining warning if appropriate
CharSequence timeString = StringUtil.formatElapsedTime(context,
FIFTEEN_MINUTES_MILLIS,
false /* withSeconds */);
false /* withSeconds */, false /* collapseTimeUnit */);
return getUnderFifteenString(context, timeString, percentageString);
} else if (drainTimeMs >= TWO_DAYS_MILLIS) {
// just say more than two day if over 48 hours
@@ -151,7 +152,7 @@ public class PowerUtil {
final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS);
CharSequence timeString = StringUtil.formatElapsedTime(context,
roundedTimeMs,
false /* withSeconds */);
false /* withSeconds */, true /* collapseTimeUnit */);
if (TextUtils.isEmpty(percentageString)) {
int id = basedOnUsage
@@ -170,7 +171,7 @@ public class PowerUtil {
int resId) {
final long roundedTimeMs = roundTimeToNearestThreshold(drainTimeMs, ONE_HOUR_MILLIS);
CharSequence timeString = StringUtil.formatElapsedTime(context, roundedTimeMs,
false /* withSeconds */);
false /* withSeconds */, false /* collapseTimeUnit */);
return context.getString(resId, timeString);
}
@@ -193,17 +194,18 @@ public class PowerUtil {
private static String getRegularTimeRemainingString(Context context, long drainTimeMs,
String percentageString, boolean basedOnUsage) {
CharSequence timeString = getDateTimeStringFromMs(context, drainTimeMs);
CharSequence timeString = StringUtil.formatElapsedTime(context,
drainTimeMs, false /* withSeconds */, true /* collapseTimeUnit */);
if (TextUtils.isEmpty(percentageString)) {
int id = basedOnUsage
? R.string.power_discharge_by_only_enhanced
: R.string.power_discharge_by_only;
? R.string.power_remaining_duration_only_enhanced
: R.string.power_remaining_duration_only;
return context.getString(id, timeString);
} else {
int id = basedOnUsage
? R.string.power_discharge_by_enhanced
: R.string.power_discharge_by;
? R.string.power_discharging_duration_enhanced
: R.string.power_discharging_duration;
return context.getString(id, timeString, percentageString);
}
}

View File

@@ -40,6 +40,8 @@ public class StringUtil {
public static final int SECONDS_PER_HOUR = 60 * 60;
public static final int SECONDS_PER_DAY = 24 * 60 * 60;
private static final int LIMITED_TIME_UNIT_COUNT = 2;
/**
* Returns elapsed time for the given millis, in the following format:
* 2 days, 5 hr, 40 min, 29 sec
@@ -47,10 +49,12 @@ public class StringUtil {
* @param context the application context
* @param millis the elapsed time in milli seconds
* @param withSeconds include seconds?
* @param collapseTimeUnit limit the output to top 2 time unit
* e.g 2 days, 5 hr, 40 min, 29 sec will convert to 2 days, 5 hr
* @return the formatted elapsed time
*/
public static CharSequence formatElapsedTime(Context context, double millis,
boolean withSeconds) {
boolean withSeconds, boolean collapseTimeUnit) {
SpannableStringBuilder sb = new SpannableStringBuilder();
int seconds = (int) Math.floor(millis / 1000);
if (!withSeconds) {
@@ -89,6 +93,12 @@ public class StringUtil {
// Everything addable was zero, so nothing was added. We add a zero.
measureList.add(new Measure(0, withSeconds ? MeasureUnit.SECOND : MeasureUnit.MINUTE));
}
if (collapseTimeUnit && measureList.size() > LIMITED_TIME_UNIT_COUNT) {
// Limit the output to top 2 time unit.
measureList.subList(LIMITED_TIME_UNIT_COUNT, measureList.size()).clear();
}
final Measure[] measureArray = measureList.toArray(new Measure[measureList.size()]);
final Locale locale = context.getResources().getConfiguration().locale;

View File

@@ -35,10 +35,12 @@ import java.util.regex.Pattern;
@RunWith(RobolectricTestRunner.class)
public class PowerUtilTest {
private static final String TEST_BATTERY_LEVEL_10 = "10%";
private static final long TEN_SEC_MILLIS = Duration.ofSeconds(10).toMillis();
private static final long SEVENTEEN_MIN_MILLIS = Duration.ofMinutes(17).toMillis();
private static final long FIVE_MINUTES_MILLIS = Duration.ofMinutes(5).toMillis();
private static final long TEN_MINUTES_MILLIS = Duration.ofMinutes(10).toMillis();
private static final long THREE_DAYS_MILLIS = Duration.ofDays(3).toMillis();
private static final long TEN_HOURS_MILLIS = Duration.ofHours(10).toMillis();
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";
@@ -152,11 +154,40 @@ public class PowerUtilTest {
THIRTY_HOURS_MILLIS,
TEST_BATTERY_LEVEL_10 /* percentageString */,
false /* basedOnUsage */);
String info3 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
THIRTY_HOURS_MILLIS + TEN_MINUTES_MILLIS,
null /* percentageString */,
false /* basedOnUsage */);
// We only add special mention for the long string
assertThat(info).isEqualTo("About 1 day, 6 hr left based on your usage");
// shortened string should not have extra text
assertThat(info2).isEqualTo("About 1 day, 6 hr left (10%)");
// present 2 time unit at most
assertThat(info3).isEqualTo("About 1 day, 6 hr left");
}
@Test
public void testGetBatteryRemainingStringFormatted_lessThanOneDay_usesCorrectString() {
String info = PowerUtil.getBatteryRemainingStringFormatted(mContext,
TEN_HOURS_MILLIS,
null /* percentageString */,
true /* basedOnUsage */);
String info2 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
TEN_HOURS_MILLIS,
TEST_BATTERY_LEVEL_10 /* percentageString */,
false /* basedOnUsage */);
String info3 = PowerUtil.getBatteryRemainingStringFormatted(mContext,
TEN_HOURS_MILLIS + TEN_MINUTES_MILLIS + TEN_SEC_MILLIS,
null /* percentageString */,
false /* basedOnUsage */);
// We only add special mention for the long string
assertThat(info).isEqualTo("About 10 hr left based on your usage");
// shortened string should not have extra text
assertThat(info2).isEqualTo("About 10 hr left (10%)");
// present 2 time unit at most
assertThat(info3).isEqualTo("About 10 hr, 10 min left");
}
@Test

View File

@@ -46,7 +46,7 @@ public class StringUtilTest {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS + 30 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "5 min, 30 sec";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, true).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, true, false).toString())
.isEqualTo(expectedTime);
}
@@ -55,7 +55,7 @@ public class StringUtilTest {
final double testMillis = 5 * DateUtils.MINUTE_IN_MILLIS + 30 * DateUtils.SECOND_IN_MILLIS;
final String expectedTime = "6 min";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false, false).toString())
.isEqualTo(expectedTime);
}
@@ -65,7 +65,17 @@ public class StringUtilTest {
+ 4 * DateUtils.HOUR_IN_MILLIS + 15 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2 days, 4 hr, 15 min";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false, false).toString())
.isEqualTo(expectedTime);
}
@Test
public void testFormatElapsedTime_TimeMoreThanOneDayAndCollapseTimeUnit_ShowCorrectly() {
final double testMillis = 2 * DateUtils.DAY_IN_MILLIS
+ 4 * DateUtils.HOUR_IN_MILLIS + 15 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2 days, 4 hr";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false, true).toString())
.isEqualTo(expectedTime);
}
@@ -74,7 +84,7 @@ public class StringUtilTest {
final double testMillis = 2 * DateUtils.DAY_IN_MILLIS + 15 * DateUtils.MINUTE_IN_MILLIS;
final String expectedTime = "2 days, 15 min";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false, false).toString())
.isEqualTo(expectedTime);
}
@@ -83,7 +93,7 @@ public class StringUtilTest {
final double testMillis = 0;
final String expectedTime = "0 sec";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, true).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, true, false).toString())
.isEqualTo(expectedTime);
}
@@ -92,7 +102,7 @@ public class StringUtilTest {
final double testMillis = 0;
final String expectedTime = "0 min";
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false).toString())
assertThat(StringUtil.formatElapsedTime(mContext, testMillis, false, false).toString())
.isEqualTo(expectedTime);
}
@@ -101,7 +111,7 @@ public class StringUtilTest {
final double testMillis = 15 * DateUtils.MINUTE_IN_MILLIS;
final CharSequence charSequence =
StringUtil.formatElapsedTime(mContext, testMillis, false);
StringUtil.formatElapsedTime(mContext, testMillis, false, false);
assertThat(charSequence).isInstanceOf(SpannableStringBuilder.class);
final SpannableStringBuilder expectedString = (SpannableStringBuilder) charSequence;