Shared Preferences
Introduction
OpenSRP makes use of shared preferences to store configuration values that are used to maintain app state between restarts.
Default Shared Preferences
Android provides PreferenceManager.getDefaultSharedPreferences(context)
to create and access values in shared preferences.
This stores the values in plain text under {app_dir}/shared_prefs/{package_name}_preferences
.
AndroidX & Encrypted Shared Preferences
To handle additional security requirements for different apps, there was added a configuration option to enable encrypting of shared preferences.
OpenSRP encrypts shared preferences using the androidx.security.crypto.EncryptedSharedPreferences
implementation. The library is supported from Android version 23 (M) and above. In case the app runs on an Android version lower than 23, OpenSRP uses default unencrypted shared preferences.
To configure shared preferences for encryption, add the entry encrypt.shared.preferences=true
in the app’s app.properties
file. Setting the value to false reverts to using PreferenceManager.getDefaultSharedPreferences(context)
. The setting is disabled by default or when the property value is not explicitly set, i.e., entry is not set in app.properties file.
Centralised Access to Shared Preferences in OpenSRP
Accessing shared preferences through PreferenceManager.getDefaultSharedPreferences
will create the entries in plain text even when the configuration to encrypt is set. This creates parallel entries.
To control this, it is encouraged that all references to shared preferences be made through CoreLibrary
’s Utils.getAllSharedPreferences()
method. This uses the CoreLibrary instance to access AllSharedPreferences
.
Whenever shared preferences are accessed this way, CoreLibrary checks the property setting before creating the shared preferences and returns the correct instance. This prevents parallel entries.
Note: CoreLibrary must have been initialised for the app before this call can be made.
This site is no longer maintained. Please visit docs.opensrp.io for current documentation.