Search in sources :

Example 26 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testOldFormatWithList.

@Test
public void testOldFormatWithList() {
    List<Measure> measures = new ArrayList<Measure>(2);
    measures.add(new Measure(5, MeasureUnit.ACRE));
    measures.add(new Measure(3000, MeasureUnit.SQUARE_FOOT));
    MeasureFormat fmt = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE);
    assertEquals("", "5 acres, 3,000 square feet", fmt.format(measures));
    assertEquals("", "5 acres", fmt.format(measures.subList(0, 1)));
    List<String> badList = new ArrayList<String>();
    badList.add("be");
    badList.add("you");
    try {
        fmt.format(badList);
        fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
}
Also used : Measure(android.icu.util.Measure) ArrayList(java.util.ArrayList) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 27 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testOldFormatWithArray.

@Test
public void testOldFormatWithArray() {
    Measure[] measures = new Measure[] { new Measure(5, MeasureUnit.ACRE), new Measure(3000, MeasureUnit.SQUARE_FOOT) };
    MeasureFormat fmt = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE);
    assertEquals("", "5 acres, 3,000 square feet", fmt.format(measures));
}
Also used : Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 28 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testNumeratorPlurals.

@Test
public void testNumeratorPlurals() {
    ULocale polish = new ULocale("pl");
    Object[][] data = new Object[][] { { 1, "1 stopa na sekundę" }, { 2, "2 stopy na sekundę" }, { 5, "5 stóp na sekundę" }, { 1.5, "1,5 stopy na sekundę" } };
    for (Object[] row : data) {
        MeasureFormat mf = MeasureFormat.getInstance(polish, FormatWidth.WIDE);
        assertEquals("", row[1], mf.formatMeasurePerUnit(new Measure((Number) row[0], MeasureUnit.FOOT), MeasureUnit.SECOND, new StringBuilder(), new FieldPosition(0)).toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) Measure(android.icu.util.Measure) FieldPosition(java.text.FieldPosition) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 29 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testGram.

@Test
public void testGram() {
    MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT);
    assertEquals("testGram", "1 g", mf.format(new Measure(1, MeasureUnit.GRAM)));
    assertEquals("testGram", "1 G", mf.format(new Measure(1, MeasureUnit.G_FORCE)));
}
Also used : Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 30 with Measure

use of android.icu.util.Measure in project android_packages_apps_Settings by SudaMod.

the class Utils method formatElapsedTime.

/**
 * Returns elapsed time for the given millis, in the following format:
 * 2d 5h 40m 29s
 * @param context the application context
 * @param millis the elapsed time in milli seconds
 * @param withSeconds include seconds?
 * @return the formatted elapsed time
 */
public static CharSequence formatElapsedTime(Context context, double millis, boolean withSeconds) {
    SpannableStringBuilder sb = new SpannableStringBuilder();
    int seconds = (int) Math.floor(millis / 1000);
    if (!withSeconds) {
        // Round up.
        seconds += 30;
    }
    int days = 0, hours = 0, minutes = 0;
    if (seconds >= SECONDS_PER_DAY) {
        days = seconds / SECONDS_PER_DAY;
        seconds -= days * SECONDS_PER_DAY;
    }
    if (seconds >= SECONDS_PER_HOUR) {
        hours = seconds / SECONDS_PER_HOUR;
        seconds -= hours * SECONDS_PER_HOUR;
    }
    if (seconds >= SECONDS_PER_MINUTE) {
        minutes = seconds / SECONDS_PER_MINUTE;
        seconds -= minutes * SECONDS_PER_MINUTE;
    }
    final ArrayList<Measure> measureList = new ArrayList(4);
    if (days > 0) {
        measureList.add(new Measure(days, MeasureUnit.DAY));
    }
    if (hours > 0) {
        measureList.add(new Measure(hours, MeasureUnit.HOUR));
    }
    if (minutes > 0) {
        measureList.add(new Measure(minutes, MeasureUnit.MINUTE));
    }
    if (withSeconds && seconds > 0) {
        measureList.add(new Measure(seconds, MeasureUnit.SECOND));
    }
    if (measureList.size() == 0) {
        // Everything addable was zero, so nothing was added. We add a zero.
        measureList.add(new Measure(0, withSeconds ? MeasureUnit.SECOND : MeasureUnit.MINUTE));
    }
    final Measure[] measureArray = measureList.toArray(new Measure[measureList.size()]);
    final Locale locale = context.getResources().getConfiguration().locale;
    final MeasureFormat measureFormat = MeasureFormat.getInstance(locale, MeasureFormat.FormatWidth.NARROW);
    sb.append(measureFormat.formatMeasures(measureArray));
    if (measureArray.length == 1 && MeasureUnit.MINUTE.equals(measureArray[0].getUnit())) {
        // Add ttsSpan if it only have minute value, because it will be read as "meters"
        final TtsSpan ttsSpan = new TtsSpan.MeasureBuilder().setNumber(minutes).setUnit("minute").build();
        sb.setSpan(ttsSpan, 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return sb;
}
Also used : Locale(java.util.Locale) TtsSpan(android.text.style.TtsSpan) Measure(android.icu.util.Measure) ArrayList(java.util.ArrayList) SpannableStringBuilder(android.text.SpannableStringBuilder) MeasureFormat(android.icu.text.MeasureFormat)

Aggregations

Measure (android.icu.util.Measure)31 MeasureFormat (android.icu.text.MeasureFormat)26 Test (org.junit.Test)22 ULocale (android.icu.util.ULocale)7 FieldPosition (java.text.FieldPosition)7 ArrayList (java.util.ArrayList)7 SpannableStringBuilder (android.text.SpannableStringBuilder)6 TtsSpan (android.text.style.TtsSpan)6 Locale (java.util.Locale)6 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)4 MeasureUnit (android.icu.util.MeasureUnit)4 NumberFormat (android.icu.text.NumberFormat)3 DontCareFieldPosition (android.icu.impl.DontCareFieldPosition)2 TimeUnitFormat (android.icu.text.TimeUnitFormat)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1