SharedPreferencesBase as to not have to deal with any migration from a non-versioned
SharedPreferences to a versioned SharedPreferences.@Deprecated public abstract class DeprecatedSharedPreferencesBase extends SharedPreferencesBase
SharedPreferencesBase when you are dealing with
legacy systems that require the preferences to be migrated to having a key prefix. For brand new
implementations rather use the SharedPreferencesBase as you will not have to handle any
migration code.
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.
This DeprecatedSharedPreferencesBase is now versioned. On construction the currently stored version
will be checked and if the version of the subclass is found to not be the same
SharedPreferencesBase.updateStoreData(int) will be called, incrementally with the new version, until the
stored version matches the implementation version.
Subclasses must implement the following methods:
SharedPreferencesBase.getVersion()SharedPreferencesBase.updateStoreData(int)getDeprecatedPreferenceKeys()
SharedPreferencesBase.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.
SharedPreferencesBase.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 SharedPreferencesBase.getVersion().
getDeprecatedPreferenceKeys() must be implemented to provide information during the
migration as to what keys should are deprecated and should be migrated to have the new prefix.
This method should ALWAYS return the keys that need to be migrated. An error will be thrown
if this list is empty or null.
As of version 0.2.1 the keyPrefix is mandatory. When upgrading from a
non-versioned implementation of the DeprecatedSharedPreferencesBase the old keys that did
NOT have the keyPrefix will be removed and updated with the
new keyPrefix.
DEFAULT_VERSION, mContext, mKeyPrefix, PREF_FILE| Constructor and Description |
|---|
DeprecatedSharedPreferencesBase(android.content.Context context,
java.lang.String keyPrefix)
Deprecated.
Initialises a new instance of the
DeprecatedSharedPreferencesBase. |
| Modifier and Type | Method and Description |
|---|---|
protected abstract java.util.List<java.lang.String> |
getDeprecatedPreferenceKeys()
Deprecated.
To allow for the migration of the preferences with the deprecated keys we need to define
which keys need to be migrated.
|
protected void |
onPreCheckVersion()
Deprecated.
A hook to allow for subclasses to perform any tasks before the version is checked and any
upgrades are done.
|
buildCompleteKey, contains, getBooleanPreference, getIntPreference, getLongPreference, getSharedEditor, getSharedPreferences, getSharedPreferencesName, getStoredVersion, getStringPreference, getStringSetPreference, getVersion, removePreference, setBooleanPreference, setIntPreference, setLongPreference, setStoredVersion, setStringPreference, setStringSetPreference, updateStoreDatapublic DeprecatedSharedPreferencesBase(@NonNull
android.content.Context context,
@NonNull
java.lang.String keyPrefix)
DeprecatedSharedPreferencesBase.
On construction the version will be check and if found to be out of date the incremental
update will be executed. Keys returned by getDeprecatedPreferenceKeys() will
be migrated to use the new key prefix.
context - The Android ContextkeyPrefix - The prefix to be used for all keys.@NonNull protected abstract java.util.List<java.lang.String> getDeprecatedPreferenceKeys()
DeprecatedSharedPreferencesBase.DeprecatedSharedPreferencesBase has to have
keys with out a prefix that need to be migrated. If an implementation does not have any
keys that need to be migrated then rather use SharedPreferencesBase.protected final void onPreCheckVersion()
SharedPreferencesBaseonPreCheckVersion in class SharedPreferencesBase