]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java
Use a spinner to select folders in the bookmarks database view. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksActivity.java
index 6ea013c9d6675c90437a9a0ad07b0385f807382a..e640b792291e208736931a21637a5a4ea51639cc 100644 (file)
@@ -64,16 +64,15 @@ import java.io.ByteArrayOutputStream;
 public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener,
         EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener {
 
-    // `bookmarksDatabaseHelper` is public static so it can be accessed from `CreateBookmarkFolderDialog`, `EditBookmarkDialog`, `EditBookmarkFolderDialog` and `MoveToFolderDialog`.  It is also used in `onCreate()`, `onOptionsItemSelected()`,
-    // `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder().
+    // `bookmarksDatabaseHelper` is public static so it can be accessed from `MoveToFolderDialog`.  It is also used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`,
+    // `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder().
     public static BookmarksDatabaseHelper bookmarksDatabaseHelper;
 
     // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`.
     // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `loadFolder()`.
     public static String currentFolder;
 
-    // `checkedItemIds` is public static so it can be accessed from `EditBookmarkDialog`, `EditBookmarkFolderDialog`, and `MoveToFolderDialog`.
-    // It is also used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`.
+    // `checkedItemIds` is public static so it can be accessed from `MoveToFolderDialog`.  It is also used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`.
     public static long[] checkedItemIds;
 
 
@@ -114,6 +113,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         // Run the default commands.
         super.onCreate(savedInstanceState);
 
+        // Get the intent that launched the activity.
+        Intent launchingIntent = getIntent();
+
+        // Set the current folder variable.
+        if (launchingIntent.getStringExtra("Current Folder") != null) {  // Set the current folder from the intent.
+            currentFolder = launchingIntent.getStringExtra("Current Folder");
+        } else {  // Set the current folder to be `""`, which is the home folder.
+            currentFolder = "";
+        }
+
         // Set the content view.
         setContentView(R.layout.bookmarks_coordinatorlayout);
 
@@ -136,9 +145,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
 
-        // Set currentFolder to the home folder, which is `""` in the database.
-        currentFolder = "";
-
         // Load the home folder.
         loadFolder();
 
@@ -164,6 +170,12 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                     // Get the bookmark URL and assign it to `formattedUrlString`.  `mainWebView` will automatically reload when `BookmarksActivity` closes.
                     MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
 
+                    // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+                    MainWebViewActivity.currentBookmarksFolder = currentFolder;
+
+                    // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+                    MainWebViewActivity.restartFromBookmarksActivity = true;
+
                     // Return to `MainWebViewActivity`.
                     NavUtils.navigateUpFromSameTask(bookmarksActivity);
                 }
@@ -177,7 +189,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
             // Instantiate the common variables.
             MenuItem editBookmarkMenuItem;
+            MenuItem deleteBookmarksMenuItem;
             MenuItem selectAllBookmarksMenuItem;
+            boolean deletingBookmarks;
 
             @Override
             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
@@ -196,8 +210,14 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                 moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up);
                 moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down);
                 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
+                deleteBookmarksMenuItem = menu.findItem(R.id.delete_bookmark);
                 selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks);
 
+                // Disable the delete bookmarks menu item if a delete is pending.
+                if (deletingBookmarks) {
+                    deleteBookmarksMenuItem.setEnabled(false);
+                }
+
                 // Store `contextualActionMode` so it can be closed programatically.
                 contextualActionMode = mode;
 
@@ -380,28 +400,31 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                         // Get the position of the selected bookmark.  Only one bookmark is selected when `edit_bookmark_down` is enabled.
                         selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
 
-                        // Move to the selected database ID and find out if it is a folder.
+                        // Move to the selected position and find out if it is a folder.
                         bookmarksCursor.moveToPosition(selectedBookmarkPosition);
                         boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
 
