Skip to content

Commit bf7e998

Browse files
Add dbFactory
1 parent d493d77 commit bf7e998

12 files changed

Lines changed: 125 additions & 20 deletions

File tree

debug-db-base/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ android {
3939

4040
dependencies {
4141
implementation 'com.google.code.gson:gson:2.8.5'
42-
implementation 'net.zetetic:android-database-sqlcipher:3.5.9'
4342
implementation 'android.arch.persistence.room:runtime:1.1.1'
4443
implementation 'com.android.support:appcompat-v7:28.0.0'
4544
testImplementation 'junit:junit:4.12'

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.util.Pair;
2626

2727
import com.amitshekhar.server.ClientServer;
28+
import com.amitshekhar.sqlite.DBFactory;
2829
import com.amitshekhar.utils.NetworkUtils;
2930

3031
import java.io.File;
@@ -45,7 +46,7 @@ private DebugDB() {
4546
// This class in not publicly instantiable
4647
}
4748

48-
public static void initialize(Context context) {
49+
public static void initialize(Context context, DBFactory dbFactory) {
4950
int portNumber;
5051

5152
try {
@@ -56,7 +57,7 @@ public static void initialize(Context context) {
5657
Log.i(TAG, "Using Default port : " + DEFAULT_PORT);
5758
}
5859

59-
clientServer = new ClientServer(context, portNumber);
60+
clientServer = new ClientServer(context, portNumber, dbFactory);
6061
clientServer.start();
6162
addressLog = NetworkUtils.getAddressLog(context, portNumber);
6263
Log.d(TAG, addressLog);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
* Created by amitshekhar on 15/11/16.
2424
*/
2525

26-
2726
import android.arch.persistence.db.SupportSQLiteDatabase;
2827
import android.content.Context;
2928
import android.util.Log;
3029
import android.util.Pair;
3130

31+
import com.amitshekhar.sqlite.DBFactory;
32+
3233
import java.io.File;
3334
import java.io.IOException;
3435
import java.net.ServerSocket;
@@ -45,8 +46,8 @@ public class ClientServer implements Runnable {
4546
private boolean mIsRunning;
4647
private ServerSocket mServerSocket;
4748

48-
public ClientServer(Context context, int port) {
49-
mRequestHandler = new RequestHandler(context);
49+
public ClientServer(Context context, int port, DBFactory dbFactory) {
50+
mRequestHandler = new RequestHandler(context, dbFactory);
5051
mPort = port;
5152
}
5253

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import com.amitshekhar.model.RowDataRequest;
3131
import com.amitshekhar.model.TableDataResponse;
3232
import com.amitshekhar.model.UpdateRowResponse;
33-
import com.amitshekhar.sqlite.DebugSQLiteDB;
33+
import com.amitshekhar.sqlite.DBFactory;
3434
import com.amitshekhar.sqlite.InMemoryDebugSQLiteDB;
3535
import com.amitshekhar.sqlite.SQLiteDB;
3636
import com.amitshekhar.utils.Constants;
@@ -42,8 +42,6 @@
4242
import com.google.gson.GsonBuilder;
4343
import com.google.gson.reflect.TypeToken;
4444

45-
import net.sqlcipher.database.SQLiteDatabase;
46-
4745
import java.io.BufferedReader;
4846
import java.io.File;
4947
import java.io.IOException;
@@ -63,17 +61,19 @@ public class RequestHandler {
6361
private final Context mContext;
6462
private final Gson mGson;
6563
private final AssetManager mAssets;
64+
private final DBFactory mDbFactory;
6665
private boolean isDbOpened;
6766
private SQLiteDB sqLiteDB;
6867
private HashMap<String, Pair<File, String>> mDatabaseFiles;
6968
private HashMap<String, Pair<File, String>> mCustomDatabaseFiles;
7069
private String mSelectedDatabase = null;
7170
private HashMap<String, SupportSQLiteDatabase> mRoomInMemoryDatabases = new HashMap<>();
7271

73-
public RequestHandler(Context context) {
72+
public RequestHandler(Context context, DBFactory dbFactory) {
7473
mContext = context;
7574
mAssets = context.getResources().getAssets();
7675
mGson = new GsonBuilder().serializeNulls().create();
76+
mDbFactory = dbFactory;
7777
}
7878

7979
public void handle(Socket socket) throws IOException {
@@ -184,8 +184,7 @@ private void openDatabase(String database) {
184184
} else {
185185
File databaseFile = mDatabaseFiles.get(database).first;
186186
String password = mDatabaseFiles.get(database).second;
187-
SQLiteDatabase.loadLibs(mContext);
188-
sqLiteDB = new DebugSQLiteDB(SQLiteDatabase.openOrCreateDatabase(databaseFile.getAbsolutePath(), password, null));
187+
sqLiteDB = mDbFactory.create(mContext, databaseFile.getAbsolutePath(), password);
189188
}
190189
isDbOpened = true;
191190
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.amitshekhar.sqlite;
2+
3+
import android.content.Context;
4+
5+
public interface DBFactory {
6+
7+
SQLiteDB create(Context context, String path, String password);
8+
9+
}

debug-db-encrypt/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ android {
1919

2020
dependencies {
2121
implementation project(':debug-db-base')
22+
implementation 'net.zetetic:android-database-sqlcipher:3.5.9'
2223
testImplementation 'junit:junit:4.12'
2324
androidTestImplementation 'com.android.support.test:runner:1.0.2'
2425
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

debug-db-encrypt/src/main/java/com/amitshekhar/debug/encrypt/DebugDBEncryptInitProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.net.Uri;
99

1010
import com.amitshekhar.DebugDB;
11+
import com.amitshekhar.debug.encrypt.sqlite.DebugDBEncryptFactory;
1112

1213
public class DebugDBEncryptInitProvider extends ContentProvider {
1314

@@ -17,7 +18,7 @@ public DebugDBEncryptInitProvider() {
1718

1819
@Override
1920
public boolean onCreate() {
20-
DebugDB.initialize(getContext());
21+
DebugDB.initialize(getContext(), new DebugDBEncryptFactory());
2122
return true;
2223
}
2324

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.amitshekhar.debug.encrypt.sqlite;
2+
3+
import android.content.Context;
4+
5+
import com.amitshekhar.sqlite.DBFactory;
6+
import com.amitshekhar.sqlite.SQLiteDB;
7+
8+
import net.sqlcipher.database.SQLiteDatabase;
9+
10+
public class DebugDBEncryptFactory implements DBFactory {
11+
12+
@Override
13+
public SQLiteDB create(Context context, String path, String password) {
14+
SQLiteDatabase.loadLibs(context);
15+
return new DebugEncryptSQLiteDB(SQLiteDatabase.openOrCreateDatabase(path, password, null));
16+
}
17+
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.amitshekhar.debug.encrypt.sqlite;
2+
3+
import android.content.ContentValues;
4+
import android.database.Cursor;
5+
import android.database.SQLException;
6+
7+
import com.amitshekhar.sqlite.SQLiteDB;
8+
9+
import net.sqlcipher.database.SQLiteDatabase;
10+
11+
/**
12+
* Created by anandgaurav on 12/02/18.
13+
*/
14+
15+
public class DebugEncryptSQLiteDB implements SQLiteDB {
16+
17+
private final SQLiteDatabase database;
18+
19+
public DebugEncryptSQLiteDB(SQLiteDatabase database) {
20+
this.database = database;
21+
}
22+
23+
@Override
24+
public int delete(String table, String whereClause, String[] whereArgs) {
25+
return database.delete(table, whereClause, whereArgs);
26+
}
27+
28+
@Override
29+
public boolean isOpen() {
30+
return database.isOpen();
31+
}
32+
33+
@Override
34+
public void close() {
35+
database.close();
36+
}
37+
38+
@Override
39+
public Cursor rawQuery(String sql, String[] selectionArgs) {
40+
return database.rawQuery(sql, selectionArgs);
41+
}
42+
43+
@Override
44+
public void execSQL(String sql) throws SQLException {
45+
database.execSQL(sql);
46+
}
47+
48+
@Override
49+
public long insert(String table, String nullColumnHack, ContentValues values) {
50+
return database.insert(table, nullColumnHack, values);
51+
}
52+
53+
@Override
54+
public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
55+
return database.update(table, values, whereClause, whereArgs);
56+
}
57+
58+
@Override
59+
public int getVersion() {
60+
return database.getVersion();
61+
}
62+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.net.Uri;
2828

2929
import com.amitshekhar.DebugDB;
30+
import com.amitshekhar.debug.sqlite.DebugDBFactory;
3031

3132
/**
3233
* Created by amitshekhar on 16/11/16.
@@ -40,7 +41,7 @@ public DebugDBInitProvider() {
4041

4142
@Override
4243
public boolean onCreate() {
43-
DebugDB.initialize(getContext());
44+
DebugDB.initialize(getContext(), new DebugDBFactory());
4445
return true;
4546
}
4647

0 commit comments

Comments
 (0)