Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2389bf4 authored by tmfang's avatar tmfang
Browse files

Format relative time with specific style

In some cases, we expect the relative time in a short
format. So, we make this method can input a format style.

Fixes: 126871866
Test: visual
Change-Id: Iba5fb8d3acbf357b8c7d54065abf3e2fd74fac03
parent d20bf0ef
Loading
Loading
Loading
Loading
+30 −6
Original line number Diff line number Diff line
@@ -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
@@ -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;
@@ -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);
    }
}
+23 −22
Original line number Diff line number Diff line
@@ -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;
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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);
    }
}