-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6513e5
commit 8136383
Showing
3 changed files
with
64 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...n/java/com/doubleangels/nextdnsmanagement/sharedpreferences/SharedPreferencesManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.doubleangels.nextdnsmanagement.sharedpreferences; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
public class SharedPreferencesManager { | ||
private static final String PREF_NAME = "MyAppPreferences"; | ||
private static SharedPreferences sharedPreferences; | ||
|
||
private SharedPreferencesManager() { | ||
throw new UnsupportedOperationException("Cannot instantiate SharedPreferencesManager."); | ||
} | ||
|
||
public static synchronized void init(Context context) { | ||
if (sharedPreferences == null) { | ||
sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); | ||
} | ||
} | ||
|
||
public static void putString(String key, String value) { | ||
checkInitialization(); | ||
sharedPreferences.edit().putString(key, value).apply(); | ||
} | ||
|
||
public static String getString(String key, String defaultValue) { | ||
checkInitialization(); | ||
return sharedPreferences.getString(key, defaultValue); | ||
} | ||
|
||
public static void putBoolean(String key, boolean value) { | ||
checkInitialization(); | ||
sharedPreferences.edit().putBoolean(key, value).apply(); | ||
} | ||
|
||
public static boolean getBoolean(String key, boolean defaultValue) { | ||
checkInitialization(); | ||
return sharedPreferences.getBoolean(key, defaultValue); | ||
} | ||
|
||
private static void checkInitialization() { | ||
if (sharedPreferences == null) { | ||
throw new IllegalStateException("SharedPreferencesManager is not initialized. Call init() before using it."); | ||
} | ||
} | ||
} |