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