1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.permission.utils;
18 
19 import android.content.Context;
20 import android.icu.text.MessageFormat;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.StringRes;
24 
25 import java.util.HashMap;
26 import java.util.Locale;
27 import java.util.Map;
28 
29 /** Utils related to strings and string formatting */
30 public class StringUtils {
31 
32     /**
33      * Returns plural string formatted using the provided resource id with icu syntax and count
34      *
35      * @param context Context used to get string resource
36      * @param stringResId String resource with icu syntax to be used for formatting
37      * @param count Count used to format the plural string
38      * @param formatArgs String arguments used for substitution
39      */
getIcuPluralsString(@onNull Context context, @StringRes int stringResId, int count, Object... formatArgs)40     public static String getIcuPluralsString(@NonNull Context context, @StringRes int stringResId,
41             int count, Object... formatArgs) {
42         MessageFormat msgFormat = new MessageFormat(
43                 context.getResources().getString(stringResId, formatArgs),
44                 Locale.getDefault());
45         Map<String, Object> arguments = new HashMap<>();
46         arguments.put("count", count);
47         return msgFormat.format(arguments);
48     }
49 }
50