public class PreferencesManager extends Object implements SharedPreferencesFacade, SharedPreferencesProvider
SharedPreferencesFacade along with SharedPreferencesProvider
in order to provide a simple API for putting and obtaining of values persisted within SharedPreferences.
SharedPreference. Any implementation of
SharedPreference class may be used to represent a concrete preference value to be persisted in
shared preferences. Methods listed below may be used to put, get or remove a desired shared preference
into or from preferences managed by the preferences manager:
putPreference(SharedPreference, Object)getPreference(SharedPreference)containsPreference(SharedPreference)removePreference(SharedPreference)
public final class AppPreferences extends PreferencesManager {
private static final String KEY_PREFIX = BuildConfig.APPLICATION_ID = ".PREFERENCE.";
private static final String KEY_APP_FIRST_LAUNCH = KEY_PREFIX + "AppFirstLaunch";
public AppPreferences(@NonNull Context context) {
super(context);
}
public void setAppFirstLaunch(boolean firstLaunch) {
putBoolean(KEY_APP_FIRST_LAUNCH, firstLaunch);
}
public boolean isAppFirstLaunch() {
return getBoolean(KEY_APP_FIRST_LAUNCH, true);
}
}
| Constructor and Description |
|---|
PreferencesManager(android.content.Context context)
Creates a new instance of PreferencesManager with the specified context.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(String key)
Checks whether the
SharedPreferences hidden behind this facade contain value for the
specified key or not. |
boolean |
containsPreference(SharedPreference preference)
Checks whether there is value associated with the specified preference contained
within
SharedPreferences that are managed by this manager. |
boolean |
getBoolean(String key,
boolean defValue)
Retrieves the boolean value for the specified key from
SharedPreferences
hidden behind this facade. |
android.content.Context |
getContext()
Returns the context with which has been this manager created.
|
float |
getFloat(String key,
float defValue)
Retrieves the float value for the specified key from
SharedPreferences
hidden behind this facade. |
int |
getInt(String key,
int defValue)
Retrieves the integer value for the specified key from
SharedPreferences
hidden behind this facade. |
long |
getLong(String key,
long defValue)
Retrieves the long value for the specified key from
SharedPreferences
hidden behind this facade. |
<T> T |
getPreference(SharedPreference<T> preference)
Obtains the value for the given preference from
SharedPreferences that are
managed by this manager. |
android.content.SharedPreferences |
getSharedPreferences()
Returns the instance of SharedPreferences provided by this provider.
|
int |
getSharedPreferencesMode()
Returns the mode for preferences file specified for this provider.
|
String |
getSharedPreferencesName()
Returns the name for preferences file specified for this provider.
|
String |
getString(String key,
String defValue)
Retrieves the string value for the specified key from
SharedPreferences
hidden behind this facade. |
Set<String> |
getStringSet(String key,
Set<String> defValues)
Retrieves the set of string values for the specified key from
SharedPreferences
hidden behind this facade. |
boolean |
isCachingEnabled()
Returns flag indicating whether the caching of
SharedPreference's
values is enabled or not. |
String |
key(int resId)
Returns a string key for the specified resId.
|
boolean |
putBoolean(String key,
boolean value)
Puts the given boolean value for the specified key into
SharedPreferences
hidden behind this facade. |
boolean |
putFloat(String key,
float value)
Puts the given float value for the specified key into
SharedPreferences
hidden behind this facade. |
boolean |
putInt(String key,
int value)
Puts the given integer value for the specified key into
SharedPreferences
hidden behind this facade. |
boolean |
putLong(String key,
long value)
Puts the given long value for the specified key into
SharedPreferences
hidden behind this facade. |
<T> boolean |
putPreference(SharedPreference<T> preference,
T value)
Persists the given value for the specified preference into
SharedPreferences
that are managed by this manager. |
boolean |
putString(String key,
String value)
Puts the given string value for the specified key into
SharedPreferences
hidden behind this facade. |
boolean |
putStringSet(String key,
Set<String> values)
Puts the given set of string values for the specified key into
SharedPreferences
hidden behind this facade. |
void |
registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Registers a listener in order to receive a callback whenever a value of preference persisted
within the
SharedPreferences hidden behind this facade changes. |
boolean |
remove(String key)
Removes a value for the specified key from the
SharedPreferences hidden
behind this facade. |
int |
removeAll()
Removes all values from the
SharedPreferences hidden behind this facade. |
boolean |
removePreference(SharedPreference preference)
Removes a value for the specified preference from
SharedPreferences that
are managed by this manager. |
void |
setCachingEnabled(boolean enabled)
Enables/disables the caching
SharedPreference's values. |
void |
setSharedPreferencesMode(int mode)
Sets a mode for preferences file that is used by this provider to access instance of
SharedPreferences via Context.getSharedPreferences(String, int). |
void |
setSharedPreferencesName(String name)
Sets a name for preferences file that is used by this provider to access instance of
SharedPreferences via Context.getSharedPreferences(String, int). |
void |
unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
Un-registers a previously registered listener in order to not receive further any callbacks
about preference changes.
|
public PreferencesManager(android.content.Context context)
Manager will be created with default name for shared preferences obtained via
SharedPreferencesPolicy.defaultPreferencesName(Context) and mode set to
SharedPreferencesPolicy.MODE_PRIVATE.
context - Context that is used to obtain instance of SharedPreferences for the
new manager via Context.getSharedPreferences(String, int) for the
name and mode specified via setSharedPreferencesName(String)
and setSharedPreferencesMode(int).public final android.content.Context getContext()
public void setSharedPreferencesName(String name)
SharedPreferencesProviderSharedPreferences via Context.getSharedPreferences(String, int).setSharedPreferencesName in interface SharedPreferencesProvidername - The desired preferences name. May be null if this host should use default
name provide by the framework.SharedPreferencesProvider.getSharedPreferencesName(),
SharedPreferencesProvider.setSharedPreferencesMode(int),
SharedPreferencesProvider.getSharedPreferences()public final String getSharedPreferencesName()
SharedPreferencesProvider
Default value: PreferenceManager#getDefaultSharedPreferencesName(Context)
getSharedPreferencesName in interface SharedPreferencesProviderSharedPreferencesProvider.setSharedPreferencesName(String),
SharedPreferencesProvider.getSharedPreferencesMode(),
SharedPreferencesProvider.getSharedPreferences()public void setSharedPreferencesMode(int mode)
SharedPreferencesProviderSharedPreferences via Context.getSharedPreferences(String, int).setSharedPreferencesMode in interface SharedPreferencesProvidermode - The desired preferences mode.SharedPreferencesProvider.getSharedPreferencesMode(),
SharedPreferencesProvider.setSharedPreferencesName(String),
SharedPreferencesProvider.getSharedPreferences()public final int getSharedPreferencesMode()
SharedPreferencesProvider
Default value: MODE_PRIVATE
getSharedPreferencesMode in interface SharedPreferencesProviderSharedPreferencesProvider.setSharedPreferencesMode(int),
SharedPreferencesProvider.getSharedPreferencesName(),
SharedPreferencesProvider.getSharedPreferences()public final android.content.SharedPreferences getSharedPreferences()
SharedPreferencesProvidergetSharedPreferences in interface SharedPreferencesProviderSharedPreferencesProvider.setSharedPreferencesName(String),
SharedPreferencesProvider.setSharedPreferencesMode(int)public final void setCachingEnabled(boolean enabled)
SharedPreference's values.enabled - True to enable caching, false to disable.isCachingEnabled()public final boolean isCachingEnabled()
SharedPreference's
values is enabled or not.True if caching is enabled, false otherwise.setCachingEnabled(boolean)public final String key(int resId)
resId - Resource id of the desired preference key.public void registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
SharedPreferencesFacadeSharedPreferences hidden behind this facade changes.registerOnSharedPreferenceChangeListener in interface SharedPreferencesFacadelistener - The desired listener to be registered.SharedPreferencesFacade.unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener),
SharedPreferences.registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener)public void unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
SharedPreferencesFacadeunregisterOnSharedPreferenceChangeListener in interface SharedPreferencesFacadelistener - The desired listener to be un-registered.SharedPreferencesFacade.registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener),
SharedPreferences.unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener)public boolean contains(String key)
SharedPreferencesFacadeSharedPreferences hidden behind this facade contain value for the
specified key or not.contains in interface SharedPreferencesFacadekey - The key of which value's presence to check.True if there is value contained for the specified key, false otherwise.SharedPreferencesFacade.remove(String),
SharedPreferences.contains(String)public boolean putString(String key, String value)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putString in interface SharedPreferencesFacadekey - The key for which to put the value into preferences.value - The desired value to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putString(String, String)public String getString(String key, String defValue)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getString in interface SharedPreferencesFacadekey - The key for which to retrieve its associated value.defValue - Default value to be returned in case when there is no value persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getString(String, String)public boolean putStringSet(String key, Set<String> values)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putStringSet in interface SharedPreferencesFacadekey - The key for which to put the values into preferences.values - The desired values to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putStringSet(String, Set)public Set<String> getStringSet(String key, Set<String> defValues)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getStringSet in interface SharedPreferencesFacadekey - The key for which to retrieve its associated values.defValues - Default values to be returned in case when there are no values persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getStringSet(String, Set)public boolean putInt(String key, int value)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putInt in interface SharedPreferencesFacadekey - The key for which to put the value into preferences.value - The desired value to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putInt(String, int)public int getInt(String key, int defValue)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getInt in interface SharedPreferencesFacadekey - The key for which to retrieve its associated value.defValue - Default value to be returned in case when there is no value persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getInt(String, int)public boolean putFloat(String key, float value)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putFloat in interface SharedPreferencesFacadekey - The key for which to put the value into preferences.value - The desired value to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putFloat(String, float)public float getFloat(String key, float defValue)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getFloat in interface SharedPreferencesFacadekey - The key for which to retrieve its associated value.defValue - Default value to be returned in case when there is no value persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getFloat(String, float)public boolean putLong(String key, long value)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putLong in interface SharedPreferencesFacadekey - The key for which to put the value into preferences.value - The desired value to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putLong(String, long)public long getLong(String key, long defValue)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getLong in interface SharedPreferencesFacadekey - The key for which to retrieve its associated value.defValue - Default value to be returned in case when there is no value persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getLong(String, long)public boolean putBoolean(String key, boolean value)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.putBoolean in interface SharedPreferencesFacadekey - The key for which to put the value into preferences.value - The desired value to put into preferences.True if put has been successful, false otherwise.SharedPreferences.edit(),
SharedPreferences.Editor#putBoolean(String, boolean)public boolean getBoolean(String key, boolean defValue)
SharedPreferencesFacadeSharedPreferences
hidden behind this facade.getBoolean in interface SharedPreferencesFacadekey - The key for which to retrieve its associated value.defValue - Default value to be returned in case when there is no value persisted for
the specified key.SharedPreferencesFacade.contains(String),
SharedPreferences.getBoolean(String, boolean)public boolean remove(String key)
SharedPreferencesFacadeSharedPreferences hidden
behind this facade.remove in interface SharedPreferencesFacadekey - The key for which to remove its associated value.True if removal has been successful, false otherwise.SharedPreferencesFacade.removeAll(),
SharedPreferencesFacade.contains(String),
SharedPreferences.edit(),
SharedPreferences.Editor#remove(String)public int removeAll()
SharedPreferencesFacadeSharedPreferences hidden behind this facade.removeAll in interface SharedPreferencesFacade0
if there are no values persisted.SharedPreferencesFacade.remove(String),
SharedPreferences.getAll(),
SharedPreferences.edit(),
SharedPreferences.Editor#remove(String)public final <T> boolean putPreference(SharedPreference<T> preference, T value)
SharedPreferences
that are managed by this manager.T - Type of the value associated with the preference.preference - Preference for which to persist the value.value - The value to be persisted.True if put has been successful, false otherwise.getPreference(SharedPreference),
containsPreference(SharedPreference),
removePreference(SharedPreference),
SharedPreference.updateValue(Object),
SharedPreference.putIntoPreferences(SharedPreferences)public final <T> T getPreference(SharedPreference<T> preference)
SharedPreferences that are
managed by this manager.T - Type of the value associated with the preference.preference - Preference for which to obtain its associated value.null if there is no value persisted
for the preference yet.putPreference(SharedPreference, Object),
contains(String),
SharedPreference.getFromPreferences(SharedPreferences)public final boolean containsPreference(SharedPreference preference)
SharedPreferences that are managed by this manager.preference - The desired preference of which value's presence to check.True if there is value contained for key of the specified preference,
false otherwise.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
removePreference(SharedPreference)public final boolean removePreference(SharedPreference preference)
SharedPreferences that
are managed by this manager.preference - The preference for which to remove its associated value.True if removal has been successful, false otherwise.putPreference(SharedPreference, Object),
getPreference(SharedPreference),
containsPreference(SharedPreference)