termux-app
136 строк · 5.8 Кб
1package com.termux.shared.android.resource;2
3import android.content.Context;4
5import androidx.annotation.NonNull;6import androidx.annotation.Nullable;7
8import com.termux.shared.data.DataUtils;9import com.termux.shared.logger.Logger;10
11public class ResourceUtils {12
13public static final String RES_TYPE_COLOR = "color";14public static final String RES_TYPE_DRAWABLE = "drawable";15public static final String RES_TYPE_ID = "id";16public static final String RES_TYPE_LAYOUT = "layout";17public static final String RES_TYPE_STRING = "string";18public static final String RES_TYPE_STYLE = "style";19
20
21private static final String LOG_TAG = "ResourceUtils";22
23
24/** Wrapper for {@link #getResourceId(Context, String, String, String, boolean)} without {@code defPackage}. */25@Nullable26public static Integer getResourceId(@NonNull Context context, String name,27@Nullable String defType,28boolean logErrorMessage) {29return getResourceId(context, name, defType, null, logErrorMessage);30}31
32/**33* Get resource identifier for the given resource name. A fully qualified resource name is of
34* the form "package:type/entry". The first two components (package and type) are optional if
35* defType and defPackage, respectively, are specified here.
36*
37* @param context The {@link Context} for operations.
38* @param name The name of the desired resource.
39* @param defType Optional default resource type to find, if "type/" is not included in the name.
40* Can be null to require an explicit type.
41* @param defPackage Optional default package to find, if "package:" is not included in the name.
42* Can be null to require an explicit package.
43* @param logErrorMessage If an error message should be logged if failed to find resource.
44* @return Returns the resource identifier if found. Otherwise {@code null} if an exception was
45* raised or resource was not found.
46*/
47@Nullable48public static Integer getResourceId(@NonNull Context context, String name,49@Nullable String defType, @Nullable String defPackage,50boolean logErrorMessage) {51if (DataUtils.isNullOrEmpty(name)) return null;52
53Integer resourceId = null;54try {55resourceId = context.getResources().getIdentifier(name, defType, defPackage);56if (resourceId == 0) resourceId = null;57} catch (Exception e) {58// Ignore59}60
61if (resourceId == null && logErrorMessage) {62Logger.logError(LOG_TAG, "Resource id not found. name: \"" + name + "\", type: \"" + defType+ "\", package: \"" + defPackage + "\", component \"" + context.getClass().getName() + "\"");63}64
65return resourceId;66}67
68
69
70/**71* Get resource identifier for the given {@link #RES_TYPE_COLOR} resource name.
72*
73* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
74*/
75@Nullable76public static Integer getColorResourceId(@NonNull Context context, String name,77@Nullable String defPackage, boolean logErrorMessage) {78return getResourceId(context, name, RES_TYPE_COLOR, defPackage, logErrorMessage);79}80
81/**82* Get resource identifier for the given {@link #RES_TYPE_DRAWABLE} resource name.
83*
84* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
85*/
86@Nullable87public static Integer getDrawableResourceId(@NonNull Context context, String name,88@Nullable String defPackage, boolean logErrorMessage) {89return getResourceId(context, name, RES_TYPE_DRAWABLE, defPackage, logErrorMessage);90}91
92/**93* Get resource identifier for the given {@link #RES_TYPE_ID} resource name.
94*
95* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
96*/
97@Nullable98public static Integer getIdResourceId(@NonNull Context context, String name,99@Nullable String defPackage, boolean logErrorMessage) {100return getResourceId(context, name, RES_TYPE_ID, defPackage, logErrorMessage);101}102
103/**104* Get resource identifier for the given {@link #RES_TYPE_LAYOUT} resource name.
105*
106* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
107*/
108@Nullable109public static Integer getLayoutResourceId(@NonNull Context context, String name,110@Nullable String defPackage, boolean logErrorMessage) {111return getResourceId(context, name, RES_TYPE_LAYOUT, defPackage, logErrorMessage);112}113
114/**115* Get resource identifier for the given {@link #RES_TYPE_STRING} resource name.
116*
117* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
118*/
119@Nullable120public static Integer getStringResourceId(@NonNull Context context, String name,121@Nullable String defPackage, boolean logErrorMessage) {122return getResourceId(context, name, RES_TYPE_STRING, defPackage, logErrorMessage);123}124
125/**126* Get resource identifier for the given {@link #RES_TYPE_STYLE} resource name.
127*
128* This is a wrapper for {@link #getResourceId(Context, String, String, String, boolean)}.
129*/
130@Nullable131public static Integer getStyleResourceId(@NonNull Context context, String name,132@Nullable String defPackage, boolean logErrorMessage) {133return getResourceId(context, name, RES_TYPE_STYLE, defPackage, logErrorMessage);134}135
136}
137