From 42eee49d308958ee23376eb6e029a7bf61907f94 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sun, 12 Jun 2022 02:35:46 +0500 Subject: [PATCH] Added: Add `SettingsProviderUtils` to get `Setting` global, secure and system namespace values --- .../shared/android/SettingsProviderUtils.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 termux-shared/src/main/java/com/termux/shared/android/SettingsProviderUtils.java diff --git a/termux-shared/src/main/java/com/termux/shared/android/SettingsProviderUtils.java b/termux-shared/src/main/java/com/termux/shared/android/SettingsProviderUtils.java new file mode 100644 index 00000000..4b7cde59 --- /dev/null +++ b/termux-shared/src/main/java/com/termux/shared/android/SettingsProviderUtils.java @@ -0,0 +1,99 @@ +package com.termux.shared.android; + +import android.content.Context; +import android.provider.Settings; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.termux.shared.logger.Logger; + +public class SettingsProviderUtils { + + private static final String LOG_TAG = "SettingsProviderUtils"; + + /** The namespaces for {@link Settings} provider. */ + public enum SettingNamespace { + /** The {@link Settings.Global} namespace */ + GLOBAL, + + /** The {@link Settings.Secure} namespace */ + SECURE, + + /** The {@link Settings.System} namespace */ + SYSTEM + } + + /** The type of values for {@link Settings} provider. */ + public enum SettingType { + FLOAT, + INT, + LONG, + STRING, + URI + } + + /** + * Get settings key value from {@link SettingNamespace} namespace and of {@link SettingType} type. + * + * @param context The {@link Context} for operations. + * @param namespace The {@link SettingNamespace} in which to get key value from. + * @param type The {@link SettingType} for the key. + * @param key The {@link String} name for key. + * @param def The {@link Object} default value for key. + * @return Returns the key value. This will be {@code null} if an exception is raised. + */ + @Nullable + public static Object getSettingsValue(@NonNull Context context, @NonNull SettingNamespace namespace, + @NonNull SettingType type, @NonNull String key, @Nullable Object def) { + try { + switch (namespace) { + case GLOBAL: + switch (type) { + case FLOAT: + return Settings.Global.getFloat(context.getContentResolver(), key); + case INT: + return Settings.Global.getInt(context.getContentResolver(), key); + case LONG: + return Settings.Global.getLong(context.getContentResolver(), key); + case STRING: + return Settings.Global.getString(context.getContentResolver(), key); + case URI: + return Settings.Global.getUriFor(key); + } + case SECURE: + switch (type) { + case FLOAT: + return Settings.Secure.getFloat(context.getContentResolver(), key); + case INT: + return Settings.Secure.getInt(context.getContentResolver(), key); + case LONG: + return Settings.Secure.getLong(context.getContentResolver(), key); + case STRING: + return Settings.Secure.getString(context.getContentResolver(), key); + case URI: + return Settings.Secure.getUriFor(key); + } + case SYSTEM: + switch (type) { + case FLOAT: + return Settings.System.getFloat(context.getContentResolver(), key); + case INT: + return Settings.System.getInt(context.getContentResolver(), key); + case LONG: + return Settings.System.getLong(context.getContentResolver(), key); + case STRING: + return Settings.System.getString(context.getContentResolver(), key); + case URI: + return Settings.System.getUriFor(key); + } + } + } catch (Settings.SettingNotFoundException e) { + // Ignore + } catch (Exception e) { + Logger.logError(LOG_TAG, "Failed to get \"" + key + "\" key value from settings \"" + namespace.name() + "\" namespace of type \"" + type.name() + "\""); + } + return def; + } + +}