Skip to content

Commit 7ffd132

Browse files
Simplify setCustomDatabaseFiles
1 parent 4d453a1 commit 7ffd132

8 files changed

Lines changed: 88 additions & 106 deletions

File tree

app/src/main/java/com/sample/ExternalDatabaseFileProvider.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

app/src/main/java/com/sample/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ protected void onCreate(Bundle savedInstanceState) {
9595
extTestDBHelper.insertTest(value);
9696
}
9797
}
98+
99+
Utils.setCustomDatabaseFiles(getApplicationContext());
98100
}
99101

100102
public void showDebugDbAddress(View view) {

app/src/main/java/com/sample/utils/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import android.widget.Toast;
2424

2525
import com.sample.BuildConfig;
26+
import com.sample.database.ExtTestDBHelper;
2627

28+
import java.io.File;
2729
import java.lang.reflect.Method;
30+
import java.util.HashMap;
2831

2932
/**
3033
* Created by amitshekhar on 07/02/17.
@@ -48,4 +51,23 @@ public static void showDebugDBAddressLogToast(Context context) {
4851
}
4952
}
5053
}
54+
55+
public static void setCustomDatabaseFiles(Context context) {
56+
if (BuildConfig.DEBUG) {
57+
try {
58+
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
59+
Class[] argTypes = new Class[]{HashMap.class};
60+
Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
61+
HashMap<String, File> customDatabaseFiles = new HashMap<>();
62+
// set custom database files
63+
customDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,
64+
new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
65+
"/" + ExtTestDBHelper.DATABASE_NAME));
66+
setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
67+
} catch (Exception ignore) {
68+
69+
}
70+
}
71+
}
72+
5173
}

debug-db/src/main/java/com/amitshekhar/DebugDB.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import com.amitshekhar.server.ClientServer;
2626
import com.amitshekhar.utils.NetworkUtils;
2727

28+
import java.io.File;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
2832
/**
2933
* Created by amitshekhar on 15/11/16.
3034
*/
@@ -68,6 +72,12 @@ public static void shutDown() {
6872
clientServer = null;
6973
}
7074
}
75+
76+
public static void setCustomDatabaseFiles(HashMap<String, File> customDatabaseFiles){
77+
if(clientServer!=null){
78+
clientServer.setCustomDatabaseFiles(customDatabaseFiles);
79+
}
80+
}
7181

7282
public static boolean isServerRunning() {
7383
return clientServer != null && clientServer.isRunning();

debug-db/src/main/java/com/amitshekhar/server/ClientServer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import android.content.Context;
2828
import android.util.Log;
2929

30+
import java.io.File;
3031
import java.io.IOException;
3132
import java.net.ServerSocket;
3233
import java.net.Socket;
3334
import java.net.SocketException;
35+
import java.util.HashMap;
36+
import java.util.Map;
3437

3538
public class ClientServer implements Runnable {
3639

@@ -84,6 +87,10 @@ public void run() {
8487
}
8588
}
8689

90+
public void setCustomDatabaseFiles(HashMap<String, File> customDatabaseFiles){
91+
mRequestHandler.setCustomDatabaseFiles(customDatabaseFiles);
92+
}
93+
8794
public boolean isRunning() {
8895
return mIsRunning;
8996
}

debug-db/src/main/java/com/amitshekhar/server/RequestHandler.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.amitshekhar.model.TableDataResponse;
3131
import com.amitshekhar.model.UpdateRowResponse;
3232
import com.amitshekhar.utils.Constants;
33-
import com.amitshekhar.utils.InternalDatabaseFileProvider;
33+
import com.amitshekhar.utils.DatabaseFileProvider;
3434
import com.amitshekhar.utils.DatabaseHelper;
3535
import com.amitshekhar.utils.PrefHelper;
3636
import com.amitshekhar.utils.Utils;
@@ -59,7 +59,8 @@ public class RequestHandler {
5959
private final AssetManager mAssets;
6060
private boolean isDbOpened;
6161
private SQLiteDatabase mDatabase;
62-
private HashMap<String, File> databaseFiles;
62+
private HashMap<String, File> mDatabaseFiles;
63+
private HashMap<String, File> mCustomDatabaseFiles;
6364
private String mSelectedDatabase = null;
6465

6566
public RequestHandler(Context context) {
@@ -117,7 +118,7 @@ public void handle(Socket socket) throws IOException {
117118
final String response = executeQueryAndGetResponse(route);
118119
bytes = response.getBytes();
119120
} else if (route.startsWith("downloadDb")) {
120-
bytes = Utils.getDatabase(mSelectedDatabase, databaseFiles);
121+
bytes = Utils.getDatabase(mSelectedDatabase, mDatabaseFiles);
121122
} else {
122123
bytes = Utils.loadContent(route, mAssets);
123124
}
@@ -153,14 +154,18 @@ public void handle(Socket socket) throws IOException {
153154
}
154155
}
155156

157+
public void setCustomDatabaseFiles(HashMap<String, File> customDatabaseFiles){
158+
mCustomDatabaseFiles = customDatabaseFiles;
159+
}
160+
156161
private void writeServerError(PrintStream output) {
157162
output.println("HTTP/1.0 500 Internal Server Error");
158163
output.flush();
159164
}
160165

161166
private void openDatabase(String database) {
162167
closeDatabase();
163-
File databaseFile = databaseFiles.get(database);
168+
File databaseFile = mDatabaseFiles.get(database);
164169
mDatabase = SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), null);
165170
isDbOpened = true;
166171
}
@@ -174,10 +179,13 @@ private void closeDatabase() {
174179
}
175180

