|
| 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 | + |
1 | 20 | package com.sample.encrypt; |
2 | 21 |
|
3 | | -import android.support.v7.app.AppCompatActivity; |
| 22 | +import android.annotation.SuppressLint; |
| 23 | +import android.content.Context; |
| 24 | +import android.content.SharedPreferences; |
4 | 25 | 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; |
5 | 42 |
|
6 | 43 | public class MainActivity extends AppCompatActivity { |
7 | 44 |
|
| 45 | + @SuppressLint("CommitPrefEdits") |
8 | 46 | @Override |
9 | 47 | protected void onCreate(Bundle savedInstanceState) { |
| 48 | + |
10 | 49 | super.onCreate(savedInstanceState); |
| 50 | + |
11 | 51 | 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()); |
12 | 146 | } |
13 | 147 | } |
0 commit comments