termux-app
99 строк · 4.0 Кб
1package com.termux.shared.android;
2
3import android.content.Context;
4import android.provider.Settings;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8
9import com.termux.shared.logger.Logger;
10
11public class SettingsProviderUtils {
12
13private static final String LOG_TAG = "SettingsProviderUtils";
14
15/** The namespaces for {@link Settings} provider. */
16public enum SettingNamespace {
17/** The {@link Settings.Global} namespace */
18GLOBAL,
19
20/** The {@link Settings.Secure} namespace */
21SECURE,
22
23/** The {@link Settings.System} namespace */
24SYSTEM
25}
26
27/** The type of values for {@link Settings} provider. */
28public enum SettingType {
29FLOAT,
30INT,
31LONG,
32STRING,
33URI
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
47public static Object getSettingsValue(@NonNull Context context, @NonNull SettingNamespace namespace,
48@NonNull SettingType type, @NonNull String key, @Nullable Object def) {
49try {
50switch (namespace) {
51case GLOBAL:
52switch (type) {
53case FLOAT:
54return Settings.Global.getFloat(context.getContentResolver(), key);
55case INT:
56return Settings.Global.getInt(context.getContentResolver(), key);
57case LONG:
58return Settings.Global.getLong(context.getContentResolver(), key);
59case STRING:
60return Settings.Global.getString(context.getContentResolver(), key);
61case URI:
62return Settings.Global.getUriFor(key);
63}
64case SECURE:
65switch (type) {
66case FLOAT:
67return Settings.Secure.getFloat(context.getContentResolver(), key);
68case INT:
69return Settings.Secure.getInt(context.getContentResolver(), key);
70case LONG:
71return Settings.Secure.getLong(context.getContentResolver(), key);
72case STRING:
73return Settings.Secure.getString(context.getContentResolver(), key);
74case URI:
75return Settings.Secure.getUriFor(key);
76}
77case SYSTEM:
78switch (type) {
79case FLOAT:
80return Settings.System.getFloat(context.getContentResolver(), key);
81case INT:
82return Settings.System.getInt(context.getContentResolver(), key);
83case LONG:
84return Settings.System.getLong(context.getContentResolver(), key);
85case STRING:
86return Settings.System.getString(context.getContentResolver(), key);
87case URI:
88return Settings.System.getUriFor(key);
89}
90}
91} catch (Settings.SettingNotFoundException e) {
92// Ignore
93} catch (Exception e) {
94Logger.logError(LOG_TAG, "Failed to get \"" + key + "\" key value from settings \"" + namespace.name() + "\" namespace of type \"" + type.name() + "\"");
95}
96return def;
97}
98
99}
100