Class SecurePreferenceDataStore
-
- All Implemented Interfaces:
public final class SecurePreferenceDataStore extends PreferenceDataStoreA
PreferenceDataStoreimplementation encrypts contents using 256-bit AES encryption, and is provided to thePreferenceframework via setPreferenceDataStore.This class can be used to replace the default
You must create and open the store with an encryption key before you can interact with it. It is recommended to useSharedPreferencesand provides an extra layer of application data protection.EncryptionUtilto obtain the encryption key. If this is the first time store is being opened andnullis provided, an encryption key will be generated transparently and will be used for subsequent opens.final String storeName = "myPreferenceStore"; final byte[] encryptionKey = EncryptionUtil.getEncryptionKey("aliasForPreferenceStore", myPasscode); SecurePreferenceDataStore store = null; try { store = new SecurePreferenceDataStore( androidContext, // Android application context. storeName, // Store name. encryptionKey); // Store's encryption key; auto-generated if null. } catch (OpenFailureException ex) { logger.error("An error occurred while opening the preference data store.", ex); }The following preference value types are supported:
- String
- Integer
- Long
- Float
- Boolean
- Any
Setimplementation that isSerializable
You can use the appropriate
putandgetmethods to add and retrieve preference values to/from the data store.
You can remove preferences one at a time or remove all at once.try { // Adds preferences to the store. store.putInt("integerKey1", 58); store.putString("StringKey1", "A String value"); store.putFloat("floatKey1", 9.8F); store.putLong("longKey1", 9223372036854775000L); store.putBoolean("booleanKey1", true); // Adds a set of values as a preference to the store. // 'stringSet' is a HashSet<String> that contains values. store.putStringSet(stringSetKey, stringSet); // Retrieves existing values. int intValue = store.getInt("integerKey1", 0); String stringValue = store.getString("StringKey1", "default String if not found"); float floatValue = store.getFloat("floatKey1", 0.2F); long longValue = store.getLong("longKey1", 9223372036854775023L); boolean booleanValue = store.getBoolean("booleanKey1", false); } catch (BackingStoreException ex) { logger.error("Failed to add/retrieve preference.", ex); }
You can change the encryption of the underlying persistence store when the store is in open state.try { // Removes preferences one at a time. store.remove("booleanKey1"); store.remove("longKey1"); // Removes all existing preferences. store.removeAll(); } catch (BackingStoreException ex) { logger.error("Failed to remove preference.", ex); }
When you finish CRUD operations on the store, close thestore.changeEncryptionKey(newEncryptionKey);SecurePreferenceDataStoreto relinquishes any resources it has acquired during its operations.
You may want to remove the persistence store in some occasions. Note that after the persistence store is removed, any further operation performed on the instance will causestore.close();FileMissingException.store.deleteStore(context); // The store instance will not be usable after this method!
-
-
Constructor Summary
Constructors Constructor Description SecurePreferenceDataStore(Context context, String storeName, Array<byte> encryptionKey)Constructs a secure PreferenceDataStore.
-
Method Summary
Modifier and Type Method Description voidchangeEncryptionKey(@Nullable() Array<byte> newEncryptionKey)Changes the encryption key of the store while the store is opened (close is not yet called). voiddeleteStore(@NonNull() Context context)Deletes the underlying persistence store file. voidputString(@NonNull() String key, @Nullable() String value)Sets a Stringvalue to data store.voidputStringSet(@NonNull() String key, @Nullable() Set<String> values)Sets a set of Stringsto the data store.voidputInt(@NonNull() String key, int value)Sets an integervalue to data store.voidputLong(@NonNull() String key, long value)Sets a longvalue to data store.voidputFloat(@NonNull() String key, float value)Sets a floatvalue to data store.voidputBoolean(@NonNull() String key, boolean value)Sets a booleanvalue to data store.StringgetString(@NonNull() String key, @Nullable() String defValue)Retrieves a Stringvalue from the data store.Set<String>getStringSet(@NonNull() String key, @Nullable() Set<String> defValues)Retrieves a set of Stringsfrom the data store.intgetInt(@NonNull() String key, int defValue)Retrieves an integervalue from the data store.longgetLong(@NonNull() String key, long defValue)Retrieves a longvalue from the data store.floatgetFloat(@NonNull() String key, float defValue)Retrieves a floatvalue from the data store.booleangetBoolean(@NonNull() String key, boolean defValue)Retrieves a booleanvalue from the data store.voidremove(@NonNull() String key)Removes a preference from the data store. voidremoveAll()Removes all preferences from the data store. voidclose()Closes the store and relinquishes all resources acquired during its operations . -
-
Constructor Detail
-
SecurePreferenceDataStore
SecurePreferenceDataStore(Context context, String storeName, Array<byte> encryptionKey)
Constructs a securePreferenceDataStore.- Parameters:
context- Android application contextstoreName- a non-empty name of the store that can only contain-, _, a-z, A-Z, 0-9and..encryptionKey- the key which is used to encrypt/decrypt the store.
-
-
Method Detail
-
changeEncryptionKey
void changeEncryptionKey(@Nullable() Array<byte> newEncryptionKey)
Changes the encryption key of the store while the store is opened (close is not yet called).
- Parameters:
newEncryptionKey- new encryption key.
-
deleteStore
void deleteStore(@NonNull() Context context)
Deletes the underlying persistence store file. Note that this instance will not be usable after calling this method, and any operation performed on the instance will get FileMissingException.
- Parameters:
context- Android application context
-
putString
void putString(@NonNull() String key, @Nullable() String value)
Sets a
Stringvalue to data store.- Parameters:
key- the name of the preference to modifyvalue- the new value for the preference
-
putStringSet
void putStringSet(@NonNull() String key, @Nullable() Set<String> values)
Sets a set of
Stringsto the data store.- Parameters:
key- the name of the preference to modifyvalues- the set of new values for the preference
-
putInt
void putInt(@NonNull() String key, int value)
Sets an
integervalue to data store.- Parameters:
key- the name of the preference to modifyvalue- the new value for the preference
-
putLong
void putLong(@NonNull() String key, long value)
Sets a
longvalue to data store.- Parameters:
key- the name of the preference to modifyvalue- the new value for the preference
-
putFloat
void putFloat(@NonNull() String key, float value)
Sets a
floatvalue to data store.- Parameters:
key- the name of the preference to modifyvalue- the new value for the preference
-
putBoolean
void putBoolean(@NonNull() String key, boolean value)
Sets a
booleanvalue to data store.- Parameters:
key- the name of the preference to modifyvalue- the new value for the preference
-
getString
@Nullable() String getString(@NonNull() String key, @Nullable() String defValue)
Retrieves a
Stringvalue from the data store.- Parameters:
key- the name of the preference to retrievedefValue- the value to return if this preference does not exist in the data store- Returns:
The value from the data store or the default return value.
-
getStringSet
@Nullable() Set<String> getStringSet(@NonNull() String key, @Nullable() Set<String> defValues)
Retrieves a set of
Stringsfrom the data store.- Parameters:
key- the name of the preference to retrievedefValues- the values to return if this preference does not exist in the storage- Returns:
The values from the data store or the default return values.
-
getInt
int getInt(@NonNull() String key, int defValue)
Retrieves an
integervalue from the data store.- Parameters:
key- the name of the preference to retrievedefValue- the value to return if this preference does not exist in the data store- Returns:
The value from the data store or the default return value.
-
getLong
long getLong(@NonNull() String key, long defValue)
Retrieves a
longvalue from the data store.- Parameters:
key- the name of the preference to retrievedefValue- the value to return if this preference does not exist in the data store- Returns:
The value from the data store or the default return value.
-
getFloat
float getFloat(@NonNull() String key, float defValue)
Retrieves a
floatvalue from the data store.- Parameters:
key- the name of the preference to retrievedefValue- the value to return if this preference does not exist in the data store- Returns:
The value from the data store or the default return value.
-
getBoolean
boolean getBoolean(@NonNull() String key, boolean defValue)
Retrieves a
booleanvalue from the data store.- Parameters:
key- the name of the preference to retrievedefValue- the value to return if this preference does not exist in the data store- Returns:
The value from the data store or the default return value.
-
remove
void remove(@NonNull() String key)
Removes a preference from the data store.
- Parameters:
key- the name of the preference to remove
-
removeAll
void removeAll()
Removes all preferences from the data store.
-
close
void close()
Closes the store and relinquishes all resources acquired during its operations . Note that once the store is closed, no operations are allowed.
-
-
-
-