X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FBookmarksDatabaseHelper.java;h=ad97e38c555106fb9c419a1d00dc6b8165b62337;hp=e152b14881afb8ff82c182da69ee439d6c2f5691;hb=9db0afa3b1f85a74c6ddf7a7cedd7cb7ddd1035a;hpb=9f551f25b53a30cca7b19b6e6bfc2d2520d9aa1b diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java index e152b148..ad97e38c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -28,8 +28,8 @@ import android.database.sqlite.SQLiteOpenHelper; public class BookmarksDatabaseHelper extends SQLiteOpenHelper { private static final int SCHEMA_VERSION = 1; - private static final String BOOKMARKS_DATABASE = "bookmarks.db"; - private static final String BOOKMARKS_TABLE = "bookmarks"; + static final String BOOKMARKS_DATABASE = "bookmarks.db"; + static final String BOOKMARKS_TABLE = "bookmarks"; public static final String _ID = "_id"; public static final String BOOKMARK_NAME = "bookmarkname"; @@ -39,6 +39,15 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { public static final String IS_FOLDER = "isfolder"; public static final String FAVORITE_ICON = "favoriteicon"; + static final String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" + + _ID + " INTEGER PRIMARY KEY, " + + BOOKMARK_NAME + " TEXT, " + + BOOKMARK_URL + " TEXT, " + + PARENT_FOLDER + " TEXT, " + + DISPLAY_ORDER + " INTEGER, " + + IS_FOLDER + " BOOLEAN, " + + FAVORITE_ICON + " BLOB)"; + // Initialize the database. The lint warnings for the unused parameters are suppressed. public BookmarksDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) { super(context, BOOKMARKS_DATABASE, cursorFactory, SCHEMA_VERSION); @@ -46,17 +55,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase bookmarksDatabase) { - // Setup the SQL string to create the `bookmarks` table. - final String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" + - _ID + " integer primary key, " + - BOOKMARK_NAME + " text, " + - BOOKMARK_URL + " text, " + - PARENT_FOLDER + " text, " + - DISPLAY_ORDER + " integer, " + - IS_FOLDER + " boolean, " + - FAVORITE_ICON + " blob);"; - - // Create the `bookmarks` table. + // Create the bookmarks table. bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE); } @@ -67,7 +66,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // Create a bookmark. public void createBookmark(String bookmarkName, String bookmarkURL, String parentFolder, int displayOrder, byte[] favoriteIcon) { - // We need to store the bookmark data in a `ContentValues`. + // Store the bookmark data in a `ContentValues`. ContentValues bookmarkContentValues = new ContentValues(); // ID is created automatically. @@ -88,6 +87,18 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { bookmarksDatabase.close(); } + // Create a bookmark from content values. + void createBookmark(ContentValues contentValues) { + // Get a writable database. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created. + bookmarksDatabase.insert(BOOKMARKS_TABLE, null, contentValues); + + // Close the database handle. + bookmarksDatabase.close(); + } + // Create a folder. public void createFolder(String folderName, String parentFolder, byte[] favoriteIcon) { ContentValues bookmarkContentValues = new ContentValues(); @@ -114,8 +125,8 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare the SQL statement to get the `Cursor` for `databaseId` - final String GET_ONE_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE + + // Prepare the SQL statement to get the cursor for the database ID. + String GET_ONE_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + _ID + " = " + databaseId; // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. We can't close the `Cursor` because we need to use it in the parent activity. @@ -127,14 +138,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare the SQL statement to get the `Cursor` for the folder. - final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + // Prepare the SQL statement to get the cursor for the folder. + String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + _ID + " = " + databaseId; - // Get `folderCursor`. The second argument is `null` because there are no `selectionArgs`. + // Get a folder cursor. Cursor folderCursor = bookmarksDatabase.rawQuery(GET_FOLDER, null); - // Get `folderName`. + // Get the folder name. folderCursor.moveToFirst(); String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME)); @@ -155,7 +166,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { folderName = DatabaseUtils.sqlEscapeString(folderName); // Prepare the SQL statement to get the `Cursor` for the folder. - final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + BOOKMARK_NAME + " = " + folderName + " AND " + IS_FOLDER + " = " + 1; @@ -183,7 +194,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { folderName = DatabaseUtils.sqlEscapeString(folderName); // Prepare the SQL statement to get the `Cursor` for the folder. - final String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + BOOKMARK_NAME + " = " + folderName + " AND " + IS_FOLDER + " = " + 1; @@ -198,7 +209,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // Prepare the SQL statement to get the `Cursor` for the folders. - final String GET_FOLDERS_EXCEPT = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_FOLDERS_EXCEPT = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = " + 1 + " AND " + BOOKMARK_NAME + " NOT IN (" + exceptFolders + ") ORDER BY " + BOOKMARK_NAME + " ASC"; @@ -217,7 +228,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { currentFolder = DatabaseUtils.sqlEscapeString(currentFolder); // Prepare the SQL statement to get the `Cursor` for the subfolders. - final String GET_SUBFOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_SUBFOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + currentFolder + " AND " + IS_FOLDER + " = " + 1; @@ -235,11 +246,11 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { currentFolder = DatabaseUtils.sqlEscapeString(currentFolder); // Prepare the SQL statement to get the parent folder. - final String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = " + 1 + " AND " + BOOKMARK_NAME + " = " + currentFolder; - // The second argument is `null` because there are no `selectionArgs`. + // Get the bookmark cursor and move to the first entry. Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null); bookmarkCursor.moveToFirst(); @@ -258,7 +269,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // Prepare the SQL statement to get the `Cursor` for all the folders. - final String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_ALL_FOLDERS = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = " + 1 + " ORDER BY " + BOOKMARK_NAME + " ASC"; @@ -267,16 +278,15 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null); } - // Get a `Cursor` for all bookmarks and folders. + // Get a cursor for all bookmarks and folders. public Cursor getAllBookmarksCursor() { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Get everything in `BOOKMARKS_TABLE`. - final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE; + // Get everything in the bookmarks table. + String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE; - // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. - // We can't close the Cursor because we need to use it in the parent activity. + // Return the result as a Cursor. The Cursor cannot be closed because it is used in the parent activity. return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } @@ -285,18 +295,31 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // SQL escape `folderName`. + // SQL escape the folder name. folderName = DatabaseUtils.sqlEscapeString(folderName); - // Get everything in the `BOOKMARKS_TABLE` with `folderName` as the `PARENT_FOLDER`. - final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`. + String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName; - // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. We can't close the `Cursor` because we need to use it in the parent activity. + // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity. return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } - // Get a `Cursor` for all bookmarks and folders in the specified folder ordered by display order. + // Get a cursor for all bookmarks and folders ordered by display order. + public Cursor getAllBookmarksCursorByDisplayOrder() { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Get everything in the bookmarks table ordered by display order. + String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + " ORDER BY " + DISPLAY_ORDER + " ASC"; + + // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); + } + + // Get a cursor for all bookmarks and folders in the specified folder ordered by display order. public Cursor getAllBookmarksCursorByDisplayOrder(String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -304,12 +327,12 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // SQL escape `folderName`. folderName = DatabaseUtils.sqlEscapeString(folderName); - // Get everything in the `BOOKMARKS_TABLE` with `folderName` as the `PARENT_FOLDER`. - final String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`. + String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName + " ORDER BY " + DISPLAY_ORDER + " ASC"; - // Return the results as a `Cursor`. The second argument is `null` because there are no `selectionArgs`. We can't close the `Cursor` because we need to use it in the parent activity. + // Return the result as a cursor. The cursor cannot be closed because it is used in the parent activity. return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } @@ -336,7 +359,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { folderName = DatabaseUtils.sqlEscapeString(folderName); // Prepare the SQL statement to select all items except those with the specified IDs. - final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName + " AND " + _ID + " NOT IN (" + doNotGetIdsStringBuilder.toString() + ") ORDER BY " + DISPLAY_ORDER + " ASC"; @@ -352,7 +375,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // Prepare the SQL statement to determine if `databaseId` is a folder. - final String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + _ID + " = " + databaseId; // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`. @@ -602,7 +625,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { String newFolderSqlEscaped = DatabaseUtils.sqlEscapeString(newFolder); // Prepare a SQL query to select all the bookmarks in the new folder. - final String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + String NEW_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + newFolderSqlEscaped + " ORDER BY " + DISPLAY_ORDER + " ASC";