Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getAll() method #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, String> getAll(@NonNull Context context) {
Set<String> keys = getAllKeys(context);

Map<String, String> 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) {
Expand All @@ -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<String> getAllKeys(@NonNull Context context) {
SharedPreferences preferences = context
.getSharedPreferences(KEY_SHARED_PREFERENCES_NAME, MODE_PRIVATE);
return preferences.getAll().keySet();
}
}