-
Notifications
You must be signed in to change notification settings - Fork 0
Feature implementation from commits f091d61..4014bd2 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature-base-branch-1
Are you sure you want to change the base?
Changes from 6 commits
f76b95a
92de6c4
c08ba25
3d3a232
6b4db14
d14adf9
bae78e9
c227bcc
cfdf936
1cb6137
ee9f048
c138c11
30cbc73
47d2d48
4014bd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,147 @@ | ||
| /* | ||
| * | ||
| * * Copyright (C) 2019 Amit Shekhar | ||
| * * Copyright (C) 2011 Android Open Source Project | ||
| * * | ||
| * * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * * you may not use this file except in compliance with the License. | ||
| * * You may obtain a copy of the License at | ||
| * * | ||
| * * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * * | ||
| * * Unless required by applicable law or agreed to in writing, software | ||
| * * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * * See the License for the specific language governing permissions and | ||
| * * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package com.sample.encrypt; | ||
|
|
||
| import android.support.v7.app.AppCompatActivity; | ||
| import android.annotation.SuppressLint; | ||
| import android.content.Context; | ||
| import android.content.SharedPreferences; | ||
| import android.os.Bundle; | ||
| import android.preference.PreferenceManager; | ||
| import android.support.v7.app.AppCompatActivity; | ||
| import android.view.View; | ||
|
|
||
| import com.sample.encrypt.database.CarDBHelper; | ||
| import com.sample.encrypt.database.ContactDBHelper; | ||
| import com.sample.encrypt.database.ExtTestDBHelper; | ||
| import com.sample.encrypt.database.PersonDBHelper; | ||
| import com.sample.encrypt.database.room.User; | ||
| import com.sample.encrypt.database.room.UserDBHelper; | ||
| import com.sample.encrypt.utils.Utils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class MainActivity extends AppCompatActivity { | ||
|
|
||
| @SuppressLint("CommitPrefEdits") | ||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
|
|
||
| super.onCreate(savedInstanceState); | ||
|
|
||
| setContentView(R.layout.activity_main); | ||
|
|
||
| Set<String> stringSet = new HashSet<>(); | ||
| stringSet.add("SetOne"); | ||
| stringSet.add("SetTwo"); | ||
| stringSet.add("SetThree"); | ||
|
|
||
| SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | ||
|
|
||
| SharedPreferences prefsOne = getSharedPreferences("countPrefOne", Context.MODE_PRIVATE); | ||
| SharedPreferences prefsTwo = getSharedPreferences("countPrefTwo", Context.MODE_PRIVATE); | ||
|
|
||
| sharedPreferences.edit().putString("testOne", "one").commit(); | ||
| sharedPreferences.edit().putInt("testTwo", 2).commit(); | ||
| sharedPreferences.edit().putLong("testThree", 100000L).commit(); | ||
| sharedPreferences.edit().putFloat("testFour", 3.01F).commit(); | ||
| sharedPreferences.edit().putBoolean("testFive", true).commit(); | ||
| sharedPreferences.edit().putStringSet("testSix", stringSet).commit(); | ||
|
|
||
| prefsOne.edit().putString("testOneNew", "one").commit(); | ||
|
|
||
| prefsTwo.edit().putString("testTwoNew", "two").commit(); | ||
|
|
||
| ContactDBHelper contactDBHelper = new ContactDBHelper(getApplicationContext()); | ||
| if (contactDBHelper.count() == 0) { | ||
| for (int i = 0; i < 100; i++) { | ||
| String name = "name_" + i; | ||
| String phone = "phone_" + i; | ||
| String email = "email_" + i; | ||
| String street = "street_" + i; | ||
| String place = "place_" + i; | ||
| contactDBHelper.insertContact(name, phone, email, street, place); | ||
| } | ||
| } | ||
|
|
||
| CarDBHelper carDBHelper = new CarDBHelper(getApplicationContext()); | ||
| if (carDBHelper.count() == 0) { | ||
| for (int i = 0; i < 50; i++) { | ||
| String name = "name_" + i; | ||
| String color = "RED"; | ||
| float mileage = i + 10.45f; | ||
| carDBHelper.insertCar(name, color, mileage); | ||
| } | ||
| } | ||
|
|
||
| ExtTestDBHelper extTestDBHelper = new ExtTestDBHelper(getApplicationContext()); | ||
| if (extTestDBHelper.count() == 0) { | ||
| for (int i = 0; i < 20; i++) { | ||
| String value = "value_" + i; | ||
| extTestDBHelper.insertTest(value); | ||
| } | ||
| } | ||
|
|
||
| // Create Person encrypted database | ||
| PersonDBHelper personDBHelper = new PersonDBHelper(getApplicationContext()); | ||
| if (personDBHelper.count() == 0) { | ||
| for (int i = 0; i < 100; i++) { | ||
| String firstName = PersonDBHelper.PERSON_COLUMN_FIRST_NAME + "_" + i; | ||
| String lastName = PersonDBHelper.PERSON_COLUMN_LAST_NAME + "_" + i; | ||
| String address = PersonDBHelper.PERSON_COLUMN_ADDRESS + "_" + i; | ||
| personDBHelper.insertPerson(firstName, lastName, address); | ||
| } | ||
| } | ||
|
|
||
| // Room database | ||
| UserDBHelper userDBHelper = new UserDBHelper(getApplicationContext()); | ||
| if (userDBHelper.count() == 0) { | ||
| List<User> userList = new ArrayList<>(); | ||
| for (int i = 0; i < 20; i++) { | ||
| User user = new User(); | ||
| user.id = (long) (i + 1); | ||
| user.name = "user_" + i; | ||
| userList.add(user); | ||
| } | ||
| 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) { | ||
| Utils.showDebugDBAddressLogToast(getApplicationContext()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,134 @@ | ||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| * * Copyright (C) 2019 Amit Shekhar | ||||||||||||||||||||||||||||||||||||||
| * * Copyright (C) 2011 Android Open Source Project | ||||||||||||||||||||||||||||||||||||||
| * * | ||||||||||||||||||||||||||||||||||||||
| * * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||
| * * you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||
| * * You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||
| * * | ||||||||||||||||||||||||||||||||||||||
| * * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||
| * * | ||||||||||||||||||||||||||||||||||||||
| * * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||
| * * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||
| * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||
| * * See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||
| * * limitations under the License. | ||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| package com.sample.encrypt.database; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import android.content.ContentValues; | ||||||||||||||||||||||||||||||||||||||
| import android.content.Context; | ||||||||||||||||||||||||||||||||||||||
| import android.database.Cursor; | ||||||||||||||||||||||||||||||||||||||
| import android.database.DatabaseUtils; | ||||||||||||||||||||||||||||||||||||||
| import android.database.sqlite.SQLiteDatabase; | ||||||||||||||||||||||||||||||||||||||
| import android.database.sqlite.SQLiteOpenHelper; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Created by amitshekhar on 06/02/17. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public class CarDBHelper extends SQLiteOpenHelper { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public static final String DATABASE_NAME = "Car.db"; | ||||||||||||||||||||||||||||||||||||||
| public static final String CARS_TABLE_NAME = "cars"; | ||||||||||||||||||||||||||||||||||||||
| public static final String CARS_COLUMN_ID = "id"; | ||||||||||||||||||||||||||||||||||||||
| public static final String CARS_COLUMN_NAME = "name"; | ||||||||||||||||||||||||||||||||||||||
| public static final String CARS_COLUMN_COLOR = "color"; | ||||||||||||||||||||||||||||||||||||||
| public static final String CCARS_COLUMN_MILEAGE = "mileage"; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public CarDBHelper(Context context) { | ||||||||||||||||||||||||||||||||||||||
| super(context, DATABASE_NAME, null, 1); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||
| public void onCreate(SQLiteDatabase db) { | ||||||||||||||||||||||||||||||||||||||
| // TODO Auto-generated method stub | ||||||||||||||||||||||||||||||||||||||
| db.execSQL( | ||||||||||||||||||||||||||||||||||||||
| "create table cars " + | ||||||||||||||||||||||||||||||||||||||
| "(id integer primary key, name text, color text, mileage real)" | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| db.execSQL("create table [transaction] (id integer primary key, name text)"); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < 10; i++) { | ||||||||||||||||||||||||||||||||||||||
| db.execSQL("insert into [transaction] (name) values ('hello');"); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||
| public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||||||||||||||||||||||||||||||||||||||
| // TODO Auto-generated method stub | ||||||||||||||||||||||||||||||||||||||
| db.execSQL("DROP TABLE IF EXISTS cars"); | ||||||||||||||||||||||||||||||||||||||
| onCreate(db); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public boolean insertCar(String name, String color, float mileage) { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getWritableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| ContentValues contentValues = new ContentValues(); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("name", name); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("color", color); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("mileage", mileage); | ||||||||||||||||||||||||||||||||||||||
| db.insert("cars", null, contentValues); | ||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public Cursor getData(int id) { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getReadableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| Cursor res = db.rawQuery("select * from cars where id=" + id + "", null); | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security Issue SQL Injection Vulnerability. Direct concatenation of user input into SQL query creates a critical security vulnerability allowing attackers to execute arbitrary SQL commands. Current Code (Diff): - Cursor res = db.rawQuery("select * from cars where id=" + id + "", null);
+ Cursor res = db.rawQuery("select * from cars where id=?", new String[]{String.valueOf(id)});📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| return res; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public int numberOfRows() { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getReadableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| int numRows = (int) DatabaseUtils.queryNumEntries(db, CARS_TABLE_NAME); | ||||||||||||||||||||||||||||||||||||||
| return numRows; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public boolean updateCar(Integer id, String name, String color, float mileage) { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getWritableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| ContentValues contentValues = new ContentValues(); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("name", name); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("color", color); | ||||||||||||||||||||||||||||||||||||||
| contentValues.put("mileage", mileage); | ||||||||||||||||||||||||||||||||||||||
| db.update("cars", contentValues, "id = ? ", new String[]{Integer.toString(id)}); | ||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public Integer deleteCar(Integer id) { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getWritableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| return db.delete("cars", | ||||||||||||||||||||||||||||||||||||||
| "id = ? ", | ||||||||||||||||||||||||||||||||||||||
| new String[]{Integer.toString(id)}); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public ArrayList<String> getAllCars() { | ||||||||||||||||||||||||||||||||||||||
| ArrayList<String> arrayList = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| //hp = new HashMap(); | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = this.getReadableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| Cursor res = db.rawQuery("select * from cars", null); | ||||||||||||||||||||||||||||||||||||||
| res.moveToFirst(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| while (!res.isAfterLast()) { | ||||||||||||||||||||||||||||||||||||||
| arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME))); | ||||||||||||||||||||||||||||||||||||||
| res.moveToNext(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| return arrayList; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+114
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Correctness Issue Resource Leak - Unclosed Cursor. The Cursor object in getAllCars() is never closed, which will cause memory leaks and potentially crash the application. Current Code (Diff): - Cursor res = db.rawQuery("select * from cars", null);
- res.moveToFirst();
-
- while (!res.isAfterLast()) {
- arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
- res.moveToNext();
- }
- return arrayList;
+ Cursor res = db.rawQuery("select * from cars", null);
+ res.moveToFirst();
+
+ while (!res.isAfterLast()) {
+ arrayList.add(res.getString(res.getColumnIndex(CARS_COLUMN_NAME)));
+ res.moveToNext();
+ }
+ res.close();
+ return arrayList;📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public int count() { | ||||||||||||||||||||||||||||||||||||||
| SQLiteDatabase db = getReadableDatabase(); | ||||||||||||||||||||||||||||||||||||||
| Cursor cursor = db.rawQuery("select * from cars", null); | ||||||||||||||||||||||||||||||||||||||
| if (cursor != null && cursor.getCount() > 0) { | ||||||||||||||||||||||||||||||||||||||
| cursor.moveToFirst(); | ||||||||||||||||||||||||||||||||||||||
| return cursor.getInt(0); | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Correctness Issue Logic Error in count() Method. The count() method incorrectly returns the first column value (id) instead of the actual count of rows, causing incorrect data to be returned. Current Code (Diff): - return cursor.getInt(0);
+ return cursor.getCount();📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+126
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐛 Correctness Issue Resource Leak - Unclosed Cursor. The Cursor object in count() is never closed, which will cause memory leaks and potentially crash the application. Current Code (Diff): - Cursor cursor = db.rawQuery("select * from cars", null);
- if (cursor != null && cursor.getCount() > 0) {
- cursor.moveToFirst();
- return cursor.getInt(0);
- } else {
- return 0;
- }
+ Cursor cursor = db.rawQuery("select * from cars", null);
+ try {
+ if (cursor != null && cursor.getCount() > 0) {
+ cursor.moveToFirst();
+ return cursor.getInt(0);
+ } else {
+ return 0;
+ }
+ } finally {
+ if (cursor != null) cursor.close();
+ }📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Correctness Issue
Using SQL Reserved Keyword as Table Name.
Using 'transaction' as a table name is problematic as it's a SQL reserved keyword, which could cause unexpected behavior or errors.
Current Code (Diff):
📝 Committable suggestion
🔄 Dependencies Affected
src/main/java/com/sample/encrypt/database/CarDBHelper.java
Function:
CarDBHelper.onCreateIssue: The insert statements also need to be updated to match the new table name
Suggestion: Update insert statements to use the new table name
Current Code (Diff):