1 /*
2  * Copyright (C) 2020 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 package com.android.settings.testutils;
17 
18 import android.content.Context;
19 
20 /**
21  * Test util to provide the correct resources.
22  */
23 public final class ResourcesUtils {
24     /**
25      * Return a resource identifier for the given resource name.
26      * @param context Context to use.
27      * @param type Optional default resource type to find, if "type/" is not included in the name.
28      *             Can be null to require an explicit type.
29      * @param name The name of the desired resource.
30      * @return The associated resource identifier. Returns 0 if no such resource was found.
31      * (0 is not a valid resource ID.)
32      */
getResourcesId(Context context, String type, String name)33     public static int getResourcesId(Context context, String type, String name) {
34         return context.getResources().getIdentifier(name, type, context.getPackageName());
35     }
36 
37     /**
38      * Returns a localized string from the application's package's default string table.
39      * @param context Context to use.
40      * @param name The name of the desired resource.
41      * @return The string data associated with the resource, stripped of styled text information.
42      */
getResourcesString(Context context, String name)43     public static String getResourcesString(Context context, String name) {
44         return context.getResources().getString(getResourcesId(context, "string", name));
45     }
46 
47     /**
48      * Return the string value associated with a particular neame of resource,
49      * substituting the format arguments as defined in {@link java.util.Formatter}
50      * and {@link java.lang.String#format}. It will be stripped of any styled text
51      * information.
52      * @param context Context to use.
53      * @param name The name of the desired resource.
54      * @param value The format arguments that will be used for substitution.
55      * @return The string data associated with the resource, stripped of styled text information.
56      */
getResourcesString(Context context, String name, Object... value)57     public static String getResourcesString(Context context, String name, Object... value) {
58         return context.getResources().getString(getResourcesId(context, "string", name), value);
59     }
60 }
61