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