X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;ds=inline;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FBookmarksDatabaseHelper.kt;h=fac7001ad6fa2fb1952c8d3ad47c5008912f393d;hb=HEAD;hp=6241b49f77d9202d844ca6bab586e7043903dce4;hpb=b664d689b9757ffd1fd224c85a9659d2f4df1204;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt index 6241b49f..929ce15c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt @@ -1,7 +1,7 @@ /* - * Copyright 2016-2023 Soren Stoutner . + * Copyright 2016-2024 Soren Stoutner . * - * This file is part of Privacy Browser Android . + * This file is part of Privacy Browser Android . * * Privacy Browser Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -172,6 +172,16 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE", null) } + // Get a cursor with just the database IDs of all the bookmarks and folders. This is useful for counting the number of bookmarks imported. + val allBookmarkAndFolderIds: Cursor + get() { + // Get a readable database handle. + val bookmarksDatabase = this.readableDatabase + + // Return a cursor with all the database IDs. The cursor cannot be closed because it is used in the parent activity. + return bookmarksDatabase.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE", null) + } + // Get a cursor for all bookmarks and folders ordered by display order. val allBookmarksByDisplayOrder: Cursor get() { @@ -218,16 +228,19 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK } // Create a folder. - fun createFolder(folderName: String, parentFolderId: Long, favoriteIcon: ByteArray) { + fun createFolder(folderName: String, parentFolderId: Long, displayOrder: Int, favoriteIcon: ByteArray): Long { // Create a bookmark folder content values. val bookmarkFolderContentValues = ContentValues() + // Generate the folder ID. + val folderId = generateFolderId() + // The ID is created automatically. Folders are always created at the top of the list. bookmarkFolderContentValues.put(BOOKMARK_NAME, folderName) bookmarkFolderContentValues.put(PARENT_FOLDER_ID, parentFolderId) - bookmarkFolderContentValues.put(DISPLAY_ORDER, 0) + bookmarkFolderContentValues.put(DISPLAY_ORDER, displayOrder) bookmarkFolderContentValues.put(IS_FOLDER, true) - bookmarkFolderContentValues.put(FOLDER_ID, generateFolderId()) + bookmarkFolderContentValues.put(FOLDER_ID, folderId) bookmarkFolderContentValues.put(FAVORITE_ICON, favoriteIcon) // Get a writable database handle. @@ -238,6 +251,9 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK // Close the database handle. bookmarksDatabase.close() + + // Return the new folder ID. + return folderId } // Delete one bookmark. @@ -309,8 +325,8 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID NOT IN ($idsNotToGetStringBuilder)", null) } - // Get a cursor with just database ID of bookmarks and folders in the specified folder. This is useful for deleting folders with bookmarks that have favorite icons too large to fit in a cursor. - fun getBookmarkIds(parentFolderId: Long): Cursor { + // Get a cursor with just the database IDs of bookmarks and folders in the specified folder. This is useful for deleting folders with bookmarks that have favorite icons too large to fit in a cursor. + fun getBookmarkAndFolderIds(parentFolderId: Long): Cursor { // Get a readable database handle. val bookmarksDatabase = this.readableDatabase @@ -356,8 +372,7 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK idsNotToGetStringBuilder.append(databaseIdLong) } - // Return a cursor with all the bookmarks in the specified folder except for those database IDs specified ordered by display order. - // The cursor cannot be closed because it will be used in the parent activity. + // Return a cursor with all the bookmarks in the specified folder except for those database IDs specified ordered by display order. The cursor cannot be closed because it will be used in the parent activity. return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $ID NOT IN ($idsNotToGetStringBuilder) ORDER BY $DISPLAY_ORDER ASC", null) } @@ -385,6 +400,51 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $ID NOT IN ($idsNotToGetStringBuilder)", null) } + fun getBookmarksSortedAlphabetically(parentFolderId: Long): Cursor { + // Get a readable database handle. + val bookmarksDatabase = this.readableDatabase + + // Get the folders sorted alphabetically. + val foldersCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $IS_FOLDER = 1 ORDER BY $BOOKMARK_NAME ASC", null) + + // Get the bookmarks sorted alphabetically. + val bookmarksCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $IS_FOLDER = 0 ORDER BY $BOOKMARK_NAME ASC", null) + + // Return the merged cursors. + return MergeCursor(arrayOf(foldersCursor, bookmarksCursor)) + } + + fun getBookmarksSortedAlphabeticallyExcept(exceptIdLongArray: LongArray, parentFolderId: Long): Cursor { + // Get a readable database handle. + val bookmarksDatabase = this.readableDatabase + + // Prepare a string builder to contain the comma-separated list of IDs not to get. + val idsNotToGetStringBuilder = StringBuilder() + + // Extract the array of IDs not to get to the string builder. + for (databaseIdLong in exceptIdLongArray) { + // Check to see if there is already a number in the builder. + if (idsNotToGetStringBuilder.isNotEmpty()) { + // This is not the first number, so place a `,` before the new number. + idsNotToGetStringBuilder.append(",") + } + + // Add the new number to the builder. + idsNotToGetStringBuilder.append(databaseIdLong) + } + + // Get the folders sorted alphabetically except for those database IDs specified, ordered by name. + val foldersCursor = bookmarksDatabase.rawQuery( + "SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $IS_FOLDER = 1 AND $ID NOT IN ($idsNotToGetStringBuilder) ORDER BY $BOOKMARK_NAME ASC", null) + + // Get the bookmarks sorted alphabetically except for those database IDs specified, ordered by name. + val bookmarksCursor = bookmarksDatabase.rawQuery( + "SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $parentFolderId AND $IS_FOLDER = 0 AND $ID NOT IN ($idsNotToGetStringBuilder) ORDER BY $BOOKMARK_NAME ASC", null) + + // Return the merged cursors. + return MergeCursor(arrayOf(foldersCursor, bookmarksCursor)) + } + fun getFolderBookmarks(parentFolderId: Long): Cursor { // Get a readable database handle. val bookmarksDatabase = this.readableDatabase @@ -706,6 +766,37 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK bookmarksDatabase.close() } + fun recalculateFolderContentsDisplayOrder(folderId: Long) { + // Get a readable database. + val bookmarksDatabase = this.readableDatabase + + // Get a cursor with the current content of the folder. + val folderContentsCursor = bookmarksDatabase.rawQuery("SELECT $ID, $DISPLAY_ORDER FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER_ID = $folderId ORDER BY $DISPLAY_ORDER ASC", null) + + // Get the count of the folder contents. + val folderContentsCount = folderContentsCursor.count + + // Get the query columns. + val databaseIdColumnInt = folderContentsCursor.getColumnIndexOrThrow(ID) + val displayOrderColumnInt = folderContentsCursor.getColumnIndexOrThrow(DISPLAY_ORDER) + + // Move to the first entry. + folderContentsCursor.moveToFirst() + + // Update the display order if it isn't currently correct. + for (i in 0 until folderContentsCount) { + // Use the current value for `i` as the display order if it isn't currently so. + if (i != folderContentsCursor.getInt(displayOrderColumnInt)) + updateDisplayOrder(folderContentsCursor.getInt(databaseIdColumnInt), i) + + // Move to the next entry. + folderContentsCursor.moveToNext() + } + + // Close the cursor. + folderContentsCursor.close() + } + // Update the bookmark name and URL. fun updateBookmark(databaseId: Int, bookmarkName: String, bookmarkUrl: String) { // Initialize a content values.