]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java
Add a context menu to delete bookmarks from the database view activity. https:/...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksDatabaseViewActivity.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.activities;
21
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MergeCursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.Typeface;
29 import android.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.support.design.widget.Snackbar;
33 import android.support.v4.content.ContextCompat;
34 import android.support.v4.widget.CursorAdapter;
35 import android.support.v4.widget.ResourceCursorAdapter;
36 import android.support.v7.app.ActionBar;
37 import android.support.v7.app.AppCompatActivity;
38 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
39 import android.support.v7.app.AppCompatDialogFragment;
40 import android.support.v7.widget.Toolbar;
41 import android.util.SparseBooleanArray;
42 import android.view.ActionMode;
43 import android.view.Menu;
44 import android.view.MenuItem;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.view.WindowManager;
48 import android.widget.AbsListView;
49 import android.widget.AdapterView;
50 import android.widget.EditText;
51 import android.widget.ImageView;
52 import android.widget.ListView;
53 import android.widget.RadioButton;
54 import android.widget.Spinner;
55 import android.widget.TextView;
56
57 import com.stoutner.privacybrowser.R;
58 import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog;
59 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog;
60 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
61
62 import java.io.ByteArrayOutputStream;
63
64 public class BookmarksDatabaseViewActivity extends AppCompatActivity implements EditBookmarkDatabaseViewDialog.EditBookmarkDatabaseViewListener,
65         EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
66     // Instantiate the constants.
67     private static final int ALL_FOLDERS_DATABASE_ID = -2;
68     private static final int HOME_FOLDER_DATABASE_ID = -1;
69
70     // `bookmarksDatabaseHelper` is used in `onCreate()`, `updateBookmarksListView()`, `selectAllBookmarksInFolder()`, and `onDestroy()`.
71     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
72
73     // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, and `onDestroy()`.
74     private Cursor bookmarksCursor;
75
76     // `bookmarksCursorAdapter` is used in `onCreate()`, `selectAllBookmarksInFolder()`, and `updateBookmarksListView()`.
77     private CursorAdapter bookmarksCursorAdapter;
78
79     // `oldFolderNameString` is used in `onCreate()` and `onSaveBookmarkFolder()`.
80     private String oldFolderNameString;
81
82     // `currentFolderDatabaseId` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
83     private int currentFolderDatabaseId;
84
85     // `currentFolder` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
86     private String currentFolderName;
87
88     // `sortByDisplayOrder` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateBookmarksListView()`.
89     private boolean sortByDisplayOrder;
90
91     // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
92     private Snackbar bookmarksDeletedSnackbar;
93
94     // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
95     private boolean closeActivityAfterDismissingSnackbar;
96
97     @Override
98     public void onCreate(Bundle savedInstanceState) {
99         // Disable screenshots if not allowed.
100         if (!MainWebViewActivity.allowScreenshots) {
101             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
102         }
103
104         // Set the activity theme.
105         if (MainWebViewActivity.darkTheme) {
106             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
107         } else {
108             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
109         }
110
111         // Run the default commands.
112         super.onCreate(savedInstanceState);
113
114         // Set the content view.
115         setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
116
117         // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
118         Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar);
119         setSupportActionBar(bookmarksDatabaseViewAppBar);
120
121         // Get a handle for the `AppBar`.
122         ActionBar appBar = getSupportActionBar();
123
124         // Remove the incorrect warning in Android Studio that `appBar` might be null.
125         assert appBar != null;
126
127         // Display the spinner and the back arrow in the app bar.
128         appBar.setCustomView(R.layout.spinner);
129         appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
130
131         // Initialize the database handler.  `this` specifies the context.  The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
132         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
133
134         // Setup a matrix cursor for "All Folders" and "Home Folder".
135         String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
136         MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
137         matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)});
138         matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)});
139
140         // Get a cursor with the list of all the folders.
141         Cursor foldersCursor = bookmarksDatabaseHelper.getAllFolders();
142
143         // Combine `matrixCursor` and `foldersCursor`.
144         MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
145
146         // Create a resource cursor adapter for the spinner.
147         ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.appbar_spinner_item, foldersMergeCursor, 0) {
148             @Override
149             public void bindView(View view, Context context, Cursor cursor) {
150                 // Get a handle for the spinner item text view.
151                 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
152
153                 // Set the text view to display the folder name.
154                 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
155             }
156         };
157
158         // Set the resource cursor adapter drop drown view resource.
159         foldersCursorAdapter.setDropDownViewResource(R.layout.appbar_spinner_dropdown_item);
160
161         // Get a handle for the folder spinner and set the adapter.
162         Spinner folderSpinner = findViewById(R.id.spinner);
163         folderSpinner.setAdapter(foldersCursorAdapter);
164
165         // Handle taps on the spinner dropdown.
166         folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
167             @Override
168             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
169                 // Store the current folder database ID.
170                 currentFolderDatabaseId = (int) id;
171
172                 // Get a handle for the selected view.
173                 TextView selectedFolderTextView = findViewById(R.id.spinner_item_textview);
174
175                 // Store the current folder name.
176                 currentFolderName = selectedFolderTextView.getText().toString();
177
178                 // Update the list view.
179                 updateBookmarksListView();
180             }
181
182             @Override
183             public void onNothingSelected(AdapterView<?> parent) {
184                 // Do nothing.
185             }
186         });
187
188         // Get a handle for the bookmarks `ListView`.
189         ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
190
191         // Get a `Cursor` with the current contents of the bookmarks database.
192         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
193
194         // Setup a `CursorAdapter` with `this` context.  `false` disables autoRequery.
195         bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
196             @Override
197             public View newView(Context context, Cursor cursor, ViewGroup parent) {
198                 // Inflate the individual item layout.  `false` does not attach it to the root.
199                 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
200             }
201
202             @Override
203             public void bindView(View view, Context context, Cursor cursor) {
204                 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
205
206                 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
207                 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
208                 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
209                 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
210
211                 // Get the favorite icon byte array from the `Cursor`.
212                 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
213                 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
214                 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
215                 // Display the bitmap in `bookmarkFavoriteIcon`.
216                 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
217                 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
218
219                 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
220                 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
221                 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
222                 bookmarkNameTextView.setText(bookmarkNameString);
223
224                 // Make the font bold for folders.
225                 if (isFolder) {
226                     // The first argument is `null` prevent changing of the font.
227                     bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
228                 } else {  // Reset the font to default.
229                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
230                 }
231
232                 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
233                 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
234                 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
235                 bookmarkUrlTextView.setText(bookmarkUrlString);
236
237                 // Hide the URL if the bookmark is a folder.
238                 if (isFolder) {
239                     bookmarkUrlTextView.setVisibility(View.GONE);
240                 } else {
241                     bookmarkUrlTextView.setVisibility(View.VISIBLE);
242                 }
243
244                 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
245                 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
246                 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
247                 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
248
249                 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
250                 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
251                 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
252                 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
253
254                 // Make the folder name gray if it is the home folder.
255                 if (bookmarkParentFolder.isEmpty()) {
256                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
257                     bookmarkParentFolderTextView.setText(R.string.home_folder);
258                     bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
259                 } else {
260                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
261                     bookmarkParentFolderTextView.setText(bookmarkParentFolder);
262
263                     // Set the text color according to the theme.
264                     if (MainWebViewActivity.darkTheme) {
265                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
266                     } else {
267                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
268                     }
269                 }
270             }
271         };
272
273         // Update the ListView.
274         bookmarksListView.setAdapter(bookmarksCursorAdapter);
275
276         // Set the current folder database ID.
277         currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
278
279         // Set a listener to edit a bookmark when it is tapped.
280         bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
281             // Convert the database ID to an int.
282             int databaseId = (int) id;
283
284             // Show the edit bookmark or edit bookmark folder dialog.
285             if (bookmarksDatabaseHelper.isFolder(databaseId)) {
286                 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
287                 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
288
289                 // Show the edit bookmark folder dialog.
290                 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
291                 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
292             } else {
293                 // Show the edit bookmark dialog.
294                 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
295                 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
296             }
297         });
298
299         // Handle long presses on the list view.
300         bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
301             // Instantiate the common variables.
302             MenuItem selectAllMenuItem;
303             MenuItem deleteMenuItem;
304             boolean deletingBookmarks;
305
306             @Override
307             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
308                 // Inflate the menu for the contextual app bar.
309                 getMenuInflater().inflate(R.menu.bookmarks_databaseview_context_menu, menu);
310
311                 // Set the title.
312                 mode.setTitle(R.string.bookmarks);
313
314                 // Get handles for the menu items.
315                 selectAllMenuItem = menu.findItem(R.id.select_all);
316                 deleteMenuItem = menu.findItem(R.id.delete);
317
318                 // Disable the delete menu item if a delete is pending.
319                 deleteMenuItem.setEnabled(!deletingBookmarks);
320
321                 // Make it so.
322                 return true;
323             }
324
325             @Override
326             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
327                 // Do nothing.
328                 return false;
329             }
330
331             @Override
332             public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
333                 // Calculate the number of selected bookmarks.
334                 int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
335
336                 // Adjust the ActionMode according to the number of selected bookmarks.
337                 mode.setSubtitle(getString(R.string.selected) + "  " + numberOfSelectedBookmarks);
338
339                 // Do not show the select all menu item if all the bookmarks are already checked.
340                 if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) {
341                     selectAllMenuItem.setVisible(false);
342                 } else {
343                     selectAllMenuItem.setVisible(true);
344                 }
345
346                 // Convert the database ID to an int.
347                 int databaseId = (int) id;
348
349                 // If a folder was selected, also select all the contents.
350                 if (checked && bookmarksDatabaseHelper.isFolder(databaseId)) {
351                     selectAllBookmarksInFolder(databaseId);
352                 }
353
354                 // Do not allow a bookmark to be deselected if the folder is selected.
355                 if (!checked) {
356                     // Get the folder name.
357                     String folderName = bookmarksDatabaseHelper.getParentFolderName((int) id);
358
359                     // If the bookmark is not in the root folder, check to see if the folder is selected.
360                     if (!folderName.isEmpty()) {
361                         // Get the database ID of the folder.
362                         int folderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderName);
363
364                         // Move the bookmarks cursor to the first position.
365                         bookmarksCursor.moveToFirst();
366
367                         // Initialize the folder position variable.
368                         int folderPosition = -1;
369
370                         // Get the position of the folder in the bookmarks cursor.
371                         while ((folderPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) {
372                             // Check if the folder database ID matches the bookmark database ID.
373                             if (folderDatabaseId == bookmarksCursor.getInt((bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID)))) {
374                                 // Get the folder position.
375                                 folderPosition = bookmarksCursor.getPosition();
376
377                                 // Check if the folder is selected.
378                                 if (bookmarksListView.isItemChecked(folderPosition)) {
379                                     // Reselect the bookmark.
380                                     bookmarksListView.setItemChecked(position, true);
381
382                                     // Display a snackbar explaining why the bookmark cannot be deselected.
383                                     Snackbar.make(bookmarksListView, R.string.cannot_deselect_bookmark, Snackbar.LENGTH_LONG).show();
384                                 }
385                             }
386
387                             // Increment the bookmarks cursor.
388                             bookmarksCursor.moveToNext();
389                         }
390                     }
391                 }
392             }
393
394             @Override
395             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
396                 switch (item.getItemId()) {
397                     case R.id.select_all:
398                         // Get the total number of bookmarks.
399                         int numberOfBookmarks = bookmarksListView.getCount();
400
401                         // Select them all.
402                         for (int i = 0; i < numberOfBookmarks; i++) {
403                             bookmarksListView.setItemChecked(i, true);
404                         }
405                         break;
406
407                     case R.id.delete:
408                         // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
409                         deletingBookmarks = true;
410
411                         // Get an array of the selected row IDs.
412                         long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
413
414                         // Get an array of checked bookmarks.  `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete.
415                         SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
416
417                         // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
418                         switch (currentFolderDatabaseId) {
419                             // Get a cursor with all the folders.
420                             case ALL_FOLDERS_DATABASE_ID:
421                                 if (sortByDisplayOrder) {
422                                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray);
423                                 } else {
424                                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksExcept(selectedBookmarksIdsLongArray);
425                                 }
426                                 break;
427
428                             // Get a cursor for the home folder.
429                             case HOME_FOLDER_DATABASE_ID:
430                                 if (sortByDisplayOrder) {
431                                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, "");
432                                 } else {
433                                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, "");
434                                 }
435                                 break;
436
437                             // Display the selected folder.
438                             default:
439                                 // Get a cursor for the selected folder.
440                                 if (sortByDisplayOrder) {
441                                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolderName);
442                                 } else {
443                                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, currentFolderName);
444                                 }
445                         }
446
447                         // Update the list view.
448                         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
449
450                         // Show a Snackbar with the number of deleted bookmarks.
451                         bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_databaseview_coordinatorlayout),
452                                 getString(R.string.bookmarks_deleted) + "  " + selectedBookmarksIdsLongArray.length, Snackbar.LENGTH_LONG)
453                                 .setAction(R.string.undo, view -> {
454                                     // Do nothing because everything will be handled by `onDismissed()` below.
455                                 })
456                                 .addCallback(new Snackbar.Callback() {
457                                     @Override
458                                     public void onDismissed(Snackbar snackbar, int event) {
459                                         switch (event) {
460                                             // The user pushed the `Undo` button.
461                                             case Snackbar.Callback.DISMISS_EVENT_ACTION:
462                                                 // Update the bookmarks list view with the current contents of the bookmarks database, including the "deleted bookmarks.
463                                                 updateBookmarksListView();
464
465                                                 // Re-select the previously selected bookmarks.
466                                                 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
467                                                     bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
468                                                 }
469                                                 break;
470
471                                                 // The Snackbar was dismissed without the `Undo` button being pushed.
472                                             default:
473                                                 // Delete each selected bookmark.
474                                                 for (long databaseIdLong : selectedBookmarksIdsLongArray) {
475                                                     // Convert `databaseIdLong` to an int.
476                                                     int databaseIdInt = (int) databaseIdLong;
477
478                                                     // Delete the selected bookmark.
479                                                     bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
480                                                 }
481                                         }
482
483                                         // Reset the deleting bookmarks flag.
484                                         deletingBookmarks = false;
485
486                                         // Enable the delete menu item.
487                                         deleteMenuItem.setEnabled(true);
488
489                                         // Close the activity if back has been pressed.
490                                         if (closeActivityAfterDismissingSnackbar) {
491                                             onBackPressed();
492                                         }
493                                     }
494                                 });
495
496                         // Show the Snackbar.
497                         bookmarksDeletedSnackbar.show();
498                         break;
499                 }
500
501                 // Consume the click.
502                 return false;
503             }
504
505             @Override
506             public void onDestroyActionMode(ActionMode mode) {
507                 // Do nothing.
508             }
509         });
510     }
511
512     @Override
513     public boolean onCreateOptionsMenu(Menu menu) {
514         // Inflate the menu.
515         getMenuInflater().inflate(R.menu.bookmarks_databaseview_options_menu, menu);
516
517         // Success.
518         return true;
519     }
520
521     @Override
522     public boolean onOptionsItemSelected(MenuItem menuItem) {
523         // Get the ID of the menu item that was selected.
524         int menuItemId = menuItem.getItemId();
525
526         switch (menuItemId) {
527             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
528                 // Exit the activity.
529                 onBackPressed();
530                 break;
531
532             case R.id.options_menu_sort:
533                 // Update the sort by display order tracker.
534                 sortByDisplayOrder = !sortByDisplayOrder;
535
536                 // Get a handle for the bookmarks `ListView`.
537                 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
538
539                 // Update the icon and display a snackbar.
540                 if (sortByDisplayOrder) {  // Sort by display order.
541                     // Update the icon according to the theme.
542                     if (MainWebViewActivity.darkTheme) {
543                         menuItem.setIcon(R.drawable.sort_selected_dark);
544                     } else {
545                         menuItem.setIcon(R.drawable.sort_selected_light);
546                     }
547
548                     // Display a Snackbar indicating the current sort type.
549                     Snackbar.make(bookmarksListView, R.string.sorted_by_display_order, Snackbar.LENGTH_SHORT).show();
550                 } else {  // Sort by database id.
551                     // Update the icon according to the theme.
552                     if (MainWebViewActivity.darkTheme) {
553                         menuItem.setIcon(R.drawable.sort_dark);
554                     } else {
555                         menuItem.setIcon(R.drawable.sort_light);
556                     }
557
558                     // Display a Snackbar indicating the current sort type.
559                     Snackbar.make(bookmarksListView, R.string.sorted_by_database_id, Snackbar.LENGTH_SHORT).show();
560                 }
561
562                 // Update the list view.
563                 updateBookmarksListView();
564                 break;
565         }
566         return true;
567     }
568
569     @Override
570     public void onBackPressed() {
571         // Check to see if a snackbar is currently displayed.  If so, it must be closed before existing so that a pending delete is completed before reloading the list view in the bookmarks activity.
572         if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home.
573             // Set the close flag.
574             closeActivityAfterDismissingSnackbar = true;
575
576             // Dismiss the snackbar.
577             bookmarksDeletedSnackbar.dismiss();
578         } else {  // Go home immediately.
579             // Update the current folder in the bookmarks activity.
580             switch (currentFolderDatabaseId) {
581                 case ALL_FOLDERS_DATABASE_ID:
582                     // Load the home folder.
583                     BookmarksActivity.currentFolder = "";
584                     break;
585
586                 case HOME_FOLDER_DATABASE_ID:
587                     // Load the home folder.
588                     BookmarksActivity.currentFolder = "";
589                     break;
590
591                 default:
592                     // Load the current folder.
593                     BookmarksActivity.currentFolder = currentFolderName;
594             }
595
596             // Reload the bookmarks list view when returning to the bookmarks activity.
597             BookmarksActivity.restartFromBookmarksDatabaseViewActivity = true;
598
599             // Exit the bookmarks database view activity.
600             super.onBackPressed();
601         }
602     }
603
604     private void updateBookmarksListView() {
605         // Populate the bookmarks list view based on the spinner selection.
606         switch (currentFolderDatabaseId) {
607             // Get a cursor with all the folders.
608             case ALL_FOLDERS_DATABASE_ID:
609                 if (sortByDisplayOrder) {
610                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksByDisplayOrder();
611                 } else {
612                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
613                 }
614                 break;
615
616             // Get a cursor for the home folder.
617             case HOME_FOLDER_DATABASE_ID:
618                 if (sortByDisplayOrder) {
619                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder("");
620                 } else {
621                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarks("");
622                 }
623                 break;
624
625             // Display the selected folder.
626             default:
627                 // Get a cursor for the selected folder.
628                 if (sortByDisplayOrder) {
629                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolderName);
630                 } else {
631                     bookmarksCursor = bookmarksDatabaseHelper.getBookmarks(currentFolderName);
632                 }
633         }
634
635         // Update the list view.
636         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
637     }
638
639     private void selectAllBookmarksInFolder(int folderId) {
640         // Get a handle for the bookmarks list view.
641         ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
642
643         // Get the folder name.
644         String folderName = bookmarksDatabaseHelper.getFolderName(folderId);
645
646         // Get a cursor with the contents of the folder.
647         Cursor folderCursor = bookmarksDatabaseHelper.getBookmarks(folderName);
648
649         // Move to the beginning of the cursor.
650         folderCursor.moveToFirst();
651
652         while (folderCursor.getPosition() < folderCursor.getCount()) {
653             // Get the bookmark database ID.
654             int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
655
656             // Move the bookmarks cursor to the first position.
657             bookmarksCursor.moveToFirst();
658
659             // Initialize the bookmark position variable.
660             int bookmarkPosition = -1;
661
662             // Get the position of this bookmark in the bookmarks cursor.
663             while ((bookmarkPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) {
664                 // Check if the bookmark IDs match.
665                 if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID))) {
666                     // Get the bookmark position.
667                     bookmarkPosition = bookmarksCursor.getPosition();
668
669                     // If this bookmark is a folder, select all the bookmarks inside it.
670                     if (bookmarksDatabaseHelper.isFolder(bookmarkId)) {
671                         selectAllBookmarksInFolder(bookmarkId);
672                     }
673
674                     // Select the bookmark.
675                     bookmarksListView.setItemChecked(bookmarkPosition, true);
676                 }
677
678                 // Increment the bookmarks cursor position.
679                 bookmarksCursor.moveToNext();
680             }
681
682             // Move to the next position.
683             folderCursor.moveToNext();
684         }
685     }
686
687     @Override
688     public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
689         // Get handles for the views from dialog fragment.
690         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
691         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
692         EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
693         Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
694         EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
695
696         // Extract the bookmark information.
697         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
698         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
699         int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
700         int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
701
702         // Instantiate the parent folder name `String`.
703         String parentFolderNameString;
704
705         // Set the parent folder name.
706         if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
707             parentFolderNameString = "";
708         } else {  // Get the parent folder name from the database.
709             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
710         }
711
712         // Update the bookmark.
713         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
714             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
715         } else {  // Update the bookmark using the `WebView` favorite icon.
716             // Convert the favorite icon to a byte array.  `0` is for lossless compression (the only option for a PNG).
717             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
718             MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
719             byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
720
721             //  Update the bookmark and the favorite icon.
722             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
723         }
724
725         // Update the list view.
726         updateBookmarksListView();
727     }
728
729     @Override
730     public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
731         // Get handles for the views from dialog fragment.
732         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
733         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
734         ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
735         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
736         Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
737         EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
738
739         // Extract the folder information.
740         String newFolderNameString = editBookmarkNameEditText.getText().toString();
741         int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
742         int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
743
744         // Instantiate the parent folder name `String`.
745         String parentFolderNameString;
746
747         // Set the parent folder name.
748         if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
749             parentFolderNameString = "";
750         } else {  // Get the parent folder name from the database.
751             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
752         }
753
754         // Update the folder.
755         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the folder without changing the favorite icon.
756             bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
757         } else {  // Update the folder and the icon.
758             // Instantiate the new folder icon `Bitmap`.
759             Bitmap folderIconBitmap;
760
761             // Populate the new folder icon bitmap.
762             if (defaultFolderIconRadioButton.isChecked()) {
763                 // Get the default folder icon and convert it to a `Bitmap`.
764                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
765                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
766                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
767             } else {  // Use the `WebView` favorite icon.
768                 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
769             }
770
771             // Convert the folder icon to a byte array.  `0` is for lossless compression (the only option for a PNG).
772             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
773             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
774             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
775
776             //  Update the folder and the icon.
777             bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
778         }
779
780         // Update the list view.
781         updateBookmarksListView();
782     }
783
784     @Override
785     public void onDestroy() {
786         // Close the bookmarks cursor and database.
787         bookmarksCursor.close();
788         bookmarksDatabaseHelper.close();
789
790         // Run the default commands.
791         super.onDestroy();
792     }
793 }