2 * Copyright © 2016-2018 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.app.Activity;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.database.Cursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.Typeface;
29 import android.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.support.design.widget.FloatingActionButton;
33 import android.support.design.widget.Snackbar;
34 import android.support.v4.app.NavUtils;
35 import android.support.v7.app.ActionBar;
36 import android.support.v7.app.AppCompatActivity;
37 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
38 import android.support.v7.app.AppCompatDialogFragment;
39 import android.support.v7.widget.Toolbar;
40 import android.util.SparseBooleanArray;
41 import android.view.ActionMode;
42 import android.view.Menu;
43 import android.view.MenuItem;
44 import android.view.View;
45 import android.view.ViewGroup;
46 import android.view.WindowManager;
47 import android.widget.AbsListView;
48 import android.widget.CursorAdapter;
49 import android.widget.EditText;
50 import android.widget.ImageView;
51 import android.widget.ListView;
52 import android.widget.RadioButton;
53 import android.widget.TextView;
55 import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
56 import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
57 import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
58 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog;
59 import com.stoutner.privacybrowser.dialogs.MoveToFolderDialog;
60 import com.stoutner.privacybrowser.R;
61 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
63 import java.io.ByteArrayOutputStream;
65 public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener,
66 EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener {
68 // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`.
69 // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `loadFolder()`.
70 public static String currentFolder;
72 // `checkedItemIds` is public static so it can be accessed from `MoveToFolderDialog`. It is also used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`.
73 public static long[] checkedItemIds;
76 // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`,
78 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
80 // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `updateMoveIcons()`, `scrollBookmarks()`,
81 // and `loadFolder()`.
82 private ListView bookmarksListView;
84 // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder()`.
85 private Cursor bookmarksCursor;
87 // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`.
88 private CursorAdapter bookmarksCursorAdapter;
90 // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`.
91 private ActionMode contextualActionMode;
93 // `appBar` is used in `onCreate()` and `loadFolder()`.
94 private ActionBar appBar;
96 // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`.
97 private String oldFolderNameString;
99 // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
100 private MenuItem moveBookmarkUpMenuItem;
102 // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons()`.
103 private MenuItem moveBookmarkDownMenuItem;
105 // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
106 private Snackbar bookmarksDeletedSnackbar;
108 // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
109 private boolean closeActivityAfterDismissingSnackbar;
112 protected void onCreate(Bundle savedInstanceState) {
113 // Disable screenshots if not allowed.
114 if (!MainWebViewActivity.allowScreenshots) {
115 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
118 // Set the activity theme.
119 if (MainWebViewActivity.darkTheme) {
120 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
122 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
125 // Run the default commands.
126 super.onCreate(savedInstanceState);
128 // Get the intent that launched the activity.
129 Intent launchingIntent = getIntent();
131 // Set the current folder variable.
132 if (launchingIntent.getStringExtra("Current Folder") != null) { // Set the current folder from the intent.
133 currentFolder = launchingIntent.getStringExtra("Current Folder");
134 } else { // Set the current folder to be `""`, which is the home folder.
138 // Set the content view.
139 setContentView(R.layout.bookmarks_coordinatorlayout);
141 // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
142 final Toolbar bookmarksAppBar = findViewById(R.id.bookmarks_toolbar);
143 setSupportActionBar(bookmarksAppBar);
145 // Get a handle for the activity, the app bar, and the ListView.
146 final Activity bookmarksActivity = this;
147 appBar = getSupportActionBar();
148 bookmarksListView = findViewById(R.id.bookmarks_listview);
150 // Remove the incorrect lint warning that `appBar` might be null.
151 assert appBar != null;
153 // Display the home arrow on the app bar.
154 appBar.setDisplayHomeAsUpEnabled(true);
156 // Initialize the database helper. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
157 // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
158 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
160 // Load the home folder.
163 // Set a listener so that tapping a list item loads the URL or folder.
164 bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
165 // Convert the id from long to int to match the format of the bookmarks database.
166 int databaseID = (int) id;
168 // Get the bookmark cursor for this ID and move it to the first row.
169 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID);
170 bookmarkCursor.moveToFirst();
172 // Act upon the bookmark according to the type.
173 if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
174 // Update the current folder.
175 currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
177 // Load the new folder.
179 } else { // The selected bookmark is not a folder.
180 // Get the bookmark URL and assign it to `formattedUrlString`.
181 MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
183 // Set `MainWebViewActivity` to load the new URL on restart.
184 MainWebViewActivity.loadUrlOnRestart = true;
186 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
187 MainWebViewActivity.currentBookmarksFolder = currentFolder;
189 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
190 MainWebViewActivity.restartFromBookmarksActivity = true;
192 // Return to `MainWebViewActivity`.
193 NavUtils.navigateUpFromSameTask(bookmarksActivity);
196 // Close the `Cursor`.
197 bookmarkCursor.close();
200 // Get a handle for the activity.
201 Activity activity = this;
203 // `MultiChoiceModeListener` handles long clicks.
204 bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
205 // Instantiate the common variables.
206 MenuItem editBookmarkMenuItem;
207 MenuItem deleteBookmarksMenuItem;
208 MenuItem selectAllBookmarksMenuItem;
209 boolean deletingBookmarks;
212 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
213 // Inflate the menu for the contextual app bar and set the title.
214 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
217 if (currentFolder.isEmpty()) {
218 // Use `R.string.bookmarks` if we are in the home folder.
219 mode.setTitle(R.string.bookmarks);
220 } else { // Use the current folder name as the title.
221 mode.setTitle(currentFolder);
224 // Get a handle for `MenuItems` that need to be selectively disabled.
225 moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up);
226 moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down);
227 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
228 deleteBookmarksMenuItem = menu.findItem(R.id.delete_bookmark);
229 selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks);
231 // Disable the delete bookmarks menu item if a delete is pending.
232 if (deletingBookmarks) {
233 deleteBookmarksMenuItem.setEnabled(false);
236 // Store `contextualActionMode` so it can be closed programatically.
237 contextualActionMode = mode;
244 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
245 // Get a handle for the move to folder menu item.
246 MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder);
248 // Get a `Cursor` with all of the folders.
249 Cursor folderCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
251 // Enable the move to folder menu item if at least one folder exists.
252 moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0);
259 public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
260 // Get a long array of the selected bookmarks.
261 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
263 // Calculate the number of selected bookmarks.
264 int numberOfSelectedBookmarks = selectedBookmarksLongArray.length;
266 // Adjust the `ActionMode` and the `Menu` according to the number of selected bookmarks.
267 if (numberOfSelectedBookmarks == 0) { // No bookmarks are selected.
268 // Close the `ActionMode`.
270 } else if (numberOfSelectedBookmarks == 1) { // One bookmark is selected.
271 // List the number of selected bookmarks in the subtitle.
272 mode.setSubtitle(getString(R.string.one_selected));
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 according to the language.
283 if (getString(R.string.android_asset_path).equals("ru")) { // The Russian translation is used.
284 // Convert the number of selected bookmarks to a string.
285 String numberOfSelectedBookmarksString = String.valueOf(numberOfSelectedBookmarks);
287 // Russian follows rule #7 at <https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals>.
288 if (numberOfSelectedBookmarksString.endsWith("1") && !numberOfSelectedBookmarksString.equals("11")) { // Ends in 1.
289 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_ends_in_1));
290 } else if ((numberOfSelectedBookmarksString.endsWith("2") || numberOfSelectedBookmarksString.endsWith("3") || numberOfSelectedBookmarksString.endsWith("4")) &&
291 !numberOfSelectedBookmarksString.equals("12") && !numberOfSelectedBookmarksString.equals("13") && !numberOfSelectedBookmarksString.equals("14")) { // Ends in 2-4.
292 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_ends_in_2));
293 } else { // Everything else.
294 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_everything_else));
296 } else { // Another language is used.
297 // List the number of selected bookmarks in the subtitle.
298 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
301 // Hide non-applicable `MenuItems`.
302 moveBookmarkUpMenuItem.setVisible(false);
303 moveBookmarkDownMenuItem.setVisible(false);
304 editBookmarkMenuItem.setVisible(false);
307 // Do not show `Select All` if all the bookmarks are already checked.
308 if (bookmarksListView.getCheckedItemIds().length == bookmarksListView.getCount()) {
309 selectAllBookmarksMenuItem.setVisible(false);
311 selectAllBookmarksMenuItem.setVisible(true);
316 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
317 // Get the menu item ID.
318 int menuItemId = item.getItemId();
320 // Instantiate the common variables.
321 int selectedBookmarkPosition;
322 int selectedBookmarkNewPosition;
323 final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray;
325 switch (menuItemId) {
326 case R.id.move_bookmark_up:
327 // Get the array of checked bookmark positions.
328 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
330 // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_up` is enabled.
331 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
333 // Calculate the new position of the selected bookmark.
334 selectedBookmarkNewPosition = selectedBookmarkPosition - 1;
336 // Iterate through the bookmarks.
337 for (int i = 0; i < bookmarksListView.getCount(); i++) {
338 // Get the database ID for the current bookmark.
339 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
341 // Update the display order for the current bookmark.
342 if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
343 // Move the current bookmark up one.
344 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
345 } else if ((i + 1) == selectedBookmarkPosition){ // The current bookmark is immediately above the selected bookmark.
346 // Move the current bookmark down one.
347 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
348 } else { // The current bookmark is not changing positions.
349 // Move `bookmarksCursor` to the current bookmark position.
350 bookmarksCursor.moveToPosition(i);
352 // Update the display order only if it is not correct in the database.
353 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
354 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
359 // Update `bookmarksCursor` with the current contents of the bookmarks database.
360 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
362 // Update the `ListView`.
363 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
365 // Scroll with the bookmark.
366 scrollBookmarks(selectedBookmarkNewPosition);
368 // Update the enabled status of the move icons.
372 case R.id.move_bookmark_down:
373 // Get the array of checked bookmark positions.
374 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
376 // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_down` is enabled.
377 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
379 // Calculate the new position of the selected bookmark.
380 selectedBookmarkNewPosition = selectedBookmarkPosition + 1;
382 // Iterate through the bookmarks.
383 for (int i = 0; i <bookmarksListView.getCount(); i++) {
384 // Get the database ID for the current bookmark.
385 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
387 // Update the display order for the current bookmark.
388 if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
389 // Move the current bookmark down one.
390 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
391 } else if ((i - 1) == selectedBookmarkPosition) { // The current bookmark is immediately below the selected bookmark.
392 // Move the bookmark below the selected bookmark up one.
393 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
394 } else { // The current bookmark is not changing positions.
395 // Move `bookmarksCursor` to the current bookmark position.
396 bookmarksCursor.moveToPosition(i);
398 // Update the display order only if it is not correct in the database.
399 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
400 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
405 // Update `bookmarksCursor` with the current contents of the bookmarks database.
406 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
408 // Update the `ListView`.
409 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
411 // Scroll with the bookmark.
412 scrollBookmarks(selectedBookmarkNewPosition);
414 // Update the enabled status of the move icons.
418 case R.id.move_to_folder:
419 // Store `checkedItemIds` for use by the `AlertDialog`.
420 checkedItemIds = bookmarksListView.getCheckedItemIds();
422 // Show the `MoveToFolderDialog` `AlertDialog` and name the instance `@string/move_to_folder
423 AppCompatDialogFragment moveToFolderDialog = new MoveToFolderDialog();
424 moveToFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.move_to_folder));
427 case R.id.edit_bookmark:
428 // Get the array of checked bookmark positions.
429 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
431 // Get the position of the selected bookmark. Only one bookmark is selected when `edit_bookmark_down` is enabled.
432 selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
434 // Move the `Cursor` to the selected position and find out if it is a folder.
435 bookmarksCursor.moveToPosition(selectedBookmarkPosition);
436 boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
438 // Get the selected bookmark database ID.
439 int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
441 // Show the edit bookmark or edit bookmark folder dialog.
443 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
444 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
446 // Show the edit bookmark folder dialog.
447 AppCompatDialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
448 editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
450 // Show the edit bookmark dialog.
451 AppCompatDialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
452 editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
456 case R.id.delete_bookmark:
457 // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
458 deletingBookmarks = true;
460 // Get an array of the selected row IDs.
461 final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
463 // 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.
464 selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
466 // Update `bookmarksCursor` with the current contents of the bookmarks database except for the specified database IDs.
467 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksCursorExcept(selectedBookmarksIdsLongArray, currentFolder);
469 // Update the `ListView`.
470 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
472 // Instantiate `snackbarMessage`.
473 String snackbarMessage;
475 // Determine how many items are in the array and prepare an appropriate snackbar message.
476 if (selectedBookmarksIdsLongArray.length == 1) {
477 snackbarMessage = getString(R.string.one_bookmark_deleted);
479 // Prepare a snackbar according to the language.
480 if (getString(R.string.android_asset_path).equals("ru")) { // The Russian translation is used.
481 // Convert the number of selected bookmarks to a string.
482 String numberOfBookmarksString = String.valueOf(selectedBookmarksIdsLongArray.length);
484 // Russian follows rule #7 at <https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals>.
485 if (numberOfBookmarksString.endsWith("1") && !numberOfBookmarksString.equals("11")) { // Ends in 1.
486 snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_ends_in_1);
487 } else if ((numberOfBookmarksString.endsWith("2") || numberOfBookmarksString.endsWith("3") || numberOfBookmarksString.endsWith("4")) &&
488 !numberOfBookmarksString.equals("12") && !numberOfBookmarksString.equals("13") && !numberOfBookmarksString.equals("14")) { // Ends in 2-4.
489 snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_ends_in_2);
490 } else { // Everything else.
491 snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_everything_else);
493 } else { // Another language is used.
494 snackbarMessage = selectedBookmarksIdsLongArray.length + " " + getString(R.string.bookmarks_deleted);
499 bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
500 .setAction(R.string.undo, view -> {
501 // Do nothing because everything will be handled by `onDismissed()` below.
503 .addCallback(new Snackbar.Callback() {
505 public void onDismissed(Snackbar snackbar, int event) {
507 // The user pushed the `Undo` button.
508 case Snackbar.Callback.DISMISS_EVENT_ACTION:
509 // Update `bookmarksCursor` with the current contents of the bookmarks database, including the "deleted" bookmarks.
510 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
512 // Update the `ListView`.
513 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
515 // Re-select the previously selected bookmarks.
516 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
517 bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
522 // The `Snackbar` was dismissed without the `Undo` button being pushed.
524 // Delete each selected bookmark.
525 for (long databaseIdLong : selectedBookmarksIdsLongArray) {
526 // Convert `databaseIdLong` to an int.
527 int databaseIdInt = (int) databaseIdLong;
529 // Delete the contents of the folder if the selected bookmark is a folder.
530 if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
531 deleteBookmarkFolderContents(databaseIdInt);
534 // Delete the selected bookmark.
535 bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
538 // Update the display order.
539 for (int i = 0; i < bookmarksListView.getCount(); i++) {
540 // Get the database ID for the current bookmark.
541 int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
543 // Move `bookmarksCursor` to the current bookmark position.
544 bookmarksCursor.moveToPosition(i);
546 // Update the display order only if it is not correct in the database.
547 if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
548 bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
553 // Enable the delete bookmarks menu item.
554 deleteBookmarksMenuItem.setEnabled(true);
556 // Reset the deleting bookmarks flag.
557 deletingBookmarks = false;
559 // Close the activity if back has been pressed.
560 if (closeActivityAfterDismissingSnackbar) {
561 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
562 MainWebViewActivity.currentBookmarksFolder = "";
564 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
565 MainWebViewActivity.restartFromBookmarksActivity = true;
567 // Return to `MainWebViewActivity`.
568 NavUtils.navigateUpFromSameTask(activity);
573 //Show the `SnackBar`.
574 bookmarksDeletedSnackbar.show();
576 // Close the `ActionBar`.
580 case R.id.context_menu_select_all_bookmarks:
581 // Get the total number of bookmarks.
582 int numberOfBookmarks = bookmarksListView.getCount();
585 for (int i = 0; i < numberOfBookmarks; i++) {
586 bookmarksListView.setItemChecked(i, true);
591 // Consume the click.
596 public void onDestroyActionMode(ActionMode mode) {
601 // Get handles for the `FloatingActionButtons`.
602 FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab);
603 FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab);
605 // Set the create new bookmark folder FAB to display the `AlertDialog`.
606 createBookmarkFolderFab.setOnClickListener(v -> {
607 // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`.
608 AppCompatDialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog();
609 createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder));
612 // Set the create new bookmark FAB to display the `AlertDialog`.
613 createBookmarkFab.setOnClickListener(view -> {
614 // Show the `CreateBookmarkDialog` `AlertDialog` and name the instance `@string/create_bookmark`.
615 AppCompatDialogFragment createBookmarkDialog = new CreateBookmarkDialog();
616 createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark));
621 public boolean onCreateOptionsMenu(Menu menu) {
623 getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
630 public boolean onOptionsItemSelected(MenuItem menuItem) {
631 // Get the ID of the `MenuItem` that was selected.
632 int menuItemId = menuItem.getItemId();
634 switch (menuItemId) {
635 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
636 if (currentFolder.isEmpty()) { // Currently in the home folder.
638 if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks delete snackbar before going home.
639 // Set the close flag.
640 closeActivityAfterDismissingSnackbar = true;
642 // Dismiss the snackbar.
643 bookmarksDeletedSnackbar.dismiss();
644 } else { // Go home immediately.
645 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
646 MainWebViewActivity.currentBookmarksFolder = "";
648 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
649 MainWebViewActivity.restartFromBookmarksActivity = true;
651 // Return to `MainWebViewActivity`.
652 NavUtils.navigateUpFromSameTask(this);
654 } else { // Currently in a subfolder.
655 // Place the former parent folder in `currentFolder`.
656 currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
658 // Load the new folder.
663 case R.id.options_menu_select_all_bookmarks:
664 // Get the total number of bookmarks.
665 int numberOfBookmarks = bookmarksListView.getCount();
668 for (int i = 0; i < numberOfBookmarks; i++) {
669 bookmarksListView.setItemChecked(i, true);
673 case R.id.bookmarks_database_view:
674 // Launch `BookmarksDatabaseViewActivity`.
675 Intent bookmarksDatabaseViewIntent = new Intent(this, BookmarksDatabaseViewActivity.class);
676 startActivity(bookmarksDatabaseViewIntent);
683 public void onBackPressed() {
684 if (currentFolder.isEmpty()) { // Currently in the home folder.
686 if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks delete snackbar before going home.
687 // Set the close flag.
688 closeActivityAfterDismissingSnackbar = true;
690 // Dismiss the snackbar.
691 bookmarksDeletedSnackbar.dismiss();
692 } else { // Go home immediately.
693 // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
694 MainWebViewActivity.currentBookmarksFolder = "";
696 // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`.
697 MainWebViewActivity.restartFromBookmarksActivity = true;
699 // Exit `BookmarksActivity`.
700 super.onBackPressed();
702 } else { // Currently in a subfolder.
703 // Place the former parent folder in `currentFolder`.
704 currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
706 // Load the new folder.
712 public void onCreateBookmark(AppCompatDialogFragment dialogFragment) {
713 // Get the `EditTexts` from the `dialogFragment`.
714 EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
715 EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
717 // Extract the strings from the `EditTexts`.
718 String bookmarkNameString = createBookmarkNameEditText.getText().toString();
719 String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
721 // Convert the favoriteIcon Bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
722 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
723 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
724 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
726 // Display the new bookmark below the current items in the (0 indexed) list.
727 int newBookmarkDisplayOrder = bookmarksListView.getCount();
729 // Create the bookmark.
730 bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
732 // Update `bookmarksCursor` with the current contents of this folder.
733 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
735 // Update the `ListView`.
736 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
738 // Scroll to the new bookmark.
739 bookmarksListView.setSelection(newBookmarkDisplayOrder);
743 public void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment) {
744 // Get handles for the views in `dialogFragment`.
745 EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
746 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
747 ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
749 // Get new folder name string.
750 String folderNameString = createFolderNameEditText.getText().toString();
752 // Get the new folder icon `Bitmap`.
753 Bitmap folderIconBitmap;
754 if (defaultFolderIconRadioButton.isChecked()) { // Use the default folder icon.
755 // Get the default folder icon and convert it to a `Bitmap`.
756 Drawable folderIconDrawable = folderIconImageView.getDrawable();
757 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
758 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
759 } else { // Use the `WebView` favorite icon.
760 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
763 // Convert `folderIconBitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
764 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
765 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
766 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
768 // Move all the bookmarks down one in the display order.
769 for (int i = 0; i < bookmarksListView.getCount(); i++) {
770 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
771 bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1);
774 // Create the folder, which will be placed at the top of the `ListView`.
775 bookmarksDatabaseHelper.createFolder(folderNameString, currentFolder, folderIconByteArray);
777 // Update `bookmarksCursor` with the current contents of this folder.
778 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
780 // Update the `ListView`.
781 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
783 // Scroll to the new folder.
784 bookmarksListView.setSelection(0);
788 public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
789 // Get handles for the views from `dialogFragment`.
790 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
791 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
792 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
794 // Store the bookmark strings.
795 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
796 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
798 // Update the bookmark.
799 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
800 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
801 } else { // Update the bookmark using the `WebView` favorite icon.
802 // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
803 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
804 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
805 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
807 // Update the bookmark and the favorite icon.
808 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, newFavoriteIconByteArray);
811 // Close the contextual action mode.
812 contextualActionMode.finish();
814 // Update `bookmarksCursor` with the contents of the current folder.
815 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
817 // Update the `ListView`.
818 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
822 public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) {
823 // Get handles for the views from `dialogFragment`.
824 RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
825 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
826 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
827 EditText editFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
829 // Get the new folder name.
830 String newFolderNameString = editFolderNameEditText.getText().toString();
832 // Check if the favorite icon has changed.
833 if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
834 // Update the name in the database.
835 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
836 } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
837 // Get the new folder icon `Bitmap`.
838 Bitmap folderIconBitmap;
839 if (defaultFolderIconRadioButton.isChecked()) {
840 // Get the default folder icon and convert it to a `Bitmap`.
841 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
842 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
843 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
844 } else { // Use the `WebView` favorite icon.
845 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
848 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
849 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
850 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
851 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
853 // Update the folder icon in the database.
854 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, folderIconByteArray);
855 } else { // The folder icon and the name have changed.
856 // Instantiate the new folder icon `Bitmap`.
857 Bitmap folderIconBitmap;
859 // Populate the new folder icon bitmap.
860 if (defaultFolderIconRadioButton.isChecked()) {
861 // Get the default folder icon and convert it to a `Bitmap`.
862 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
863 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
864 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
865 } else { // Use the `WebView` favorite icon.
866 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
869 // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
870 ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
871 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
872 byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
874 // Update the folder name and icon in the database.
875 bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray);
878 // Update `bookmarksCursor` with the current contents of this folder.
879 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
881 // Update the `ListView`.
882 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
884 // Close the contextual action mode.
885 contextualActionMode.finish();
889 public void onMoveToFolder(AppCompatDialogFragment dialogFragment) {
890 // Get a handle for the `ListView` from `dialogFragment`.
891 ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview);
893 // Store a long array of the selected folders.
894 long[] newFolderLongArray = folderListView.getCheckedItemIds();
896 // Get the new folder database ID. Only one folder will be selected.
897 int newFolderDatabaseId = (int) newFolderLongArray[0];
899 // Instantiate `newFolderName`.
900 String newFolderName;
902 // Set the new folder name.
903 if (newFolderDatabaseId == 0) {
904 // The new folder is the home folder, represented as `""` in the database.
907 // Get the new folder name from the database.
908 newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
911 // Get a long array with the the database ID of the selected bookmarks.
912 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
914 // Move each of the selected bookmarks to the new folder.
915 for (long databaseIdLong : selectedBookmarksLongArray) {
916 // Get `databaseIdInt` for each selected bookmark.
917 int databaseIdInt = (int) databaseIdLong;
919 // Move the selected bookmark to the new folder.
920 bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
923 // Update `bookmarksCursor` with the current contents of this folder.
924 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
926 // Update the `ListView`.
927 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
929 // Close the contextual app bar.
930 contextualActionMode.finish();
933 private void deleteBookmarkFolderContents(int databaseId) {
934 // Get the name of the folder.
935 String folderName = bookmarksDatabaseHelper.getFolderName(databaseId);
937 // Get the contents of the folder.
938 Cursor folderCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName);
940 // Delete each of the bookmarks in the folder.
941 for (int i = 0; i < folderCursor.getCount(); i++) {
942 // Move `folderCursor` to the current row.
943 folderCursor.moveToPosition(i);
945 // Get the database ID of the item.
946 int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
948 // If this is a folder, recursively delete the contents first.
949 if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) {
950 deleteBookmarkFolderContents(itemDatabaseId);
953 // Delete the bookmark.
954 bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId);
958 private void updateMoveIcons() {
959 // Get a long array of the selected bookmarks.
960 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
962 // Get the database IDs for the first, last, and selected bookmarks.
963 int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
964 int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0);
965 // bookmarksListView is 0 indexed.
966 int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1);
968 // Update the move bookmark up `MenuItem`.
969 if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { // The selected bookmark is in the first position.
970 // Disable the move bookmark up `MenuItem`.
971 moveBookmarkUpMenuItem.setEnabled(false);
973 // Set the move bookmark up icon to be ghosted.
974 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled);
975 } else { // The selected bookmark is not in the first position.
976 // Enable the move bookmark up `MenuItem`.
977 moveBookmarkUpMenuItem.setEnabled(true);
979 // Set the icon according to the theme.
980 if (MainWebViewActivity.darkTheme) {
981 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark);
983 moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light);
987 // Update the move bookmark down `MenuItem`.
988 if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { // The selected bookmark is in the last position.
989 // Disable the move bookmark down `MenuItem`.
990 moveBookmarkDownMenuItem.setEnabled(false);
992 // Set the move bookmark down icon to be ghosted.
993 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled);
994 } else { // The selected bookmark is not in the last position.
995 // Enable the move bookmark down `MenuItem`.
996 moveBookmarkDownMenuItem.setEnabled(true);
998 // Set the icon according to the theme.
999 if (MainWebViewActivity.darkTheme) {
1000 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark);
1002 moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light);
1007 private void scrollBookmarks(int selectedBookmarkPosition) {
1008 // Get the first and last visible bookmark positions.
1009 int firstVisibleBookmarkPosition = bookmarksListView.getFirstVisiblePosition();
1010 int lastVisibleBookmarkPosition = bookmarksListView.getLastVisiblePosition();
1012 // Calculate the number of bookmarks per screen.
1013 int numberOfBookmarksPerScreen = lastVisibleBookmarkPosition - firstVisibleBookmarkPosition;
1015 // Scroll with the moved bookmark if necessary.
1016 if (selectedBookmarkPosition <= firstVisibleBookmarkPosition) { // The selected bookmark position is at or above the top of the screen.
1017 // Scroll to the selected bookmark position.
1018 bookmarksListView.setSelection(selectedBookmarkPosition);
1019 } 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`.
1020 // 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`.
1021 bookmarksListView.setSelection(selectedBookmarkPosition - numberOfBookmarksPerScreen + 1);
1025 private void loadFolder() {
1026 // Update `bookmarksCursor` with the contents of the bookmarks database for the current folder.
1027 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
1029 // Setup a `CursorAdapter`. `this` specifies the `Context`. `false` disables `autoRequery`.
1030 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
1032 public View newView(Context context, Cursor cursor, ViewGroup parent) {
1033 // Inflate the individual item layout. `false` does not attach it to the root.
1034 return getLayoutInflater().inflate(R.layout.bookmarks_activity_item_linearlayout, parent, false);
1038 public void bindView(View view, Context context, Cursor cursor) {
1039 // Get handles for the views.
1040 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon);
1041 TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name);
1043 // Get the favorite icon byte array from the `Cursor`.
1044 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
1046 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
1047 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
1049 // Display the bitmap in `bookmarkFavoriteIcon`.
1050 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
1052 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
1053 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
1054 bookmarkNameTextView.setText(bookmarkNameString);
1056 // Make the font bold for folders.
1057 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) {
1058 bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD);
1059 } else { // Reset the font to default for normal bookmarks.
1060 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
1065 // Populate the `ListView` with the adapter.
1066 bookmarksListView.setAdapter(bookmarksCursorAdapter);
1068 // Set the `AppBar` title.
1069 if (currentFolder.isEmpty()) {
1070 appBar.setTitle(R.string.bookmarks);
1072 appBar.setTitle(currentFolder);