176181
private String getDBListResponse() {
177-
databaseFiles = InternalDatabaseFileProvider.getDatabaseFiles(mContext);
182+
mDatabaseFiles = DatabaseFileProvider.getDatabaseFiles(mContext);
183+
if(mCustomDatabaseFiles!=null){
184+
mDatabaseFiles.putAll(mCustomDatabaseFiles);
185+
}
178186
Response response = new Response();
179-
if (databaseFiles != null) {
180-
for (HashMap.Entry<String, File> entry : databaseFiles.entrySet()) {
187+
if (mDatabaseFiles != null) {
188+
for (HashMap.Entry<String, File> entry : mDatabaseFiles.entrySet()) {
181189
response.rows.add(entry.getKey());
182190
}
183191
}
Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1+
/*
2+
*
3+
* * Copyright (C) 2016 Amit Shekhar
4+
* * Copyright (C) 2011 Android Open Source Project
5+
* *
6+
* * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * you may not use this file except in compliance with the License.
8+
* * You may obtain a copy of the License at
9+
* *
10+
* * http://www.apache.org/licenses/LICENSE-2.0
11+
* *
12+
* * Unless required by applicable law or agreed to in writing, software
13+
* * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * See the License for the specific language governing permissions and
16+
* * limitations under the License.
17+
*
18+
*/
19+
120
package com.amitshekhar.utils;
221

322
import android.content.Context;
4-
import android.content.pm.ApplicationInfo;
5-
import android.content.pm.PackageManager;
623

724
import java.io.File;
8-
import java.util.Map;
25+
import java.util.HashMap;
26+
import java.util.List;
927

10-
public abstract class DatabaseFileProvider {
28+
/**
29+
* Created by amitshekhar on 06/02/17.
30+
*/
1131

12-
public static final String METADATA_TAG = "DatabaseFileProvider";
13-
protected final Context context;
32+
public class DatabaseFileProvider {
1433

15-
public DatabaseFileProvider(Context context) {
16-
this.context = context;
34+
private DatabaseFileProvider() {
35+
// This class in not publicly instantiable
1736
}
1837

19-
public static DatabaseFileProvider fromMetadata(Context context) {
20-
DatabaseFileProvider databaseFileProvider = null;
38+
public static HashMap<String, File> getDatabaseFiles(Context context) {
39+
HashMap<String, File> databaseFiles = new HashMap<>();
2140
try {
22-
ApplicationInfo appInfo = context.getPackageManager()
23-
.getApplicationInfo(context.getPackageName(),
24-
PackageManager.GET_META_DATA);
25-
String className = appInfo.metaData.getString(METADATA_TAG);
26-
if (className != null) {
27-
Class<DatabaseFileProvider> databaseFileProviderClass = (Class<DatabaseFileProvider>) Class.forName(className);
28-
databaseFileProvider = databaseFileProviderClass.getConstructor(Context.class).newInstance(context);
41+
for (String databaseName : context.databaseList()) {
42+
databaseFiles.put(databaseName, context.getDatabasePath(databaseName));
2943
}
3044
} catch (Exception e) {
3145
e.printStackTrace();
3246
}
33-
return databaseFileProvider;
47+
return databaseFiles;
3448
}
3549

36-
protected abstract Map<String, File> getDatabaseFiles();
3750
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)