public abstract class BasePreferences extends Object implements SharedPreferences, SharedPreferences.Editor, Initializer, MemoryTrimmer
Base-class for SharedPreferences handling.
This base-class implements the
SharedPreferences and SharedPreferences.Editor interfaces directly for a more
convenient access and easier handling.
Since this class is using caching mechanisms for
certain aspects, it also implements the MemoryTrimmer interface. In order to be properly
available after a trim memory event, the Initializer interface is implemented as well,
doing necessary initialization.
Furthermore, this class has support for easily storing string-sets into the properties-file, a
feature that is only available from API-Level 11 (Honeycomb) and later. If the appropriate
API-Level is detected during runtime, the "native" method for storing string sets will be used,
otherwise the "support-implementation" introduced by this class will be used.
In order to
be backwards compatible for devices that get updated from an API-Level below 11 to an API-Level
of 11 or above, this implementation can detect that previously the old routines were used, and
will silently make a transition to the new native routines.
SharedPreferences.Editor, SharedPreferences.OnSharedPreferenceChangeListener| Modifier and Type | Field and Description |
|---|---|
protected static String |
cachePath
The application's cachePath.
|
protected Context |
context
The application context
|
protected SharedPreferences.Editor |
editor
An editor object used internally to write the preferences and finally commit/apply them.
|
protected SharedPreferences |
settings
The actual
SharedPreferences object that is used internally. |
| Constructor and Description |
|---|
BasePreferences(Context context)
Creates and initializes
BasePreferences object with the given Context. |
BasePreferences(Context context,
String fileName)
Creates and initializes
BasePreferences object with the given Context. |
| Modifier and Type | Method and Description |
|---|---|
void |
apply() |
SharedPreferences.Editor |
clear() |
boolean |
commit() |
boolean |
contains(String key) |
SharedPreferences.Editor |
edit() |
Map<String,?> |
getAll() |
boolean |
getBoolean(String key,
boolean defValue) |
static String |
getCachePath()
Returns the application's cache-path
|
abstract String |
getFileName()
Returns the filename of the
SharedPreferences file used by this BasePreferences
object. |
float |
getFloat(String key,
float defValue) |
protected String |
getHash(String value)
Aquires the hash for the given
value. |
int |
getInt(String key,
int defValue) |
protected HashMap<String,String> |
getKeyMap()
Returns the keyMap cache.
|
long |
getLong(String key,
long defValue) |
String |
getString(String key,
String defValue) |
Set<String> |
getStringSet(String key,
Set<String> defValues) |
void |
onForceTrim()
Strict release of all not strictly necessary memory resources.
|
void |
onInitialize(Context context,
TrimState trimState)
This method triggers the initialization of its implementer.
|
void |
onTrim()
Relaxed release of memory
|
SharedPreferences.Editor |
putBoolean(String key,
boolean value) |
SharedPreferences.Editor |
putFloat(String key,
float value) |
SharedPreferences.Editor |
putInt(String key,
int value) |
SharedPreferences.Editor |
putLong(String key,
long value) |
SharedPreferences.Editor |
putString(String key,
String value) |
SharedPreferences.Editor |
putStringSet(String key,
Set<String> values) |
void |
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) |
SharedPreferences.Editor |
remove(String key) |
void |
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) |
protected SharedPreferences settings
SharedPreferences object that is used internally.protected SharedPreferences.Editor editor
getEditor(), and will be released by
calling apply() or commit().protected static String cachePath
getCachePath()protected Context context
public BasePreferences(Context context)
BasePreferences object with the given Context.
For the setting of the required preferences-file's file name the abstract method getFileName()context - The application contextpublic BasePreferences(Context context, String fileName)
BasePreferences object with the given Context.context - The application contextfileName - The name of the preference file.public static String getCachePath()
public abstract String getFileName()
SharedPreferences file used by this BasePreferences
object.protected String getHash(String value)
Aquires the hash for the given value.
This method checks the keyMap first if
the value has already been hashed before, and only if that's not the case, attempts to compute
a new hash of the value.
If possible, the SHA1-Algorithm will be used for hashing. Should
that fail, a the simple standard String.hashCode() method will be used.
In case the value has been hashed before, it will be taken from the in-memory cache.
value - The value to be hashed.String.protected HashMap<String,String> getKeyMap()
Returns the keyMap cache.
In case it wasn't instantiated, yet, this method will take care of that.
public void onTrim()
MemoryTrimmerRelaxed release of memory
This is the method that should be called from
Application.ActivityLifecycleCallbacks#onTrimMemory(int) on a level like
Application.ActivityLifecycleCallbacks#TRIM_MEMORY_BACKGROUND.
Though this is not mandatory, and this method can be run at any level, it is recommended
to have this method run at a lower level than the MemoryTrimmer.onForceTrim() method.
onTrim in interface MemoryTrimmerpublic void onForceTrim()
MemoryTrimmerStrict release of all not strictly necessary memory resources.
The call to this method should also ensure that all resources that would be freed by
MemoryTrimmer.onTrim() are definitely freed.
This is the method that should be called from
Application.ActivityLifecycleCallbacks#onLowMemory() or preferrably from
Application.ActivityLifecycleCallbacks#onTrimMemory(int) with the
Application.ActivityLifecycleCallbacks#TRIM_MEMORY_COMPLETE level.
onForceTrim in interface MemoryTrimmerpublic void onInitialize(Context context, TrimState trimState)
InitializeronInitialize in interface Initializercontext - The Context-object, because it might be necessary for the initialization.trimState - The current of the app, which should be the same TrimState of all
Initializers.public Map<String,?> getAll()
getAll in interface SharedPreferencespublic String getString(String key, String defValue)
getString in interface SharedPreferencespublic Set<String> getStringSet(String key, Set<String> defValues)
getStringSet in interface SharedPreferencespublic int getInt(String key, int defValue)
getInt in interface SharedPreferencespublic long getLong(String key, long defValue)
getLong in interface SharedPreferencespublic float getFloat(String key, float defValue)
getFloat in interface SharedPreferencespublic boolean getBoolean(String key, boolean defValue)
getBoolean in interface SharedPreferencespublic boolean contains(String key)
contains in interface SharedPreferencespublic SharedPreferences.Editor edit()
edit in interface SharedPreferencespublic void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
registerOnSharedPreferenceChangeListener in interface SharedPreferencespublic void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
unregisterOnSharedPreferenceChangeListener in interface SharedPreferencespublic SharedPreferences.Editor putString(String key, String value)
putString in interface SharedPreferences.Editorpublic SharedPreferences.Editor putStringSet(String key, Set<String> values)
putStringSet in interface SharedPreferences.Editorpublic SharedPreferences.Editor putInt(String key, int value)
putInt in interface SharedPreferences.Editorpublic SharedPreferences.Editor putLong(String key, long value)
putLong in interface SharedPreferences.Editorpublic SharedPreferences.Editor putFloat(String key, float value)
putFloat in interface SharedPreferences.Editorpublic SharedPreferences.Editor putBoolean(String key, boolean value)
putBoolean in interface SharedPreferences.Editorpublic SharedPreferences.Editor remove(String key)
remove in interface SharedPreferences.Editorpublic SharedPreferences.Editor clear()
clear in interface SharedPreferences.Editorpublic boolean commit()
commit in interface SharedPreferences.Editorpublic void apply()
apply in interface SharedPreferences.Editor