]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.java
Fix scrolling of the bottom app bar. https://redmine.stoutner.com/issues/791
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BookmarksDatabaseHelper.java
index 8508931461bfa38a1a67a87e9130c45ba95f5a78..0bc200c4468d10fa7d8f34e5bf25698669f31137 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2021 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -147,7 +147,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // Get the folder name.
         folderCursor.moveToFirst();
-        String folderName = folderCursor.getString(folderCursor.getColumnIndex(BOOKMARK_NAME));
+        String folderName = folderCursor.getString(folderCursor.getColumnIndexOrThrow(BOOKMARK_NAME));
 
         // Close the cursor and the database handle.
         folderCursor.close();
@@ -175,7 +175,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // Get the database ID.
         folderCursor.moveToFirst();
-        int databaseId = folderCursor.getInt(folderCursor.getColumnIndex(_ID));
+        int databaseId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(_ID));
 
         // Close the cursor and the database handle.
         folderCursor.close();
@@ -255,7 +255,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         bookmarkCursor.moveToFirst();
 
         // Store the name of the parent folder.
-        String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
+        String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER));
 
         // Close the cursor.
         bookmarkCursor.close();
@@ -277,7 +277,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         bookmarkCursor.moveToFirst();
 
         // Store the name of the parent folder.
-        String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(PARENT_FOLDER));
+        String parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER));
 
         // Close the cursor.
         bookmarkCursor.close();
@@ -295,8 +295,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
                 " WHERE " + IS_FOLDER + " = " + 1 +
                 " ORDER BY " + BOOKMARK_NAME + " 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 results as a cursor.  The cursor cannot be closed because it is used in the parent activity.
         return bookmarksDatabase.rawQuery(GET_ALL_FOLDERS, null);
     }
 
@@ -335,12 +334,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // 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.
+            // Check to see if there is already a number in the builder.
+            if (idsNotToGetStringBuilder.length() > 0) {
+                // This is not the first number, so place a `,` before the new number.
                 idsNotToGetStringBuilder.append(",");
-                idsNotToGetStringBuilder.append(databaseIdLong);
             }
+
+            // Add the new number to the builder.
+            idsNotToGetStringBuilder.append(databaseIdLong);
         }
 
         // Prepare the SQL statement to select all items except those with the specified IDs.
@@ -361,12 +362,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // 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.
+            // Check to see if there is already a number in the builder.
+            if (idsNotToGetStringBuilder.length() > 0) {
+                // This is not the first number, so place a `,` before the new number.
                 idsNotToGetStringBuilder.append(",");
-                idsNotToGetStringBuilder.append(databaseIdLong);
             }
+
+            // Add the new number to the builder.
+            idsNotToGetStringBuilder.append(databaseIdLong);
         }
 
         // Prepare the SQL statement to select all items except those with the specified IDs.
@@ -411,6 +414,22 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         return bookmarksDatabase.rawQuery(GET_BOOKMARKS, 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.
+    public Cursor getBookmarkIds(String folderName) {
+        // Get a readable database handle.
+        SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
+
+        // SQL escape the folder name.
+        folderName = DatabaseUtils.sqlEscapeString(folderName);
+
+        // Get everything in the bookmarks table with `folderName` as the `PARENT_FOLDER`.
+        String GET_BOOKMARKS = "SELECT " + _ID + " 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_BOOKMARKS, null);
+    }
+
     // 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.
@@ -421,12 +440,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // 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.
+            // Check to see if there is already a number in the builder.
+            if (idsNotToGetStringBuilder.length() > 0) {
+                // This is not the first number, so place a `,` before the new number.
                 idsNotToGetStringBuilder.append(",");
-                idsNotToGetStringBuilder.append(databaseIdLong);
             }
+
+            // Add the new number to the builder.
+            idsNotToGetStringBuilder.append(databaseIdLong);
         }
 
         // SQL escape the folder name.
@@ -451,12 +472,14 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // 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.
+            // Check to see if there is already a number in the builder.
+            if (idsNotToGetStringBuilder.length() > 0) {
+                // This is not the first number, so place a `,` before the new number.
                 idsNotToGetStringBuilder.append(",");
-                idsNotToGetStringBuilder.append(databaseIdLong);
             }
+
+            // Add the new number to the builder.
+            idsNotToGetStringBuilder.append(databaseIdLong);
         }
 
         // SQL escape `folderName`.
@@ -478,7 +501,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
         SQLiteDatabase bookmarksDatabase = this.getReadableDatabase();
 
         // Prepare the SQL statement to determine if `databaseId` is a folder.
-        String CHECK_IF_FOLDER = "SELECT * FROM " + BOOKMARKS_TABLE +
+        String CHECK_IF_FOLDER = "SELECT " + IS_FOLDER + " FROM " + BOOKMARKS_TABLE +
                 " WHERE " + _ID + " = " + databaseId;
 
         // Populate the folder cursor.
@@ -486,7 +509,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
 
         // Ascertain if this database ID is a folder.
         folderCursor.moveToFirst();
-        boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndex(IS_FOLDER)) == 1);
+        boolean isFolder = (folderCursor.getInt(folderCursor.getColumnIndexOrThrow(IS_FOLDER)) == 1);
 
         // Close the cursor and the database handle.
         folderCursor.close();
@@ -744,7 +767,7 @@ public class BookmarksDatabaseHelper extends SQLiteOpenHelper {
             newFolderCursor.moveToLast();
 
             // Set the display order to be one greater that the last bookmark.
-            displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndex(DISPLAY_ORDER)) + 1;
+            displayOrder = newFolderCursor.getInt(newFolderCursor.getColumnIndexOrThrow(DISPLAY_ORDER)) + 1;
         } else {  // There are no bookmarks in the new folder.
             // Set the display order to be `0`.
             displayOrder = 0;