]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java
bd274ec2213985cbd02be090ee19b0016f360ad9
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksActivity.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.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.graphics.Bitmap;
28 import android.graphics.BitmapFactory;
29 import android.graphics.Typeface;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.os.Bundle;
33 import android.util.SparseBooleanArray;
34 import android.view.ActionMode;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.WindowManager;
40 import android.widget.AbsListView;
41 import android.widget.CursorAdapter;
42 import android.widget.EditText;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.RadioButton;
46 import android.widget.TextView;
47
48 import androidx.appcompat.app.ActionBar;
49 import androidx.appcompat.app.AppCompatActivity;
50 import androidx.appcompat.widget.Toolbar;  // The AndroidX toolbar must be used until the minimum API is >= 21.
51 import androidx.core.app.NavUtils;
52 import androidx.fragment.app.DialogFragment;
53
54 import com.google.android.material.floatingactionbutton.FloatingActionButton;
55 import com.google.android.material.snackbar.Snackbar;
56
57 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
58 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
59 import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
60 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
61 import com.stoutner.privacybrowser.dialogs.MoveToFolderDialog;
62 import com.stoutner.privacybrowser.R;
63 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
64
65 import java.io.ByteArrayOutputStream;
66
67 public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener,
68         EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener {
69
70     // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog` and `BookmarksDatabaseViewActivity`.
71     // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
72     // and `loadFolder()`.
73     public static String currentFolder;
74
75     // `checkedItemIds` is public static so it can be accessed from `MoveToFolderDialog`.  It is also used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
76     // and `updateMoveIcons()`.
77     public static long[] checkedItemIds;
78
79     // `restartFromBookmarksDatabaseViewActivity` is public static so it can be accessed from `BookmarksDatabaseViewActivity`.  It is also used in `onRestart()`.
80     public static boolean restartFromBookmarksDatabaseViewActivity;
81
82     //  The current WebView strings are public static so they can be updated from `MainWebViewActivity`.  They are use in `onCreate()`.
83     public static String currentWebViewUrl;
84     public static String currentWebViewTitle;
85
86
87     // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`,
88     // `onMoveToFolder()`, `deleteBookmarkFolderContents()`, `loadFolder()`, and `onDestroy()`.
89     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
90
91     // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
92     // `updateMoveIcons()`, `scrollBookmarks()`, and `loadFolder()`.
93     private ListView bookmarksListView;
94
95     // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`,
96     // `loadFolder()`, and `onDestroy()`.
97     private Cursor bookmarksCursor;
98
99     // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`.
100     private CursorAdapter bookmarksCursorAdapter;
101
102     // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`.
103     private ActionMode contextualActionMode;
104
105     // `appBar` is used in `onCreate()` and `loadFolder()`.
106     private ActionBar appBar;
107
108     // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`.
109     private String oldFolderNameString;
110
111     // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
112     private MenuItem moveBookmarkUpMenuItem;
113
114     // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
115     private MenuItem moveBookmarkDownMenuItem;
116
117     // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
118     private Snackbar bookmarksDeletedSnackbar;
119
120     // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
121     private boolean closeActivityAfterDismissingSnackbar;
122
123     @Override
124     protected void onCreate(Bundle savedInstanceState) {
125         // Disable screenshots if not allowed.
126         if (!MainWebViewActivity.allowScreenshots) {
127             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
128         }
129
130         // Set the activity theme.
131         if (MainWebViewActivity.darkTheme) {
132             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
133         } else {
134             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
135         }
136
137         // Run the default commands.
138         super.onCreate(savedInstanceState);
139
140         // Get the intent that launched the activity.
141         Intent launchingIntent = getIntent();
142
143         // Set the current folder variable.
144         if (launchingIntent.getStringExtra("Current Folder") != null) {  // Set the current folder from the intent.
145             currentFolder = launchingIntent.getStringExtra("Current Folder");
146         } else {  // Set the current folder to be `""`, which is the home folder.
147             currentFolder = "";
148         }
149
150         // Set the content view.
151         setContentView(R.layout.bookmarks_coordinatorlayout);
152
153         // The AndroidX toolbar must be used until the minimum API is >= 21.
154         final Toolbar toolbar = findViewById(R.id.bookmarks_toolbar);
155         setSupportActionBar(toolbar);
156
157         // Get a handle for the activity, the app bar, and the ListView.
158         final Activity bookmarksActivity = this;
159         appBar = getSupportActionBar();
160         bookmarksListView = findViewById(R.id.bookmarks_listview);
161
162         // Remove the incorrect lint warning that `appBar` might be null.
163         assert appBar != null;
164
165         // Display the home arrow on the app bar.
166         appBar.setDisplayHomeAsUpEnabled(true);
167
168         // Initialize the database helper.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
169         // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
170         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
171
172         // Load the home folder.
173         loadFolder();
174
175         // Set a listener so that tapping a list item loads the URL or folder.
176         bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
177             // Convert the id from long to int to match the format of the bookmarks database.
178             int databaseID = (int) id;
179
180             // Get the bookmark cursor for this ID and move it to the first row.
181             Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseID);
182             bookmarkCursor.moveToFirst();
183
184             // Act upon the bookmark according to the type.
185             if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {  // The selected bookmark is a folder.
186                 // Update the current folder.
187                 currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
188
189                 // Load the new folder.
190                 loadFolder();
191             } else {  // The selected bookmark is not a folder.
192                 // Get the bookmark URL and assign it to `formattedUrlString`.
193                 MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
194
195                 // Set `MainWebViewActivity` to load the new URL on restart.
196                 MainWebViewActivity.loadUrlOnRestart = true;
197
198                 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
199                 MainWebViewActivity.currentBookmarksFolder = currentFolder;
200
201                 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
202                 MainWebViewActivity.restartFromBookmarksActivity = true;
203
204                 // Return to `MainWebViewActivity`.
205                 NavUtils.navigateUpFromSameTask(bookmarksActivity);
206             }
207
208             // Close the `Cursor`.
209             bookmarkCursor.close();
210         });
211
212         // Handle long presses on the list view.
213         bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
214             // Instantiate the common variables.
215             MenuItem editBookmarkMenuItem;
216             MenuItem deleteBookmarksMenuItem;
217             MenuItem selectAllBookmarksMenuItem;
218             boolean deletingBookmarks;
219
220             @Override
221             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
222                 // Inflate the menu for the contextual app bar.
223                 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
224
225                 // Set the title.
226                 if (currentFolder.isEmpty()) {  // Use `R.string.bookmarks` if in the home folder.
227                     mode.setTitle(R.string.bookmarks);
228                 } else {  // Use the current folder name as the title.
229                     mode.setTitle(currentFolder);
230                 }
231
232                 // Get handles for menu items that need to be selectively disabled.
233                 moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up);
234                 moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down);
235                 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
236                 deleteBookmarksMenuItem = menu.findItem(R.id.delete_bookmark);
237                 selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks);
238
239                 // Disable the delete bookmarks menu item if a delete is pending.
240                 deleteBookmarksMenuItem.setEnabled(!deletingBookmarks);
241
242                 // Store a handle for the contextual action mode so it can be closed programatically.
243                 contextualActionMode = mode;
244
245                 // Make it so.
246                 return true;
247             }
248
249             @Override
250             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
251                 // Get a handle for the move to folder menu item.
252                 MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder);
253
254                 // Get a Cursor with all of the folders.
255                 Cursor folderCursor = bookmarksDatabaseHelper.getAllFolders();
256
257                 // Enable the move to folder menu item if at least one folder exists.
258                 moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0);
259
260                 // Make it so.
261                 return true;
262             }
263
264             @Override
265             public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
266                 // Get the number of selected bookmarks.
267                 int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
268
269                 // Adjust the ActionMode and the menu according to the number of selected bookmarks.
270                 if (numberOfSelectedBookmarks == 1) {  // One bookmark is selected.
271                     // List the number of selected bookmarks in the subtitle.
272                     mode.setSubtitle(getString(R.string.selected) + "  1");
273
274                     // Show the `Move Up`, `Move Down`, and  `Edit` options.
275                     moveBookmarkUpMenuItem.setVisible(true);
276                     moveBookmarkDownMenuItem.setVisible(true);
277                     editBookmarkMenuItem.setVisible(true);
278
279                     // Update the enabled status of the move icons.
280                     updateMoveIcons();
281                 } else {  // More than one bookmark is selected.
282                     // List the number of selected bookmarks in the subtitle.
283                     mode.setSubtitle(getString(R.string.selected) + "  " + numberOfSelectedBookmarks);
284
285                     // Hide non-applicable `MenuItems`.
286                     moveBookmarkUpMenuItem.setVisible(false);
287                     moveBookmarkDownMenuItem.setVisible(false);
288                     editBookmarkMenuItem.setVisible(false);
289                 }
290
291                 // Do not show the select all menu item if all the bookmarks are already checked.
292                 if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) {
293                     selectAllBookmarksMenuItem.setVisible(false);
294                 } else {
295                     selectAllBookmarksMenuItem.setVisible(true);
296                 }
297             }
298
299             @Override
300             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
301                 // Instantiate the common variables.
302                 int selectedBookmarkPosition;
303                 int selectedBookmarkNewPosition;
304                 final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray;
305
306                 switch (item.getItemId()) {
307                     case R.id.move_bookmark_up:
308                         // Get the array of checked bookmark positions.
309                         selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
310
311                         // Store the position of the selected bookmark.  Only one bookmark is selected when `move_bookmark_up` is enabled.
312                         selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
313
314                         // Calculate the new position of the selected bookmark.
315                         selectedBookmarkNewPosition = selectedBookmarkPosition - 1;
316
317                         // Iterate through the bookmarks.
318                         for (int i = 0; i < bookmarksListView.getCount(); i++) {
319                             // Get the database ID for the current bookmark.
320                             int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
321
322                             // Update the display order for the current bookmark.
323                             if (i == selectedBookmarkPosition) {  // The current bookmark is the selected bookmark.
324                                 // Move the current bookmark up one.
325                                 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
326                             } else if ((i + 1) == selectedBookmarkPosition){  // The current bookmark is immediately above the selected bookmark.
327                                 // Move the current bookmark down one.
328                                 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
329                             } else {  // The current bookmark is not changing positions.
330                                 // Move `bookmarksCursor` to the current bookmark position.
331                                 bookmarksCursor.moveToPosition(i);
332
333                                 // Update the display order only if it is not correct in the database.
334                                 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
335                                     bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
336                                 }
337                             }
338                         }
339
340                         // Update the bookmarks cursor with the current contents of the bookmarks database.
341                         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
342
343                         // Update the `ListView`.
344                         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
345
346                         // Scroll with the bookmark.
347                         scrollBookmarks(selectedBookmarkNewPosition);
348
349                         // Update the enabled status of the move icons.
350                         updateMoveIcons();
351                         break;
352
353                     case R.id.move_bookmark_down:
354                         // Get the array of checked bookmark positions.
355                         selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
356
357                         // Store the position of the selected bookmark.  Only one bookmark is selected when `move_bookmark_down` is enabled.
358                         selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
359
360                         // Calculate the new position of the selected bookmark.
361                         selectedBookmarkNewPosition = selectedBookmarkPosition + 1;
362
363                         // Iterate through the bookmarks.
364                         for (int i = 0; i <bookmarksListView.getCount(); i++) {
365                             // Get the database ID for the current bookmark.
366                             int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
367
368                             // Update the display order for the current bookmark.
369                             if (i == selectedBookmarkPosition) {  // The current bookmark is the selected bookmark.
370                                 // Move the current bookmark down one.
371                                 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
372                             } else if ((i - 1) == selectedBookmarkPosition) {  // The current bookmark is immediately below the selected bookmark.
373                                 // Move the bookmark below the selected bookmark up one.
374                                 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
375                             } else {  // The current bookmark is not changing positions.
376                                 // Move `bookmarksCursor` to the current bookmark position.
377                                 bookmarksCursor.moveToPosition(i);
378
379                                 // Update the display order only if it is not correct in the database.
380                                 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
381                                     bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
382                                 }
383                             }
384                         }
385
386                         // Update the bookmarks cursor with the current contents of the bookmarks database.
387                         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
388
389                         // Update the `ListView`.
390                         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
391
392                         // Scroll with the bookmark.
393                         scrollBookmarks(selectedBookmarkNewPosition);
394
395                         // Update the enabled status of the move icons.
396                         updateMoveIcons();
397                         break;
398
399                     case R.id.move_to_folder:
400                         // Store `checkedItemIds` for use by the `AlertDialog`.
401                         checkedItemIds = bookmarksListView.getCheckedItemIds();
402
403                         // Show the `MoveToFolderDialog` `AlertDialog` and name the instance `@string/move_to_folder
404                         DialogFragment moveToFolderDialog = new MoveToFolderDialog();
405                         moveToFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.move_to_folder));
406                         break;
407
408                     case R.id.edit_bookmark:
409                         // Get the array of checked bookmark positions.
410                         selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
411
412                         // Get the position of the selected bookmark.  Only one bookmark is selected when `edit_bookmark_down` is enabled.
413                         selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
414
415                         // Move the `Cursor` to the selected position and find out if it is a folder.
416                         bookmarksCursor.moveToPosition(selectedBookmarkPosition);
417                         boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
418
419                         // Get the selected bookmark database ID.
420                         int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
421
422                         // Show the edit bookmark or edit bookmark folder dialog.
423                         if (isFolder) {
424                             // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
425                             oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
426
427                             // Show the edit bookmark folder dialog.
428                             DialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
429                             editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
430                         } else {
431                             // Show the edit bookmark dialog.
432                             DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
433                             editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
434                         }
435                         break;
436
437                     case R.id.delete_bookmark:
438                         // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
439                         deletingBookmarks = true;
440
441                         // Get an array of the selected row IDs.
442                         final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
443
444                         // 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.
445                         selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
446
447                         // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
448                         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
449
450                         // Update the list view.
451                         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
452
453                         // Show a Snackbar with the number of deleted bookmarks.
454                         bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.bookmarks_deleted) + "  " + selectedBookmarksIdsLongArray.length,
455                                 Snackbar.LENGTH_LONG)
456                                 .setAction(R.string.undo, view -> {
457                                     // Do nothing because everything will be handled by `onDismissed()` below.
458                                 })
459                                 .addCallback(new Snackbar.Callback() {
460                                     @SuppressLint("SwitchIntDef")  // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
461                                     @Override
462                                     public void onDismissed(Snackbar snackbar, int event) {
463                                         switch (event) {
464                                             // The user pushed the `Undo` button.
465                                             case Snackbar.Callback.DISMISS_EVENT_ACTION:
466                                                 // Update the bookmarks cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
467                                                 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
468
469                                                 // Update the list view.
470                                                 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
471
472                                                 // Re-select the previously selected bookmarks.
473                                                 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
474                                                     bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
475                                                 }
476                                                 break;
477
478                                             // The Snackbar was dismissed without the `Undo` button being pushed.
479                                             default:
480                                                 // Delete each selected bookmark.
481                                                 for (long databaseIdLong : selectedBookmarksIdsLongArray) {
482                                                     // Convert `databaseIdLong` to an int.
483                                                     int databaseIdInt = (int) databaseIdLong;
484
485                                                     // Delete the contents of the folder if the selected bookmark is a folder.
486                                                     if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
487                                                         deleteBookmarkFolderContents(databaseIdInt);
488                                                     }
489
490                                                     // Delete the selected bookmark.
491                                                     bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
492                                                 }
493
494                                                 // Update the display order.
495                                                 for (int i = 0; i < bookmarksListView.getCount(); i++) {
496                                                     // Get the database ID for the current bookmark.
497                                                     int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
498
499                                                     // Move `bookmarksCursor` to the current bookmark position.
500                                                     bookmarksCursor.moveToPosition(i);
501
502                                                     // Update the display order only if it is not correct in the database.
503                                                     if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
504                                                         bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
505                                                     }
506                                                 }
507                                         }
508
509                                         // Reset the deleting bookmarks flag.
510                                         deletingBookmarks = false;
511
512                                         // Enable the delete bookmarks menu item.
513                                         deleteBookmarksMenuItem.setEnabled(true);
514
515                                         // Close the activity if back has been pressed.
516                                         if (closeActivityAfterDismissingSnackbar) {
517                                             onBackPressed();
518                                         }
519                                     }
520                                 });
521
522                         //Show the Snackbar.
523                         bookmarksDeletedSnackbar.show();
524                         break;
525
526                     case R.id.context_menu_select_all_bookmarks:
527                         // Get the total number of bookmarks.
528                         int numberOfBookmarks = bookmarksListView.getCount();
529
530                         // Select them all.
531                         for (int i = 0; i < numberOfBookmarks; i++) {
532                             bookmarksListView.setItemChecked(i, true);
533                         }
534                         break;
535                 }
536
537                 // Consume the click.
538                 return true;
539             }
540
541             @Override
542             public void onDestroyActionMode(ActionMode mode) {
543                 // Do nothing.
544             }
545         });
546
547         // Get handles for the `FloatingActionButtons`.
548         FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
549         FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
550
551         // Set the create new bookmark folder FAB to display the `AlertDialog`.
552         createBookmarkFolderFab.setOnClickListener(v -> {
553             // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`.
554             DialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog();
555             createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder));
556         });
557
558         // Set the create new bookmark FAB to display the `AlertDialog`.
559         createBookmarkFab.setOnClickListener(view -> {
560             // Instantiate the create bookmark dialog.
561             DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebViewUrl, currentWebViewTitle, MainWebViewActivity.favoriteIconBitmap);
562
563             // Display the create bookmark dialog.
564             createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark));
565         });
566     }
567
568     @Override
569     public void onRestart() {
570         // Run the default commands.
571         super.onRestart();
572
573         // Update the list view if returning from the bookmarks database view activity.
574         if (restartFromBookmarksDatabaseViewActivity) {
575             // Load the current folder in the list view.
576             loadFolder();
577
578             // Reset `restartFromBookmarksDatabaseViewActivity`.
579             restartFromBookmarksDatabaseViewActivity = false;
580         }
581     }
582
583     @Override
584     public boolean onCreateOptionsMenu(Menu menu) {
585         // Inflate the menu.
586         getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
587
588         // Success.
589         return true;
590     }
591
592     @Override
593     public boolean onOptionsItemSelected(MenuItem menuItem) {
594         switch (menuItem.getItemId()) {
595             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
596                 if (currentFolder.isEmpty()) {  // Currently in the home folder.
597                     // Run the back commands.
598                     onBackPressed();
599                 } else {  // Currently in a subfolder.
600                     // Place the former parent folder in `currentFolder`.
601                     currentFolder = bookmarksDatabaseHelper.getParentFolderName(currentFolder);
602
603                     // Load the new folder.
604                     loadFolder();
605                 }
606                 break;
607
608             case R.id.options_menu_select_all_bookmarks:
609                 // Get the total number of bookmarks.
610                 int numberOfBookmarks = bookmarksListView.getCount();
611
612                 // Select them all.
613                 for (int i = 0; i < numberOfBookmarks; i++) {
614                     bookmarksListView.setItemChecked(i, true);
615                 }
616                 break;
617
618             case R.id.bookmarks_database_view:
619                 // Launch `BookmarksDatabaseViewActivity`.
620                 Intent bookmarksDatabaseViewIntent = new Intent(this, BookmarksDatabaseViewActivity.class);
621                 startActivity(bookmarksDatabaseViewIntent);
622                 break;
623         }
624         return true;
625     }
626
627     @Override
628     public void onBackPressed() {
629         // Check to see if a snackbar is currently displayed.  If so, it must be closed before exiting so that a pending delete is completed before reloading the list view in the bookmarks drawer.
630         if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) {  // Close the bookmarks deleted snackbar before going home.
631             // Set the close flag.
632             closeActivityAfterDismissingSnackbar = true;
633
634             // Dismiss the snackbar.
635             bookmarksDeletedSnackbar.dismiss();
636         } else {  // Go home immediately.
637             // Update the bookmarks folder for the bookmarks drawer in the main WebView activity.
638             MainWebViewActivity.currentBookmarksFolder = currentFolder;
639
640             // Close the bookmarks drawer and reload the bookmarks ListView when returning to the main WebView activity.
641             MainWebViewActivity.restartFromBookmarksActivity = true;
642
643             // Exit the bookmarks activity.
644             super.onBackPressed();
645         }
646     }
647
648     @Override
649     public void onCreateBookmark(DialogFragment dialogFragment) {
650         // Get the views from the dialog fragment.
651         EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
652         EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
653
654         // Extract the strings from the edit texts.
655         String bookmarkNameString = createBookmarkNameEditText.getText().toString();
656         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
657
658         // Get a copy of the favorite icon bitmap.
659         Bitmap favoriteIcon = MainWebViewActivity.favoriteIconBitmap;
660
661         // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
662         if ((favoriteIcon.getHeight() > 256) || (favoriteIcon.getWidth() > 256)) {
663             favoriteIcon = Bitmap.createScaledBitmap(favoriteIcon, 256, 256, true);
664         }
665
666         // Create a favorite icon byte array output stream.
667         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
668
669         // Convert the favorite icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
670         favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
671
672         // Convert the favorite icon byte array stream to a byte array.
673         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
674
675         // Display the new bookmark below the current items in the (0 indexed) list.
676         int newBookmarkDisplayOrder = bookmarksListView.getCount();
677
678         // Create the bookmark.
679         bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
680
681         // Update the bookmarks cursor with the current contents of this folder.
682         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
683
684         // Update the `ListView`.
685         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
686
687         // Scroll to the new bookmark.
688         bookmarksListView.setSelection(newBookmarkDisplayOrder);
689     }
690
691     @Override
692     public void onCreateBookmarkFolder(DialogFragment dialogFragment) {
693         // Get handles for the views in the dialog fragment.
694         EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
695         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
696         ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
697
698         // Get new folder name string.
699         String folderNameString = createFolderNameEditText.getText().toString();
700
701         // Create a folder icon bitmap.
702         Bitmap folderIconBitmap;
703
704         // Set the folder icon bitmap according to the dialog.
705         if (defaultFolderIconRadioButton.isChecked()) {  // Use the default folder icon.
706             // Get the default folder icon drawable.
707             Drawable folderIconDrawable = folderIconImageView.getDrawable();
708
709             // Convert the folder icon drawable to a bitmap drawable.
710             BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
711
712             // Convert the folder icon bitmap drawable to a bitmap.
713             folderIconBitmap = folderIconBitmapDrawable.getBitmap();
714         } else {  // Use the WebView favorite icon.
715             // Get a copy of the favorite icon bitmap.
716             folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
717
718             // Scale the folder icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
719             if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
720                 folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
721             }
722         }
723
724         // Create a folder icon byte array output stream.
725         ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
726
727         // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
728         folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
729
730         // Convert the folder icon byte array stream to a byte array.
731         byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
732
733         // Move all the bookmarks down one in the display order.
734         for (int i = 0; i < bookmarksListView.getCount(); i++) {
735             int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
736             bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
737         }
738
739         // Create the folder, which will be placed at the top of the `ListView`.
740         bookmarksDatabaseHelper.createFolder(folderNameString, currentFolder, folderIconByteArray);
741
742         // Update the bookmarks cursor with the current contents of this folder.
743         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
744
745         // Update the `ListView`.
746         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
747
748         // Scroll to the new folder.
749         bookmarksListView.setSelection(0);
750     }
751
752     @Override
753     public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
754         // Get handles for the views from `dialogFragment`.
755         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
756         EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
757         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
758
759         // Store the bookmark strings.
760         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
761         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
762
763         // Update the bookmark.
764         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
765             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
766         } else {  // Update the bookmark using the `WebView` favorite icon.
767             // Get a copy of the favorite icon bitmap.
768             Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
769
770             // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
771             if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
772                 favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
773             }
774
775             // Create a favorite icon byte array output stream.
776             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
777
778             // Convert the favorite icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
779             favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
780
781             // Convert the favorite icon byte array stream to a byte array.
782             byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
783
784             //  Update the bookmark and the favorite icon.
785             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray);
786         }
787
788         // Close the contextual action mode.
789         contextualActionMode.finish();
790
791         // Update the bookmarks cursor with the contents of the current folder.
792         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
793
794         // Update the `ListView`.
795         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
796     }
797
798     @Override
799     public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId) {
800         // Get handles for the views from `dialogFragment`.
801         RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
802         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
803         ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
804         EditText editFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
805
806         // Get the new folder name.
807         String newFolderNameString = editFolderNameEditText.getText().toString();
808
809         // Check if the favorite icon has changed.
810         if (currentFolderIconRadioButton.isChecked()) {  // Only the name has changed.
811             // Update the name in the database.
812             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
813         } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) {  // Only the icon has changed.
814             // Create the new folder icon Bitmap.
815             Bitmap folderIconBitmap;
816
817             // Populate the new folder icon bitmap.
818             if (defaultFolderIconRadioButton.isChecked()) {
819                 // Get the default folder icon drawable.
820                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
821
822                 // Convert the folder icon drawable to a bitmap drawable.
823                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
824
825                 // Convert the folder icon bitmap drawable to a bitmap.
826                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
827             } else {  // Use the WebView favorite icon.
828                 // Get a copy of the favorite icon bitmap.
829                 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
830
831                 // Scale the folder icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
832                 if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
833                     folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
834                 }
835             }
836
837             // Create a folder icon byte array output stream.
838             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
839
840             // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
841             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
842
843             // Convert the folder icon byte array stream to a byte array.
844             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
845
846             // Update the folder icon in the database.
847             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
848         } else {  // The folder icon and the name have changed.
849             // Instantiate the new folder icon `Bitmap`.
850             Bitmap folderIconBitmap;
851
852             // Populate the new folder icon bitmap.
853             if (defaultFolderIconRadioButton.isChecked()) {
854                 // Get the default folder icon drawable.
855                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
856
857                 // Convert the folder icon drawable to a bitmap drawable.
858                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
859
860                 // Convert the folder icon bitmap drawable to a bitmap.
861                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
862             } else {  // Use the WebView favorite icon.
863                 // Get a copy of the favorite icon bitmap.
864                 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
865
866                 // Scale the folder icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
867                 if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
868                     folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
869                 }
870             }
871
872             // Create a folder icon byte array output stream.
873             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
874
875             // Convert the folder icon bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
876             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
877
878             // Convert the folder icon byte array stream to a byte array.
879             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
880
881             // Update the folder name and icon in the database.
882             bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
883         }
884
885         // Update the bookmarks cursor with the current contents of this folder.
886         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
887
888         // Update the `ListView`.
889         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
890
891         // Close the contextual action mode.
892         contextualActionMode.finish();
893     }
894
895     @Override
896     public void onMoveToFolder(DialogFragment dialogFragment) {
897         // Get a handle for the `ListView` from `dialogFragment`.
898         ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview);
899
900         // Store a long array of the selected folders.
901         long[] newFolderLongArray = folderListView.getCheckedItemIds();
902
903         // Get the new folder database ID.  Only one folder will be selected.
904         int newFolderDatabaseId = (int) newFolderLongArray[0];
905
906         // Instantiate `newFolderName`.
907         String newFolderName;
908
909         // Set the new folder name.
910         if (newFolderDatabaseId == 0) {
911             // The new folder is the home folder, represented as `""` in the database.
912             newFolderName = "";
913         } else {
914             // Get the new folder name from the database.
915             newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
916         }
917
918         // Get a long array with the the database ID of the selected bookmarks.
919         long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
920
921         // Move each of the selected bookmarks to the new folder.
922         for (long databaseIdLong : selectedBookmarksLongArray) {
923             // Get `databaseIdInt` for each selected bookmark.
924             int databaseIdInt = (int) databaseIdLong;
925
926             // Move the selected bookmark to the new folder.
927             bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
928         }
929
930         // Update the bookmarks cursor with the current contents of this folder.
931         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
932
933         // Update the `ListView`.
934         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
935
936         // Close the contextual app bar.
937         contextualActionMode.finish();
938     }
939
940     private void deleteBookmarkFolderContents(int databaseId) {
941         // Get the name of the folder.
942         String folderName = bookmarksDatabaseHelper.getFolderName(databaseId);
943
944         // Get the contents of the folder.
945         Cursor folderCursor = bookmarksDatabaseHelper.getBookmarkIDs(folderName);
946
947         // Delete each of the bookmarks in the folder.
948         for (int i = 0; i < folderCursor.getCount(); i++) {
949             // Move `folderCursor` to the current row.
950             folderCursor.moveToPosition(i);
951
952             // Get the database ID of the item.
953             int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
954
955             // If this is a folder, recursively delete the contents first.
956             if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) {
957                 deleteBookmarkFolderContents(itemDatabaseId);
958             }
959
960             // Delete the bookmark.
961             bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId);
962         }
963     }
964
965     private void updateMoveIcons() {
966         // Get a long array of the selected bookmarks.
967         long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
968
969         // Get the database IDs for the first, last, and selected bookmarks.
970         int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
971         int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0);
972         // bookmarksListView is 0 indexed.
973         int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1);
974
975         // Update the move bookmark up `MenuItem`.
976         if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) {  // The selected bookmark is in the first position.
977             // Disable the move bookmark up `MenuItem`.
978             moveBookmarkUpMenuItem.setEnabled(false);
979
980             //  Set the move bookmark up icon to be ghosted.
981             moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled);
982         } else {  // The selected bookmark is not in the first position.
983             // Enable the move bookmark up `MenuItem`.
984             moveBookmarkUpMenuItem.setEnabled(true);
985
986             // Set the icon according to the theme.
987             if (MainWebViewActivity.darkTheme) {
988                 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark);
989             } else {
990                 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light);
991             }
992         }
993
994         // Update the move bookmark down `MenuItem`.
995         if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) {  // The selected bookmark is in the last position.
996             // Disable the move bookmark down `MenuItem`.
997             moveBookmarkDownMenuItem.setEnabled(false);
998
999             // Set the move bookmark down icon to be ghosted.
1000             moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled);
1001         } else {  // The selected bookmark is not in the last position.
1002             // Enable the move bookmark down `MenuItem`.
1003             moveBookmarkDownMenuItem.setEnabled(true);
1004
1005             // Set the icon according to the theme.
1006             if (MainWebViewActivity.darkTheme) {
1007                 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark);
1008             } else {
1009                 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light);
1010             }
1011         }
1012     }
1013
1014     private void scrollBookmarks(int selectedBookmarkPosition) {
1015         // Get the first and last visible bookmark positions.
1016         int firstVisibleBookmarkPosition = bookmarksListView.getFirstVisiblePosition();
1017         int lastVisibleBookmarkPosition = bookmarksListView.getLastVisiblePosition();
1018
1019         // Calculate the number of bookmarks per screen.
1020         int numberOfBookmarksPerScreen = lastVisibleBookmarkPosition - firstVisibleBookmarkPosition;
1021
1022         // Scroll with the moved bookmark if necessary.
1023         if (selectedBookmarkPosition <= firstVisibleBookmarkPosition) {  // The selected bookmark position is at or above the top of the screen.
1024             // Scroll to the selected bookmark position.
1025             bookmarksListView.setSelection(selectedBookmarkPosition);
1026         } else if (selectedBookmarkPosition >= (lastVisibleBookmarkPosition - 1)) {  // The selected bookmark is at or below the bottom of the screen.
1027             // The `-1` handles partial bookmarks displayed at the bottom of the list view.  This command scrolls to display the selected bookmark at the bottom of the screen.
1028             // `+1` assures that the entire bookmark will be displayed in situations where only a partial bookmark fits at the bottom of the list view.
1029             bookmarksListView.setSelection(selectedBookmarkPosition - numberOfBookmarksPerScreen + 1);
1030         }
1031     }
1032
1033     private void loadFolder() {
1034         // Update bookmarks cursor with the contents of the bookmarks database for the current folder.
1035         bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
1036
1037         // Setup a `CursorAdapter`.  `this` specifies the `Context`.  `false` disables `autoRequery`.
1038         bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
1039             @Override
1040             public View newView(Context context, Cursor cursor, ViewGroup parent) {
1041                 // Inflate the individual item layout.  `false` does not attach it to the root.
1042                 return getLayoutInflater().inflate(R.layout.bookmarks_activity_item_linearlayout, parent, false);
1043             }
1044
1045             @Override
1046             public void bindView(View view, Context context, Cursor cursor) {
1047                 // Get handles for the views.
1048                 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon);
1049                 TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name);
1050
1051                 // Get the favorite icon byte array from the `Cursor`.
1052                 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
1053
1054                 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
1055                 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
1056
1057                 // Display the bitmap in `bookmarkFavoriteIcon`.
1058                 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
1059
1060                 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
1061                 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
1062                 bookmarkNameTextView.setText(bookmarkNameString);
1063
1064                 // Make the font bold for folders.
1065                 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {
1066                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD);
1067                 } else {  // Reset the font to default for normal bookmarks.
1068                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
1069                 }
1070             }
1071         };
1072
1073         // Populate the list view with the adapter.
1074         bookmarksListView.setAdapter(bookmarksCursorAdapter);
1075
1076         // Set the `AppBar` title.
1077         if (currentFolder.isEmpty()) {
1078             appBar.setTitle(R.string.bookmarks);
1079         } else {
1080             appBar.setTitle(currentFolder);
1081         }
1082     }
1083
1084     @Override
1085     public void onDestroy() {
1086         // Close the bookmarks cursor and database.
1087         bookmarksCursor.close();
1088         bookmarksDatabaseHelper.close();
1089
1090         // Run the default commands.
1091         super.onDestroy();
1092     }
1093 }