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