• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.tv.settings.util;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.drawable.Drawable;
22 
23 public final class ResourcesUtil {
24     private static final String SETTINGS_PACKAGE_NAME = "com.android.tv.settings";
25 
getString(Context context, String name)26     public static String getString(Context context, String name) {
27         try {
28             Resources resources = context.getPackageManager()
29                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
30             int id = resources.getIdentifier(name, "string", SETTINGS_PACKAGE_NAME);
31             if (id != 0) {
32                 return resources.getString(id);
33             }
34             return null;
35         } catch (Exception e) {
36             return null;
37         }
38     }
39 
getBoolean(Context context, String name)40     public static boolean getBoolean(Context context, String name) {
41         try {
42             Resources resources = context.getPackageManager()
43                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
44             int id = resources.getIdentifier(name, "string", SETTINGS_PACKAGE_NAME);
45             if (id != 0) {
46                 return resources.getBoolean(id);
47             }
48             return false;
49         } catch (Exception e) {
50             return false;
51         }
52     }
53 
getQuantityString(Context context, String name, int count, Object... formatArgs)54     public static String getQuantityString(Context context, String name, int count,
55             Object... formatArgs) {
56 
57         try {
58             Resources resources = context.getPackageManager()
59                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
60             int id = resources.getIdentifier(name, "string", SETTINGS_PACKAGE_NAME);
61             if (id != 0) {
62                 return resources.getQuantityString(id, count, formatArgs);
63             }
64             return null;
65         } catch (Exception e) {
66             return null;
67         }
68     }
69 
getString(Context context, String name, Object... formatArgs)70     public static String getString(Context context, String name, Object... formatArgs) {
71         try {
72             Resources resources = context.getPackageManager()
73                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
74             int id = resources.getIdentifier(name, "string", SETTINGS_PACKAGE_NAME);
75             if (id != 0) {
76                 return resources.getString(id, formatArgs);
77             }
78             return null;
79         } catch (Exception e) {
80             return null;
81         }
82     }
83 
84 
getDrawable(Context context, String name)85     public static Drawable getDrawable(Context context, String name) {
86         try {
87             Resources resources = context.getPackageManager()
88                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
89             int id = resources.getIdentifier(name, "drawable", SETTINGS_PACKAGE_NAME);
90             if (id != 0) {
91                 return resources.getDrawable(id);
92             }
93             return null;
94         } catch (Exception e) {
95             return null;
96         }
97     }
98 
getStringArray(Context context, String name)99     public static String[] getStringArray(Context context, String name) {
100         try {
101             Resources resources = context.getPackageManager()
102                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
103             int id = resources.getIdentifier(name, "array", SETTINGS_PACKAGE_NAME);
104             if (id != 0) {
105                 return resources.getStringArray(id);
106             }
107             return null;
108         } catch (Exception e) {
109             return null;
110         }
111     }
112 
getIntArray(Context context, String name)113     public static int[] getIntArray(Context context, String name) {
114         try {
115             Resources resources = context.getPackageManager()
116                     .getResourcesForApplication(SETTINGS_PACKAGE_NAME);
117             int id = resources.getIdentifier(name, "array", SETTINGS_PACKAGE_NAME);
118             if (id != 0) {
119                 return resources.getIntArray(id);
120             }
121             return null;
122         } catch (Exception e) {
123             return null;
124         }
125     }
126 }
127