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) {