Let your patron do the work.
A Patron is a preference wrapper. Come also with an encryption system.
Use the adequate patron for the preference value type you would like to store.
Patrons handle all types handled by shared preferences:
- boolean
- float
- float
- int
- String
- StringSet
Patrons also provide an encrypted version of the shared preferences. Use patrons-conceal
to encrypt your preference values.
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.prolificinteractive.patrons:patron:${patronsVersion}'
// If you want patrons with encryption of your shared preferences
implementation 'com.github.prolificinteractive.patrons:patron-conceal:${patronsVersion}'
}
A default patron exists for all shared preferences types.
val boolPref: Preference<Boolean> = BooleanPreference(prefs, "key")
val floatPref: Preference<Float> = FloatPreference(prefs, "key")
val intPref: Preference<Integer> = IntPreference(prefs, "key")
val longPref: Preference<Long> = LongPreference(prefs, "key")
val stringPref: Preference<String> = StringPreference(prefs, "key")
val stringSetPref: Preference<Set<String>> = StringSetPreference(prefs, "key")
Init Conceal in your application class:
import com.facebook.soloader.SoLoader;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, false);
}
}
val prefs = ConcealSharedPreferences(context)
By default, preference's key is NOT encrypted but the behaviour can be customized by passing your own custom KeyEncryption
val defaultPref = PreferenceManager.getDefaultSharedPreferences(context)
val concealedPrefs = ConcealSharedPreferences(
this,
defaultPref,
NoKeyEncryption(),
ConcealEncryption(
context,
"conceal_encryption",
AndroidConceal
.get()
.createDefaultCrypto(KeystoreBackedKeychain(context, defaultPref))
)
)
TODO
To report a bug or enhancement request, feel free to file an issue under the respective heading.
If you wish to contribute to the project, fork this repo and submit a pull request. Code contributions should follow the standards specified in the Prolific Android Style Guide.
Copyright (c) 2018 Prolific Interactive
Patrons is maintained and sponsored by Prolific Interactive. It may be redistributed under the terms specified in the LICENSE file.