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.content.Context;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MergeCursor;
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.Snackbar;
33 import android.support.v4.content.ContextCompat;
34 import android.support.v4.widget.CursorAdapter;
35 import android.support.v4.widget.ResourceCursorAdapter;
36 import android.support.v7.app.ActionBar;
37 import android.support.v7.app.AppCompatActivity;
38 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
39 import android.support.v7.app.AppCompatDialogFragment;
40 import android.support.v7.widget.Toolbar;
41 import android.util.SparseBooleanArray;
42 import android.view.ActionMode;
43 import android.view.Menu;
44 import android.view.MenuItem;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.view.WindowManager;
48 import android.widget.AbsListView;
49 import android.widget.AdapterView;
50 import android.widget.EditText;
51 import android.widget.ImageView;
52 import android.widget.ListView;
53 import android.widget.RadioButton;
54 import android.widget.Spinner;
55 import android.widget.TextView;
57 import com.stoutner.privacybrowser.R;
58 import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog;
59 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog;
60 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
62 import java.io.ByteArrayOutputStream;
64 public class BookmarksDatabaseViewActivity extends AppCompatActivity implements EditBookmarkDatabaseViewDialog.EditBookmarkDatabaseViewListener,
65 EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
66 // Instantiate the constants.
67 private static final int ALL_FOLDERS_DATABASE_ID = -2;
68 private static final int HOME_FOLDER_DATABASE_ID = -1;
70 // `bookmarksDatabaseHelper` is used in `onCreate()`, `updateBookmarksListView()`, `selectAllBookmarksInFolder()`, and `onDestroy()`.
71 private BookmarksDatabaseHelper bookmarksDatabaseHelper;
73 // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, and `onDestroy()`.
74 private Cursor bookmarksCursor;
76 // `bookmarksCursorAdapter` is used in `onCreate()`, `selectAllBookmarksInFolder()`, and `updateBookmarksListView()`.
77 private CursorAdapter bookmarksCursorAdapter;
79 // `oldFolderNameString` is used in `onCreate()` and `onSaveBookmarkFolder()`.
80 private String oldFolderNameString;
82 // `currentFolderDatabaseId` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
83 private int currentFolderDatabaseId;
85 // `currentFolder` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
86 private String currentFolderName;
88 // `sortByDisplayOrder` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateBookmarksListView()`.
89 private boolean sortByDisplayOrder;
91 // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
92 private Snackbar bookmarksDeletedSnackbar;
94 // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
95 private boolean closeActivityAfterDismissingSnackbar;
98 public void onCreate(Bundle savedInstanceState) {
99 // Disable screenshots if not allowed.
100 if (!MainWebViewActivity.allowScreenshots) {
101 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
104 // Set the activity theme.
105 if (MainWebViewActivity.darkTheme) {
106 setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
108 setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
111 // Run the default commands.
112 super.onCreate(savedInstanceState);
114 // Set the content view.
115 setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
117 // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
118 Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar);
119 setSupportActionBar(bookmarksDatabaseViewAppBar);
121 // Get a handle for the `AppBar`.
122 ActionBar appBar = getSupportActionBar();
124 // Remove the incorrect warning in Android Studio that `appBar` might be null.
125 assert appBar != null;
127 // Display the spinner and the back arrow in the app bar.
128 appBar.setCustomView(R.layout.spinner);
129 appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
131 // Initialize the database handler. `this` specifies the context. The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
132 bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
134 // Setup a matrix cursor for "All Folders" and "Home Folder".
135 String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
136 MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
137 matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)});
138 matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)});
140 // Get a cursor with the list of all the folders.
141 Cursor foldersCursor = bookmarksDatabaseHelper.getAllFolders();
143 // Combine `matrixCursor` and `foldersCursor`.
144 MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
146 // Create a resource cursor adapter for the spinner.
147 ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.appbar_spinner_item, foldersMergeCursor, 0) {
149 public void bindView(View view, Context context, Cursor cursor) {
150 // Get a handle for the spinner item text view.
151 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
153 // Set the text view to display the folder name.
154 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
158 // Set the resource cursor adapter drop drown view resource.
159 foldersCursorAdapter.setDropDownViewResource(R.layout.appbar_spinner_dropdown_item);
161 // Get a handle for the folder spinner and set the adapter.
162 Spinner folderSpinner = findViewById(R.id.spinner);
163 folderSpinner.setAdapter(foldersCursorAdapter);
165 // Handle taps on the spinner dropdown.
166 folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
168 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
169 // Store the current folder database ID.
170 currentFolderDatabaseId = (int) id;
172 // Get a handle for the selected view.
173 TextView selectedFolderTextView = findViewById(R.id.spinner_item_textview);
175 // Store the current folder name.
176 currentFolderName = selectedFolderTextView.getText().toString();
178 // Update the list view.
179 updateBookmarksListView();
183 public void onNothingSelected(AdapterView<?> parent) {
188 // Get a handle for the bookmarks `ListView`.
189 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
191 // Get a `Cursor` with the current contents of the bookmarks database.
192 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
194 // Setup a `CursorAdapter` with `this` context. `false` disables autoRequery.
195 bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
197 public View newView(Context context, Cursor cursor, ViewGroup parent) {
198 // Inflate the individual item layout. `false` does not attach it to the root.
199 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
203 public void bindView(View view, Context context, Cursor cursor) {
204 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
206 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
207 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
208 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
209 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
211 // Get the favorite icon byte array from the `Cursor`.
212 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
213 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
214 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
215 // Display the bitmap in `bookmarkFavoriteIcon`.
216 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
217 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
219 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
220 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
221 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
222 bookmarkNameTextView.setText(bookmarkNameString);
224 // Make the font bold for folders.
226 // The first argument is `null` prevent changing of the font.
227 bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
228 } else { // Reset the font to default.
229 bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
232 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
233 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
234 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
235 bookmarkUrlTextView.setText(bookmarkUrlString);
237 // Hide the URL if the bookmark is a folder.
239 bookmarkUrlTextView.setVisibility(View.GONE);
241 bookmarkUrlTextView.setVisibility(View.VISIBLE);
244 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
245 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
246 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
247 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
249 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
250 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
251 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
252 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
254 // Make the folder name gray if it is the home folder.
255 if (bookmarkParentFolder.isEmpty()) {
256 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
257 bookmarkParentFolderTextView.setText(R.string.home_folder);
258 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
260 parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
261 bookmarkParentFolderTextView.setText(bookmarkParentFolder);
263 // Set the text color according to the theme.
264 if (MainWebViewActivity.darkTheme) {
265 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
267 bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
273 // Update the ListView.
274 bookmarksListView.setAdapter(bookmarksCursorAdapter);
276 // Set the current folder database ID.
277 currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
279 // Set a listener to edit a bookmark when it is tapped.
280 bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
281 // Convert the database ID to an int.
282 int databaseId = (int) id;
284 // Show the edit bookmark or edit bookmark folder dialog.
285 if (bookmarksDatabaseHelper.isFolder(databaseId)) {
286 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
287 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
289 // Show the edit bookmark folder dialog.
290 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
291 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
293 // Show the edit bookmark dialog.
294 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
295 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
299 // Handle long presses on the list view.
300 bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
301 // Instantiate the common variables.
302 MenuItem selectAllMenuItem;
303 MenuItem deleteMenuItem;
304 boolean deletingBookmarks;
307 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
308 // Inflate the menu for the contextual app bar.
309 getMenuInflater().inflate(R.menu.bookmarks_databaseview_context_menu, menu);
312 mode.setTitle(R.string.bookmarks);
314 // Get handles for the menu items.
315 selectAllMenuItem = menu.findItem(R.id.select_all);
316 deleteMenuItem = menu.findItem(R.id.delete);
318 // Disable the delete menu item if a delete is pending.
319 deleteMenuItem.setEnabled(!deletingBookmarks);
326 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
332 public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
333 // Calculate the number of selected bookmarks.
334 int numberOfSelectedBookmarks = bookmarksListView.getCheckedItemCount();
336 // Adjust the ActionMode according to the number of selected bookmarks.
337 mode.setSubtitle(getString(R.string.selected) + " " + numberOfSelectedBookmarks);
339 // Do not show the select all menu item if all the bookmarks are already checked.
340 if (bookmarksListView.getCheckedItemCount() == bookmarksListView.getCount()) {
341 selectAllMenuItem.setVisible(false);
343 selectAllMenuItem.setVisible(true);
346 // Convert the database ID to an int.
347 int databaseId = (int) id;
349 // If a folder was selected, also select all the contents.
350 if (checked && bookmarksDatabaseHelper.isFolder(databaseId)) {
351 selectAllBookmarksInFolder(databaseId);
354 // Do not allow a bookmark to be deselected if the folder is selected.
356 // Get the folder name.
357 String folderName = bookmarksDatabaseHelper.getParentFolderName((int) id);
359 // If the bookmark is not in the root folder, check to see if the folder is selected.
360 if (!folderName.isEmpty()) {
361 // Get the database ID of the folder.
362 int folderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderName);
364 // Move the bookmarks cursor to the first position.
365 bookmarksCursor.moveToFirst();
367 // Initialize the folder position variable.
368 int folderPosition = -1;
370 // Get the position of the folder in the bookmarks cursor.
371 while ((folderPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) {
372 // Check if the folder database ID matches the bookmark database ID.
373 if (folderDatabaseId == bookmarksCursor.getInt((bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID)))) {
374 // Get the folder position.
375 folderPosition = bookmarksCursor.getPosition();
377 // Check if the folder is selected.
378 if (bookmarksListView.isItemChecked(folderPosition)) {
379 // Reselect the bookmark.
380 bookmarksListView.setItemChecked(position, true);
382 // Display a snackbar explaining why the bookmark cannot be deselected.
383 Snackbar.make(bookmarksListView, R.string.cannot_deselect_bookmark, Snackbar.LENGTH_LONG).show();
387 // Increment the bookmarks cursor.
388 bookmarksCursor.moveToNext();
395 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
396 switch (item.getItemId()) {
397 case R.id.select_all:
398 // Get the total number of bookmarks.
399 int numberOfBookmarks = bookmarksListView.getCount();
402 for (int i = 0; i < numberOfBookmarks; i++) {
403 bookmarksListView.setItemChecked(i, true);
408 // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
409 deletingBookmarks = true;
411 // Get an array of the selected row IDs.
412 long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
414 // 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.
415 SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
417 // Update the bookmarks cursor with the current contents of the bookmarks database except for the specified database IDs.
418 switch (currentFolderDatabaseId) {
419 // Get a cursor with all the folders.
420 case ALL_FOLDERS_DATABASE_ID:
421 if (sortByDisplayOrder) {
422 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray);
424 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksExcept(selectedBookmarksIdsLongArray);
428 // Get a cursor for the home folder.
429 case HOME_FOLDER_DATABASE_ID:
430 if (sortByDisplayOrder) {
431 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, "");
433 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, "");
437 // Display the selected folder.
439 // Get a cursor for the selected folder.
440 if (sortByDisplayOrder) {
441 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrderExcept(selectedBookmarksIdsLongArray, currentFolderName);
443 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksExcept(selectedBookmarksIdsLongArray, currentFolderName);
447 // Update the list view.
448 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
450 // Show a Snackbar with the number of deleted bookmarks.
451 bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_databaseview_coordinatorlayout),
452 getString(R.string.bookmarks_deleted) + " " + selectedBookmarksIdsLongArray.length, Snackbar.LENGTH_LONG)
453 .setAction(R.string.undo, view -> {
454 // Do nothing because everything will be handled by `onDismissed()` below.
456 .addCallback(new Snackbar.Callback() {
458 public void onDismissed(Snackbar snackbar, int event) {
460 // The user pushed the `Undo` button.
461 case Snackbar.Callback.DISMISS_EVENT_ACTION:
462 // Update the bookmarks list view with the current contents of the bookmarks database, including the "deleted bookmarks.
463 updateBookmarksListView();
465 // Re-select the previously selected bookmarks.
466 for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
467 bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
471 // The Snackbar was dismissed without the `Undo` button being pushed.
473 // Delete each selected bookmark.
474 for (long databaseIdLong : selectedBookmarksIdsLongArray) {
475 // Convert `databaseIdLong` to an int.
476 int databaseIdInt = (int) databaseIdLong;
478 // Delete the selected bookmark.
479 bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
483 // Reset the deleting bookmarks flag.
484 deletingBookmarks = false;
486 // Enable the delete menu item.
487 deleteMenuItem.setEnabled(true);
489 // Close the activity if back has been pressed.
490 if (closeActivityAfterDismissingSnackbar) {
496 // Show the Snackbar.
497 bookmarksDeletedSnackbar.show();
501 // Consume the click.
506 public void onDestroyActionMode(ActionMode mode) {
513 public boolean onCreateOptionsMenu(Menu menu) {
515 getMenuInflater().inflate(R.menu.bookmarks_databaseview_options_menu, menu);
522 public boolean onOptionsItemSelected(MenuItem menuItem) {
523 // Get the ID of the menu item that was selected.
524 int menuItemId = menuItem.getItemId();
526 switch (menuItemId) {
527 case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
528 // Exit the activity.
532 case R.id.options_menu_sort:
533 // Update the sort by display order tracker.
534 sortByDisplayOrder = !sortByDisplayOrder;
536 // Get a handle for the bookmarks `ListView`.
537 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
539 // Update the icon and display a snackbar.
540 if (sortByDisplayOrder) { // Sort by display order.
541 // Update the icon according to the theme.
542 if (MainWebViewActivity.darkTheme) {
543 menuItem.setIcon(R.drawable.sort_selected_dark);
545 menuItem.setIcon(R.drawable.sort_selected_light);
548 // Display a Snackbar indicating the current sort type.
549 Snackbar.make(bookmarksListView, R.string.sorted_by_display_order, Snackbar.LENGTH_SHORT).show();
550 } else { // Sort by database id.
551 // Update the icon according to the theme.
552 if (MainWebViewActivity.darkTheme) {
553 menuItem.setIcon(R.drawable.sort_dark);
555 menuItem.setIcon(R.drawable.sort_light);
558 // Display a Snackbar indicating the current sort type.
559 Snackbar.make(bookmarksListView, R.string.sorted_by_database_id, Snackbar.LENGTH_SHORT).show();
562 // Update the list view.
563 updateBookmarksListView();
570 public void onBackPressed() {
571 // Check to see if a snackbar is currently displayed. If so, it must be closed before existing so that a pending delete is completed before reloading the list view in the bookmarks activity.
572 if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home.
573 // Set the close flag.
574 closeActivityAfterDismissingSnackbar = true;
576 // Dismiss the snackbar.
577 bookmarksDeletedSnackbar.dismiss();
578 } else { // Go home immediately.
579 // Update the current folder in the bookmarks activity.
580 switch (currentFolderDatabaseId) {
581 case ALL_FOLDERS_DATABASE_ID:
582 // Load the home folder.
583 BookmarksActivity.currentFolder = "";
586 case HOME_FOLDER_DATABASE_ID:
587 // Load the home folder.
588 BookmarksActivity.currentFolder = "";
592 // Load the current folder.
593 BookmarksActivity.currentFolder = currentFolderName;
596 // Reload the bookmarks list view when returning to the bookmarks activity.
597 BookmarksActivity.restartFromBookmarksDatabaseViewActivity = true;
599 // Exit the bookmarks database view activity.
600 super.onBackPressed();
604 private void updateBookmarksListView() {
605 // Populate the bookmarks list view based on the spinner selection.
606 switch (currentFolderDatabaseId) {
607 // Get a cursor with all the folders.
608 case ALL_FOLDERS_DATABASE_ID:
609 if (sortByDisplayOrder) {
610 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksByDisplayOrder();
612 bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
616 // Get a cursor for the home folder.
617 case HOME_FOLDER_DATABASE_ID:
618 if (sortByDisplayOrder) {
619 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder("");
621 bookmarksCursor = bookmarksDatabaseHelper.getBookmarks("");
625 // Display the selected folder.
627 // Get a cursor for the selected folder.
628 if (sortByDisplayOrder) {
629 bookmarksCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(currentFolderName);
631 bookmarksCursor = bookmarksDatabaseHelper.getBookmarks(currentFolderName);
635 // Update the list view.
636 bookmarksCursorAdapter.changeCursor(bookmarksCursor);
639 private void selectAllBookmarksInFolder(int folderId) {
640 // Get a handle for the bookmarks list view.
641 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
643 // Get the folder name.
644 String folderName = bookmarksDatabaseHelper.getFolderName(folderId);
646 // Get a cursor with the contents of the folder.
647 Cursor folderCursor = bookmarksDatabaseHelper.getBookmarks(folderName);
649 // Move to the beginning of the cursor.
650 folderCursor.moveToFirst();
652 while (folderCursor.getPosition() < folderCursor.getCount()) {
653 // Get the bookmark database ID.
654 int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
656 // Move the bookmarks cursor to the first position.
657 bookmarksCursor.moveToFirst();
659 // Initialize the bookmark position variable.
660 int bookmarkPosition = -1;
662 // Get the position of this bookmark in the bookmarks cursor.
663 while ((bookmarkPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) {
664 // Check if the bookmark IDs match.
665 if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID))) {
666 // Get the bookmark position.
667 bookmarkPosition = bookmarksCursor.getPosition();
669 // If this bookmark is a folder, select all the bookmarks inside it.
670 if (bookmarksDatabaseHelper.isFolder(bookmarkId)) {
671 selectAllBookmarksInFolder(bookmarkId);
674 // Select the bookmark.
675 bookmarksListView.setItemChecked(bookmarkPosition, true);
678 // Increment the bookmarks cursor position.
679 bookmarksCursor.moveToNext();
682 // Move to the next position.
683 folderCursor.moveToNext();
688 public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
689 // Get handles for the views from dialog fragment.
690 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
691 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
692 EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
693 Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
694 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
696 // Extract the bookmark information.
697 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
698 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
699 int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
700 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
702 // Instantiate the parent folder name `String`.
703 String parentFolderNameString;
705 // Set the parent folder name.
706 if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
707 parentFolderNameString = "";
708 } else { // Get the parent folder name from the database.
709 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
712 // Update the bookmark.
713 if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
714 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
715 } else { // Update the bookmark using the `WebView` favorite icon.
716 // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
717 ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
718 MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
719 byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
721 // Update the bookmark and the favorite icon.
722 bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
725 // Update the list view.
726 updateBookmarksListView();
730 public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
731 // Get handles for the views from dialog fragment.
732 RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
733 RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
734 ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
735 EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
736 Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
737 EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
739 // Extract the folder information.
740 String newFolderNameString = editBookmarkNameEditText.getText().toString();
741 int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
742 int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
744 // Instantiate the parent folder name `String`.
745 String parentFolderNameString;
747 // Set the parent folder name.
748 if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) { // The home folder is selected. Use `""`.
749 parentFolderNameString = "";
750 } else { // Get the parent folder name from the database.
751 parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
754 // Update the folder.
755 if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon.
756 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
757 } else { // Update the folder and the icon.
758 // Instantiate the new folder icon `Bitmap`.
759 Bitmap folderIconBitmap;
761 // Populate the new folder icon bitmap.
762 if (defaultFolderIconRadioButton.isChecked()) {
763 // Get the default folder icon and convert it to a `Bitmap`.
764 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
765 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
766 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
767 } else { // Use the `WebView` favorite icon.
768 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
771 // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG).
772 ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
773 folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
774 byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
776 // Update the folder and the icon.
777 bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
780 // Update the list view.
781 updateBookmarksListView();
785 public void onDestroy() {
786 // Close the bookmarks cursor and database.
787 bookmarksCursor.close();
788 bookmarksDatabaseHelper.close();
790 // Run the default commands.