public abstract class SharedPreferencesBase
extends java.lang.Object
DeprecatedSharedPreferencesBase.
As SharedPreferences is just a basic key-value store, subclasses are expected to specify the prefix to be applied to the key to ensure that they are not inadvertently conflicting with other keys.
Subclasses must implement the following methods:
getVersion() must return the implementation version. This is the version that the
SharedPreferences should be. If there is a breaking change to the information that is stored
then this version should be incremented.
updateStoreData(int) must be implemented to perform the necessary updates of the data.
This method will be called for every version between the currently stored version and the
new implementation version returned by getVersion().
As of version 0.2.1 the keyPrefix is mandatory.
| Modifier and Type | Field and Description |
|---|---|
protected static int |
DEFAULT_VERSION |
protected android.content.Context |
mContext |
protected java.lang.String |
mKeyPrefix |
static java.lang.String |
PREF_FILE |
| Constructor and Description |
|---|
SharedPreferencesBase(android.content.Context context,
java.lang.String keyPrefix)
Initialises a new instance of the
SharedPreferencesBase. |
| Modifier and Type | Method and Description |
|---|---|
protected java.lang.String |
buildCompleteKey(java.lang.String key) |
protected boolean |
contains(java.lang.String key) |
protected boolean |
getBooleanPreference(java.lang.String key,
boolean defaultValue) |
protected int |
getIntPreference(java.lang.String key,
int defaultValue) |
protected long |
getLongPreference(java.lang.String key,
long defaultValue) |
protected android.content.SharedPreferences.Editor |
getSharedEditor() |
protected android.content.SharedPreferences |
getSharedPreferences() |
protected java.lang.String |
getSharedPreferencesName()
Gets the name of the class purely for logging purposes.
|
protected int |
getStoredVersion()
Get the currently stored version of the SharedPreferences implementation.
|
protected java.lang.String |
getStringPreference(java.lang.String key,
java.lang.String defaultValue) |
protected java.util.Set<java.lang.String> |
getStringSetPreference(java.lang.String key,
java.util.Set<java.lang.String> defaultValues) |
protected abstract int |
getVersion()
Gets the current version of the SharedPreferences implementation.
|
protected void |
onPreCheckVersion()
A hook to allow for subclasses to perform any tasks before the version is checked and any
upgrades are done.
|
protected void |
removePreference(java.lang.String key) |
protected void |
setBooleanPreference(java.lang.String key,
boolean value) |
protected void |
setIntPreference(java.lang.String key,
int value) |
protected void |
setLongPreference(java.lang.String key,
long value) |
protected void |
setStoredVersion(int newVersion)
Sets the version of the SharedPreferences.
|
protected void |
setStringPreference(java.lang.String key,
java.lang.String value) |
protected void |
setStringSetPreference(java.lang.String key,
java.util.Set<java.lang.String> values) |
protected abstract void |
updateStoreData(int newVersion)
Updates the SharedPreferences implementation to the new version.
|
public static final java.lang.String PREF_FILE
protected static final int DEFAULT_VERSION
protected final android.content.Context mContext
protected final java.lang.String mKeyPrefix
public SharedPreferencesBase(@NonNull
android.content.Context context,
@NonNull
java.lang.String keyPrefix)
SharedPreferencesBase.
On construction the version will be check and if found to be out of date the incremental update will be executed.
context - The Android ContextkeyPrefix - The prefix to be used for all keys.protected void onPreCheckVersion()
protected java.lang.String getSharedPreferencesName()
protected abstract void updateStoreData(int newVersion)
This will be called for every version between the currently stored version and the new implementation version. The newVersion will be incremented by 1 on each call. Classes that override this should take this into account when implementing this method as it should not directly upgrade the latest version, but rather do the proper upgrade path by upgrading from one version to the next.
After every call to this method the version stored in the SharedPreferences will be updated to the newVersion.
newVersion - The new version to which the SharedPreferences implementation will be updated.protected void setStringPreference(java.lang.String key,
java.lang.String value)
protected java.lang.String getStringPreference(java.lang.String key,
java.lang.String defaultValue)
protected void setLongPreference(java.lang.String key,
long value)
protected long getLongPreference(java.lang.String key,
long defaultValue)
protected void setIntPreference(java.lang.String key,
int value)
protected int getIntPreference(java.lang.String key,
int defaultValue)
protected void setBooleanPreference(java.lang.String key,
boolean value)
protected boolean getBooleanPreference(java.lang.String key,
boolean defaultValue)
protected java.util.Set<java.lang.String> getStringSetPreference(java.lang.String key,
java.util.Set<java.lang.String> defaultValues)
protected void setStringSetPreference(java.lang.String key,
java.util.Set<java.lang.String> values)
protected void removePreference(java.lang.String key)
protected boolean contains(java.lang.String key)
protected void setStoredVersion(int newVersion)
newVersion - The new version.protected int getStoredVersion()
To get the current implementation version call getVersion().
protected abstract int getVersion()
getStoredVersion().
To get the cached version use getStoredVersion().
All implementations MUST override this with the implementation version. The implementation version should only be changed in the subclass when there is a breaking change that requires extra work to upgrade to the new version.
protected android.content.SharedPreferences getSharedPreferences()
protected android.content.SharedPreferences.Editor getSharedEditor()
SharedPreferences.Editor for the default file "default_shared_prefs"protected java.lang.String buildCompleteKey(java.lang.String key)