From e4692b7caecfab2adddb0907318e124b3f7a48ab Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:05:48 +0800 Subject: [PATCH] Clean in DateTimeView Bug: 199230228 Test: make Change-Id: Ib484fc0c59f7900b751f4e782dd67c000cc855bb --- core/java/android/webkit/DateSorter.java | 12 +- core/java/android/widget/DateTimeView.java | 87 ++++++------- core/res/res/values/strings.xml | 142 ++++++++++----------- core/res/res/values/symbols.xml | 35 +++-- 4 files changed, 135 insertions(+), 141 deletions(-) diff --git a/core/java/android/webkit/DateSorter.java b/core/java/android/webkit/DateSorter.java index 90d44db3eff46..c29e774c9614c 100644 --- a/core/java/android/webkit/DateSorter.java +++ b/core/java/android/webkit/DateSorter.java @@ -18,11 +18,14 @@ package android.webkit; import android.content.Context; import android.content.res.Resources; +import android.util.PluralsMessageFormatter; import com.android.icu.text.DateSorterBridge; import java.util.Calendar; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; /** * Sorts dates into the following groups: @@ -73,9 +76,12 @@ public class DateSorter { mLabels[0] = dateSorterBridge.getToday(); mLabels[1] = dateSorterBridge.getYesterday(); - int resId = com.android.internal.R.plurals.last_num_days; - String format = resources.getQuantityString(resId, NUM_DAYS_AGO); - mLabels[2] = String.format(format, NUM_DAYS_AGO); + Map arguments = new HashMap<>(); + arguments.put("count", NUM_DAYS_AGO); + mLabels[2] = PluralsMessageFormatter.format( + resources, + arguments, + com.android.internal.R.string.last_num_days); mLabels[3] = context.getString(com.android.internal.R.string.last_month); mLabels[4] = context.getString(com.android.internal.R.string.older); diff --git a/core/java/android/widget/DateTimeView.java b/core/java/android/widget/DateTimeView.java index 955552289c3a0..2c6264783f565 100644 --- a/core/java/android/widget/DateTimeView.java +++ b/core/java/android/widget/DateTimeView.java @@ -33,6 +33,7 @@ import android.database.ContentObserver; import android.os.Build; import android.os.Handler; import android.util.AttributeSet; +import android.util.PluralsMessageFormatter; import android.view.accessibility.AccessibilityNodeInfo; import android.view.inspector.InspectableProperty; import android.widget.RemoteViews.RemoteView; @@ -48,6 +49,8 @@ import java.time.ZoneId; import java.time.temporal.JulianFields; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.Map; // // TODO @@ -260,19 +263,17 @@ public class DateTimeView extends TextView { return; } 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_shortest - : com.android.internal.R.plurals.duration_minutes_shortest_future, - count), + result = getContext().getResources().getString(past + ? com.android.internal.R.string.duration_minutes_shortest + : com.android.internal.R.string.duration_minutes_shortest_future, count); millisIncrease = MINUTE_IN_MILLIS; } 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_shortest - : com.android.internal.R.plurals.duration_hours_shortest_future, - count), - count); + result = getContext().getResources().getString(past + ? com.android.internal.R.string.duration_hours_shortest + : com.android.internal.R.string.duration_hours_shortest_future, + count); millisIncrease = HOUR_IN_MILLIS; } else if (duration < YEAR_IN_MILLIS) { // In weird cases it can become 0 because of daylight savings @@ -281,10 +282,9 @@ public class DateTimeView extends TextView { LocalDateTime localNow = toLocalDateTime(now, zoneId); count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 1); - result = String.format(getContext().getResources().getQuantityString(past - ? com.android.internal.R.plurals.duration_days_shortest - : com.android.internal.R.plurals.duration_days_shortest_future, - count), + result = getContext().getResources().getString(past + ? com.android.internal.R.string.duration_days_shortest + : com.android.internal.R.string.duration_days_shortest_future, count); if (past || count != 1) { mUpdateTimeMillis = computeNextMidnight(localNow, zoneId); @@ -295,10 +295,9 @@ public class DateTimeView extends TextView { } else { count = (int)(duration / YEAR_IN_MILLIS); - result = String.format(getContext().getResources().getQuantityString(past - ? com.android.internal.R.plurals.duration_years_shortest - : com.android.internal.R.plurals.duration_years_shortest_future, - count), + result = getContext().getResources().getString(past + ? com.android.internal.R.string.duration_years_shortest + : com.android.internal.R.string.duration_years_shortest_future, count); millisIncrease = YEAR_IN_MILLIS; } @@ -363,26 +362,25 @@ public class DateTimeView extends TextView { int count; boolean past = (now >= mTimeMillis); String result; + Map arguments = new HashMap<>(); 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); + arguments.put("count", count); + result = PluralsMessageFormatter.format( + getContext().getResources(), + arguments, + past ? R.string.duration_minutes_relative + : R.string.duration_minutes_relative_future); } 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); + arguments.put("count", count); + result = PluralsMessageFormatter.format( + getContext().getResources(), + arguments, + past ? R.string.duration_hours_relative + : R.string.duration_hours_relative_future); } else if (duration < YEAR_IN_MILLIS) { // In weird cases it can become 0 because of daylight savings LocalDateTime localDateTime = mLocalTime; @@ -390,23 +388,20 @@ public class DateTimeView extends TextView { LocalDateTime localNow = toLocalDateTime(now, zoneId); count = Math.max(Math.abs(dayDistance(localDateTime, localNow)), 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); - + arguments.put("count", count); + result = PluralsMessageFormatter.format( + getContext().getResources(), + arguments, + past ? R.string.duration_days_relative + : R.string.duration_days_relative_future); } 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); + arguments.put("count", count); + result = PluralsMessageFormatter.format( + getContext().getResources(), + arguments, + past ? R.string.duration_years_relative + : R.string.duration_years_relative_future); } info.setText(result); } diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 7d1705720699a..fdd3601bbc59b 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -3041,10 +3041,10 @@ Before 1 month ago - - Last %d day - Last %d days - + { count, plural, + =1 {Last # day} + other {Last # days} + } Last month @@ -3088,100 +3088,100 @@ now - - %dm - %dm - + + %dm + - - %dh - %dh - + + %dh + - - %dd - %dd - + + %dd + - - %dy - %dy - + + %dy + - - in %dm - in %dm - + + in %dm + - - in %dh - in %dh - + + in %dh + - - in %dd - in %dd - + + in %dd + - - in %dy - in %dy - + + in %dy + - - %d minute ago - %d minutes ago - + {count, plural, + =1 {# minute ago} + other {# minutes ago} + } + - - %d hour ago - %d hours ago - + {count, plural, + =1 {# hour ago} + other {# hours ago} + } + - - %d day ago - %d days ago - + {count, plural, + =1 {# day ago} + other {# days ago} + } + - - %d year ago - %d years ago - + {count, plural, + =1 {# year ago} + other {# years ago} + } + - - in %d minute - in %d minutes - + {count, plural, + =1 {# minute} + other {# minutes} + } + - - in %d hour - in %d hours - + {count, plural, + =1 {# hour} + other {# hours} + } + - - in %d day - in %d days - + {count, plural, + =1 {# day} + other {# days} + } + - - in %d year - in %d years - + {count, plural, + =1 {# year} + other {# years} + } + Video problem @@ -5094,12 +5094,6 @@ PINs don\'t match. Try again. PIN is too short. Must be at least 4 digits. - - - - Try again in 1 second - Try again in %d seconds - Try again later diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index c466f8e38c868..a9125adbd96f2 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1260,10 +1260,9 @@ - - + @@ -3098,23 +3097,23 @@ - - - - - - - - + + + + + + + + - - - - - - - - + + + + + + + +