Skip to content

Commit a31ad4e

Browse files
Merge pull request #94 from amitshekhariitbhu/Development
Added SqlCipher support - Development
2 parents 7bb699a + 5a6ff0c commit a31ad4e

14 files changed

Lines changed: 237 additions & 31 deletions

File tree

app/build.gradle

Lines changed: 3 additions & 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_PERSON", "a_password")
3637
}
3738
release {
3839
minifyEnabled false
@@ -49,4 +50,6 @@ dependencies {
4950
compile 'com.android.support:appcompat-v7:25.0.0'
5051
testCompile 'junit:junit:4.12'
5152
debugCompile project(':debug-db')
53+
54+
debugCompile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
5255
}

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2121
package="com.sample">
2222

23+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
24+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
25+
2326
<application
2427
android:allowBackup="true"
2528
android:icon="@mipmap/ic_launcher"

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.sample.database.CarDBHelper;
3131
import com.sample.database.ContactDBHelper;
3232
import com.sample.database.ExtTestDBHelper;
33+
import com.sample.database.PersonDBHelper;
3334
import com.sample.utils.Utils;
3435

3536
import java.util.HashSet;
@@ -96,6 +97,17 @@ protected void onCreate(Bundle savedInstanceState) {
9697
}
9798
}
9899

100+
// Create Person encrypted database
101+
PersonDBHelper personDBHelper = new PersonDBHelper(getApplicationContext());
102+
if (personDBHelper.count() == 0) {
103+
for (int i = 0; i < 100; i++) {
104+
String firstName = PersonDBHelper.PERSON_COLUMN_FIRST_NAME + "_" + i;
105+
String lastName = PersonDBHelper.PERSON_COLUMN_LAST_NAME + "_" + i;
106+
String address = PersonDBHelper.PERSON_COLUMN_ADDRESS + "_" + i;
107+
personDBHelper.insertPerson(firstName, lastName, address);
108+
}
109+
}
110+
99111
Utils.setCustomDatabaseFiles(getApplicationContext());
100112
}
101113

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
20+
package com.sample.database;
21+
22+
import android.app.Application;
23+
import android.content.ContentValues;
24+
import android.content.Context;
25+
import android.database.Cursor;
26+
import net.sqlcipher.DatabaseUtils;
27+
import net.sqlcipher.database.SQLiteDatabase;
28+
import net.sqlcipher.database.SQLiteOpenHelper;
29+
30+
import java.util.ArrayList;
31+
32+
public class PersonDBHelper extends SQLiteOpenHelper {
33+
34+
public static final String DATABASE_NAME = "Person.db";
35+
public static final String PERSON_TABLE_NAME = "person";
36+
public static final String PERSON_COLUMN_ID = "id";
37+
public static final String PERSON_COLUMN_FIRST_NAME = "first_name";
38+
public static final String PERSON_COLUMN_LAST_NAME = "last_name";
39+
public static final String PERSON_COLUMN_ADDRESS = "address";
40+
private static final String DB_PASSWORD = "a_password";
41+
42+
public PersonDBHelper(Context context) {
43+
44+
super(context, DATABASE_NAME, null, 1);
45+
SQLiteDatabase.loadLibs(context);
46+
}
47+
48+
@Override
49+
public void onCreate(SQLiteDatabase db) {
50+
db.execSQL(
51+
"create table person " +
52+
"(id integer primary key, first_name text, last_name text, address text)"
53+
);
54+
}
55+
56+
@Override
57+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
58+
db.execSQL("DROP TABLE IF EXISTS person");
59+
onCreate(db);
60+
}
61+
62+
public boolean insertPerson(String firstName, String lastName, String address) {
63+
SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD);
64+
ContentValues contentValues = new ContentValues();
65+
contentValues.put("first_name", firstName);
66+
contentValues.put("last_name", lastName);
67+
contentValues.put("address", address);
68+
db.insert("person", null, contentValues);
69+
db.close();
70+
return true;
71+
}
72+
73+
public Cursor getData(int id) {
74+
SQLiteDatabase db = this.getReadableDatabase(DB_PASSWORD);
75+
Cursor res = db.rawQuery("select * from person where id=" + id + "", null);
76+
return res;
77+
}
78+
79+
public int numberOfRows() {
80+
SQLiteDatabase db = this.getReadableDatabase(DB_PASSWORD);
81+
int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
82+
return numRows;
83+
}
84+
85+
public boolean updatePerson(Integer id, String firstName, String lastName, String address, float mileage) {
86+
SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD);
87+
ContentValues contentValues = new ContentValues();
88+
contentValues.put("first_name", firstName);
89+
contentValues.put("last_name", lastName);
90+
contentValues.put("address", address);
91+
db.update("person", contentValues, "id = ? ", new String[]{Integer.toString(id)});
92+
db.close();
93+
return true;
94+
}
95+
96+
public Integer deletePerson(Integer id) {
97+
SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD);
98+
return db.delete("person",
99+
"id = ? ",
100+
new String[]{Integer.toString(id)});
101+
}
102+
103+
public ArrayList<String> getAllPerson() {
104+
ArrayList<String> arrayList = new ArrayList<>();
105+
106+
SQLiteDatabase db = this.getReadableDatabase(DB_PASSWORD);
107+
Cursor res = db.rawQuery("select * from person", null);
108+
res.moveToFirst();
109+
110+
while (!res.isAfterLast()) {
111+
arrayList.add(
112+
res.getString(res.getColumnIndex(PERSON_COLUMN_FIRST_NAME)) + " " +
113+
res.getString(res.getColumnIndex(PERSON_COLUMN_LAST_NAME)));
114+
res.moveToNext();
115+
}
116+
res.close();
117+
db.close();
118+
return arrayList;
119+
}
120+
121+
public int count() {
122+
SQLiteDatabase db = getReadableDatabase(DB_PASSWORD);
123+
Cursor cursor = db.rawQuery("select * from person", null);
124+
try {
125+
if (cursor != null && cursor.getCount() > 0) {
126+
cursor.moveToFirst();
127+
return cursor.getInt(0);
128+
} else {
129+
return 0;
130+
}
131+
}
132+
finally {
133+
cursor.close();
134+
db.close();
135+
}
136+
}
137+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.sample.utils;
2121

