Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a79a747c authored by Dan Sandler's avatar Dan Sandler
Browse files

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
parent b0a5e78f
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
@@ -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.os.SystemClock;
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 @@ public class Chronometer extends TextView {
    }

    private long mBase;
    private long mNow; // the currently displayed time
    private boolean mVisible;
    private boolean mStarted;
    private boolean mRunning;
@@ -224,6 +227,7 @@ public class Chronometer extends TextView {
    }

    private synchronized void updateText(long now) {
        mNow = now;
        long seconds = now - mBase;
        seconds /= 1000;
        String text = DateUtils.formatElapsedTime(mRecycle, seconds);
@@ -279,6 +283,60 @@ 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;
        }

        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();