termux-app

Форк
0
99 строк · 4.0 Кб
1
package com.termux.shared.android;
2

3
import android.content.Context;
4
import android.provider.Settings;
5

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

9
import com.termux.shared.logger.Logger;
10

11
public class SettingsProviderUtils {
12

13
    private static final String LOG_TAG = "SettingsProviderUtils";
14

15
    /** The namespaces for {@link Settings} provider. */
16
    public enum SettingNamespace {
17
        /** The {@link Settings.Global} namespace */
18
        GLOBAL,
19

20
        /** The {@link Settings.Secure} namespace */
21
        SECURE,
22

23
        /** The {@link Settings.System} namespace */
24
        SYSTEM
25
    }
26

27
    /** The type of values for {@link Settings} provider. */
28
    public enum SettingType {
29
        FLOAT,
30
        INT,
31
        LONG,
32
        STRING,
33
        URI
34
    }
35

36
    /**
37
     * Get settings key value from {@link SettingNamespace} namespace and of {@link SettingType} type.
38
     *
39
     * @param context The {@link Context} for operations.
40
     * @param namespace The {@link SettingNamespace} in which to get key value from.
41
     * @param type The {@link SettingType} for the key.
42
     * @param key The {@link String} name for key.
43
     * @param def The {@link Object} default value for key.
44
     * @return Returns the key value. This will be {@code null} if an exception is raised.
45
     */
46
    @Nullable
47
    public static Object getSettingsValue(@NonNull Context context, @NonNull SettingNamespace namespace,
48
                                          @NonNull SettingType type, @NonNull String key, @Nullable Object def) {
49
        try {
50
            switch (namespace) {
51
                case GLOBAL:
52
                    switch (type) {
53
                        case FLOAT:
54
                            return Settings.Global.getFloat(context.getContentResolver(), key);
55
                        case INT:
56
                            return Settings.Global.getInt(context.getContentResolver(), key);
57
                        case LONG:
58
                            return Settings.Global.getLong(context.getContentResolver(), key);
59
                        case STRING:
60
                            return Settings.Global.getString(context.getContentResolver(), key);
61
                        case URI:
62
                            return Settings.Global.getUriFor(key);
63
                    }
64
                case SECURE:
65
                    switch (type) {
66
                        case FLOAT:
67
                            return Settings.Secure.getFloat(context.getContentResolver(), key);
68
                        case INT:
69
                            return Settings.Secure.getInt(context.getContentResolver(), key);
70
                        case LONG:
71
                            return Settings.Secure.getLong(context.getContentResolver(), key);
72
                        case STRING:
73
                            return Settings.Secure.getString(context.getContentResolver(), key);
74
                        case URI:
75
                            return Settings.Secure.getUriFor(key);
76
                    }
77
                case SYSTEM:
78
                    switch (type) {
79
                        case FLOAT:
80
                            return Settings.System.getFloat(context.getContentResolver(), key);
81
                        case INT:
82
                            return Settings.System.getInt(context.getContentResolver(), key);
83
                        case LONG:
84
                            return Settings.System.getLong(context.getContentResolver(), key);
85
                        case STRING:
86
                            return Settings.System.getString(context.getContentResolver(), key);
87
                        case URI:
88
                            return Settings.System.getUriFor(key);
89
                    }
90
            }
91
        } catch (Settings.SettingNotFoundException e) {
92
            // Ignore
93
        } catch (Exception e) {
94
            Logger.logError(LOG_TAG, "Failed to get \"" + key + "\" key value from settings \"" + namespace.name() + "\" namespace of type \"" + type.name() + "\"");
95
        }
96
        return def;
97
    }
98

99
}
100

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

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

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

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