From bbbbfe7bddcc2bb3bb71c69393955ba4abe1894c Mon Sep 17 00:00:00 2001 From: Jonathan Dick Date: Fri, 9 Feb 2024 10:17:08 -0500 Subject: [PATCH] Stop caching preferences instance (#20445) It appears that reusing the shared preferences instance sometimes causes changes to not be properly committed to disk as the instance is not disposed in some cases before the app is torn down (eg: force close). Caching isn't really necessary here anyways as the underlying android preference manager does its own layer of caching (so we are just paying the interop tax to get a 'new' instance here). The simplest fix is to stop caching which is the real content of this change. We don't cache in the regular preferences API where we use shared preferences and the issue does not seem to exist there. --- .../SecureStorage/SecureStorage.android.cs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Essentials/src/SecureStorage/SecureStorage.android.cs b/src/Essentials/src/SecureStorage/SecureStorage.android.cs index a55f9a672c56..d0f550957b6f 100644 --- a/src/Essentials/src/SecureStorage/SecureStorage.android.cs +++ b/src/Essentials/src/SecureStorage/SecureStorage.android.cs @@ -15,9 +15,11 @@ Task PlatformGetAsync(string key) { try { - ISharedPreferences sharedPreferences = GetEncryptedSharedPreferences(); - if (sharedPreferences != null) - return sharedPreferences.GetString(key, null); + var prefs = GetEncryptedSharedPreferences(); + if (prefs != null) + { + return prefs.GetString(key, null); + } // TODO: Use Logger here? System.Diagnostics.Debug.WriteLine( @@ -48,11 +50,16 @@ Task PlatformSetAsync(string key, string data) { return Task.Run(() => { - using ISharedPreferencesEditor editor = GetEncryptedSharedPreferences()?.Edit(); + using var prefs = GetEncryptedSharedPreferences(); + using var editor = prefs?.Edit(); if (data is null) + { editor?.Remove(key); + } else + { editor?.PutString(key, data); + } editor?.Apply(); }); @@ -60,41 +67,35 @@ Task PlatformSetAsync(string key, string data) bool PlatformRemove(string key) { - using ISharedPreferencesEditor editor = GetEncryptedSharedPreferences()?.Edit(); + using var prefs = GetEncryptedSharedPreferences(); + using var editor = prefs?.Edit(); editor?.Remove(key)?.Apply(); return true; } void PlatformRemoveAll() { - using var editor = PreferencesImplementation.GetSharedPreferences(Alias).Edit(); + using var prefs = GetEncryptedSharedPreferences(); + using var editor = prefs?.Edit(); editor?.Clear()?.Apply(); } - ISharedPreferences _prefs; ISharedPreferences GetEncryptedSharedPreferences() { - if (_prefs is not null) - { - return _prefs; - } - try { var context = Application.Context; - MasterKey prefsMainKey = new MasterKey.Builder(context, Alias) + var prefsMainKey = new MasterKey.Builder(context, Alias) .SetKeyScheme(MasterKey.KeyScheme.Aes256Gcm) .Build(); - var sharedPreferences = EncryptedSharedPreferences.Create( + return EncryptedSharedPreferences.Create( context, Alias, prefsMainKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.Aes256Siv, EncryptedSharedPreferences.PrefValueEncryptionScheme.Aes256Gcm); - - return _prefs = sharedPreferences; } catch (InvalidProtocolBufferException) {