args = new HashMap();
+ args.put("app_name", appName);
+ args.put("count", notificationCount);
+ return icuCountFormat.format(args);
+ }
}
diff --git a/src/com/android/launcher3/util/PluralMessageFormat.java b/src/com/android/launcher3/util/PluralMessageFormat.java
new file mode 100644
index 0000000000..5e4ce8d982
--- /dev/null
+++ b/src/com/android/launcher3/util/PluralMessageFormat.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2021 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 com.android.launcher3.util;
+
+import android.content.Context;
+import android.icu.text.MessageFormat;
+
+import androidx.annotation.StringRes;
+
+import java.util.HashMap;
+import java.util.Locale;
+
+/** A helper class to format common ICU plural strings. */
+public class PluralMessageFormat {
+
+ /**
+ * Returns a plural string from a ICU format message template, which takes "count" as an
+ * argument.
+ *
+ * An example of ICU format message template provided by {@code stringId}:
+ * {count, plural, =1{# widget} other{# widgets}}
+ */
+ public static final String getIcuPluralString(Context context, @StringRes int stringId,
+ int count) {
+ MessageFormat icuCountFormat = new MessageFormat(
+ context.getResources().getString(stringId),
+ Locale.getDefault());
+ HashMap args = new HashMap();
+ args.put("count", count);
+ return icuCountFormat.format(args);
+ }
+}
diff --git a/src/com/android/launcher3/widget/picker/WidgetsListHeader.java b/src/com/android/launcher3/widget/picker/WidgetsListHeader.java
index ef2adbb809..ebd2d1014c 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsListHeader.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsListHeader.java
@@ -39,6 +39,7 @@ import com.android.launcher3.icons.PlaceHolderIconDrawable;
import com.android.launcher3.icons.cache.HandlerRunnable;
import com.android.launcher3.model.data.ItemInfoWithIcon;
import com.android.launcher3.model.data.PackageItemInfo;
+import com.android.launcher3.util.PluralMessageFormat;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.model.WidgetsListHeaderEntry;
import com.android.launcher3.widget.model.WidgetsListSearchHeaderEntry;
@@ -217,18 +218,18 @@ public final class WidgetsListHeader extends LinearLayout implements ItemInfoUpd
String subtitle;
if (entry.widgetsCount > 0 && entry.shortcutsCount > 0) {
- String widgetsCount = resources.getQuantityString(R.plurals.widgets_count,
- entry.widgetsCount, entry.widgetsCount);
- String shortcutsCount = resources.getQuantityString(R.plurals.shortcuts_count,
- entry.shortcutsCount, entry.shortcutsCount);
+ String widgetsCount = PluralMessageFormat.getIcuPluralString(getContext(),
+ R.string.widgets_count, entry.widgetsCount);
+ String shortcutsCount = PluralMessageFormat.getIcuPluralString(getContext(),
+ R.string.shortcuts_count, entry.shortcutsCount);
subtitle = resources.getString(R.string.widgets_and_shortcuts_count, widgetsCount,
shortcutsCount);
} else if (entry.widgetsCount > 0) {
- subtitle = resources.getQuantityString(R.plurals.widgets_count,
- entry.widgetsCount, entry.widgetsCount);
+ subtitle = PluralMessageFormat.getIcuPluralString(getContext(),
+ R.string.widgets_count, entry.widgetsCount);
} else {
- subtitle = resources.getQuantityString(R.plurals.shortcuts_count,
- entry.shortcutsCount, entry.shortcutsCount);
+ subtitle = PluralMessageFormat.getIcuPluralString(getContext(),
+ R.string.shortcuts_count, entry.shortcutsCount);
}
mSubtitle.setText(subtitle);
mSubtitle.setVisibility(VISIBLE);