Announce chronometers with time-unit words.

Instead of just using the visible representation for
accessibility, for which "00:15" is announced as "zero
fifteen", we now say "fifteen seconds".

Similarly,

  00:00 - "0 seconds"
  01:00 - "1 minute"
  02:05 - "2 minutes 5 seconds"
  1:00:00 - "1 hour"
  2:00:02 - "2 hours 2 seconds"
  2:01:02 - "2 hours 1 minute 2 seconds"
  -5 - "5 seconds" (what are we, Mission Control?)

Adapted from similar code in
  packages/apps/InCallUI/src/com/android/incallui/InCallDateUtils.java

Bug: 19418509
Change-Id: I54188ef25a7a205bb5bab42663a3da357ac853ef
diff --git a/core/java/android/widget/Chronometer.java b/core/java/android/widget/Chronometer.java
index a15080e..ebb54ff 100644
--- a/core/java/android/widget/Chronometer.java
+++ b/core/java/android/widget/Chronometer.java
@@ -17,6 +17,7 @@
 package android.widget;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.os.Handler;
 import android.os.Message;
@@ -24,6 +25,7 @@
 import android.text.format.DateUtils;
 import android.util.AttributeSet;
 import android.util.Log;
+import android.view.accessibility.AccessibilityEvent;
 import android.widget.RemoteViews.RemoteView;
 
 import java.util.Formatter;
@@ -58,6 +60,7 @@
     }
 
     private long mBase;
+    private long mNow; // the currently displayed time
     private boolean mVisible;
     private boolean mStarted;
     private boolean mRunning;
@@ -224,6 +227,7 @@
     }
 
     private synchronized void updateText(long now) {
+        mNow = now;
         long seconds = now - mBase;
         seconds /= 1000;
         String text = DateUtils.formatElapsedTime(mRecycle, seconds);
@@ -279,6 +283,60 @@
         }
     }
 
+    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;
+        }
+
+        int h = 0;
+        int m = 0;
+
+        if (duration >= HOUR_IN_SEC) {
+            h = duration / HOUR_IN_SEC;
+            duration -= h * HOUR_IN_SEC;
+        }
+        if (duration >= MIN_IN_SEC) {
+            m = duration / MIN_IN_SEC;
+            duration -= m * MIN_IN_SEC;
+        }
+        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;
+        }
+        return text.toString();
+    }
+
+    @Override
+    public CharSequence getContentDescription() {
+        return formatDuration(mNow - mBase);
+    }
+
     @Override
     public CharSequence getAccessibilityClassName() {
         return Chronometer.class.getName();