2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.activities;
22 import android.annotation.SuppressLint;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.database.Cursor;
27 import android.graphics.Bitmap;
28 import android.graphics.BitmapFactory;
29 import android.graphics.Typeface;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.os.Bundle;
33 import android.util.SparseBooleanArray;
34 import android.view.ActionMode;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.WindowManager;
40 import android.widget.AbsListView;
41 import android.widget.CursorAdapter;
42 import android.widget.EditText;
43 import android.widget.ImageView;
44 import android.widget.ListView;
45 import android.widget.RadioButton;
46 import android.widget.TextView;
48 import androidx.appcompat.app.ActionBar;
49 import androidx.appcompat.app.AppCompatActivity;
50 import androidx.appcompat.widget.Toolbar; // The AndroidX toolbar must be used until the minimum API is >= 21.
51 import androidx.core.app.NavUtils;
52 import androidx.fragment.app.DialogFragment;
54 import com.google.android.material.floatingactionbutton.FloatingActionButton;
55 import com.google.android.material.snackbar.Snackbar;
57 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
58 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
59 import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
60 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
61 import com.stoutner.privacybrowser.dialogs.MoveToFolderDialog;
62 import com.stoutner.privacybrowser.R;
63 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
65 import java.io.ByteArrayOutputStream;
67 public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener,
68 EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener {
70 // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog` and `BookmarksDatabaseViewActivity`.
71 // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
72 // and `loadFolder()`.
73 public static String currentFolder;
75 // `checkedItemIds` is public static so it can be accessed from `MoveToFolderDialog`. It is also used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
76 // and `updateMoveIcons()`.
77 public static long[] checkedItemIds;
79 // `restartFromBookmarksDatabaseViewActivity` is public static so it can be accessed from `BookmarksDatabaseViewActivity`. It is also used in `onRestart()`.
80 public static boolean restartFromBookmarksDatabaseViewActivity;
82 // The current WebView strings are public static so they can be updated from `MainWebViewActivity`. They are use in `onCreate()`.
83 public static String currentWebViewUrl;
84 public static String currentWebViewTitle;
87 // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`,
88 // `onMoveToFolder()`, `deleteBookmarkFolderContents()`, `loadFolder()`, and `onDestroy()`.
89 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
91 // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`,
92 // `updateMoveIcons()`, `scrollBookmarks()`, and `loadFolder()`.
93 private ListView bookmarksListView;
95 // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`,
96 // `loadFolder()`, and `onDestroy()`.
97 private Cursor bookmarksCursor;
99 // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`.
100 private CursorAdapter bookmarksCursorAdapter;
102 // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`.
103 private ActionMode contextualActionMode;
105 // `appBar` is used in `onCreate()` and `loadFolder()`.
106 private ActionBar appBar;
108 // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`.
109 private String oldFolderNameString;
111 // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
112 private MenuItem moveBookmarkUpMenuItem;
114 // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
115 private MenuItem moveBookmarkDownMenuItem;
117 // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
118 private Snackbar bookmarksDeletedSnackbar;
120 // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
121 private boolean closeActivityAfterDismissingSnackbar;
124 protected void onCreate(Bundle savedInstanceState) {
125 // Disable screenshots if not allowed.
126 if (!MainWebViewActivity.allowScreenshots) {
127 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
130 // Set the activity theme.
131 if (MainWebViewActivity.darkTheme) {
132 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
134 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
137 // Run the default commands.
138 super.onCreate(savedInstanceState);
140 // Get the intent that launched the activity.
141 Intent launchingIntent = getIntent();
143 // Set the current folder variable.
144 if (launchingIntent.getStringExtra("Current Folder") != null) { // Set the current folder from the intent.
145 currentFolder = launchingIntent.getStringExtra("Current Folder");
146 } else { // Set the current folder to be `""`, which is the home folder.
150 // Set the content view.
151 setContentView(R.layout.bookmarks_coordinatorlayout);
153 // The AndroidX toolbar must be used until the minimum API is >= 21.
154 final Toolbar toolbar = findViewById(R.id.bookmarks_toolbar);
155 setSupportActionBar(toolbar);
157 // Get a handle for the activity, the app bar, and the ListView.
158 final Activity bookmarksActivity = this;
159 appBar = getSupportActionBar();
160 bookmarksListView = findViewById(R.id.bookmarks_listview);
162 // Remove the incorrect lint warning that `appBar` might be null.
163 assert appBar != null;
165 // Display the home arrow on the app bar.
166 appBar.setDisplayHomeAsUpEnabled(true);
168 // Initialize the database helper. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
169 // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
170 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
172 // Load the home folder.
175 // Set a listener so that tapping a list item loads the URL or folder.
176 bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
177 // Convert the id from long to int to match the format of the bookmarks database.
178 int databaseID = (int) id;
180 // Get the bookmark cursor for this ID and move it to the first row.
181 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseID);
182 bookmarkCursor.moveToFirst();
184 // Act upon the bookmark according to the type.
185 if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
186 // Update the current folder.
187 currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
189 // Load the new folder.
191 } else { // The selected bookmark is not a folder.
192 // Get the bookmark URL and assign it to `formattedUrlString`.
193 MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
195 // Set `MainWebViewActivity` to load the new URL on restart.
196 MainWebViewActivity.loadUrlOnRestart = true;
198 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
199 MainWebViewActivity.currentBookmarksFolder = currentFolder;
201 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
202 MainWebViewActivity.restartFromBookmarksActivity = true;
204 // Return to `MainWebViewActivity`.
205 NavUtils.navigateUpFromSameTask(bookmarksActivity);
208 // Close the `Cursor`.
209 bookmarkCursor.close();
212 // Handle long presses on the list view.
213 bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
214 // Instantiate the common variables.
215 MenuItem editBookmarkMenuItem;
216 MenuItem deleteBookmarksMenuItem;
217 MenuItem selectAllBookmarksMenuItem;
218 boolean deletingBookmarks;
221 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
222 // Inflate the menu for the contextual app bar.
223 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
226 if (currentFolder.isEmpty()) { // Use `R.string.bookmarks` if in the home folder.
227 mode.setTitle(R.string.bookmarks);
228 } else { // Use the current folder name as the title.
229 mode.setTitle(currentFolder);
232 // Get handles for menu items that need to be selectively disabled.
233 moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up);
234 moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down);
235 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
236 deleteBookmarksMenuItem = menu.findItem(R.id.delete_bookmark);
237 selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks);
239 // Disable the delete bookmarks menu item if a delete is pending.
240 deleteBookmarksMenuItem.setEnabled(!deletingBookmarks);
242 // Store a handle for the contextual action mode so it can be closed programatically.
243 contextualActionMode = mode;
250 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
251 // Get a handle for the move to folder menu item.
252 MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder);
254 // Get a Cursor with all of the folders.
255 Cursor folderCursor = bookmarksDatabaseHelper.getAllFolders();
257 // Enable the move to folder menu item if at least one folder exists.
258 moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0);
265 public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
266 // Get the number of selected bookmarks.
267 int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
269 // Adjust the ActionMode and the menu according to the number of selected bookmarks.
270 if (numberOfSelectedBookmarks == 1) { // One bookmark is selected.
271 // List the number of selected bookmarks in the subtitle.
272 mode.setSubtitle(getString(R.string.selected) + " 1");
274 // Show the `Move Up`, `Move Down`, and `Edit` options.
275 moveBookmarkUpMenuItem.setVisible(true);
276 moveBookmarkDownMenuItem.setVisible(true);
277 editBookmarkMenuItem.setVisible(true);
279 // Update the enabled status of the move icons.
281 } else { // More than one bookmark is selected.
282 // List the number of selected bookmarks in the subtitle.
283 mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks);
285 // Hide non-applicable `MenuItems`.
286 moveBookmarkUpMenuItem.setVisible(false);
287 moveBookmarkDownMenuItem.setVisible(false);
288 editBookmarkMenuItem.setVisible(false);
291 // Do not show the select all menu item if all the bookmarks are already checked.
292 if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) {
293 selectAllBookmarksMenuItem.setVisible(false);
295 selectAllBookmarksMenuItem.setVisible(true);
300 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
301 // Instantiate the common variables.
302 int selectedBookmarkPosition;
303 int selectedBookmarkNewPosition;
304 final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray;
306 switch (item.getItemId()) {
307 case R.id.move_bookmark_up:
308 // Get the array of checked bookmark positions.
309 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
311 // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_up` is enabled.
312 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
314 // Calculate the new position of the selected bookmark.
315 selectedBookmarkNewPosition = selectedBookmarkPosition - 1;
317 // Iterate through the bookmarks.
318 for (int i = 0; i < bookmarksListView.getCount(); i++) {
319 // Get the database ID for the current bookmark.
320 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
322 // Update the display order for the current bookmark.
323 if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
324 // Move the current bookmark up one.
325 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
326 } else if ((i + 1) == selectedBookmarkPosition){ // The current bookmark is immediately above the selected bookmark.
327 // Move the current bookmark down one.
328 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
329 } else { // The current bookmark is not changing positions.
330 // Move `bookmarksCursor` to the current bookmark position.
331 bookmarksCursor.moveToPosition(i);
333 // Update the display order only if it is not correct in the database.
334 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
335 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
340 // Update the bookmarks cursor with the current contents of the bookmarks database.
341 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
343 // Update the `ListView`.
344 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
346 // Scroll with the bookmark.
347 scrollBookmarks(selectedBookmarkNewPosition);
349 // Update the enabled status of the move icons.
353 case R.id.move_bookmark_down:
354 // Get the array of checked bookmark positions.
355 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
357 // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_down` is enabled.
358 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
360 // Calculate the new position of the selected bookmark.
361 selectedBookmarkNewPosition = selectedBookmarkPosition + 1;
363 // Iterate through the bookmarks.
364 for (int i = 0; i <bookmarksListView.getCount(); i++) {
365 // Get the database ID for the current bookmark.
366 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
368 // Update the display order for the current bookmark.
369 if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
370 // Move the current bookmark down one.
371 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
372 } else if ((i - 1) == selectedBookmarkPosition) { // The current bookmark is immediately below the selected bookmark.
373 // Move the bookmark below the selected bookmark up one.
374 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
375 } else { // The current bookmark is not changing positions.
376 // Move `bookmarksCursor` to the current bookmark position.
377 bookmarksCursor.moveToPosition(i);
379 // Update the display order only if it is not correct in the database.
380 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
381 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
386 // Update the bookmarks cursor with the current contents of the bookmarks database.
387 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
389 // Update the `ListView`.
390 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
392 // Scroll with the bookmark.
393 scrollBookmarks(selectedBookmarkNewPosition);
395 // Update the enabled status of the move icons.
399 case R.id.move_to_folder:
400 // Store `checkedItemIds` for use by the `AlertDialog`.
401 checkedItemIds = bookmarksListView.getCheckedItemIds();
403 // Show the `MoveToFolderDialog` `AlertDialog` and name the instance `@string/move_to_folder
404 DialogFragment moveToFolderDialog = new MoveToFolderDialog();
405 moveToFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.move_to_folder));
408 case R.id.edit_bookmark:
409 // Get the array of checked bookmark positions.
410 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
412 // Get the position of the selected bookmark. Only one bookmark is selected when `edit_bookmark_down` is enabled.
413 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
415 // Move the `Cursor` to the selected position and find out if it is a folder.
416 bookmarksCursor.moveToPosition(selectedBookmarkPosition);
417 boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
419 // Get the selected bookmark database ID.
420 int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
422 // Show the edit bookmark or edit bookmark folder dialog.
424 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
425 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
427 // Show the edit bookmark folder dialog.
428 DialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
429 editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
431 // Show the edit bookmark dialog.
432 DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
433 editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
437 case R.id.delete_bookmark:
438 // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
439 deletingBookmarks = true;
441 // Get an array of the selected row IDs.
442 final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
444 // Get an array of checked bookmarks. `.clone()` makes a copy that won't change if the list view is reloaded, which is needed for re-selecting the bookmarks on undelete.
445 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
447 // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
448 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolder);
450 // Update the list view.
451 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
453 // Show a Snackbar with the number of deleted bookmarks.
454 bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.bookmarks_deleted) + " " + selectedBookmarksIdsLongArray.length,
455 Snackbar.LENGTH_LONG)
456 .setAction(R.string.undo, view -> {
457 // Do nothing because everything will be handled by `onDismissed()` below.
459 .addCallback(new Snackbar.Callback() {
460 @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
462 public void onDismissed(Snackbar snackbar, int event) {
464 // The user pushed the `Undo` button.
465 case Snackbar.Callback.DISMISS_EVENT_ACTION:
466 // Update the bookmarks cursor with the current contents of the bookmarks database, including the "deleted" bookmarks.
467 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
469 // Update the list view.
470 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
472 // Re-select the previously selected bookmarks.
473 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
474 bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
478 // The Snackbar was dismissed without the `Undo` button being pushed.
480 // Delete each selected bookmark.
481 for (long databaseIdLong : selectedBookmarksIdsLongArray) {
482 // Convert `databaseIdLong` to an int.
483 int databaseIdInt = (int) databaseIdLong;
485 // Delete the contents of the folder if the selected bookmark is a folder.
486 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
487 deleteBookmarkFolderContents(databaseIdInt);
490 // Delete the selected bookmark.
491 bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
494 // Update the display order.
495 for (int i = 0; i < bookmarksListView.getCount(); i++) {
496 // Get the database ID for the current bookmark.
497 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
499 // Move `bookmarksCursor` to the current bookmark position.
500 bookmarksCursor.moveToPosition(i);
502 // Update the display order only if it is not correct in the database.
503 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
504 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
509 // Reset the deleting bookmarks flag.
510 deletingBookmarks = false;
512 // Enable the delete bookmarks menu item.
513 deleteBookmarksMenuItem.setEnabled(true);
515 // Close the activity if back has been pressed.
516 if (closeActivityAfterDismissingSnackbar) {
523 bookmarksDeletedSnackbar.show();
526 case R.id.context_menu_select_all_bookmarks:
527 // Get the total number of bookmarks.
528 int numberOfBookmarks = bookmarksListView.getCount();
531 for (int i = 0; i < numberOfBookmarks; i++) {
532 bookmarksListView.setItemChecked(i, true);
537 // Consume the click.
542 public void onDestroyActionMode(ActionMode mode) {
547 // Get handles for the `FloatingActionButtons`.
548 FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
549 FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
551 // Set the create new bookmark folder FAB to display the `AlertDialog`.
552 createBookmarkFolderFab.setOnClickListener(v -> {
553 // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`.
554 DialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog();
555 createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder));
558 // Set the create new bookmark FAB to display the `AlertDialog`.
559 createBookmarkFab.setOnClickListener(view -> {
560 // Instantiate the create bookmark dialog.
561 DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebViewUrl, currentWebViewTitle, MainWebViewActivity.favoriteIconBitmap);
563 // Display the create bookmark dialog.
564 createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark));
569 public void onRestart() {
570 // Run the default commands.
573 // Update the list view if returning from the bookmarks database view activity.
574 if (restartFromBookmarksDatabaseViewActivity) {
575 // Load the current folder in the list view.
578 // Reset `restartFromBookmarksDatabaseViewActivity`.
579 restartFromBookmarksDatabaseViewActivity = false;
584 public boolean onCreateOptionsMenu(Menu menu) {
586 getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
593 public boolean onOptionsItemSelected(MenuItem menuItem) {
594 switch (menuItem.getItemId()) {
595 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
596 if (currentFolder.isEmpty()) { // Currently in the home folder.
597 // Run the back commands.
599 } else { // Currently in a subfolder.
600 // Place the former parent folder in `currentFolder`.
601 currentFolder = bookmarksDatabaseHelper.getParentFolderName(currentFolder);
603 // Load the new folder.
608 case R.id.options_menu_select_all_bookmarks:
609 // Get the total number of bookmarks.
610 int numberOfBookmarks = bookmarksListView.getCount();
613 for (int i = 0; i < numberOfBookmarks; i++) {
614 bookmarksListView.setItemChecked(i, true);
618 case R.id.bookmarks_database_view:
619 // Launch `BookmarksDatabaseViewActivity`.
620 Intent bookmarksDatabaseViewIntent = new Intent(this, BookmarksDatabaseViewActivity.class);
621 startActivity(bookmarksDatabaseViewIntent);
628 public void onBackPressed() {
629 // Check to see if a snackbar is currently displayed. If so, it must be closed before exiting so that a pending delete is completed before reloading the list view in the bookmarks drawer.
630 if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home.
631 // Set the close flag.
632 closeActivityAfterDismissingSnackbar = true;
634 // Dismiss the snackbar.
635 bookmarksDeletedSnackbar.dismiss();
636 } else { // Go home immediately.
637 // Update the bookmarks folder for the bookmarks drawer in the main WebView activity.
638 MainWebViewActivity.currentBookmarksFolder = currentFolder;
640 // Close the bookmarks drawer and reload the bookmarks ListView when returning to the main WebView activity.
641 MainWebViewActivity.restartFromBookmarksActivity = true;
643 // Exit the bookmarks activity.
644 super.onBackPressed();
649 public void onCreateBookmark(DialogFragment dialogFragment) {
650 // Get the views from the dialog fragment.
651 EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
652 EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
654 // Extract the strings from the edit texts.
655 String bookmarkNameString = createBookmarkNameEditText.getText().toString();
656 String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
658 // Get a copy of the favorite icon bitmap.
659 Bitmap favoriteIcon = MainWebViewActivity.favoriteIconBitmap;
661 // Scale the favorite icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
662 if ((favoriteIcon.getHeight() > 256) || (favoriteIcon.getWidth() > 256)) {
663 favoriteIcon = Bitmap.createScaledBitmap(favoriteIcon, 256, 256, true);
666 // Create a favorite icon byte array output stream.
667 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
669 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
670 favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
672 // Convert the favorite icon byte array stream to a byte array.
673 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
675 // Display the new bookmark below the current items in the (0 indexed) list.
676 int newBookmarkDisplayOrder = bookmarksListView.getCount();
678 // Create the bookmark.
679 bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
681 // Update the bookmarks cursor with the current contents of this folder.
682 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
684 // Update the `ListView`.
685 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
687 // Scroll to the new bookmark.
688 bookmarksListView.setSelection(newBookmarkDisplayOrder);
692 public void onCreateBookmarkFolder(DialogFragment dialogFragment) {
693 // Get handles for the views in the dialog fragment.
694 EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
695 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
696 ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
698 // Get new folder name string.
699 String folderNameString = createFolderNameEditText.getText().toString();
701 // Create a folder icon bitmap.
702 Bitmap folderIconBitmap;
704 // Set the folder icon bitmap according to the dialog.
705 if (defaultFolderIconRadioButton.isChecked()) { // Use the default folder icon.
706 // Get the default folder icon drawable.
707 Drawable folderIconDrawable = folderIconImageView.getDrawable();
709 // Convert the folder icon drawable to a bitmap drawable.
710 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
712 // Convert the folder icon bitmap drawable to a bitmap.
713 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
714 } else { // Use the WebView favorite icon.
715 // Get a copy of the favorite icon bitmap.
716 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
718 // Scale the folder icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
719 if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
720 folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
724 // Create a folder icon byte array output stream.
725 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
727 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
728 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
730 // Convert the folder icon byte array stream to a byte array.
731 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
733 // Move all the bookmarks down one in the display order.
734 for (int i = 0; i < bookmarksListView.getCount(); i++) {
735 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
736 bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
739 // Create the folder, which will be placed at the top of the `ListView`.
740 bookmarksDatabaseHelper.createFolder(folderNameString, currentFolder, folderIconByteArray);
742 // Update the bookmarks cursor with the current contents of this folder.
743 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
745 // Update the `ListView`.
746 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
748 // Scroll to the new folder.
749 bookmarksListView.setSelection(0);
753 public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
754 // Get handles for the views from `dialogFragment`.
755 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
756 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
757 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
759 // Store the bookmark strings.
760 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
761 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
763 // Update the bookmark.
764 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
765 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
766 } else { // Update the bookmark using the `WebView` favorite icon.
767 // Get a copy of the favorite icon bitmap.
768 Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
770 // Scale the favorite icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
771 if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
772 favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
775 // Create a favorite icon byte array output stream.
776 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
778 // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
779 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
781 // Convert the favorite icon byte array stream to a byte array.
782 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
784 // Update the bookmark and the favorite icon.
785 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray);
788 // Close the contextual action mode.
789 contextualActionMode.finish();
791 // Update the bookmarks cursor with the contents of the current folder.
792 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
794 // Update the `ListView`.
795 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
799 public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId) {
800 // Get handles for the views from `dialogFragment`.
801 RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
802 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
803 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
804 EditText editFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
806 // Get the new folder name.
807 String newFolderNameString = editFolderNameEditText.getText().toString();
809 // Check if the favorite icon has changed.
810 if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
811 // Update the name in the database.
812 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
813 } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
814 // Create the new folder icon Bitmap.
815 Bitmap folderIconBitmap;
817 // Populate the new folder icon bitmap.
818 if (defaultFolderIconRadioButton.isChecked()) {
819 // Get the default folder icon drawable.
820 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
822 // Convert the folder icon drawable to a bitmap drawable.
823 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
825 // Convert the folder icon bitmap drawable to a bitmap.
826 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
827 } else { // Use the WebView favorite icon.
828 // Get a copy of the favorite icon bitmap.
829 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
831 // Scale the folder icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
832 if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
833 folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
837 // Create a folder icon byte array output stream.
838 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
840 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
841 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
843 // Convert the folder icon byte array stream to a byte array.
844 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
846 // Update the folder icon in the database.
847 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
848 } else { // The folder icon and the name have changed.
849 // Instantiate the new folder icon `Bitmap`.
850 Bitmap folderIconBitmap;
852 // Populate the new folder icon bitmap.
853 if (defaultFolderIconRadioButton.isChecked()) {
854 // Get the default folder icon drawable.
855 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
857 // Convert the folder icon drawable to a bitmap drawable.
858 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
860 // Convert the folder icon bitmap drawable to a bitmap.
861 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
862 } else { // Use the WebView favorite icon.
863 // Get a copy of the favorite icon bitmap.
864 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
866 // Scale the folder icon bitmap down if it is larger than 256 x 256. Filtering uses bilinear interpolation.
867 if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
868 folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
872 // Create a folder icon byte array output stream.
873 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
875 // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
876 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
878 // Convert the folder icon byte array stream to a byte array.
879 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
881 // Update the folder name and icon in the database.
882 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
885 // Update the bookmarks cursor with the current contents of this folder.
886 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
888 // Update the `ListView`.
889 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
891 // Close the contextual action mode.
892 contextualActionMode.finish();
896 public void onMoveToFolder(DialogFragment dialogFragment) {
897 // Get a handle for the `ListView` from `dialogFragment`.
898 ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview);
900 // Store a long array of the selected folders.
901 long[] newFolderLongArray = folderListView.getCheckedItemIds();
903 // Get the new folder database ID. Only one folder will be selected.
904 int newFolderDatabaseId = (int) newFolderLongArray[0];
906 // Instantiate `newFolderName`.
907 String newFolderName;
909 // Set the new folder name.
910 if (newFolderDatabaseId == 0) {
911 // The new folder is the home folder, represented as `""` in the database.
914 // Get the new folder name from the database.
915 newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
918 // Get a long array with the the database ID of the selected bookmarks.
919 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
921 // Move each of the selected bookmarks to the new folder.
922 for (long databaseIdLong : selectedBookmarksLongArray) {
923 // Get `databaseIdInt` for each selected bookmark.
924 int databaseIdInt = (int) databaseIdLong;
926 // Move the selected bookmark to the new folder.
927 bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
930 // Update the bookmarks cursor with the current contents of this folder.
931 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
933 // Update the `ListView`.
934 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
936 // Close the contextual app bar.
937 contextualActionMode.finish();
940 private void deleteBookmarkFolderContents(int databaseId) {
941 // Get the name of the folder.
942 String folderName = bookmarksDatabaseHelper.getFolderName(databaseId);
944 // Get the contents of the folder.
945 Cursor folderCursor = bookmarksDatabaseHelper.getBookmarkIDs(folderName);
947 // Delete each of the bookmarks in the folder.
948 for (int i = 0; i < folderCursor.getCount(); i++) {
949 // Move `folderCursor` to the current row.
950 folderCursor.moveToPosition(i);
952 // Get the database ID of the item.
953 int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
955 // If this is a folder, recursively delete the contents first.
956 if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) {
957 deleteBookmarkFolderContents(itemDatabaseId);
960 // Delete the bookmark.
961 bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId);
965 private void updateMoveIcons() {
966 // Get a long array of the selected bookmarks.
967 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
969 // Get the database IDs for the first, last, and selected bookmarks.
970 int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
971 int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0);
972 // bookmarksListView is 0 indexed.
973 int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1);
975 // Update the move bookmark up `MenuItem`.
976 if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { // The selected bookmark is in the first position.
977 // Disable the move bookmark up `MenuItem`.
978 moveBookmarkUpMenuItem.setEnabled(false);
980 // Set the move bookmark up icon to be ghosted.
981 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled);
982 } else { // The selected bookmark is not in the first position.
983 // Enable the move bookmark up `MenuItem`.
984 moveBookmarkUpMenuItem.setEnabled(true);
986 // Set the icon according to the theme.
987 if (MainWebViewActivity.darkTheme) {
988 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark);
990 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light);
994 // Update the move bookmark down `MenuItem`.
995 if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { // The selected bookmark is in the last position.
996 // Disable the move bookmark down `MenuItem`.
997 moveBookmarkDownMenuItem.setEnabled(false);
999 // Set the move bookmark down icon to be ghosted.
1000 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled);
1001 } else { // The selected bookmark is not in the last position.
1002 // Enable the move bookmark down `MenuItem`.
1003 moveBookmarkDownMenuItem.setEnabled(true);
1005 // Set the icon according to the theme.
1006 if (MainWebViewActivity.darkTheme) {
1007 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark);
1009 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light);
1014 private void scrollBookmarks(int selectedBookmarkPosition) {
1015 // Get the first and last visible bookmark positions.
1016 int firstVisibleBookmarkPosition = bookmarksListView.getFirstVisiblePosition();
1017 int lastVisibleBookmarkPosition = bookmarksListView.getLastVisiblePosition();
1019 // Calculate the number of bookmarks per screen.
1020 int numberOfBookmarksPerScreen = lastVisibleBookmarkPosition - firstVisibleBookmarkPosition;
1022 // Scroll with the moved bookmark if necessary.
1023 if (selectedBookmarkPosition <= firstVisibleBookmarkPosition) { // The selected bookmark position is at or above the top of the screen.
1024 // Scroll to the selected bookmark position.
1025 bookmarksListView.setSelection(selectedBookmarkPosition);
1026 } else if (selectedBookmarkPosition >= (lastVisibleBookmarkPosition - 1)) { // The selected bookmark is at or below the bottom of the screen.
1027 // The `-1` handles partial bookmarks displayed at the bottom of the list view. This command scrolls to display the selected bookmark at the bottom of the screen.
1028 // `+1` assures that the entire bookmark will be displayed in situations where only a partial bookmark fits at the bottom of the list view.
1029 bookmarksListView.setSelection(selectedBookmarkPosition - numberOfBookmarksPerScreen + 1);
1033 private void loadFolder() {
1034 // Update bookmarks cursor with the contents of the bookmarks database for the current folder.
1035 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolder);
1037 // Setup a `CursorAdapter`. `this` specifies the `Context`. `false` disables `autoRequery`.
1038 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
1040 public View newView(Context context, Cursor cursor, ViewGroup parent) {
1041 // Inflate the individual item layout. `false` does not attach it to the root.
1042 return getLayoutInflater().inflate(R.layout.bookmarks_activity_item_linearlayout, parent, false);
1046 public void bindView(View view, Context context, Cursor cursor) {
1047 // Get handles for the views.
1048 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon);
1049 TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name);
1051 // Get the favorite icon byte array from the `Cursor`.
1052 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
1054 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
1055 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
1057 // Display the bitmap in `bookmarkFavoriteIcon`.
1058 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
1060 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
1061 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
1062 bookmarkNameTextView.setText(bookmarkNameString);
1064 // Make the font bold for folders.
1065 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {
1066 bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD);
1067 } else { // Reset the font to default for normal bookmarks.
1068 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
1073 // Populate the list view with the adapter.
1074 bookmarksListView.setAdapter(bookmarksCursorAdapter);
1076 // Set the `AppBar` title.
1077 if (currentFolder.isEmpty()) {
1078 appBar.setTitle(R.string.bookmarks);
1080 appBar.setTitle(currentFolder);
1085 public void onDestroy() {
1086 // Close the bookmarks cursor and database.
1087 bookmarksCursor.close();
1088 bookmarksDatabaseHelper.close();
1090 // Run the default commands.