Search in sources :

Example 11 with Measure

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

the class MeasureUnitTest method testManyLocaleDurations.

@Test
public void testManyLocaleDurations() {
    Measure hours = new Measure(5, MeasureUnit.HOUR);
    Measure minutes = new Measure(37, MeasureUnit.MINUTE);
    ULocale ulocDanish = new ULocale("da");
    ULocale ulocSpanish = new ULocale("es");
    ULocale ulocFinnish = new ULocale("fi");
    ULocale ulocIcelandic = new ULocale("is");
    ULocale ulocNorwegianBok = new ULocale("nb");
    ULocale ulocNorwegianNyn = new ULocale("nn");
    ULocale ulocDutch = new ULocale("nl");
    ULocale ulocSwedish = new ULocale("sv");
    Object[][] data = new Object[][] { { ulocDanish, FormatWidth.NARROW, "5 t og 37 min" }, { ulocDanish, FormatWidth.NUMERIC, "5.37" }, { ULocale.GERMAN, FormatWidth.NARROW, "5 Std., 37 Min." }, { ULocale.GERMAN, FormatWidth.NUMERIC, "5:37" }, { ULocale.ENGLISH, FormatWidth.NARROW, "5h 37m" }, { ULocale.ENGLISH, FormatWidth.NUMERIC, "5:37" }, { ulocSpanish, FormatWidth.NARROW, "5h 37min" }, { ulocSpanish, FormatWidth.NUMERIC, "5:37" }, { ulocFinnish, FormatWidth.NARROW, "5t 37min" }, { ulocFinnish, FormatWidth.NUMERIC, "5.37" }, { ULocale.FRENCH, FormatWidth.NARROW, "5h 37m" }, { ULocale.FRENCH, FormatWidth.NUMERIC, "05:37" }, { ulocIcelandic, FormatWidth.NARROW, "5 klst. og 37 m\u00EDn." }, { ulocIcelandic, FormatWidth.NUMERIC, "5:37" }, { ULocale.JAPANESE, FormatWidth.NARROW, "5h37m" }, { ULocale.JAPANESE, FormatWidth.NUMERIC, "5:37" }, { ulocNorwegianBok, FormatWidth.NARROW, "5t, 37m" }, { ulocNorwegianBok, FormatWidth.NUMERIC, "5:37" }, { ulocDutch, FormatWidth.NARROW, "5 u, 37 m" }, { ulocDutch, FormatWidth.NUMERIC, "5:37" }, { ulocNorwegianNyn, FormatWidth.NARROW, "5 h og 37 min" }, { ulocNorwegianNyn, FormatWidth.NUMERIC, "5:37" }, { ulocSwedish, FormatWidth.NARROW, "5h 37m" }, { ulocSwedish, FormatWidth.NUMERIC, "5:37" }, { ULocale.CHINESE, FormatWidth.NARROW, "5\u5C0F\u65F637\u5206\u949F" }, { ULocale.CHINESE, FormatWidth.NUMERIC, "5:37" } };
    for (Object[] row : data) {
        MeasureFormat mf = null;
        try {
            mf = MeasureFormat.getInstance((ULocale) row[0], (FormatWidth) row[1]);
        } catch (Exception e) {
            errln("Exception creating MeasureFormat for locale " + row[0] + ", width " + row[1] + ": " + e);
            continue;
        }
        String result = mf.formatMeasures(hours, minutes);
        if (!result.equals(row[2])) {
            errln("MeasureFormat.formatMeasures for locale " + row[0] + ", width " + row[1] + ", expected \"" + (String) row[2] + "\", got \"" + result + "\"");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Measure(android.icu.util.Measure) FormatWidth(android.icu.text.MeasureFormat.FormatWidth) MeasureFormat(android.icu.text.MeasureFormat) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with Measure

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

the class MeasureUnitTest method testEqHashCodeOfMeasure.

@Test
public void testEqHashCodeOfMeasure() {
    Measure _3feetDouble = new Measure(3.0, MeasureUnit.FOOT);
    Measure _3feetInt = new Measure(3, MeasureUnit.FOOT);
    Measure _4feetInt = new Measure(4, MeasureUnit.FOOT);
    verifyEqualsHashCode(_3feetDouble, _3feetInt, _4feetInt);
}
Also used : Measure(android.icu.util.Measure) Test(org.junit.Test)

Example 13 with Measure

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

the class TimeUnitTest method TestStandInForMeasureFormat.

@Test
public void TestStandInForMeasureFormat() {
    TimeUnitFormat tuf = new TimeUnitFormat(ULocale.FRENCH, TimeUnitFormat.ABBREVIATED_NAME);
    Measure measure = new Measure(23, MeasureUnit.CELSIUS);
    assertEquals("23 °C", "23 °C", tuf.format(measure));
    tuf = new TimeUnitFormat(ULocale.FRENCH, TimeUnitFormat.FULL_NAME);
    assertEquals("70 pied et 5,3 pouces", "70 pieds et 5,3 pouces", tuf.formatMeasures(new Measure(70, MeasureUnit.FOOT), new Measure(5.3, MeasureUnit.INCH)));
    assertEquals("getLocale", ULocale.FRENCH, tuf.getLocale());
    assertEquals("getNumberFormat", ULocale.FRENCH, tuf.getNumberFormat().getLocale(ULocale.VALID_LOCALE));
    assertEquals("getWidth", MeasureFormat.FormatWidth.WIDE, tuf.getWidth());
}
Also used : TimeUnitFormat(android.icu.text.TimeUnitFormat) Measure(android.icu.util.Measure) Test(org.junit.Test)

Example 14 with Measure

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

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)

Example 15 with Measure

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

the class MeasureFormat method toHMS.

// Returns hours in [0]; minutes in [1]; seconds in [2] out of measures array. If
// unsuccessful, e.g measures has other measurements besides hours, minutes, seconds;
// hours, minutes, seconds are out of order; or have negative values, returns null.
// If hours, minutes, or seconds is missing from measures the corresponding element in
// returned array will be null.
private static Number[] toHMS(Measure[] measures) {
    Number[] result = new Number[3];
    int lastIdx = -1;
    for (Measure m : measures) {
        if (m.getNumber().doubleValue() < 0.0) {
            return null;
        }
        Integer idxObj = hmsTo012.get(m.getUnit());
        if (idxObj == null) {
            return null;
        }
        int idx = idxObj.intValue();
        if (idx <= lastIdx) {
            // hour before minute before second
            return null;
        }
        lastIdx = idx;
        result[idx] = m.getNumber();
    }
    return result;
}
Also used : Measure(android.icu.util.Measure)

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