Skip to content

Commit

Permalink
Merge pull request #102 from amitshekhariitbhu/development
Browse files Browse the repository at this point in the history
Add support for debugging inMemory Room Database
  • Loading branch information
amitshekhariitbhu authored Feb 12, 2018
2 parents 4b35ebc + 7595b16 commit e011034
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 46 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
### Using Android Debug Database Library in your application
Add this to your app's build.gradle
```groovy
debugCompile 'com.amitshekhar.android:debug-db:1.0.2'
debugCompile 'com.amitshekhar.android:debug-db:1.0.3'
```

Use `debugCompile` so that it will only compile in your debug build and not in your release build.
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void showDebugDBAddressLogToast(Context context) {
```

### Adding custom database files
As this library is auto-initialize, if you want to add custom database files, add the following method and call
As this library is auto-initialize, if you want to debug custom database files, add the following method and call
```java
public static void setCustomDatabaseFiles(Context context) {
if (BuildConfig.DEBUG) {
Expand All @@ -116,6 +116,26 @@ public static void setCustomDatabaseFiles(Context context) {
}
```

### Adding InMemory Room databases
As this library is auto-initialize, if you want to debug inMemory Room databases, add the following method and call
```java
public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
if (BuildConfig.DEBUG) {
try {
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
Class[] argTypes = new Class[]{HashMap.class};
HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
// set your inMemory databases
inMemoryDatabases.put("InMemoryOne.db", database[0]);
Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
} catch (Exception ignore) {

}
}
}
```

### Find this project useful ? :heart:
* Support it by clicking the :star: button on the upper right of this page. :v:

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,20 @@ protected void onCreate(Bundle savedInstanceState) {
userDBHelper.insertUser(userList);
}

// Room inMemory database
if (userDBHelper.countInMemory() == 0) {
List<User> userList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
User user = new User();
user.id = (long) (i + 1);
user.name = "in_memory_user_" + i;
userList.add(user);
}
userDBHelper.insertUserInMemory(userList);
}

Utils.setCustomDatabaseFiles(getApplicationContext());
Utils.setInMemoryRoomDatabases(userDBHelper.getInMemoryDatabase());
}

public void showDebugDbAddress(View view) {
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/sample/database/room/UserDBHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sample.database.room;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Room;
import android.content.Context;

Expand All @@ -12,9 +13,13 @@
public class UserDBHelper {

private final AppDatabase appDatabase;
private final AppDatabase inMemoryAppDatabase;

public UserDBHelper(Context context) {
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database")
appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User.db")
.allowMainThreadQueries()
.build();
inMemoryAppDatabase = Room.inMemoryDatabaseBuilder(context, AppDatabase.class)
.allowMainThreadQueries()
.build();
}
Expand All @@ -23,8 +28,19 @@ public void insertUser(List<User> userList) {
appDatabase.userDao().insertAll(userList);
}

public void insertUserInMemory(List<User> userList) {
inMemoryAppDatabase.userDao().insertAll(userList);
}

public int count() {
return appDatabase.userDao().loadAll().size();
}

public int countInMemory() {
return inMemoryAppDatabase.userDao().loadAll().size();
}

public SupportSQLiteDatabase getInMemoryDatabase() {
return inMemoryAppDatabase.getOpenHelper().getWritableDatabase();
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/sample/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.sample.utils;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.content.Context;
import android.util.Pair;
import android.widget.Toast;
Expand Down Expand Up @@ -71,4 +72,20 @@ public static void setCustomDatabaseFiles(Context context) {
}
}

public static void setInMemoryRoomDatabases(SupportSQLiteDatabase... database) {
if (BuildConfig.DEBUG) {
try {
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
Class[] argTypes = new Class[]{HashMap.class};
HashMap<String, SupportSQLiteDatabase> inMemoryDatabases = new HashMap<>();
// set your inMemory databases
inMemoryDatabases.put("InMemoryOne.db", database[0]);
Method setRoomInMemoryDatabase = debugDB.getMethod("setInMemoryRoomDatabases", argTypes);
setRoomInMemoryDatabase.invoke(null, inMemoryDatabases);
} catch (Exception ignore) {

}
}
}

}
2 changes: 1 addition & 1 deletion debug-db/debug-db-upload.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def siteUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database'
def gitUrl = 'https://github.com/amitshekhariitbhu/Android-Debug-Database.git'

group = "com.amitshekhar.android"
version = '1.0.2'
version = '1.0.3'

install {
repositories.mavenInstaller {
Expand Down
25 changes: 18 additions & 7 deletions debug-db/src/main/assets/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ function getDBList() {
for(var count = 0; count < dbList.length; count++){
var dbName = dbList[count][0];
var isEncrypted = dbList[count][1];
var isDownloadable = dbList[count][2];
var dbAttribute = isEncrypted == "true" ? ' <span class="glyphicon glyphicon-lock" aria-hidden="true" style="color:blue"></span>' : "";
if(dbName.indexOf("journal") == -1){
$("#db-list").append("<a href='#' id=" + dbName + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbName + "\");'>" + dbName + dbAttribute + "</a>");
$("#db-list").append("<a href='#' id=" + dbName + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbName + "\", \""+ isDownloadable + "\");'>" + dbName + dbAttribute + "</a>");
if(!isSelectionDone){
isSelectionDone = true;
$('#db-list').find('a').trigger('click');
$('#db-list').find('a').trigger('click');
}
}
}
Expand All @@ -85,7 +86,7 @@ function getDBList() {
}

var lastTableName = getHashValue('table');
function openDatabaseAndGetTableList(db) {
function openDatabaseAndGetTableList(db, isDownloadable) {

if("APP_SHARED_PREFERENCES" == db) {
$('#run-query').removeClass('active');
Expand All @@ -97,10 +98,16 @@ function openDatabaseAndGetTableList(db) {
} else {
$('#run-query').removeClass('disabled');
$('#run-query').addClass('active');
$('#selected-db-info').removeClass('disabled');
$('#selected-db-info').addClass('active');
if("true" == isDownloadable) {
$('#selected-db-info').removeClass('disabled');
$('#selected-db-info').addClass('active');
$("#selected-db-info").text("Export Selected Database : "+db);
} else {
$('#selected-db-info').removeClass('active');
$('#selected-db-info').addClass('disabled');
$("#selected-db-info").text("Selected Database : "+db);
}
isDatabaseSelected = true;
$("#selected-db-info").text("Export Selected Database : "+db);
}


Expand All @@ -110,7 +117,11 @@ function openDatabaseAndGetTableList(db) {
var tableList = result.rows;
var dbVersion = result.dbVersion;
if("APP_SHARED_PREFERENCES" != db) {
$("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion);
if("true" == isDownloadable) {
$("#selected-db-info").text("Export Selected Database : "+db +" Version : "+dbVersion);
} else {
$("#selected-db-info").text("Selected Database : "+db +" Version : "+dbVersion);
}
}
$('#table-list').empty()
for(var count = 0; count < tableList.length; count++){
Expand Down
7 changes: 7 additions & 0 deletions debug-db/src/main/java/com/amitshekhar/DebugDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.amitshekhar;

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.content.Context;
import android.util.Log;
import android.util.Pair;
Expand Down Expand Up @@ -79,6 +80,12 @@ public static void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> cu
}
}

public static void setInMemoryRoomDatabases(HashMap<String, SupportSQLiteDatabase> databases) {
if (clientServer != null) {
clientServer.setInMemoryRoomDatabases(databases);
}
}

public static boolean isServerRunning() {
return clientServer != null && clientServer.isRunning();
}
Expand Down
10 changes: 6 additions & 4 deletions debug-db/src/main/java/com/amitshekhar/server/ClientServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/


import android.arch.persistence.db.SupportSQLiteDatabase;
import android.content.Context;
import android.util.Log;
import android.util.Pair;
Expand All @@ -40,13 +41,10 @@ public class ClientServer implements Runnable {
private static final String TAG = "ClientServer";

private final int mPort;

private final RequestHandler mRequestHandler;
private boolean mIsRunning;

private ServerSocket mServerSocket;

private final RequestHandler mRequestHandler;

public ClientServer(Context context, int port) {
mRequestHandler = new RequestHandler(context);
mPort = port;
Expand Down Expand Up @@ -91,6 +89,10 @@ public void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> customDat
mRequestHandler.setCustomDatabaseFiles(customDatabaseFiles);
}

public void setInMemoryRoomDatabases(HashMap<String, SupportSQLiteDatabase> databases) {
mRequestHandler.setInMemoryRoomDatabases(databases);
}

public boolean isRunning() {
return mIsRunning;
}
Expand Down
Loading

0 comments on commit e011034

Please sign in to comment.