|
| 1 | +package com.termux.app.settings.preferences; |
| 2 | + |
| 3 | +import android.content.SharedPreferences; |
| 4 | + |
| 5 | +import com.termux.app.utils.Logger; |
| 6 | + |
| 7 | +public class SharedPreferenceUtils { |
| 8 | + |
| 9 | + private static final String LOG_TAG = "SharedPreferenceUtils"; |
| 10 | + |
| 11 | + /** |
| 12 | + * Get a {@code boolean} from {@link SharedPreferences}. |
| 13 | + * |
| 14 | + * @param sharedPreferences The {@link SharedPreferences} to get the value from. |
| 15 | + * @param key The key for the value. |
| 16 | + * @param def The default value if failed to read a valid value. |
| 17 | + * @return Returns the {@code boolean} value stored in {@link SharedPreferences}, otherwise returns |
| 18 | + * default if failed to read a valid value, like in case of an exception. |
| 19 | + */ |
| 20 | + public static boolean getBoolean(SharedPreferences sharedPreferences, String key, boolean def) { |
| 21 | + if(sharedPreferences == null) { |
| 22 | + Logger.logError(LOG_TAG, "Error getting boolean value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\"."); |
| 23 | + return def; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + return sharedPreferences.getBoolean(key, def); |
| 28 | + } |
| 29 | + catch (ClassCastException e) { |
| 30 | + Logger.logStackTraceWithMessage(LOG_TAG, "Error getting boolean value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e); |
| 31 | + return def; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Set a {@code boolean} in {@link SharedPreferences}. |
| 37 | + * |
| 38 | + * @param sharedPreferences The {@link SharedPreferences} to set the value in. |
| 39 | + * @param key The key for the value. |
| 40 | + * @param value The value to store. |
| 41 | + */ |
| 42 | + public static void setBoolean(SharedPreferences sharedPreferences, String key, boolean value) { |
| 43 | + if(sharedPreferences == null) { |
| 44 | + Logger.logError(LOG_TAG, "Ignoring setting boolean value \"" + value + "\" for the \"" + key + "\" key into null shared preferences."); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + sharedPreferences.edit().putBoolean(key, value).apply(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Get an {@code int} from {@link SharedPreferences}. |
| 55 | + * |
| 56 | + * @param sharedPreferences The {@link SharedPreferences} to get the value from. |
| 57 | + * @param key The key for the value. |
| 58 | + * @param def The default value if failed to read a valid value. |
| 59 | + * @return Returns the {@code int} value stored in {@link SharedPreferences}, otherwise returns |
| 60 | + * default if failed to read a valid value, like in case of an exception. |
| 61 | + */ |
| 62 | + public static int getInt(SharedPreferences sharedPreferences, String key, int def) { |
| 63 | + if(sharedPreferences == null) { |
| 64 | + Logger.logError(LOG_TAG, "Error getting int value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\"."); |
| 65 | + return def; |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + return sharedPreferences.getInt(key, def); |
| 70 | + } |
| 71 | + catch (ClassCastException e) { |
| 72 | + Logger.logStackTraceWithMessage(LOG_TAG, "Error getting int value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e); |
| 73 | + return def; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set an {@code int} in {@link SharedPreferences}. |
| 79 | + * |
| 80 | + * @param sharedPreferences The {@link SharedPreferences} to set the value in. |
| 81 | + * @param key The key for the value. |
| 82 | + * @param value The value to store. |
| 83 | + */ |
| 84 | + public static void setInt(SharedPreferences sharedPreferences, String key, int value) { |
| 85 | + if(sharedPreferences == null) { |
| 86 | + Logger.logError(LOG_TAG, "Ignoring setting int value \"" + value + "\" for the \"" + key + "\" key into null shared preferences."); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + sharedPreferences.edit().putInt(key, value).apply(); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Get a {@code String} from {@link SharedPreferences}. |
| 97 | + * |
| 98 | + * @param sharedPreferences The {@link SharedPreferences} to get the value from. |
| 99 | + * @param key The key for the value. |
| 100 | + * @param def The default value if failed to read a valid value. |
| 101 | + * @return Returns the {@code String} value stored in {@link SharedPreferences}, otherwise returns |
| 102 | + * default if failed to read a valid value, like in case of an exception. |
| 103 | + */ |
| 104 | + public static String getString(SharedPreferences sharedPreferences, String key, String def) { |
| 105 | + if(sharedPreferences == null) { |
| 106 | + Logger.logError(LOG_TAG, "Error getting String value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\"."); |
| 107 | + return def; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + return sharedPreferences.getString(key, def); |
| 112 | + } |
| 113 | + catch (ClassCastException e) { |
| 114 | + Logger.logStackTraceWithMessage(LOG_TAG, "Error getting String value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e); |
| 115 | + return def; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Set a {@code String} in {@link SharedPreferences}. |
| 121 | + * |
| 122 | + * @param sharedPreferences The {@link SharedPreferences} to set the value in. |
| 123 | + * @param key The key for the value. |
| 124 | + * @param value The value to store. |
| 125 | + */ |
| 126 | + public static void setString(SharedPreferences sharedPreferences, String key, String value) { |
| 127 | + if(sharedPreferences == null) { |
| 128 | + Logger.logError(LOG_TAG, "Ignoring setting String value \"" + value + "\" for the \"" + key + "\" key into null shared preferences."); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + sharedPreferences.edit().putString(key, value).apply(); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Get an {@code int} from {@link SharedPreferences} that is stored as a {@link String}. |
| 139 | + * |
| 140 | + * @param sharedPreferences The {@link SharedPreferences} to get the value from. |
| 141 | + * @param key The key for the value. |
| 142 | + * @param def The default value if failed to read a valid value. |
| 143 | + * @return Returns the {@code int} value after parsing the {@link String} value stored in |
| 144 | + * {@link SharedPreferences}, otherwise returns default if failed to read a valid value, |
| 145 | + * like in case of an exception. |
| 146 | + */ |
| 147 | + public static int getIntStoredAsString(SharedPreferences sharedPreferences, String key, int def) { |
| 148 | + if(sharedPreferences == null) { |
| 149 | + Logger.logError(LOG_TAG, "Error getting int value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\"."); |
| 150 | + return def; |
| 151 | + } |
| 152 | + |
| 153 | + String stringValue; |
| 154 | + int intValue; |
| 155 | + |
| 156 | + try { |
| 157 | + stringValue = sharedPreferences.getString(key, Integer.toString(def)); |
| 158 | + if(stringValue != null) |
| 159 | + intValue = Integer.parseInt(stringValue); |
| 160 | + else |
| 161 | + intValue = def; |
| 162 | + } catch (NumberFormatException | ClassCastException e) { |
| 163 | + intValue = def; |
| 164 | + } |
| 165 | + |
| 166 | + return intValue; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Set an {@code int} into {@link SharedPreferences} that is stored as a {@link String}. |
| 171 | + * |
| 172 | + * @param sharedPreferences The {@link SharedPreferences} to set the value in. |
| 173 | + * @param key The key for the value. |
| 174 | + * @param value The value to store. |
| 175 | + */ |
| 176 | + public static void setIntStoredAsString(SharedPreferences sharedPreferences, String key, int value) { |
| 177 | + if(sharedPreferences == null) { |
| 178 | + Logger.logError(LOG_TAG, "Ignoring setting int value \"" + value + "\" for the \"" + key + "\" key into null shared preferences."); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + sharedPreferences.edit().putString(key, Integer.toString(value)).apply(); |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments