Merge "Fixed the relative time spoken with accessibility" into nyc-dev
am: 38b5946fae

* commit '38b5946fae50d4e9e8f32c985983d03a4437910d':
  Fixed the relative time spoken with accessibility

Change-Id: I51bbe71d2b86cfb8f050882b072092a015573fdb
diff --git a/core/java/android/widget/DateTimeView.java b/core/java/android/widget/DateTimeView.java
index d2ee866..e172044 100644
--- a/core/java/android/widget/DateTimeView.java
+++ b/core/java/android/widget/DateTimeView.java
@@ -34,6 +34,7 @@
 import android.os.Handler;
 import android.text.format.Time;
 import android.util.AttributeSet;
+import android.view.accessibility.AccessibilityNodeInfo;
 import android.widget.RemoteViews.RemoteView;
 
 import com.android.internal.R;
@@ -118,7 +119,6 @@
     public void setTime(long time) {
         Time t = new Time();
         t.set(time);
-        t.second = 0;
         mTimeMillis = t.toMillis(false);
         mTime = new Date(t.year-1900, t.month, t.monthDay, t.hour, t.minute, 0);
         update();
@@ -333,6 +333,63 @@
         update();
     }
 
+    @Override
+    public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
+        super.onInitializeAccessibilityNodeInfoInternal(info);
+        if (mShowRelativeTime) {
+            // The short version of the time might not be completely understandable and for
+            // accessibility we rather have a longer version.
+            long now = System.currentTimeMillis();
+            long duration = Math.abs(now - mTimeMillis);
+            int count;
+            boolean past = (now >= mTimeMillis);
+            String result;
+            if (duration < MINUTE_IN_MILLIS) {
+                result = mNowText;
+            } else if (duration < HOUR_IN_MILLIS) {
+                count = (int)(duration / MINUTE_IN_MILLIS);
+                result = String.format(getContext().getResources().getQuantityString(past
+                                ? com.android.internal.
+                                        R.plurals.duration_minutes_relative
+                                : com.android.internal.
+                                        R.plurals.duration_minutes_relative_future,
+                        count),
+                        count);
+            } else if (duration < DAY_IN_MILLIS) {
+                count = (int)(duration / HOUR_IN_MILLIS);
+                result = String.format(getContext().getResources().getQuantityString(past
+                                ? com.android.internal.
+                                        R.plurals.duration_hours_relative
+                                : com.android.internal.
+                                        R.plurals.duration_hours_relative_future,
+                        count),
+                        count);
+            } else if (duration < YEAR_IN_MILLIS) {
+                // In weird cases it can become 0 because of daylight savings
+                TimeZone timeZone = TimeZone.getDefault();
+                count = Math.max(Math.abs(dayDistance(timeZone, mTimeMillis, now)), 1);
+                result = String.format(getContext().getResources().getQuantityString(past
+                                ? com.android.internal.
+                                        R.plurals.duration_days_relative
+                                : com.android.internal.
+                                        R.plurals.duration_days_relative_future,
+                        count),
+                        count);
+
+            } else {
+                count = (int)(duration / YEAR_IN_MILLIS);
+                result = String.format(getContext().getResources().getQuantityString(past
+                                ? com.android.internal.
+                                        R.plurals.duration_years_relative
+                                : com.android.internal.
+                                        R.plurals.duration_years_relative_future,
+                        count),
+                        count);
+            }
+            info.setText(result);
+        }
+    }
+
     private static class ReceiverInfo {
         private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>();
         private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 9d71475..e30fd1a 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -2474,6 +2474,54 @@
         <item quantity="other">in <xliff:g example="2" id="count">%d</xliff:g>y</item>
     </plurals>
 
+    <!-- Phrase describing a relative time using minutes in the past that is not shown on the screen but used for accessibility. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_minutes_relative">
+        <item quantity="one"><xliff:g example="1" id="count">%d</xliff:g> minute ago</item>
+        <item quantity="other"><xliff:g example="2" id="count">%d</xliff:g> minutes ago</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using hours in the past that is not shown on the screen but used for accessibility. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_hours_relative">
+        <item quantity="one"><xliff:g example="1" id="count">%d</xliff:g> hour ago</item>
+        <item quantity="other"><xliff:g example="2" id="count">%d</xliff:g> hours ago</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using days in the past that is not shown on the screen but used for accessibility. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_days_relative">
+        <item quantity="one"><xliff:g example="1" id="count">%d</xliff:g> day ago</item>
+        <item quantity="other"><xliff:g example="2" id="count">%d</xliff:g> days ago</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using years in the past that is not shown on the screen but used for accessibility. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_years_relative">
+        <item quantity="one"><xliff:g example="1" id="count">%d</xliff:g> year ago</item>
+        <item quantity="other"><xliff:g example="2" id="count">%d</xliff:g> years ago</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using minutes that is not shown on the screen but used for accessibility. This version should be a future point in time. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_minutes_relative_future">
+        <item quantity="one">in <xliff:g example="1" id="count">%d</xliff:g> minute</item>
+        <item quantity="other">in <xliff:g example="2" id="count">%d</xliff:g> minutes</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using hours that is not shown on the screen but used for accessibility. This version should be a future point in time. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_hours_relative_future">
+        <item quantity="one">in <xliff:g example="1" id="count">%d</xliff:g> hour</item>
+        <item quantity="other">in <xliff:g example="2" id="count">%d</xliff:g> hours</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using days that is not shown on the screen but used for accessibility. This version should be a future point in time. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_days_relative_future">
+        <item quantity="one">in <xliff:g example="1" id="count">%d</xliff:g> day</item>
+        <item quantity="other">in <xliff:g example="2" id="count">%d</xliff:g> days</item>
+    </plurals>
+
+    <!-- Phrase describing a relative time using years that is not shown on the screen but used for accessibility. This version should be a future point in time. [CHAR LIMIT=NONE] -->
+    <plurals name="duration_years_relative_future">
+        <item quantity="one">in <xliff:g example="1" id="count">%d</xliff:g> year</item>
+        <item quantity="other">in <xliff:g example="2" id="count">%d</xliff:g> years</item>
+    </plurals>
+
     <!-- Title for error alert when a video cannot be played.  it can be used by any app. -->
     <string name="VideoView_error_title">Video problem</string>
     <!-- Text for error alert when a video container is not valid for progressive download/playback. -->
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index 4925e52..115c49f 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -2522,6 +2522,15 @@
   <java-symbol type="plurals" name="duration_days_shortest_future" />
   <java-symbol type="plurals" name="duration_years_shortest_future" />
 
+  <java-symbol type="plurals" name="duration_minutes_relative" />
+  <java-symbol type="plurals" name="duration_hours_relative" />
+  <java-symbol type="plurals" name="duration_days_relative" />
+  <java-symbol type="plurals" name="duration_years_relative" />
+  <java-symbol type="plurals" name="duration_minutes_relative_future" />
+  <java-symbol type="plurals" name="duration_hours_relative_future" />
+  <java-symbol type="plurals" name="duration_days_relative_future" />
+  <java-symbol type="plurals" name="duration_years_relative_future" />
+
   <java-symbol type="string" name="now_string_shortest" />
 
   <!-- Encryption notification while accounts are locked by credential encryption -->