Search in sources :

Example 1 with MeasureFormat

use of android.icu.text.MeasureFormat in project android_packages_apps_Settings by omnirom.

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 2 with MeasureFormat

use of android.icu.text.MeasureFormat in project android_packages_apps_Settings by crdroidandroid.

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 3 with MeasureFormat

use of android.icu.text.MeasureFormat in project android_packages_apps_Settings by DirtyUnicorns.

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 4 with MeasureFormat

use of android.icu.text.MeasureFormat in project j2objc by google.

the class MeasureUnitTest method testDoubleZero.

@Test
public void testDoubleZero() {
    ULocale en = new ULocale("en");
    NumberFormat nf = NumberFormat.getInstance(en);
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    MeasureFormat mf = MeasureFormat.getInstance(en, FormatWidth.WIDE, nf);
    assertEquals("Positive Rounding", "4 hours, 23 minutes, 16.00 seconds", mf.formatMeasures(new Measure(4.7, MeasureUnit.HOUR), new Measure(23, MeasureUnit.MINUTE), new Measure(16, MeasureUnit.SECOND)));
    assertEquals("Negative Rounding", "-4 hours, 23 minutes, 16.00 seconds", mf.formatMeasures(new Measure(-4.7, MeasureUnit.HOUR), new Measure(23, MeasureUnit.MINUTE), new Measure(16, MeasureUnit.SECOND)));
}
Also used : ULocale(android.icu.util.ULocale) Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 5 with MeasureFormat

use of android.icu.text.MeasureFormat in project j2objc by google.

the class MeasureUnitTest method Test10219FractionalPlurals.

@Test
public void Test10219FractionalPlurals() {
    double[] values = { 1.588, 1.011 };
    String[][] expected = { { "1 minute", "1.5 minutes", "1.58 minutes" }, { "1 minute", "1.0 minutes", "1.01 minutes" } };
    for (int j = 0; j < values.length; j++) {
        for (int i = 0; i < expected[j].length; i++) {
            NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
            nf.setRoundingMode(BigDecimal.ROUND_DOWN);
            nf.setMinimumFractionDigits(i);
            nf.setMaximumFractionDigits(i);
            MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE, nf);
            assertEquals("Test10219", expected[j][i], mf.format(new Measure(values[j], MeasureUnit.MINUTE)));
        }
    }
}
Also used : Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Aggregations

MeasureFormat (android.icu.text.MeasureFormat)38 Test (org.junit.Test)32 Measure (android.icu.util.Measure)26 ULocale (android.icu.util.ULocale)11 Locale (java.util.Locale)8 ArrayList (java.util.ArrayList)7 SpannableStringBuilder (android.text.SpannableStringBuilder)6 TtsSpan (android.text.style.TtsSpan)6 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)5 NumberFormat (android.icu.text.NumberFormat)5 FieldPosition (java.text.FieldPosition)5 MeasureUnit (android.icu.util.MeasureUnit)4 CurrencyAmount (android.icu.util.CurrencyAmount)3 ParseException (java.text.ParseException)3 BigDecimal (android.icu.math.BigDecimal)2 IOException (java.io.IOException)2 ResourceReader (android.icu.impl.data.ResourceReader)1 TokenIterator (android.icu.impl.data.TokenIterator)1 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)1 DecimalFormat (android.icu.text.DecimalFormat)1