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
diff --git a/core/java/android/widget/Chronometer.java b/core/java/android/widget/Chronometer.java
index b9224f3..444ebc5 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 com.android.internal.R;
+import java.util.ArrayList;
import java.util.Formatter;
import java.util.IllegalFormatException;
import java.util.Locale;
@@ -329,9 +333,6 @@
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 @@
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<Measure> measures = new ArrayList<Measure>();
+ 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