-                        // Store `checkedItemIds` for use by the `AlertDialog`.
-                        checkedItemIds = bookmarksListView.getCheckedItemIds();
+                        // Get the selected bookmark database ID.
+                        int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
 
                         if (isFolder) {
                             // Save the current folder name, which is used in `onSaveEditBookmarkFolder()`.
                             oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
 
-                            // Show the `EditBookmarkFolderDialog` `AlertDialog` and name the instance `@string/edit_folder`.
-                            AppCompatDialogFragment editFolderDialog = new EditBookmarkFolderDialog();
+                            // Show the edit bookmark folder `AlertDialog` and name the instance `@string/edit_folder`.
+                            AppCompatDialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
                             editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
                         } else {
-                            // Show the `EditBookmarkDialog` `AlertDialog` and name the instance `@string/edit_bookmark`.
-                            AppCompatDialogFragment editBookmarkDialog = new EditBookmarkDialog();
+                            // Show the edit bookmark `AlertDialog` and name the instance `@string/edit_bookmark`.
+                            AppCompatDialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
                             editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
                         }
                         break;
 
                     case R.id.delete_bookmark:
+                        // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
+                        deletingBookmarks = true;
+
                         // Get an array of the selected row IDs.
                         final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
 
@@ -448,6 +471,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                                                 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
                                                     bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
                                                 }
+
                                                 break;
 
                                             // The `Snackbar` was dismissed without the `Undo` button being pushed.
@@ -479,8 +503,13 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                                                         bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
                                                     }
                                                 }
-                                                break;
                                         }
+
+                                        // Enable the delete bookmarks menu item.
+                                        deleteBookmarksMenuItem.setEnabled(true);
+
+                                        // Reset the deleting bookmarks flag.
+                                        deletingBookmarks = false;
                                     }
                                 })
                                 //Show the `SnackBar`.
@@ -553,6 +582,12 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         switch (menuItemId) {
             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
                 if (currentFolder.isEmpty()) {  // Currently in the home folder.
+                    // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+                    MainWebViewActivity.currentBookmarksFolder = "";
+
+                    // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+                    MainWebViewActivity.restartFromBookmarksActivity = true;
+
                     // Return to `MainWebViewActivity`.
                     NavUtils.navigateUpFromSameTask(this);
                 } else {  // Currently in a subfolder.
@@ -586,6 +621,12 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
     @Override
     public void onBackPressed() {
         if (currentFolder.isEmpty()) {  // Currently in the home folder.
+            // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+            MainWebViewActivity.currentBookmarksFolder = "";
+
+            // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
+            MainWebViewActivity.restartFromBookmarksActivity = true;
+
             // Exit `BookmarksActivity`.
             super.onBackPressed();
         } else {  // Currently in a subfolder.
@@ -607,9 +648,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         String bookmarkNameString = createBookmarkNameEditText.getText().toString();
         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
 
-        // Convert the favoriteIcon Bitmap to a byte array.
+        // Convert the favoriteIcon Bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
-        // `0` is for lossless compression (the only option for a PNG).
         MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
 
@@ -675,7 +715,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
     }
 
     @Override
-    public void onSaveEditBookmark(AppCompatDialogFragment dialogFragment) {
+    public void onSaveEditBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
         // Get handles for the views from `dialogFragment`.
         EditText editBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
         EditText editBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
@@ -685,12 +725,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
 
-        // Get an array of the selected row IDs.
-        long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
-
-        // Get the database ID of the selected bookmark.  Editing a bookmark is only possible where only one item is selected.
-        int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
-
         // Update the bookmark.
         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
@@ -715,7 +749,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
     }
 
     @Override
-    public void onSaveEditBookmarkFolder(AppCompatDialogFragment dialogFragment) {
+    public void onSaveEditBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) {
         // Get handles for the views from `dialogFragment`.
         EditText editFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
         RadioButton currentFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
@@ -725,12 +759,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         // Get the new folder name.
         String newFolderNameString = editFolderNameEditText.getText().toString();
 
-        // Get an array of the selected row IDs.
-        long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
-
-        // Get the database ID of the selected bookmark.  Editing a folder is only possible where only one item is selected.
-        int selectedFolderDatabaseId = (int) selectedBookmarksLongArray[0];
-
         // Check if the favorite icon has changed.
         if (currentFolderIconRadioButton.isChecked()) {  // Only the name has changed.
             // Update the name in the database.