mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added/Fixed: Add support to consider empty String values as null for SharedPreferences
This commit is contained in:
@@ -245,17 +245,22 @@ public class SharedPreferenceUtils {
|
||||
* @param sharedPreferences The {@link SharedPreferences} to get the value from.
|
||||
* @param key The key for the value.
|
||||
* @param def The default value if failed to read a valid value.
|
||||
* @param defIfEmpty If set to {@code true}, then {@code def} will be returned if value is empty.
|
||||
* @return Returns the {@code String} value stored in {@link SharedPreferences}, otherwise returns
|
||||
* default if failed to read a valid value, like in case of an exception.
|
||||
*/
|
||||
public static String getString(SharedPreferences sharedPreferences, String key, String def) {
|
||||
public static String getString(SharedPreferences sharedPreferences, String key, String def, boolean defIfEmpty) {
|
||||
if (sharedPreferences == null) {
|
||||
Logger.logError(LOG_TAG, "Error getting String value for the \"" + key + "\" key from null shared preferences. Returning default value \"" + def + "\".");
|
||||
return def;
|
||||
}
|
||||
|
||||
try {
|
||||
return sharedPreferences.getString(key, def);
|
||||
String value = sharedPreferences.getString(key, def);
|
||||
if (defIfEmpty && (value == null || value.isEmpty()))
|
||||
return def;
|
||||
else
|
||||
return value;
|
||||
}
|
||||
catch (ClassCastException e) {
|
||||
Logger.logStackTraceWithMessage(LOG_TAG, "Error getting String value for the \"" + key + "\" key from shared preferences. Returning default value \"" + def + "\".", e);
|
||||
|
@@ -178,7 +178,7 @@ public class TermuxAppSharedPreferences {
|
||||
|
||||
|
||||
public String getCurrentSession() {
|
||||
return SharedPreferenceUtils.getString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, null);
|
||||
return SharedPreferenceUtils.getString(mSharedPreferences, TERMUX_APP.KEY_CURRENT_SESSION, null, true);
|
||||
}
|
||||
|
||||
public void setCurrentSession(String value) {
|
||||
|
Reference in New Issue
Block a user