Skip to content

Commit f76b95a

Browse files
Create sample-app for encrypt
1 parent f091d61 commit f76b95a

File tree

16 files changed

+878
-38
lines changed

16 files changed

+878
-38
lines changed

sample-app-encrypt/build.gradle

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,33 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 28
5-
6-
7-
85
defaultConfig {
96
applicationId "com.sample.encrypt"
107
minSdkVersion 15
118
targetSdkVersion 28
129
versionCode 1
1310
versionName "1.0"
14-
1511
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1612

1713
}
18-
1914
buildTypes {
15+
debug {
16+
resValue("string", "PORT_NUMBER", "8080")
17+
resValue("string", "DB_PASSWORD_PERSON", "a_password")
18+
}
2019
release {
2120
minifyEnabled false
22-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2322
}
2423
}
25-
2624
}
2725

2826
dependencies {
29-
implementation fileTree(dir: 'libs', include: ['*.jar'])
30-
27+
debugImplementation project(':debug-db-encrypt')
3128
implementation 'com.android.support:appcompat-v7:28.0.0'
32-
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
29+
implementation 'net.zetetic:android-database-sqlcipher:3.5.9'
30+
implementation 'android.arch.persistence.room:runtime:1.1.1'
31+
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
3332
testImplementation 'junit:junit:4.12'
3433
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3534
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,147 @@
1+
/*
2+
*
3+
* * Copyright (C) 2019 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.sample.encrypt;
221

3-
import android.support.v7.app.AppCompatActivity;
22+
import android.annotation.SuppressLint;
23+
import android.content.Context;
24+
import android.content.SharedPreferences;
425
import android.os.Bundle;
26+
import android.preference.PreferenceManager;
27+
import android.support.v7.app.AppCompatActivity;
28+
import android.view.View;
29+
30+
import com.sample.encrypt.database.CarDBHelper;
31+
import com.sample.encrypt.database.ContactDBHelper;
32+
import com.sample.encrypt.database.ExtTestDBHelper;
33+
import com.sample.encrypt.database.PersonDBHelper;
34+
import com.sample.encrypt.database.room.User;
35+
import com.sample.encrypt.database.room.UserDBHelper;
36+
import com.sample.encrypt.utils.Utils;
37+
38+
import java.util.ArrayList;
39+
import java.util.HashSet;
40+
import java.util.List;
41+
import java.util.Set;
542

643
public class MainActivity extends AppCompatActivity {
744

45+
@SuppressLint("CommitPrefEdits")
846
@Override
947
protected void onCreate(Bundle savedInstanceState) {
48+
1049
super.onCreate(savedInstanceState);
50+
1151
setContentView(R.layout.activity_main);
52+
53+
Set<String> stringSet = new HashSet<>();
54+
stringSet.add("SetOne");
55+
stringSet.add("SetTwo");
56+
stringSet.add("SetThree");
57+
58+
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
59+
60+
SharedPreferences prefsOne = getSharedPreferences("countPrefOne", Context.MODE_PRIVATE);
61+
SharedPreferences prefsTwo = getSharedPreferences("countPrefTwo", Context.MODE_PRIVATE);
62+
63+
sharedPreferences.edit().putString("testOne", "one").commit();
64+
sharedPreferences.edit().putInt("testTwo", 2).commit();
65+
sharedPreferences.edit().putLong("testThree", 100000L).commit();
66+
sharedPreferences.edit().putFloat("testFour", 3.01F).commit();
67+
sharedPreferences.edit().putBoolean("testFive", true).commit();
68+
sharedPreferences.edit().putStringSet("testSix", stringSet).commit();
69+
70+
prefsOne.edit().putString("testOneNew", "one").commit();
71+
72+
prefsTwo.edit().putString("testTwoNew", "two").commit();
73+
74+
ContactDBHelper contactDBHelper = new ContactDBHelper(getApplicationContext());
75+
if (contactDBHelper.count() == 0) {
76+
for (int i = 0; i < 100; i++) {
77+
String name = "name_" + i;
78+
String phone = "phone_" + i;
79+
String email = "email_" + i;
80+
String street = "street_" + i;
81+
String place = "place_" + i;
82+
contactDBHelper.insertContact(name, phone, email, street, place);
83+
}
84+
}
85+
86+
CarDBHelper carDBHelper = new CarDBHelper(getApplicationContext());
87+
if (carDBHelper.count() == 0) {
88+
for (int i = 0; i < 50; i++) {
89+
String name = "name_" + i;
90+
String color = "RED";
91+
float mileage = i + 10.45f;
92+
carDBHelper.insertCar(name, color, mileage);
93+
}
94+
}
95+
96+
ExtTestDBHelper extTestDBHelper = new ExtTestDBHelper(getApplicationContext());
97+
if (extTestDBHelper.count() == 0) {
98+
for (int i = 0; i < 20; i++) {
99+
String value = "value_" + i;
100+
extTestDBHelper.insertTest(value);
101+
}
102+
}
103+
104+
// Create Person encrypted database
105+
PersonDBHelper personDBHelper = new PersonDBHelper(getApplicationContext());
106+
if (personDBHelper.count() == 0) {
107+
for (int i = 0; i < 100; i++) {
108+
String firstName = PersonDBHelper.PERSON_COLUMN_FIRST_NAME + "_" + i;
109+
String lastName = PersonDBHelper.PERSON_COLUMN_LAST_NAME + "_" + i;
110+
String address = PersonDBHelper.PERSON_COLUMN_ADDRESS + "_" + i;
111+
personDBHelper.insertPerson(firstName, lastName, address);
112+
}
113+
}
114+
115+
// Room database
116+
UserDBHelper userDBHelper = new UserDBHelper(getApplicationContext());
117+
if (userDBHelper.count() == 0) {
118+
List<User> userList = new ArrayList<>();
119+
for (int i = 0; i < 20; i++) {
120+
User user = new User();
121+
user.id = (long) (i + 1);
122+
user.name = "user_" + i;
123+
userList.add(user);
124+
}
125+
userDBHelper.insertUser(userList);
126+
}
127+
128+
// Room inMemory database
129+
if (userDBHelper.countInMemory() == 0) {
130+
List<User> userList = new ArrayList<>();
131+
for (int i = 0; i < 20; i++) {
132+
User user = new User();
133+
user.id = (long) (i + 1);
134+
user.name = "in_memory_user_" + i;
135+
userList.add(user);
136+
}
137+
userDBHelper.insertUserInMemory(userList);
138+
}
139+
140+
Utils.setCustomDatabaseFiles(getApplicationContext());
141+
Utils.setInMemoryRoomDatabases(userDBHelper.getInMemoryDatabase());
142+
}
143+
144+
public void showDebugDbAddress(View view) {
145+
Utils.showDebugDBAddressLogToast(getApplicationContext());
12146
}
13147
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
*
3+
* * Copyright (C) 2019 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.encrypt.database;
21+
22+
import android.content.ContentValues;
23+
import android.content.Context;
24+
import android.database.Cursor;
25+
import android.database.DatabaseUtils;
26+
import android.database.sqlite.SQLiteDatabase;
27+
import android.database.sqlite.SQLiteOpenHelper;
28+
29+
import java.util.ArrayList;
30+
31+
/**
32+
* Created by amitshekhar on 06/02/17.
33+
*/
34+
35+
public class CarDBHelper extends SQLiteOpenHelper {
36+
37+
public static final String DATABASE_NAME = "Car.db";
38+
public static final String CARS_TABLE_NAME = "cars";
39+
public static final String CARS_COLUMN_ID = "id";
40+
public static final String CARS_COLUMN_NAME = "name";
41+
public static final String CARS_COLUMN_COLOR = "color";
42+
public static final String CCARS_COLUMN_MILEAGE = "mileage";
43+
44+
public CarDBHelper(Context context) {
45+
super(context, DATABASE_NAME, null, 1);
46+
}
47+
48+
@Override
49+
public void onCreate(SQLiteDatabase db) {
50+
// TODO Auto-generated method stub
51+
db.execSQL(
52+
"create table cars " +
53+
"(id integer primary key, name text, color text, mileage real)"
54+
);
55+
56+
db.execSQL("create table [transaction] (id integer primary key, name text)");
57+
58+
for (int i = 0; i < 10; i++) {
59+
db.execSQL("insert into [transaction] (name) values ('hello');");
60+
}
61+
}
62+
63+
@Override
64+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
65+
// TODO Auto-generated method stub
66+
db.execSQL("DROP TABLE IF EXISTS cars");
67+
onCreate(db);
68+
}
69+
70+
public boolean insertCar(String name, String color, float mileage) {
71+
SQLiteDatabase db = this.getWritableDatabase();
72+
ContentValues contentValues = new ContentValues();
73+
contentValues.put("name", name);
74+
contentValues.put("color", color);
75+
contentValues.put("mileage", mileage);
76+
db.insert("cars", null, contentValues);
77+
return true;
78+
}
79+
80+
public Cursor getData(int id) {
81+
SQLiteDatabase db = this.getReadableDatabase();
82+
Cursor res = db.rawQuery("select * from cars where id=" + id + "", null);
83+
return res;
84+
}
85+
86+
public int numberOfRows() {
87+
SQLiteDatabase db = this.getReadableDatabase();
88+
int numRows = (int) DatabaseUtils.queryNumEntries(db, CARS_TABLE_NAME);
89+
return numRows;
90+
}
91+
92+
public boolean updateCar(Integer id, String name, String color, float mileage) {
93+
SQLiteDatabase db = this.getWritableDatabase();
94+
ContentValues contentValues = new ContentValues();
95+
contentValues.put("name", name);
96+
contentValues.put("color", color);
97+
contentValues.put("mileage", mileage);
98+
db.update("cars", contentValues, "id = ? ", new String[]{Integer.toString(id)});
99+
return true;
100+
}
101+
102+
public Integer deleteCar(Integer id) {
103+
SQLiteDatabase db = this.getWritableDatabase();
104+
return db.delete("cars",
105+
"id = ? ",
106+
new String[]{Integer.toString(id)});
107+
}
108+
109+
public ArrayList<String> getAllCars() {
110+
ArrayList<String> arrayList = new ArrayList<>();
111+
112+
//hp = new HashMap();
113+
SQLiteDatabase db = this.getReadableDatabase();
114+
Cursor res = db.rawQuery("select * from cars", null);
115+
res.moveToFirst();
116+
117+
while (!res.isAfterLast()) {
118+
arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
119+
res.moveToNext();
120+
}
121+
return arrayList;
122+
}
123+
124+
public int count() {
125+
SQLiteDatabase db = getReadableDatabase();
126+
Cursor cursor = db.rawQuery("select * from cars", null);
127+
if (cursor != null && cursor.getCount() > 0) {
128+
cursor.moveToFirst();
129+
return cursor.getInt(0);
130+
} else {
131+
return 0;
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)