diff --git a/app/src/androidTest/java/de/adorsys/android/securestoragetest/SecureStorageLogicTest.kt b/app/src/androidTest/java/de/adorsys/android/securestoragetest/SecureStorageLogicTest.kt index 67c1f17..a22d376 100644 --- a/app/src/androidTest/java/de/adorsys/android/securestoragetest/SecureStorageLogicTest.kt +++ b/app/src/androidTest/java/de/adorsys/android/securestoragetest/SecureStorageLogicTest.kt @@ -174,4 +174,18 @@ open class SecureStorageLogicTest : SecureStorageBaseTest() { // Delete keys and clear SecureStorage SecurePreferences.clearAllValues(context) } + + @Test + fun testGetAllValues() { + val context = activityRule.activity.applicationContext + val keyValueMap = mapOf("string" to "foo", "int" to "1", "boolean" to "true") + + SecurePreferences.setValue(context, "string", keyValueMap.getValue("string")) + SecurePreferences.setValue(context, "int", keyValueMap.getValue("int").toInt()) + SecurePreferences.setValue(context, "boolean", keyValueMap.getValue("boolean").toBoolean()) + + Assert.assertEquals(keyValueMap, SecurePreferences.getAll(context)) + + SecurePreferences.clearAllValues(context) + } } \ No newline at end of file diff --git a/securestoragelibrary/src/main/java/de/adorsys/android/securestoragelibrary/SecurePreferences.java b/securestoragelibrary/src/main/java/de/adorsys/android/securestoragelibrary/SecurePreferences.java index 58b3550..3d94739 100644 --- a/securestoragelibrary/src/main/java/de/adorsys/android/securestoragelibrary/SecurePreferences.java +++ b/securestoragelibrary/src/main/java/de/adorsys/android/securestoragelibrary/SecurePreferences.java @@ -20,7 +20,9 @@ import android.content.SharedPreferences; import android.text.TextUtils; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import androidx.annotation.NonNull; @@ -117,7 +119,7 @@ public static void setValue(@NonNull Context context, } /** - * Takes plain string value, encrypts it and stores it encrypted in the SecureStorage on the Android Device + * Takes Set(type: String) value, encrypts it and stores it encrypted in the SecureStorage on the Android Device * * @param context Context is used internally * @param key Key used to identify the stored value in SecureStorage @@ -216,7 +218,7 @@ public static int getIntValue(@NonNull Context context, } /** - * Gets encrypted int value for given key from the SecureStorage on the Android Device, decrypts it and returns it + * Gets encrypted Set(type: String) value for given key from the SecureStorage on the Android Device, decrypts it and returns it * * @param context Context is used internally * @param key Key used to identify the stored value in SecureStorage @@ -322,6 +324,24 @@ private static void setSecureValue(@NonNull Context context, preferences.edit().putString(key, value).apply(); } + /** + * Gets all all keys and decrypted String values as a pairs (key : decrypted String value) + * + * @param context Context is used internally + * @return A Map with pairs (key : decrypted String value) + */ + @NonNull + public static Map getAll(@NonNull Context context) { + Set keys = getAllKeys(context); + + Map res = new HashMap<>(); + for (String key : keys) { + res.put(key, getStringValue(context, key, "")); + } + + return res; + } + @Nullable private static String getSecureValue(@NonNull Context context, @NonNull String key) { @@ -342,4 +362,10 @@ private static void clearAllSecureValues(@NonNull Context context) { .getSharedPreferences(KEY_SHARED_PREFERENCES_NAME, MODE_PRIVATE); preferences.edit().clear().apply(); } + + private static Set getAllKeys(@NonNull Context context) { + SharedPreferences preferences = context + .getSharedPreferences(KEY_SHARED_PREFERENCES_NAME, MODE_PRIVATE); + return preferences.getAll().keySet(); + } } \ No newline at end of file