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=8508931461bfa38a1a67a87e9130c45ba95f5a78;hp=ad97e38c555106fb9c419a1d00dc6b8165b62337;hb=572449f6c66adfc1a3d88e761cb87581a7961df3;hpb=b17095e4a30d08098614c82d8ddab67896c8ceb0 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 ad97e38c..85089314 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java @@ -121,7 +121,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { } // Get a `Cursor` for the bookmark with the specified database ID. - public Cursor getBookmarkCursor(int databaseId) { + public Cursor getBookmark(int databaseId) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -157,7 +157,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return folderName; } - // The the database ID for the specified folder name. + // Get the database ID for the specified folder name. public int getFolderDatabaseId (String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -185,8 +185,8 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return databaseId; } - // Get a `Cursor` for the specified folder name. - public Cursor getFolderCursor(String folderName) { + // Get a cursor for the specified folder name. + public Cursor getFolder(String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -203,8 +203,8 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return bookmarksDatabase.rawQuery(GET_FOLDER, null); } - // Get a `Cursor` of all the folders except those specified. - public Cursor getFoldersCursorExcept(String exceptFolders) { + // Get a cursor of all the folders except those specified. + public Cursor getFoldersExcept(String exceptFolders) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -219,8 +219,8 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return bookmarksDatabase.rawQuery(GET_FOLDERS_EXCEPT, null); } - // Get a `Cursor` with all the subfolders of the specified folder. - public Cursor getSubfoldersCursor(String currentFolder) { + // Get a cursor with all the subfolders of the specified folder. + public Cursor getSubfolders(String currentFolder) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -237,34 +237,56 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return bookmarksDatabase.rawQuery(GET_SUBFOLDERS, null); } - // Get a `String` with the name of the parent folder. - public String getParentFolder(String currentFolder) { + // Get the name of the parent folder. + public String getParentFolderName(String currentFolder) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // SQL escape `currentFolder`. currentFolder = DatabaseUtils.sqlEscapeString(currentFolder); - // Prepare the SQL statement to get the parent folder. - String GET_PARENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + + // Prepare the SQL statement to get the current folder. + String GET_CURRENT_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + IS_FOLDER + " = " + 1 + " AND " + BOOKMARK_NAME + " = " + currentFolder; // Get the bookmark cursor and move to the first entry. - Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_PARENT_FOLDER, null); + Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_CURRENT_FOLDER, null); bookmarkCursor.moveToFirst(); // Store the name of the parent folder. String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER)); - // Close the `Cursor`. + // Close the cursor. bookmarkCursor.close(); return parentFolder; } - // Get a `Cursor` of all the folders. - public Cursor getAllFoldersCursor() { + // Get the name of the parent folder. + public String getParentFolderName(int databaseId) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Prepare the SQL statement to get the current bookmark. + String GET_BOOKMARK = "SELECT * FROM " + BOOKMARKS_TABLE + + " WHERE " + _ID + " = " + databaseId; + + // Get the bookmark cursor and move to the first entry. + Cursor bookmarkCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK, null); + bookmarkCursor.moveToFirst(); + + // Store the name of the parent folder. + String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER)); + + // Close the cursor. + bookmarkCursor.close(); + + return parentFolder; + } + + // Get a cursor of all the folders. + public Cursor getAllFolders() { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -279,7 +301,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { } // Get a cursor for all bookmarks and folders. - public Cursor getAllBookmarksCursor() { + public Cursor getAllBookmarks() { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -290,8 +312,74 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } - // Get a `Cursor` for all bookmarks and folders in the specified folder. - public Cursor getAllBookmarksCursor(String folderName) { + // Get a cursor for all bookmarks and folders ordered by display order. + public Cursor getAllBookmarksByDisplayOrder() { + // 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 except those with the specified IDs. + public Cursor getAllBookmarksExcept(long[] exceptIdLongArray) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Prepare a string builder to contain the comma-separated list of IDs not to get. + StringBuilder idsNotToGetStringBuilder = new StringBuilder(); + + // Extract the array of IDs not to get to the string builder. + for (long databaseIdLong : exceptIdLongArray) { + if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number. + idsNotToGetStringBuilder.append(databaseIdLong); + } else { // This is not the first number, so place a `,` before the new number. + idsNotToGetStringBuilder.append(","); + idsNotToGetStringBuilder.append(databaseIdLong); + } + } + + // Prepare the SQL statement to select all items except those with the specified IDs. + String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + + " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")"; + + // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null); + } + + // Get a cursor for all bookmarks and folders by display order except for a specific of IDs. + public Cursor getAllBookmarksByDisplayOrderExcept(long[] exceptIdLongArray) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Prepare a string builder to contain the comma-separated list of IDs not to get. + StringBuilder idsNotToGetStringBuilder = new StringBuilder(); + + // Extract the array of IDs not to get to the string builder. + for (long databaseIdLong : exceptIdLongArray) { + if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number. + idsNotToGetStringBuilder.append(databaseIdLong); + } else { // This is not the first number, so place a `,` before the new number. + idsNotToGetStringBuilder.append(","); + idsNotToGetStringBuilder.append(databaseIdLong); + } + } + + // Prepare the SQL statement to select all items except those with the specified IDs. + String GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + + " WHERE " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + + ") ORDER BY " + DISPLAY_ORDER + " ASC"; + + // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity. + return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS_EXCEPT_SPECIFIED, null); + } + + // Get a cursor for bookmarks and folders in the specified folder. + public Cursor getBookmarks(String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); @@ -299,59 +387,75 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { folderName = DatabaseUtils.sqlEscapeString(folderName); // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`. - String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName; // 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); + return bookmarksDatabase.rawQuery(GET_BOOKMARKS, null); } - // Get a cursor for all bookmarks and folders ordered by display order. - public Cursor getAllBookmarksCursorByDisplayOrder() { + // Get a cursor for bookmarks and folders in the specified folder ordered by display order. + public Cursor getBookmarksByDisplayOrder(String folderName) { // 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 + + // SQL escape `folderName`. + folderName = DatabaseUtils.sqlEscapeString(folderName); + + // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`. + String GET_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + " WHERE " + PARENT_FOLDER + " = " + folderName + " 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); + return bookmarksDatabase.rawQuery(GET_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 cursor for bookmarks and folders in the specified folder except for ta specific list of IDs. + public Cursor getBookmarksExcept(long[] exceptIdLongArray, String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // SQL escape `folderName`. + // Prepare a string builder to contain the comma-separated list of IDs not to get. + StringBuilder idsNotToGetStringBuilder = new StringBuilder(); + + // Extract the array of IDs not to get to the string builder. + for (long databaseIdLong : exceptIdLongArray) { + if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number. + idsNotToGetStringBuilder.append(databaseIdLong); + } else { // This is not the first number, so place a `,` before the new number. + idsNotToGetStringBuilder.append(","); + idsNotToGetStringBuilder.append(databaseIdLong); + } + } + + // SQL escape the folder name. folderName = DatabaseUtils.sqlEscapeString(folderName); - // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`. - String GET_ALL_BOOKMARKS = "SELECT * FROM " + BOOKMARKS_TABLE + + // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER` except those with the specified IDs. + String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName + - " ORDER BY " + DISPLAY_ORDER + " ASC"; + " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ")"; // 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); + return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null); } - // Get a `Cursor` for all bookmarks and folders in the specified folder except for a specific list of IDs. - public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray, String folderName) { + // Get a cursor for bookmarks and folders in the specified folder by display order except for a specific list of IDs. + public Cursor getBookmarksByDisplayOrderExcept(long[] exceptIdLongArray, String folderName) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Prepare a string builder that contains the comma-separated list of IDs not to get. - StringBuilder doNotGetIdsStringBuilder = new StringBuilder(); + // Prepare a string builder to contain the comma-separated list of IDs not to get. + StringBuilder idsNotToGetStringBuilder = new StringBuilder(); - // Extract the array to `doNotGetIdsString`. + // Extract the array of IDs not to get to the string builder. for (long databaseIdLong : exceptIdLongArray) { - // If this is the first number, only add the number. - if (doNotGetIdsStringBuilder.toString().isEmpty()) { - doNotGetIdsStringBuilder.append(databaseIdLong); - } else { // If there already is a number in the string, place a `,` before the new number. - doNotGetIdsStringBuilder.append(","); - doNotGetIdsStringBuilder.append(databaseIdLong); + if (idsNotToGetStringBuilder.length() == 0) { // This is the first number, so only add the number. + idsNotToGetStringBuilder.append(databaseIdLong); + } else { // This is not the first number, so place a `,` before the new number. + idsNotToGetStringBuilder.append(","); + idsNotToGetStringBuilder.append(databaseIdLong); } } @@ -359,14 +463,13 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { folderName = DatabaseUtils.sqlEscapeString(folderName); // Prepare the SQL statement to select all items except those with the specified IDs. - String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + + String GET_BOOKMARKS_EXCEPT_SPECIFIED = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + PARENT_FOLDER + " = " + folderName + - " AND " + _ID + " NOT IN (" + doNotGetIdsStringBuilder.toString() + + " AND " + _ID + " NOT IN (" + idsNotToGetStringBuilder.toString() + ") 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 bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null); + // Return the results as a cursor. The cursor cannot be closed because it will be used in the parent activity. + return bookmarksDatabase.rawQuery(GET_BOOKMARKS_EXCEPT_SPECIFIED, null); } // Check if a database ID is a folder. @@ -378,14 +481,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE + " WHERE " + _ID + " = " + databaseId; - // Populate folderCursor. The second argument is `null` because there are no `selectionArgs`. + // Populate the folder cursor. Cursor folderCursor = bookmarksDatabase.rawQuery(CHECK_IF_FOLDER, null); // Ascertain if this database ID is a folder. folderCursor.moveToFirst(); boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1); - // Close the `Cursor` and the database handle. + // Close the cursor and the database handle. folderCursor.close(); bookmarksDatabase.close(); @@ -394,7 +497,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper { // Update the bookmark name and URL. public void updateBookmark(int databaseId, String bookmarkName, String bookmarkUrl) { - // Initialize a `ContentValues`. + // Initialize a ContentValues. ContentValues bookmarkContentValues = new ContentValues(); // Store the updated values.