Skip to content

Commit eca5c27

Browse files
committed
Added encrypted sample database Person
1 parent 37563f9 commit eca5c27

3 files changed

Lines changed: 152 additions & 1 deletion

File tree

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ android {
3333
buildTypes {
3434
debug {
3535
resValue("string", "PORT_NUMBER", "8081")
36-
resValue("string", "DB_PASSWORD_CONTACT", "1234567890")
36+
resValue("string", "DB_PASSWORD_PERSON", "a_password")
3737
}
3838
release {
3939
minifyEnabled false
@@ -50,4 +50,6 @@ dependencies {
5050
compile 'com.android.support:appcompat-v7:25.0.0'
5151
testCompile 'junit:junit:4.12'
5252
debugCompile project(':debug-db')
53+
54+
debugCompile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
5355
}

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+
}

0 commit comments

Comments
 (0)