Loading packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java +30 −6 Original line number Diff line number Diff line Loading @@ -107,8 +107,9 @@ public class StringUtil { } /** * Returns relative time for the given millis in the past, in a short format such as "2 days * ago", "5 hr. ago", "40 min. ago", or "29 sec. ago". * Returns relative time for the given millis in the past with different format style. * In a short format such as "2 days ago", "5 hr. ago", "40 min. ago", or "29 sec. ago". * In a long format such as "2 days ago", "5 hours ago", "40 minutes ago" or "29 seconds ago". * * <p>The unit is chosen to have good information value while only using one unit. So 27 hours * and 50 minutes would be formatted as "28 hr. ago", while 50 hours would be formatted as Loading @@ -117,10 +118,11 @@ public class StringUtil { * @param context the application context * @param millis the elapsed time in milli seconds * @param withSeconds include seconds? * @param formatStyle format style * @return the formatted elapsed time */ public static CharSequence formatRelativeTime(Context context, double millis, boolean withSeconds) { boolean withSeconds, RelativeDateTimeFormatter.Style formatStyle) { final int seconds = (int) Math.floor(millis / 1000); final RelativeUnit unit; final int value; Loading @@ -144,9 +146,31 @@ public class StringUtil { final RelativeDateTimeFormatter formatter = RelativeDateTimeFormatter.getInstance( ULocale.forLocale(locale), null /* default NumberFormat */, RelativeDateTimeFormatter.Style.LONG, formatStyle, android.icu.text.DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE); return formatter.format(value, RelativeDateTimeFormatter.Direction.LAST, unit); } /** * Returns relative time for the given millis in the past, in a long format such as "2 days * ago", "5 hours ago", "40 minutes ago" or "29 seconds ago". * * <p>The unit is chosen to have good information value while only using one unit. So 27 hours * and 50 minutes would be formatted as "28 hr. ago", while 50 hours would be formatted as * "2 days ago". * * @param context the application context * @param millis the elapsed time in milli seconds * @param withSeconds include seconds? * @return the formatted elapsed time * @deprecated use {@link #formatRelativeTime(Context, double, boolean, * RelativeDateTimeFormatter.Style)} instead. */ @Deprecated public static CharSequence formatRelativeTime(Context context, double millis, boolean withSeconds) { return formatRelativeTime(context, millis, withSeconds, RelativeDateTimeFormatter.Style.LONG); } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/StringUtilTest.java +23 −22 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; import android.content.Context; import android.icu.text.RelativeDateTimeFormatter; import android.text.SpannableStringBuilder; import android.text.format.DateUtils; import android.text.style.TtsSpan; Loading Loading @@ -116,8 +117,8 @@ public class StringUtilTest { final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -125,8 +126,8 @@ public class StringUtilTest { final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "1 minute ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -134,8 +135,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -143,8 +144,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "2 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -152,8 +153,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.MINUTE_IN_MILLIS; final String expectedTime = "2 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -161,8 +162,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.MINUTE_IN_MILLIS; final String expectedTime = "119 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -170,8 +171,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.HOUR_IN_MILLIS; final String expectedTime = "2 hours ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -179,8 +180,8 @@ public class StringUtilTest { final double testMillis = 47 * DateUtils.HOUR_IN_MILLIS; final String expectedTime = "47 hours ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -188,8 +189,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.DAY_IN_MILLIS; final String expectedTime = "2 days ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -197,8 +198,8 @@ public class StringUtilTest { final double testMillis = 0; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -206,7 +207,7 @@ public class StringUtilTest { final double testMillis = 0; final String expectedTime = "0 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } } Loading
packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java +30 −6 Original line number Diff line number Diff line Loading @@ -107,8 +107,9 @@ public class StringUtil { } /** * Returns relative time for the given millis in the past, in a short format such as "2 days * ago", "5 hr. ago", "40 min. ago", or "29 sec. ago". * Returns relative time for the given millis in the past with different format style. * In a short format such as "2 days ago", "5 hr. ago", "40 min. ago", or "29 sec. ago". * In a long format such as "2 days ago", "5 hours ago", "40 minutes ago" or "29 seconds ago". * * <p>The unit is chosen to have good information value while only using one unit. So 27 hours * and 50 minutes would be formatted as "28 hr. ago", while 50 hours would be formatted as Loading @@ -117,10 +118,11 @@ public class StringUtil { * @param context the application context * @param millis the elapsed time in milli seconds * @param withSeconds include seconds? * @param formatStyle format style * @return the formatted elapsed time */ public static CharSequence formatRelativeTime(Context context, double millis, boolean withSeconds) { boolean withSeconds, RelativeDateTimeFormatter.Style formatStyle) { final int seconds = (int) Math.floor(millis / 1000); final RelativeUnit unit; final int value; Loading @@ -144,9 +146,31 @@ public class StringUtil { final RelativeDateTimeFormatter formatter = RelativeDateTimeFormatter.getInstance( ULocale.forLocale(locale), null /* default NumberFormat */, RelativeDateTimeFormatter.Style.LONG, formatStyle, android.icu.text.DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE); return formatter.format(value, RelativeDateTimeFormatter.Direction.LAST, unit); } /** * Returns relative time for the given millis in the past, in a long format such as "2 days * ago", "5 hours ago", "40 minutes ago" or "29 seconds ago". * * <p>The unit is chosen to have good information value while only using one unit. So 27 hours * and 50 minutes would be formatted as "28 hr. ago", while 50 hours would be formatted as * "2 days ago". * * @param context the application context * @param millis the elapsed time in milli seconds * @param withSeconds include seconds? * @return the formatted elapsed time * @deprecated use {@link #formatRelativeTime(Context, double, boolean, * RelativeDateTimeFormatter.Style)} instead. */ @Deprecated public static CharSequence formatRelativeTime(Context context, double millis, boolean withSeconds) { return formatRelativeTime(context, millis, withSeconds, RelativeDateTimeFormatter.Style.LONG); } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/StringUtilTest.java +23 −22 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.spy; import android.content.Context; import android.icu.text.RelativeDateTimeFormatter; import android.text.SpannableStringBuilder; import android.text.format.DateUtils; import android.text.style.TtsSpan; Loading Loading @@ -116,8 +117,8 @@ public class StringUtilTest { final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -125,8 +126,8 @@ public class StringUtilTest { final double testMillis = 40 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "1 minute ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -134,8 +135,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -143,8 +144,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.SECOND_IN_MILLIS; final String expectedTime = "2 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -152,8 +153,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.MINUTE_IN_MILLIS; final String expectedTime = "2 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -161,8 +162,8 @@ public class StringUtilTest { final double testMillis = 119 * DateUtils.MINUTE_IN_MILLIS; final String expectedTime = "119 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -170,8 +171,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.HOUR_IN_MILLIS; final String expectedTime = "2 hours ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -179,8 +180,8 @@ public class StringUtilTest { final double testMillis = 47 * DateUtils.HOUR_IN_MILLIS; final String expectedTime = "47 hours ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -188,8 +189,8 @@ public class StringUtilTest { final double testMillis = 2 * DateUtils.DAY_IN_MILLIS; final String expectedTime = "2 days ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -197,8 +198,8 @@ public class StringUtilTest { final double testMillis = 0; final String expectedTime = "Just now"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, true, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } @Test Loading @@ -206,7 +207,7 @@ public class StringUtilTest { final double testMillis = 0; final String expectedTime = "0 minutes ago"; assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false).toString()).isEqualTo( expectedTime); assertThat(StringUtil.formatRelativeTime(mContext, testMillis, false, RelativeDateTimeFormatter.Style.LONG).toString()).isEqualTo(expectedTime); } }