@@ -14,13 +14,14 @@ import android.util.Log
1414import androidx.annotation.VisibleForTesting
1515import io.flutter.embedding.engine.plugins.FlutterPlugin
1616import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
17- import io.flutter.plugin.common.BinaryMessenger
1817import java.io.ByteArrayInputStream
1918import java.io.ByteArrayOutputStream
2019import java.io.IOException
2120import java.io.ObjectInputStream
2221import java.io.ObjectOutputStream
2322import java.math.BigInteger
23+ import kotlinx.coroutines.Dispatchers
24+ import kotlinx.coroutines.withContext
2425
2526/* * LegacySharedPreferencesPlugin */
2627@SuppressLint(" UseKtx" )
@@ -29,77 +30,79 @@ class LegacySharedPreferencesPlugin
2930internal constructor (private val listEncoder: SharedPreferencesListEncoder ) :
3031 FlutterPlugin , SharedPreferencesApi {
3132 private lateinit var preferences: SharedPreferences
33+ private val backgroundDispatcher = Dispatchers .IO .limitedParallelism(1 )
3234
3335 constructor () : this (ListEncoder ())
3436
35- private fun setUp (messenger : BinaryMessenger , context : Context ) {
37+ private fun setUp (context : Context ) {
3638 preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME , Context .MODE_PRIVATE )
3739 try {
38- SharedPreferencesApi .setUp(messenger, this )
40+ SharedPreferencesApiRegistrar ().register( this )
3941 } catch (ex: Exception ) {
4042 Log .e(TAG , " Received exception while setting up SharedPreferencesPlugin" , ex)
4143 }
4244 }
4345
4446 override fun onAttachedToEngine (binding : FlutterPluginBinding ) {
45- setUp(binding.binaryMessenger, binding. applicationContext)
47+ setUp(binding.applicationContext)
4648 }
4749
4850 override fun onDetachedFromEngine (binding : FlutterPluginBinding ) {
49- SharedPreferencesApi .setUp(binding.binaryMessenger, null )
50- }
51-
52- override fun setBool (key : String , value : Boolean ): Boolean {
53- return preferences.edit().putBoolean(key, value).commit()
54- }
55-
56- override fun setString ( key : String , value : String ): Boolean {
57- // TODO (tarrinneal): Move this string prefix checking logic to Dart code and make it an
58- // Argument Error.
59- if (value.startsWith(LIST_PREFIX ) ||
60- value.startsWith(BIG_INTEGER_PREFIX ) ||
61- value.startsWith(DOUBLE_PREFIX )) {
62- throw RuntimeException (
63- " StorageError: This string cannot be stored as it clashes with special identifier" +
64- " prefixes" )
65- }
66- return preferences.edit().putString(key, value).commit()
67- }
51+ SharedPreferencesApiRegistrar ().register( null )
52+ }
53+
54+ override suspend fun setBool (key : String , value : Boolean ): Boolean =
55+ withContext(backgroundDispatcher) { preferences.edit().putBoolean(key, value).commit() }
56+
57+ override suspend fun setString ( key : String , value : String ): Boolean =
58+ withContext(backgroundDispatcher) {
59+ // TODO (tarrinneal): Move this string prefix checking logic to Dart code and make it an
60+ // Argument Error.
61+ if (value.startsWith(LIST_PREFIX ) ||
62+ value.startsWith(BIG_INTEGER_PREFIX ) ||
63+ value.startsWith(DOUBLE_PREFIX )) {
64+ throw RuntimeException (
65+ " StorageError: This string cannot be stored as it clashes with special identifier" +
66+ " prefixes" )
67+ }
68+ preferences.edit().putString(key, value).commit()
69+ }
6870
69- override fun setInt (key : String , value : Long ): Boolean {
70- return preferences.edit().putLong(key, value).commit()
71- }
71+ override suspend fun setInt (key : String , value : Long ): Boolean =
72+ withContext(backgroundDispatcher) { preferences.edit().putLong(key, value).commit() }
7273
73- override fun setDouble (key : String , value : Double ): Boolean {
74- val doubleValueStr = value.toString()
75- return preferences.edit().putString(key, DOUBLE_PREFIX + doubleValueStr).commit()
76- }
74+ override suspend fun setDouble (key : String , value : Double ): Boolean =
75+ withContext(backgroundDispatcher) {
76+ val doubleValueStr = value.toString()
77+ preferences.edit().putString(key, DOUBLE_PREFIX + doubleValueStr).commit()
78+ }
7779
78- override fun remove (key : String ): Boolean {
79- return preferences.edit().remove(key).commit()
80- }
80+ override suspend fun remove (key : String ): Boolean =
81+ withContext(backgroundDispatcher) { preferences.edit().remove(key).commit() }
8182
82- override fun setEncodedStringList (key : String , value : String ): Boolean {
83- return preferences.edit().putString(key, value).commit()
84- }
83+ override suspend fun setEncodedStringList (key : String , value : String ): Boolean =
84+ withContext(backgroundDispatcher) { preferences.edit().putString(key, value).commit() }
8585
8686 @Deprecated(" Exists for testing purposes only" )
87- override fun setDeprecatedStringList (key : String , value : List <String >): Boolean {
88- return preferences.edit().putString(key, LIST_PREFIX + listEncoder.encode(value)).commit()
89- }
90-
91- override fun getAll (prefix : String , allowList : List <String >? ): Map <String , Any > {
92- return getAllPrefs(prefix, allowList?.toSet())
93- }
87+ override suspend fun setDeprecatedStringList (key : String , value : List <String >): Boolean =
88+ withContext(backgroundDispatcher) {
89+ preferences.edit().putString(key, LIST_PREFIX + listEncoder.encode(value)).commit()
90+ }
9491
95- override fun clear (prefix : String , allowList : List <String >? ): Boolean {
96- val clearEditor = preferences.edit()
97- val allowSet = allowList?.toSet()
98- preferences.all.keys
99- .filter { key -> key.startsWith(prefix) && (allowSet == null || allowSet.contains(key)) }
100- .forEach { key -> clearEditor.remove(key) }
101- return clearEditor.commit()
102- }
92+ override suspend fun getAll (prefix : String , allowList : List <String >? ): Map <String , Any > =
93+ withContext(backgroundDispatcher) { getAllPrefs(prefix, allowList?.toSet()) }
94+
95+ override suspend fun clear (prefix : String , allowList : List <String >? ): Boolean =
96+ withContext(backgroundDispatcher) {
97+ val clearEditor = preferences.edit()
98+ val allowSet = allowList?.toSet()
99+ preferences.all.keys
100+ .filter { key ->
101+ key.startsWith(prefix) && (allowSet == null || allowSet.contains(key))
102+ }
103+ .forEach { key -> clearEditor.remove(key) }
104+ clearEditor.commit()
105+ }
103106
104107 // Gets all shared preferences, filtered to only those set with the given prefix.
105108 // Optionally filtered also to only those items in the optional [allowList].
0 commit comments