2222
import android.content.Context;
23+
import android.util.Pair;
2324
import android.widget.Toast;
2425

2526
import com.sample.BuildConfig;
@@ -58,11 +59,11 @@ public static void setCustomDatabaseFiles(Context context) {
5859
Class<?> debugDB = Class.forName("com.amitshekhar.DebugDB");
5960
Class[] argTypes = new Class[]{HashMap.class};
6061
Method setCustomDatabaseFiles = debugDB.getMethod("setCustomDatabaseFiles", argTypes);
61-
HashMap<String, File> customDatabaseFiles = new HashMap<>();
62+
HashMap<String, Pair<File, String>> customDatabaseFiles = new HashMap<>();
6263
// set your custom database files
6364
customDatabaseFiles.put(ExtTestDBHelper.DATABASE_NAME,
64-
new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
65-
"/" + ExtTestDBHelper.DATABASE_NAME));
65+
new Pair<>(new File(context.getFilesDir() + "/" + ExtTestDBHelper.DIR_NAME +
66+
"/" + ExtTestDBHelper.DATABASE_NAME), ""));
6667
setCustomDatabaseFiles.invoke(null, customDatabaseFiles);
6768
} catch (Exception ignore) {
6869

debug-db/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
})
5050
testCompile 'junit:junit:4.12'
5151
compile 'com.google.code.gson:gson:2.8.0'
52+
compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
5253
}
5354

5455
//apply from: 'debug-db-upload.gradle'

debug-db/src/main/assets/app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ function getDBList() {
6868
$('#db-list').empty();
6969
var isSelectionDone = false;
7070
for(var count = 0; count < dbList.length; count++){
71-
if(dbList[count].indexOf("journal") == -1){
72-
$("#db-list").append("<a href='#' id=" +dbList[count] + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbList[count] + "\");'>" +dbList[count] + "</a>");
71+
var dbName = dbList[count][0];
72+
var isEncrypted = dbList[count][1];
73+
var dbAttribute = isEncrypted == "true" ? ' <span class="glyphicon glyphicon-lock" aria-hidden="true" style="color:blue"></span>' : "";
74+
if(dbName.indexOf("journal") == -1){
75+
$("#db-list").append("<a href='#' id=" + dbName + " class='list-group-item' onClick='openDatabaseAndGetTableList(\""+ dbName + "\");'>" + dbName + dbAttribute + "</a>");
7376
if(!isSelectionDone){
7477
isSelectionDone = true;
7578
$('#db-list').find('a').trigger('click');
44.3 KB
Binary file not shown.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import android.content.Context;
2323
import android.util.Log;
24+
import android.util.Pair;
2425

2526
import com.amitshekhar.server.ClientServer;
2627
import com.amitshekhar.utils.NetworkUtils;
@@ -73,7 +74,7 @@ public static void shutDown() {
7374
}
7475
}
7576

76-
public static void setCustomDatabaseFiles(HashMap<String, File> customDatabaseFiles){
77+
public static void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> customDatabaseFiles){
7778
if(clientServer!=null){
7879
clientServer.setCustomDatabaseFiles(customDatabaseFiles);
7980
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import android.content.Context;
2828
import android.util.Log;
29+
import android.util.Pair;
2930

3031
import java.io.File;
3132
import java.io.IOException;
@@ -83,11 +84,11 @@ public void run() {
8384
} catch (IOException e) {
8485
Log.e(TAG, "Web server error.", e);
8586
} catch (Exception ignore) {
86-
87+
Log.e(TAG, "Exception.", ignore);
8788
}
8889
}
8990

90-
public void setCustomDatabaseFiles(HashMap<String, File> customDatabaseFiles){
91+
public void setCustomDatabaseFiles(HashMap<String, Pair<File, String>> customDatabaseFiles){
9192
mRequestHandler.setCustomDatabaseFiles(customDatabaseFiles);
9293
}
9394

0 commit comments

Comments
 (0)