From 936a4e3687b435e867f42f000859f167a54adc51 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 11:42:17 +0800 Subject: Add util class for plurals Bug: 199230228 Test: make Change-Id: I5756846dcd10f17520afcc34bf23de0a45a2bcb9 --- .../java/android/util/PluralsMessageFormatter.java | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 core/java/android/util/PluralsMessageFormatter.java diff --git a/core/java/android/util/PluralsMessageFormatter.java b/core/java/android/util/PluralsMessageFormatter.java new file mode 100644 index 000000000000..7cb9fb37f055 --- /dev/null +++ b/core/java/android/util/PluralsMessageFormatter.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.util; + +import android.annotation.NonNull; +import android.annotation.StringRes; +import android.content.res.Resources; +import android.icu.text.MessageFormat; + +import java.util.Map; + +/** + * Helper class for easier formatting of ICU {@link android.icu.text.MessageFormat} syntax. + * @hide + */ +public class PluralsMessageFormatter { + /** + * Formatting the ICU {@link android.icu.text.MessageFormat} syntax + * + * @param resources the {@link android.content.res.Resources} + * @param arguments the mapping of argument names and values + * @param messageId the string resource id with {@link android.icu.text.MessageFormat} syntax + * @return the formatted result + */ + public static String format(@NonNull Resources resources, + Map arguments, + @StringRes int messageId) { + return new MessageFormat(resources.getString(messageId)).format(arguments); + } +} -- cgit v1.2.3-59-g8ed1b From 9e38ebf366f52f7b29105705d9ea3a7ab74efe5b Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 11:46:59 +0800 Subject: Clean in ZenModeConfig Bug: 199230228 Test: make Change-Id: I712f984b440043360b8f5fc6c6613c752bb2a2dd --- .../service/notification/ZenModeConfig.java | 32 ++++++---- core/res/res/values/strings.xml | 72 ++++++++++++---------- core/res/res/values/symbols.xml | 16 ++--- 3 files changed, 68 insertions(+), 52 deletions(-) diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java index c1d5a28aa349..1556d2533080 100644 --- a/core/java/android/service/notification/ZenModeConfig.java +++ b/core/java/android/service/notification/ZenModeConfig.java @@ -46,6 +46,7 @@ import android.text.TextUtils; import android.text.format.DateFormat; import android.util.ArrayMap; import android.util.ArraySet; +import android.util.PluralsMessageFormatter; import android.util.Slog; import android.util.TypedXmlPullParser; import android.util.TypedXmlSerializer; @@ -63,8 +64,10 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import java.util.TimeZone; import java.util.UUID; @@ -1333,25 +1336,30 @@ public class ZenModeConfig implements Parcelable { final CharSequence formattedTime = getFormattedTime(context, time, isToday(time), userHandle); final Resources res = context.getResources(); + final Map arguments = new HashMap<>(); if (minutes < 60) { // display as minutes num = minutes; - int summaryResId = shortVersion ? R.plurals.zen_mode_duration_minutes_summary_short - : R.plurals.zen_mode_duration_minutes_summary; - summary = res.getQuantityString(summaryResId, num, num, formattedTime); - int line1ResId = shortVersion ? R.plurals.zen_mode_duration_minutes_short - : R.plurals.zen_mode_duration_minutes; - line1 = res.getQuantityString(line1ResId, num, num, formattedTime); + int summaryResId = shortVersion ? R.string.zen_mode_duration_minutes_summary_short + : R.string.zen_mode_duration_minutes_summary; + arguments.put("count", num); + arguments.put("formattedTime", formattedTime); + summary = PluralsMessageFormatter.format(res, arguments, summaryResId); + int line1ResId = shortVersion ? R.string.zen_mode_duration_minutes_short + : R.string.zen_mode_duration_minutes; + line1 = PluralsMessageFormatter.format(res, arguments, line1ResId); line2 = res.getString(R.string.zen_mode_until, formattedTime); } else if (minutes < DAY_MINUTES) { // display as hours num = Math.round(minutes / 60f); - int summaryResId = shortVersion ? R.plurals.zen_mode_duration_hours_summary_short - : R.plurals.zen_mode_duration_hours_summary; - summary = res.getQuantityString(summaryResId, num, num, formattedTime); - int line1ResId = shortVersion ? R.plurals.zen_mode_duration_hours_short - : R.plurals.zen_mode_duration_hours; - line1 = res.getQuantityString(line1ResId, num, num, formattedTime); + int summaryResId = shortVersion ? R.string.zen_mode_duration_hours_summary_short + : R.string.zen_mode_duration_hours_summary; + arguments.put("count", num); + arguments.put("formattedTime", formattedTime); + summary = PluralsMessageFormatter.format(res, arguments, summaryResId); + int line1ResId = shortVersion ? R.string.zen_mode_duration_hours_short + : R.string.zen_mode_duration_hours; + line1 = PluralsMessageFormatter.format(res, arguments, line1ResId); line2 = res.getString(R.string.zen_mode_until, formattedTime); } else { // display as day/time diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 2879759db521..7eee6e3cddcb 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -5197,52 +5197,60 @@ Turn on - - For one minute (until %2$s) - For %1$d minutes (until %2$s) - + {count, plural, + =1 {For one minute (until {formattedTime})} + other {For # minutes (until {formattedTime})} + } + - - For 1 min (until %2$s) - For %1$d min (until %2$s) - + {count, plural, + =1 {For 1 min (until {formattedTime})} + other {For # min (until {formattedTime})} + } + - - For 1 hour (until %2$s) - For %1$d hours (until %2$s) - + {count, plural, + =1 {For 1 hour (until {formattedTime})} + other {For # hours (until {formattedTime})} + } + - - For 1 hr (until %2$s) - For %1$d hr (until %2$s) - + {count, plural, + =1 {For 1 hr (until {formattedTime})} + other {For # hr (until {formattedTime})} + } + - - For one minute - For %d minutes - + {count, plural, + =1 {For one minute} + other {For # minutes} + } + - - For 1 min - For %d min - + {count, plural, + =1 {For 1 min} + other {For # min} + } + - - For 1 hour - For %d hours - + {count, plural, + =1 {For 1 hour} + other {For # hours} + } + - - For 1 hr - For %d hr - + {count, plural, + =1 {For 1 hr} + other {For # hr} + } + Until %1$s diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index a4b5d3caaabf..9ec1810f7cb5 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -2489,14 +2489,14 @@ - - - - - - - - + + + + + + + + -- cgit v1.2.3-59-g8ed1b From a9ace2c62033c59d3bc5e4284c44119394e70257 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 11:52:08 +0800 Subject: Clean in FindActionModeCallback Bug: 199230228 Test: make Change-Id: I59618e5467696d3173dfb4782ce55964694a42fc --- boot/hiddenapi/hiddenapi-max-target-q.txt | 2 +- core/java/android/webkit/FindActionModeCallback.java | 17 ++++++++++++++--- core/res/res/values/strings.xml | 11 +++++------ core/res/res/values/symbols.xml | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/boot/hiddenapi/hiddenapi-max-target-q.txt b/boot/hiddenapi/hiddenapi-max-target-q.txt index 4832dd184ec5..f70473c313e4 100644 --- a/boot/hiddenapi/hiddenapi-max-target-q.txt +++ b/boot/hiddenapi/hiddenapi-max-target-q.txt @@ -398,7 +398,7 @@ Lcom/android/internal/R$layout;->select_dialog_multichoice:I Lcom/android/internal/R$layout;->select_dialog_singlechoice:I Lcom/android/internal/R$layout;->webview_find:I Lcom/android/internal/R$layout;->zoom_magnify:I -Lcom/android/internal/R$plurals;->matches_found:I +Lcom/android/internal/R$string;->matches_found:I Lcom/android/internal/R$raw;->loaderror:I Lcom/android/internal/R$raw;->nodomain:I Lcom/android/internal/R$string;->byteShort:I diff --git a/core/java/android/webkit/FindActionModeCallback.java b/core/java/android/webkit/FindActionModeCallback.java index e011d51c51fd..b1d07f59dc4e 100644 --- a/core/java/android/webkit/FindActionModeCallback.java +++ b/core/java/android/webkit/FindActionModeCallback.java @@ -26,6 +26,7 @@ import android.text.Editable; import android.text.Selection; import android.text.Spannable; import android.text.TextWatcher; +import android.util.PluralsMessageFormatter; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; @@ -35,6 +36,11 @@ import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; +import com.android.internal.R; + +import java.util.HashMap; +import java.util.Map; + /** * @hide */ @@ -180,9 +186,14 @@ public class FindActionModeCallback implements ActionMode.Callback, TextWatcher, if (mNumberOfMatches == 0) { mMatches.setText(com.android.internal.R.string.no_matches); } else { - mMatches.setText(mResources.getQuantityString( - com.android.internal.R.plurals.matches_found, mNumberOfMatches, - mActiveMatchIndex + 1, mNumberOfMatches)); + Map arguments = new HashMap<>(); + arguments.put("count", mActiveMatchIndex + 1); + arguments.put("total", mNumberOfMatches); + + mMatches.setText(PluralsMessageFormatter.format( + mResources, + arguments, + R.string.matches_found)); } mMatches.setVisibility(View.VISIBLE); } diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 7eee6e3cddcb..e2a8193f171f 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -4191,12 +4191,11 @@ - - - 1 match - - %d of %d - + { count, plural, + =1 {# match} + other {# of {total}}} + } + Done diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 9ec1810f7cb5..b2d898bd1920 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1263,8 +1263,8 @@ - + -- cgit v1.2.3-59-g8ed1b From 436659396c9a1ad394fb74118709358c8eaacc84 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 11:54:34 +0800 Subject: Clear in TextUtils Bug: 199230228 Test: make Change-Id: I3c0422b588a954a9e32fdcc94ea3946dbccddcd0 --- core/java/android/text/TextUtils.java | 9 --------- core/res/res/values/strings.xml | 6 ------ core/res/res/values/symbols.xml | 1 - 3 files changed, 16 deletions(-) diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java index d0fd2b393633..28f5c2114b1d 100644 --- a/core/java/android/text/TextUtils.java +++ b/core/java/android/text/TextUtils.java @@ -2070,15 +2070,6 @@ public class TextUtils { : View.LAYOUT_DIRECTION_LTR; } - /** - * Return localized string representing the given number of selected items. - * - * @hide - */ - public static CharSequence formatSelectedCount(int count) { - return Resources.getSystem().getQuantityString(R.plurals.selected_count, count, count); - } - /** * Simple alternative to {@link String#format} which purposefully supports * only a small handful of substitutions to improve execution speed. diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index e2a8193f171f..c3e5b73abedc 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -5372,12 +5372,6 @@ Screening an incoming call - - - %1$d selected - %1$d selected - - Uncategorized You set the importance of these notifications. diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index b2d898bd1920..c5dcbebe95d7 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -2928,7 +2928,6 @@ - -- cgit v1.2.3-59-g8ed1b From 67e780def4e398fb2bd11d7105a40ce44f86e991 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 11:58:45 +0800 Subject: Clean in ChooserActivity Bug: 199230228 Test: make Change-Id: Id1364a9c67187ee4839392751f6ed41ca1b281bc --- core/java/com/android/internal/app/ChooserActivity.java | 13 +++++++++++-- core/res/res/values/strings.xml | 9 +++++---- core/res/res/values/symbols.xml | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/java/com/android/internal/app/ChooserActivity.java b/core/java/com/android/internal/app/ChooserActivity.java index be7388bfca13..4ae6bf7e8379 100644 --- a/core/java/com/android/internal/app/ChooserActivity.java +++ b/core/java/com/android/internal/app/ChooserActivity.java @@ -90,6 +90,7 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.util.HashedStringCache; import android.util.Log; +import android.util.PluralsMessageFormatter; import android.util.Size; import android.util.Slog; import android.view.LayoutInflater; @@ -216,6 +217,9 @@ public class ChooserActivity extends ResolverActivity implements private static final int APP_PREDICTION_SHARE_TARGET_QUERY_PACKAGE_LIMIT = 20; public static final String APP_PREDICTION_INTENT_FILTER_KEY = "intent_filter"; + private static final String PLURALS_COUNT = "count"; + private static final String PLURALS_FILE_NAME = "file_name"; + @VisibleForTesting public static final int LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS = 250; @@ -1551,8 +1555,13 @@ public class ChooserActivity extends ResolverActivity implements } else { FileInfo fileInfo = extractFileInfo(uris.get(0), getContentResolver()); int remUriCount = uriCount - 1; - String fileName = getResources().getQuantityString(R.plurals.file_count, - remUriCount, fileInfo.name, remUriCount); + Map arguments = new HashMap<>(); + arguments.put(PLURALS_COUNT, remUriCount); + arguments.put(PLURALS_FILE_NAME, fileInfo.name); + String fileName = PluralsMessageFormatter.format( + getResources(), + arguments, + R.string.file_count); TextView fileNameView = contentPreviewLayout.findViewById( R.id.content_preview_filename); diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index c3e5b73abedc..40fd12efe710 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -5807,10 +5807,11 @@ Loading - - %s + %d file - %s + %d files - + {count, plural, + =1 {{file_name} + # file} + other {{file_name} + # files} + } + No recommended people to share with diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index c5dcbebe95d7..76e0994d7b18 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1261,9 +1261,9 @@ - + -- cgit v1.2.3-59-g8ed1b From bf4e15d0176c5e0d24ddcb84d839b7056faf6d16 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:00:22 +0800 Subject: Clean in keyguard Bug: 199230228 Test: make Change-Id: I03723cfe18e5c948ca1896c5c2c389f46c48d891 --- core/res/res/values/strings.xml | 5 ----- packages/SystemUI/res-keyguard/values/strings.xml | 9 +++++---- .../android/keyguard/KeyguardAbsKeyInputViewController.java | 13 ++++++++++--- .../com/android/keyguard/KeyguardPatternViewController.java | 13 ++++++++++--- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 40fd12efe710..5242b955917b 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -4557,11 +4557,6 @@ Wrong Password Wrong PIN - - - Try again in 1 second. - Try again in %d seconds. - Draw your pattern diff --git a/packages/SystemUI/res-keyguard/values/strings.xml b/packages/SystemUI/res-keyguard/values/strings.xml index 16010430df11..293c86e35f25 100644 --- a/packages/SystemUI/res-keyguard/values/strings.xml +++ b/packages/SystemUI/res-keyguard/values/strings.xml @@ -117,10 +117,11 @@ Wrong PIN - - Try again in 1 second. - Try again in %d seconds. - + {count, plural, + =1 {Try again in # second.} + other {Try again in # seconds.} + } + Enter SIM PIN. diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java index 1c4559eb0364..d5a0201d13e5 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardAbsKeyInputViewController.java @@ -25,6 +25,7 @@ import android.content.res.ColorStateList; import android.os.AsyncTask; import android.os.CountDownTimer; import android.os.SystemClock; +import android.util.PluralsMessageFormatter; import android.view.KeyEvent; import com.android.internal.util.LatencyTracker; @@ -38,6 +39,9 @@ import com.android.systemui.R; import com.android.systemui.classifier.FalsingClassifier; import com.android.systemui.classifier.FalsingCollector; +import java.util.HashMap; +import java.util.Map; + public abstract class KeyguardAbsKeyInputViewController extends KeyguardInputViewController { private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; @@ -153,9 +157,12 @@ public abstract class KeyguardAbsKeyInputViewController arguments = new HashMap<>(); + arguments.put("count", secondsRemaining); + mMessageAreaController.setMessage(PluralsMessageFormatter.format( + mView.getResources(), + arguments, + R.string.kg_too_many_failed_attempts_countdown)); } @Override diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java index 94e07b713915..900f28213114 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPatternViewController.java @@ -23,6 +23,7 @@ import android.content.res.ColorStateList; import android.os.AsyncTask; import android.os.CountDownTimer; import android.os.SystemClock; +import android.util.PluralsMessageFormatter; import android.view.MotionEvent; import android.view.View; @@ -40,7 +41,9 @@ import com.android.systemui.classifier.FalsingClassifier; import com.android.systemui.classifier.FalsingCollector; import com.android.systemui.statusbar.policy.DevicePostureController; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class KeyguardPatternViewController extends KeyguardInputViewController { @@ -368,9 +371,13 @@ public class KeyguardPatternViewController @Override public void onTick(long millisUntilFinished) { final int secondsRemaining = (int) Math.round(millisUntilFinished / 1000.0); - mMessageAreaController.setMessage(mView.getResources().getQuantityString( - R.plurals.kg_too_many_failed_attempts_countdown, - secondsRemaining, secondsRemaining)); + Map arguments = new HashMap<>(); + arguments.put("count", secondsRemaining); + + mMessageAreaController.setMessage(PluralsMessageFormatter.format( + mView.getResources(), + arguments, + R.string.kg_too_many_failed_attempts_countdown)); } @Override -- cgit v1.2.3-59-g8ed1b From 4ffb7e888439cf1d6fd6be4d3b06104c83a1f0d7 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:03:28 +0800 Subject: Clean in BugreportProgressService Bug: 199230228 Test: make Change-Id: I888c65c8819eb5443178ddfc2097d3519b89cefb --- core/res/res/values/strings.xml | 9 +++++---- core/res/res/values/symbols.xml | 2 +- .../src/com/android/shell/BugreportProgressService.java | 12 +++++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 5242b955917b..3fc0d95ded38 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -689,10 +689,11 @@ your device is unresponsive or too slow, or when you need all report sections. Does not allow you to enter more details or take additional screenshots. - - Taking screenshot for bug report in %d second. - Taking screenshot for bug report in %d seconds. - + {count, plural, + =1 {Taking screenshot for bug report in # second.} + other {Taking screenshot for bug report in # seconds.} + } + %s (%s) diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 76e0994d7b18..6fca95926323 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1260,9 +1260,9 @@ - + diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java index c5a01a1c2b93..0b8bd9784b7d 100644 --- a/packages/Shell/src/com/android/shell/BugreportProgressService.java +++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java @@ -72,6 +72,7 @@ import android.text.format.DateUtils; import android.util.Log; import android.util.Pair; import android.util.Patterns; +import android.util.PluralsMessageFormatter; import android.util.SparseArray; import android.view.ContextThemeWrapper; import android.view.IWindowManager; @@ -111,7 +112,9 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -943,9 +946,12 @@ public class BugreportProgressService extends Service { } setTakingScreenshot(true); collapseNotificationBar(); - final String msg = mContext.getResources() - .getQuantityString(com.android.internal.R.plurals.bugreport_countdown, - mScreenshotDelaySec, mScreenshotDelaySec); + Map arguments = new HashMap<>(); + arguments.put("count", mScreenshotDelaySec); + final String msg = PluralsMessageFormatter.format( + mContext.getResources(), + arguments, + com.android.internal.R.string.bugreport_countdown); Log.i(TAG, msg); // Show a toast just once, otherwise it might be captured in the screenshot. Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); -- cgit v1.2.3-59-g8ed1b From 9ebedd8f3e2f315515a0488f18ee080dbe4b5132 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:04:32 +0800 Subject: Clean in FillUi Bug: 199230228 Test: make Change-Id: I0a31b1178e60b2e5acf312b40bf749e433b95475 --- core/res/res/values/strings.xml | 9 +++++---- core/res/res/values/symbols.xml | 2 +- .../autofill/java/com/android/server/autofill/ui/FillUi.java | 10 ++++++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 3fc0d95ded38..9c546a62a547 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -5530,10 +5530,11 @@ No autofill suggestions - - One autofill suggestion - %1$s autofill suggestions - + {count, plural, + =1 {One autofill suggestion} + other {# autofill suggestions} + } + diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 6fca95926323..71e2a21a98ec 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -3506,7 +3506,7 @@ - + diff --git a/services/autofill/java/com/android/server/autofill/ui/FillUi.java b/services/autofill/java/com/android/server/autofill/ui/FillUi.java index 27ea3d618afa..8fbdd81cc4cc 100644 --- a/services/autofill/java/com/android/server/autofill/ui/FillUi.java +++ b/services/autofill/java/com/android/server/autofill/ui/FillUi.java @@ -32,6 +32,7 @@ import android.service.autofill.Dataset; import android.service.autofill.Dataset.DatasetFieldFilter; import android.service.autofill.FillResponse; import android.text.TextUtils; +import android.util.PluralsMessageFormatter; import android.util.Slog; import android.util.TypedValue; import android.view.ContextThemeWrapper; @@ -63,7 +64,9 @@ import com.android.server.autofill.Helper; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -898,8 +901,11 @@ final class FillUi { if (count <= 0) { text = mContext.getString(R.string.autofill_picker_no_suggestions); } else { - text = mContext.getResources().getQuantityString( - R.plurals.autofill_picker_some_suggestions, count, count); + Map arguments = new HashMap<>(); + arguments.put("count", count); + text = PluralsMessageFormatter.format(mContext.getResources(), + arguments, + R.string.autofill_picker_some_suggestions); } mListView.announceForAccessibility(text); } -- cgit v1.2.3-59-g8ed1b From d593f653452ac58a00dae5610fa6deb03af1a2f2 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:05:20 +0800 Subject: Clean in CertificateMonitor Bug: 199230228 Test: make Change-Id: I2b9ff69c1d14f134c94191a89a155b5a872dbc73 --- core/res/res/values/strings.xml | 9 +++++---- core/res/res/values/symbols.xml | 2 +- .../android/server/devicepolicy/CertificateMonitor.java | 12 ++++++++++-- .../server/devicepolicy/DevicePolicyManagerTest.java | 14 ++++++++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 9c546a62a547..7d1705720699 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -463,10 +463,11 @@ - - Certificate authority installed - Certificate authorities installed - + {count, plural, + =1 {Certificate authority installed} + other {Certificate authorities installed} + } + By an unknown third party diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 71e2a21a98ec..c466f8e38c86 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1266,7 +1266,7 @@ - + diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/CertificateMonitor.java b/services/devicepolicy/java/com/android/server/devicepolicy/CertificateMonitor.java index cc385c700e84..a1cba946354f 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/CertificateMonitor.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/CertificateMonitor.java @@ -35,6 +35,7 @@ import android.provider.Settings; import android.security.Credentials; import android.security.KeyChain; import android.security.KeyChain.KeyChainConnection; +import android.util.PluralsMessageFormatter; import com.android.internal.R; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; @@ -46,7 +47,9 @@ import java.io.IOException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class CertificateMonitor { protected static final int MONITORING_CERT_NOTIFICATION_ID = SystemMessage.NOTE_SSL_CERT_INFO; @@ -212,10 +215,15 @@ public class CertificateMonitor { dialogIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE, null, UserHandle.of(parentUserId)); + Map arguments = new HashMap<>(); + arguments.put("count", pendingCertificateCount); + return new Notification.Builder(userContext, SystemNotificationChannels.SECURITY) .setSmallIcon(smallIconId) - .setContentTitle(resources.getQuantityText(R.plurals.ssl_ca_cert_warning, - pendingCertificateCount)) + .setContentTitle(PluralsMessageFormatter.format( + resources, + arguments, + R.string.ssl_ca_cert_warning)) .setContentText(contentText) .setContentIntent(notifyIntent) .setShowWhen(false) diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java index 3c809f9e4814..df306e63cd1d 100644 --- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java @@ -273,6 +273,14 @@ public class DevicePolicyManagerTest extends DpmTestBase { mIsAutomotive = mContext.getPackageManager() .hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); + + final String TEST_STRING = "{count, plural,\n" + + " =1 {Test for exactly 1 cert out of 4}\n" + + " other {Test for exactly # certs out of 4}\n" + + "}"; + doReturn(TEST_STRING) + .when(mContext.resources) + .getString(R.string.ssl_ca_cert_warning); } private TransferOwnershipMetadataManager getMockTransferMetadataManager() { @@ -1767,9 +1775,6 @@ public class DevicePolicyManagerTest extends DpmTestBase { StringParceledListSlice oneCert = asSlice(new String[] {"1"}); StringParceledListSlice fourCerts = asSlice(new String[] {"1", "2", "3", "4"}); - final String TEST_STRING = "Test for exactly 2 certs out of 4"; - doReturn(TEST_STRING).when(mContext.resources).getQuantityText(anyInt(), eq(2)); - // Given that we have exactly one certificate installed, when(getServices().keyChainConnection.getService().getUserCaAliases()).thenReturn(oneCert); // when that certificate is approved, @@ -1785,9 +1790,10 @@ public class DevicePolicyManagerTest extends DpmTestBase { dpms.approveCaCert(fourCerts.getList().get(0), userId, true); dpms.approveCaCert(fourCerts.getList().get(1), userId, true); // a notification should be shown saying that there are two certificates left to approve. + final String TEST_STRING_RESULT = "Test for exactly 2 certs out of 4"; verify(getServices().notificationManager, timeout(1000)) .notifyAsUser(anyString(), anyInt(), argThat(hasExtra(EXTRA_TITLE, - TEST_STRING + TEST_STRING_RESULT )), eq(user)); } -- cgit v1.2.3-59-g8ed1b From e4692b7caecfab2adddb0907318e124b3f7a48ab Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Mon, 17 Jan 2022 12:05:48 +0800 Subject: 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 | 37 ++++---- 4 files changed, 136 insertions(+), 142 deletions(-) diff --git a/core/java/android/webkit/DateSorter.java b/core/java/android/webkit/DateSorter.java index 90d44db3eff4..c29e774c9614 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 955552289c3a..2c6264783f56 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 7d1705720699..fdd3601bbc59 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 c466f8e38c86..a9125adbd96f 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1260,10 +1260,9 @@ - - + @@ -3098,23 +3097,23 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + -- cgit v1.2.3-59-g8ed1b