From 241872a93beff5822f6ed5a23bba51862f63bf14 Mon Sep 17 00:00:00 2001 From: Roozbeh Pournader Date: Thu, 15 Dec 2016 15:17:49 -0800 Subject: [PATCH] Switch Chronometer to use ICU instead of duration_hours etc The data in ICU has higher quality and is more comprehensive. (Additionally, the old code was using spaces to separate hours, minutes, and seconds, which was not correct for some locales.) Finally, remove duration_hours etc, since there is no longer any user of it. Bug: 33670388 Bug: 19418509 Test: manual and CTS (cts-tradefed run cts-dev --module CtsWidgetTestCases --test android.widget.cts.ChronometerTest) Change-Id: I97efa62186b94ed444fff90fa26bf71ac1599261 --- core/java/android/widget/Chronometer.java | 43 +++++++++-------------- core/res/res/values/strings.xml | 16 --------- core/res/res/values/symbols.xml | 3 -- 3 files changed, 16 insertions(+), 46 deletions(-) diff --git a/core/java/android/widget/Chronometer.java b/core/java/android/widget/Chronometer.java index b9224f36a9228..444ebc520cb79 100644 --- a/core/java/android/widget/Chronometer.java +++ b/core/java/android/widget/Chronometer.java @@ -17,8 +17,11 @@ package android.widget; import android.content.Context; -import android.content.res.Resources; import android.content.res.TypedArray; +import android.icu.text.MeasureFormat; +import android.icu.text.MeasureFormat.FormatWidth; +import android.icu.util.Measure; +import android.icu.util.MeasureUnit; import android.os.SystemClock; import android.text.format.DateUtils; import android.util.AttributeSet; @@ -28,6 +31,7 @@ import android.widget.RemoteViews.RemoteView; import com.android.internal.R; +import java.util.ArrayList; import java.util.Formatter; import java.util.IllegalFormatException; import java.util.Locale; @@ -329,9 +333,6 @@ public class Chronometer extends TextView { private static final int MIN_IN_SEC = 60; private static final int HOUR_IN_SEC = MIN_IN_SEC*60; private static String formatDuration(long ms) { - final Resources res = Resources.getSystem(); - final StringBuilder text = new StringBuilder(); - int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS); if (duration < 0) { duration = -duration; @@ -348,31 +349,19 @@ public class Chronometer extends TextView { m = duration / MIN_IN_SEC; duration -= m * MIN_IN_SEC; } - int s = duration; + final int s = duration; - try { - if (h > 0) { - text.append(res.getQuantityString( - com.android.internal.R.plurals.duration_hours, h, h)); - } - if (m > 0) { - if (text.length() > 0) { - text.append(' '); - } - text.append(res.getQuantityString( - com.android.internal.R.plurals.duration_minutes, m, m)); - } - - if (text.length() > 0) { - text.append(' '); - } - text.append(res.getQuantityString( - com.android.internal.R.plurals.duration_seconds, s, s)); - } catch (Resources.NotFoundException e) { - // Ignore; plurals throws an exception for an untranslated quantity for a given locale. - return null; + final ArrayList measures = new ArrayList(); + if (h > 0) { + measures.add(new Measure(h, MeasureUnit.HOUR)); } - return text.toString(); + if (m > 0) { + measures.add(new Measure(m, MeasureUnit.MINUTE)); + } + measures.add(new Measure(s, MeasureUnit.SECOND)); + + return MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE) + .formatMeasures((Measure[]) measures.toArray()); } @Override diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 87f38c1e99917..20728faec95c6 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -2431,22 +2431,6 @@ years - - - 1 second - %d seconds - - - - 1 minute - %d minutes - - - - 1 hour - %d hours - - now diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index a85ddf1e54d64..5b81b8f0a81ac 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1131,9 +1131,6 @@ - - -