Skip to content

Commit 37563f9

Browse files
committed
Add support for android database password via resource string like DB_PASSWORD_[NAME]
1 parent fdc362c commit 37563f9

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ android {
3333
buildTypes {
3434
debug {
3535
resValue("string", "PORT_NUMBER", "8081")
36+
resValue("string", "DB_PASSWORD_CONTACT", "1234567890")
3637
}
3738
release {
3839
minifyEnabled false

debug-db/src/main/java/com/amitshekhar/utils/DatabaseFileProvider.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
package com.amitshekhar.utils;
2121

2222
import android.content.Context;
23+
import android.content.res.Resources;
2324
import android.util.Pair;
2425

26+
import com.amitshekhar.R;
27+
2528
import java.io.File;
29+
import java.text.MessageFormat;
2630
import java.util.HashMap;
2731
import java.util.List;
2832

@@ -32,6 +36,8 @@
3236

3337
public class DatabaseFileProvider {
3438

39+
private final static String DB_PASSWORD_RESOURCE = "DB_PASSWORD_{0}";
40+
3541
private DatabaseFileProvider() {
3642
// This class in not publicly instantiable
3743
}
@@ -40,12 +46,29 @@ public static HashMap<String, Pair<File, String>> getDatabaseFiles(Context conte
4046
HashMap<String, Pair<File, String>> databaseFiles = new HashMap<>();
4147
try {
4248
for (String databaseName : context.databaseList()) {
43-
databaseFiles.put(databaseName, new Pair<>(context.getDatabasePath(databaseName), ""));
49+
String password = getDbPasswordFromStringResources(context, databaseName);
50+
databaseFiles.put(databaseName, new Pair<>(context.getDatabasePath(databaseName), password));
4451
}
4552
} catch (Exception e) {
4653
e.printStackTrace();
4754
}
4855
return databaseFiles;
4956
}
5057

58+
private static String getDbPasswordFromStringResources(Context context, String name) {
59+
String nameWithoutExt = name;
60+
if (nameWithoutExt.endsWith(".db")) {
61+
nameWithoutExt = nameWithoutExt.substring(0, nameWithoutExt.lastIndexOf('.'));
62+
}
63+
String resourceName = MessageFormat.format(DB_PASSWORD_RESOURCE, nameWithoutExt.toUpperCase());
64+
String password = "";
65+
66+
int resourceId = context.getResources().getIdentifier(resourceName, "string", context.getPackageName());
67+
68+
if (resourceId != 0) {
69+
password = context.getString(resourceId);
70+
}
71+
72+
return password;
73+
}
5174
}

0 commit comments

Comments
 (0)