termux-app

Форк
0
136 строк · 5.8 Кб
1
package com.termux.shared.android.resource;
2

3
import android.content.Context;
4

5
import androidx.annotation.NonNull;
6
import androidx.annotation.Nullable;
7

8
import com.termux.shared.data.DataUtils;
9
import com.termux.shared.logger.Logger;
10

11
public class ResourceUtils {
12

13
    public static final String RES_TYPE_COLOR = "color";
14
    public static final String RES_TYPE_DRAWABLE = "drawable";
15
    public static final String RES_TYPE_ID = "id";
16
    public static final String RES_TYPE_LAYOUT = "layout";
17
    public static final String RES_TYPE_STRING = "string";
18
    public static final String RES_TYPE_STYLE = "style";
19

20

21
    private static final String LOG_TAG = "ResourceUtils";
22

23

24
    /** Wrapper for {@link #getResourceId(Context, String, String, String, boolean)} without {@code defPackage}. */
25
    @Nullable
26
    public static Integer getResourceId(@NonNull Context context, String name,
27
                                        @Nullable String defType,
28
                                        boolean logErrorMessage) {
29
        return 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
    @Nullable
48
    public static Integer getResourceId(@NonNull Context context, String name,
49
                                        @Nullable String defType, @Nullable String defPackage,
50
                                        boolean logErrorMessage) {
51
        if (DataUtils.isNullOrEmpty(name)) return null;
52

53
        Integer resourceId = null;
54
        try {
55
            resourceId = context.getResources().getIdentifier(name, defType, defPackage);
56
            if (resourceId == 0) resourceId = null;
57
        } catch (Exception e) {
58
            // Ignore
59
        }
60

61
        if (resourceId == null && logErrorMessage) {
62
            Logger.logError(LOG_TAG, "Resource id not found. name: \"" + name + "\", type: \"" + defType+ "\", package: \"" + defPackage + "\", component \"" + context.getClass().getName() + "\"");
63
        }
64

65
        return 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
    @Nullable
76
    public static Integer getColorResourceId(@NonNull Context context, String name,
77
                                             @Nullable String defPackage, boolean logErrorMessage) {
78
        return 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
    @Nullable
87
    public static Integer getDrawableResourceId(@NonNull Context context, String name,
88
                                                @Nullable String defPackage, boolean logErrorMessage) {
89
        return 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
    @Nullable
98
    public static Integer getIdResourceId(@NonNull Context context, String name,
99
                                          @Nullable String defPackage, boolean logErrorMessage) {
100
        return 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
    @Nullable
109
    public static Integer getLayoutResourceId(@NonNull Context context, String name,
110
                                              @Nullable String defPackage, boolean logErrorMessage) {
111
        return 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
    @Nullable
120
    public static Integer getStringResourceId(@NonNull Context context, String name,
121
                                              @Nullable String defPackage, boolean logErrorMessage) {
122
        return 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
    @Nullable
131
    public static Integer getStyleResourceId(@NonNull Context context, String name,
132
                                             @Nullable String defPackage, boolean logErrorMessage) {
133
        return getResourceId(context, name, RES_TYPE_STYLE, defPackage, logErrorMessage);
134
    }
135

136
}
137

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.