Skip to content

Commit 9e82561

Browse files
Added SharedPreferenceUtils
The `SharedPreferenceUtils` class has been added to provide static util functions for shared preferences like get and set while safely handling exceptions. The `TermuxSharedPreferences` class has been renamed to `TermuxAppSharedPreferences` since its to be used to handle only Termux App related shared preferences and not of other plugin apps. For plugin apps, separate classes can be created. However, Termux app and its plugins will share the same `TermuxPreferenceConstants` class that now has per app scoping since `v0.3.0`.
1 parent 4427c16 commit 9e82561

7 files changed

+221
-50
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import com.termux.R;
3434
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
35-
import com.termux.app.settings.preferences.TermuxSharedPreferences;
35+
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
3636
import com.termux.app.terminal.TermuxSessionsListViewController;
3737
import com.termux.app.terminal.io.TerminalToolbarViewPager;
3838
import com.termux.app.terminal.TermuxSessionClient;
@@ -93,7 +93,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
9393
/**
9494
* Termux app shared preferences manager.
9595
*/
96-
private TermuxSharedPreferences mPreferences;
96+
private TermuxAppSharedPreferences mPreferences;
9797

9898
/**
9999
* Termux app shared properties manager, loaded from termux.properties
@@ -154,7 +154,7 @@ public void onCreate(Bundle savedInstanceState) {
154154
Logger.logDebug(LOG_TAG, "onCreate");
155155

156156
// Load termux shared preferences and properties
157-
mPreferences = new TermuxSharedPreferences(this);
157+
mPreferences = new TermuxAppSharedPreferences(this);
158158
mProperties = new TermuxSharedProperties(this);
159159

160160
setActivityTheme();
@@ -670,7 +670,7 @@ public TerminalSession getCurrentSession() {
670670
return null;
671671
}
672672

673-
public TermuxSharedPreferences getPreferences() {
673+
public TermuxAppSharedPreferences getPreferences() {
674674
return mPreferences;
675675
}
676676

app/src/main/java/com/termux/app/TermuxApplication.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import android.app.Application;
44

5-
import com.termux.app.settings.preferences.TermuxSharedPreferences;
5+
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
66
import com.termux.app.utils.Logger;
77

88

@@ -15,7 +15,7 @@ public void onCreate() {
1515

1616
private void updateLogLevel() {
1717
// Load the log level from shared preferences and set it to the {@link Loggger.CURRENT_LOG_LEVEL}
18-
TermuxSharedPreferences preferences = new TermuxSharedPreferences(getApplicationContext());
18+
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(getApplicationContext());
1919
preferences.setLogLevel(null, preferences.getLogLevel());
2020
Logger.logDebug("Starting Application");
2121
}

app/src/main/java/com/termux/app/TermuxService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.termux.R;
2424
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
2525
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
26-
import com.termux.app.settings.preferences.TermuxSharedPreferences;
26+
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
2727
import com.termux.app.terminal.TermuxSessionClient;
2828
import com.termux.app.terminal.TermuxSessionClientBase;
2929
import com.termux.app.utils.Logger;
@@ -161,7 +161,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
161161
}
162162

163163
// Make the newly created session the current one to be displayed:
164-
TermuxSharedPreferences preferences = new TermuxSharedPreferences(this);
164+
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(this);
165165
preferences.setCurrentSession(newSession.mHandle);
166166

167167
// Launch the main Termux app, which will now show the current session:

app/src/main/java/com/termux/app/settings/DebuggingPreferencesFragment.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import com.termux.R;
1414
import com.termux.app.settings.preferences.TermuxPreferenceConstants;
15-
import com.termux.app.settings.preferences.TermuxSharedPreferences;
15+
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
1616
import com.termux.app.utils.Logger;
1717

1818
public class DebuggingPreferencesFragment extends PreferenceFragmentCompat {
@@ -41,7 +41,6 @@ protected ListPreference setLogLevelListPreferenceData(ListPreference logLevelLi
4141
logLevelListPreference.setEntryValues(logLevels);
4242
logLevelListPreference.setEntries(logLevelLabels);
4343

44-
logLevelListPreference.setKey(TermuxPreferenceConstants.KEY_LOG_LEVEL);
4544
logLevelListPreference.setValue(String.valueOf(Logger.getLogLevel()));
4645
logLevelListPreference.setDefaultValue(Logger.getLogLevel());
4746

@@ -53,13 +52,13 @@ protected ListPreference setLogLevelListPreferenceData(ListPreference logLevelLi
5352
class DebuggingPreferencesDataStore extends PreferenceDataStore {
5453

5554
private final Context mContext;
56-
private final TermuxSharedPreferences mPreferences;
55+
private final TermuxAppSharedPreferences mPreferences;
5756

5857
private static DebuggingPreferencesDataStore mInstance;
5958

6059
private DebuggingPreferencesDataStore(Context context) {
6160
mContext = context;
62-
mPreferences = new TermuxSharedPreferences(context);
61+
mPreferences = new TermuxAppSharedPreferences(context);
6362
}
6463

6564
public static synchronized DebuggingPreferencesDataStore getInstance(Context context) {
@@ -69,6 +68,8 @@ public static synchronized DebuggingPreferencesDataStore getInstance(Context con
6968
return mInstance;
7069
}
7170

71+
72+
7273
@Override
7374
@Nullable
7475
public String getString(String key, @Nullable String defValue) {
@@ -97,6 +98,8 @@ public void putString(String key, @Nullable String value) {
9798
}
9899
}
99100

101+
102+
100103
@Override
101104
public void putBoolean(String key, boolean value) {
102105
if(key == null) return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)