@@ -3,6 +3,8 @@ package com.banrossyn.sharedprefexample
33import android.content.Context
44import android.content.SharedPreferences
55import androidx.core.content.edit
6+ import androidx.security.crypto.EncryptedSharedPreferences
7+ import androidx.security.crypto.MasterKey
68import com.google.gson.Gson
79import com.google.gson.JsonParseException
810import com.google.gson.JsonSyntaxException
@@ -17,45 +19,64 @@ import kotlin.reflect.KProperty
1719 */
1820object SharedPref {
1921 private var TAG = " SharedPref"
20- private var preferences: SharedPreferences ? = null
22+ private var preferences : SharedPreferences ? = null
2123 private const val SHARED_PREFERENCE_FILE_NAME = " MY-PREFERENCE"
24+
2225 /* *
2326 * Initializes the SharedPreferences.
2427 * @param context The application context
2528 */
26- fun init (context : Context ) {
29+ fun init (context : Context ) {
2730 Timber .tag(TAG ).d(" init: %s" , context)
28- this .preferences = context.getSharedPreferences(
29- SHARED_PREFERENCE_FILE_NAME , Context .MODE_PRIVATE
31+
32+
33+ /* Normal way*/
34+ /* this.preferences = context.getSharedPreferences(
35+ SHARED_PREFERENCE_FILE_NAME, Context.MODE_PRIVATE
36+ )*/
37+
38+
39+ val masterKeyAlias = MasterKey .Builder (context)
40+ .setKeyScheme(MasterKey .KeyScheme .AES256_GCM )
41+ .build()
42+
43+ this .preferences = EncryptedSharedPreferences .create(
44+ context,
45+ SHARED_PREFERENCE_FILE_NAME ,
46+ masterKeyAlias,
47+ EncryptedSharedPreferences .PrefKeyEncryptionScheme .AES256_SIV ,
48+ EncryptedSharedPreferences .PrefValueEncryptionScheme .AES256_GCM
3049 )
50+
3151 }
52+
3253 /* *
3354 * Checks the initialization status of SharedPreferences.
3455 * @return true if SharedPreferences has been initialized, false otherwise
3556 */
36- fun isInitialized (): Boolean {
57+ fun isInitialized () : Boolean {
3758 return preferences != null
3859 }
60+
3961 /* *
4062 * Registers a listener to listen for changes in SharedPreferences.
4163 * @param listener An object implementing the OnSharedPreferenceChangeListener interface
4264 */
43- fun registerOnSharedPreferenceChangeListener (listener : SharedPreferences .OnSharedPreferenceChangeListener ) {
65+ fun registerOnSharedPreferenceChangeListener (listener : SharedPreferences .OnSharedPreferenceChangeListener ) {
4466 preferences!! .registerOnSharedPreferenceChangeListener(listener)
4567 }
68+
4669 /* *
4770 * Unregisters a previously registered listener.
4871 * @param listener The OnSharedPreferenceChangeListener to be unregistered
4972 */
50- fun unregisterOnSharedPreferenceChangeListener (listener : SharedPreferences .OnSharedPreferenceChangeListener ) {
73+ fun unregisterOnSharedPreferenceChangeListener (listener : SharedPreferences .OnSharedPreferenceChangeListener ) {
5174 preferences!! .unregisterOnSharedPreferenceChangeListener(listener)
5275 }
5376
5477
55-
56-
57- const val isLogged = " isLoggedIn"
58- const val age = " userAge"
78+ const val isLogged = " isLoggedIn"
79+ const val age = " userAge"
5980
6081 var isLoggedIn by BooleanPreference (isLogged, false )
6182 var userAge by IntPreference (age, 0 )
@@ -70,73 +91,74 @@ object SharedPref {
7091 * @property name The key for the preference
7192 * @property defaultValue The default value if the preference is not set
7293 */
73- private class BooleanPreference (private val name : String , private val defaultValue : Boolean ) :
94+ private class BooleanPreference (private val name : String , private val defaultValue : Boolean ) :
7495 ReadWriteProperty <SharedPref , Boolean > {
75- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
96+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
7697 preferences!! .getBoolean(name, defaultValue)
7798
78- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Boolean ) =
99+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Boolean ) =
79100 preferences!! .edit { putBoolean(name, value) }
80101 }
102+
81103 /* *
82104 * Delegate for Int preferences.
83105 * @property name The key for the preference
84106 * @property defaultValue The default value if the preference is not set
85107 */
86- private class IntPreference (private val name : String , private val defaultValue : Int ) :
108+ private class IntPreference (private val name : String , private val defaultValue : Int ) :
87109 ReadWriteProperty <SharedPref , Int > {
88- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
110+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
89111 preferences!! .getInt(name, defaultValue)
90112
91- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Int ) =
113+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Int ) =
92114 preferences!! .edit { putInt(name, value) }
93115 }
94116
95- private class LongPreference (private val name : String , private val defaultValue : Long ) :
117+ private class LongPreference (private val name : String , private val defaultValue : Long ) :
96118 ReadWriteProperty <SharedPref , Long > {
97- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
119+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
98120 preferences!! .getLong(name, defaultValue)
99121
100- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Long ) =
122+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Long ) =
101123 preferences!! .edit { putLong(name, value) }
102124 }
103125
104- private class FloatPreference (private val name : String , private val defaultValue : Float ) :
126+ private class FloatPreference (private val name : String , private val defaultValue : Float ) :
105127 ReadWriteProperty <SharedPref , Float > {
106- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
128+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
107129 preferences!! .getFloat(name, defaultValue)
108130
109- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Float ) =
131+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Float ) =
110132 preferences!! .edit { putFloat(name, value) }
111133 }
112134
113- private class StringPreference (private val name : String , private val defaultValue : String? ) :
135+ private class StringPreference (private val name : String , private val defaultValue : String? ) :
114136 ReadWriteProperty <SharedPref , String ?> {
115- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
137+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
116138 preferences!! .getString(name, defaultValue) ? : defaultValue
117139
118- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : String? ) =
140+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : String? ) =
119141 preferences!! .edit { putString(name, value) }
120142 }
121143
122144 private class StringSetPreference (
123- private val name : String , private val defaultValue : Set <String >
145+ private val name : String , private val defaultValue : Set <String >
124146 ) : ReadWriteProperty<SharedPref, Set<String>> {
125- override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
147+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) =
126148 preferences!! .getStringSet(name, defaultValue) ? : defaultValue
127149
128- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Set <String >) =
150+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : Set <String >) =
129151 preferences!! .edit { putStringSet(name, value) }
130152 }
131153
132- private class ObjectPreference <T : Any >(
133- private val key : String ,
134- private val defaultValue : T ,
135- private val typeToken : TypeToken <T >
154+ private class ObjectPreference <T : Any >(
155+ private val key : String ,
156+ private val defaultValue : T ,
157+ private val typeToken : TypeToken <T >
136158 ) : ReadWriteProperty<SharedPref, T> {
137159
138160 @Throws(JsonParseException ::class )
139- override fun getValue (thisRef : SharedPref , property : KProperty <* >): T {
161+ override fun getValue (thisRef : SharedPref , property : KProperty <* >) : T {
140162 if (key.isEmpty()) {
141163 Timber .w(" > getObject, key must not be empty" )
142164 return defaultValue
@@ -148,15 +170,15 @@ object SharedPref {
148170 } else {
149171 try {
150172 Gson ().fromJson(json, typeToken.type) as T
151- } catch (e: JsonSyntaxException ) {
173+ } catch (e : JsonSyntaxException ) {
152174 Timber .e(" > getObject, Object stored with Key $key not able to instance to ${typeToken.type} " )
153175 throw e
154176 }
155177 }
156178 }
157179
158180 @Throws(IllegalArgumentException ::class )
159- override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : T ) {
181+ override fun setValue (thisRef : SharedPref , property : KProperty <* >, value : T ) {
160182 require(key.isNotEmpty()) { " Key must not be empty" }
161183 Timber .i(" > putObject, storing $value with key $key " )
162184 preferences!! .edit().putString(key, Gson ().toJson(value)).apply ()
0 commit comments