Add the ability to select all bookmarks.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / BookmarksActivity.java
index 108dcc043625b0595a39b4e3f1b6235299087fdc..b6e67c89da48de1144a1bd8c736992a8c6196763 100644 (file)
@@ -108,14 +108,18 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
             // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
             MenuItem editBookmarkMenuItem;
 
+            // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`.
+            MenuItem selectAllBookmarks;
+
             @Override
             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                 // Inflate the menu for the contextual app bar and set the title.
                 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
                 mode.setTitle(R.string.bookmarks);
 
-                // Get a handle for `R.id.edit_bookmark`.
+                // Get a handle for `R.id.edit_bookmark` and `R.id.select_all_bookmarks`.
                 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
+                selectAllBookmarks = menu.findItem(R.id.context_menu_select_all_bookmarks);
 
                 return true;
             }
@@ -137,11 +141,18 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
 
                 // Show the `Edit` option only if 1 bookmark is selected.
-                if (numberOfSelectedBookmarks == 1) {
+                if (numberOfSelectedBookmarks < 2) {
                     editBookmarkMenuItem.setVisible(true);
                 } else {
                     editBookmarkMenuItem.setVisible(false);
                 }
+
+                // Do not show `Select All` if all the bookmarks are already checked.
+                if (bookmarksListView.getCheckedItemIds().length == bookmarksListView.getCount()) {
+                    selectAllBookmarks.setVisible(false);
+                } else {
+                    selectAllBookmarks.setVisible(true);
+                }
             }
 
             @Override
@@ -210,6 +221,14 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                         // Close the contextual app bar.
                         mode.finish();
                         break;
+
+                    case R.id.context_menu_select_all_bookmarks:
+                        int numberOfBookmarks = bookmarksListView.getCount();
+
+                        for (int i = 0; i < numberOfBookmarks; i++) {
+                            bookmarksListView.setItemChecked(i, true);
+                        }
+                        break;
                 }
                 // Consume the click.
                 return true;
@@ -234,6 +253,41 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         });
     }
 
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        //Inflate the menu.
+        getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
+
+        return true;
+    }
+
+    @Override
+    public boolean onPrepareOptionsMenu(Menu menu) {
+        super.onPrepareOptionsMenu(menu);
+
+        return true;
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        int menuItemId = menuItem.getItemId();
+
+        switch (menuItemId) {
+            case android.R.id.home:
+                NavUtils.navigateUpFromSameTask(this);
+                break;
+
+            case R.id.options_menu_select_all_bookmarks:
+                int numberOfBookmarks = bookmarksListView.getCount();
+
+                for (int i = 0; i < numberOfBookmarks; i++) {
+                    bookmarksListView.setItemChecked(i, true);
+                }
+                break;
+        }
+        return true;
+    }
+
     @Override
     public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) {
         // Do nothing because the user selected `Cancel`.