X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksActivity.java;h=2f6738cd88a9064b02b6d9a405f411b90037d0bc;hp=68830965c84182d1abcada6c7061740500e79fab;hb=897b2d7f61a0492f228e3c172d7c6a76e3c5f3ac;hpb=871520acac2bd75f528e943c1b2a650017c852b9 diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java index 68830965..2f6738cd 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java @@ -34,6 +34,7 @@ import android.support.design.widget.Snackbar; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +// `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22. import android.support.v7.app.AppCompatDialogFragment; import android.support.v7.widget.Toolbar; import android.util.SparseBooleanArray; @@ -43,7 +44,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; -import android.widget.AdapterView; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ImageView; @@ -61,40 +61,46 @@ import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; import java.io.ByteArrayOutputStream; -public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener, EditBookmarkFolderDialog.EditBookmarkFolderListener, - MoveToFolderDialog.MoveToFolderListener { - - // `bookmarksDatabaseHelper` is public static so it can be accessed from `CreateBookmarkFolderDialog`, `EditBookmarkDialog`, `EditBookmarkFolderDialog` and `MoveToFolderDialog`. It is also used in `onCreate()`, - // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - public static BookmarksDatabaseHelper bookmarksDatabaseHelper; +public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener, + EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener { // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`. - // It is used in `onCreate`, `onOptionsItemSelected()`, `onCreateBookmarkCreate`, `onCreateBookmarkFolderCreate`, and `onEditBookmarkSave`. + // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `loadFolder()`. public static String currentFolder; - // `checkedItemIds` is public static so it can be accessed from `EditBookmarkDialog`, `EditBookmarkFolderDialog`, and `MoveToFolderDialog`. - // It is also used in `onActionItemClicked`. + // `checkedItemIds` is public static so it can be accessed from `MoveToFolderDialog`. It is also used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`. public static long[] checkedItemIds; - // `bookmarksListView` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. + // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, + // and `loadFolder(). + private BookmarksDatabaseHelper bookmarksDatabaseHelper; + + // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `updateMoveIcons()`, `scrollBookmarks()`, + // and `loadFolder()`. private ListView bookmarksListView; - // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`. - private ActionMode contextualActionMode; + // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder()`. + private Cursor bookmarksCursor; - // `selectedBookmarkPosition` is used in `onCreate()` and `onEditBookmarkSave()`. - private int selectedBookmarkPosition; + // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`. + private CursorAdapter bookmarksCursorAdapter; - // `appBar` is used in `onCreate()` and `updateBookmarksListView()`. - private ActionBar appBar; + // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`. + private ActionMode contextualActionMode; - // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - private Cursor bookmarksCursor; + // `appBar` is used in `onCreate()` and `loadFolder()`. + private ActionBar appBar; - // `oldFolderName` is used in `onCreate()` and `onEditBookmarkFolderSave()`. + // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`. private String oldFolderNameString; + // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons`. + private MenuItem moveBookmarkUpMenuItem; + + // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons`. + private MenuItem moveBookmarkDownMenuItem; + @Override protected void onCreate(Bundle savedInstanceState) { // Set the activity theme. @@ -107,74 +113,85 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Run the default commands. super.onCreate(savedInstanceState); + // Get the intent that launched the activity. + Intent launchingIntent = getIntent(); + + // Set the current folder variable. + if (launchingIntent.getStringExtra("Current Folder") != null) { // Set the current folder from the intent. + currentFolder = launchingIntent.getStringExtra("Current Folder"); + } else { // Set the current folder to be `""`, which is the home folder. + currentFolder = ""; + } + // Set the content view. setContentView(R.layout.bookmarks_coordinatorlayout); // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. - final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); + final Toolbar bookmarksAppBar = findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); - // Display the home arrow on `SupportActionBar`. + // Get a handle for the activity, the app bar, and the `ListView`. + final Activity bookmarksActivity = this; appBar = getSupportActionBar(); - assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. - appBar.setDisplayHomeAsUpEnabled(true); + bookmarksListView = findViewById(R.id.bookmarks_listview); + // This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. + assert appBar != null; - // Initialize the database helper and the `ListView`. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. + // Display the home arrow on `SupportActionBar`. + appBar.setDisplayHomeAsUpEnabled(true); + + // Initialize the database helper. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); - bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); - // Set currentFolder to the home folder, which is `""` in the database. - currentFolder = ""; + // Load the home folder. + loadFolder(); - // Display the bookmarks in the ListView. - updateBookmarksListView(currentFolder); + // Set a listener so that tapping a list item loads the URL or folder. + bookmarksListView.setOnItemClickListener((parent, view, position, id) -> { + // Convert the id from long to int to match the format of the bookmarks database. + int databaseID = (int) id; - // Set a listener so that tapping a list item loads the URL. We need to store the activity in a variable so that we can return to the parent activity after loading the URL. - final Activity bookmarksActivity = this; - bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - // Convert the id from long to int to match the format of the bookmarks database. - int databaseID = (int) id; - - // Get the bookmark `Cursor` for this ID and move it to the first row. - Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID); - bookmarkCursor.moveToFirst(); - - // If the bookmark is a folder load its contents into the ListView. - if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { - // Update `currentFolder`. - currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - - // Reload the ListView with `currentFolder`. - updateBookmarksListView(currentFolder); - } else { // Load the URL into `mainWebView`. - // Get the bookmark URL and assign it to formattedUrlString. `mainWebView` will automatically reload when `BookmarksActivity` closes. - MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); - - NavUtils.navigateUpFromSameTask(bookmarksActivity); - } + // Get the bookmark cursor for this ID and move it to the first row. + Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID); + bookmarkCursor.moveToFirst(); + + // Act upon the bookmark according to the type. + if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder. + // Update the current folder. + currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); + + // Load the new folder. + loadFolder(); + } else { // The selected bookmark is not a folder. + // Get the bookmark URL and assign it to `formattedUrlString`. + MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); + + // Set `MainWebViewActivity` to load the new URL on restart. + MainWebViewActivity.loadUrlOnRestart = true; + + // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`. + MainWebViewActivity.currentBookmarksFolder = currentFolder; - // Close the `Cursor`. - bookmarkCursor.close(); + // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`. + MainWebViewActivity.restartFromBookmarksActivity = true; + + // Return to `MainWebViewActivity`. + NavUtils.navigateUpFromSameTask(bookmarksActivity); } + + // Close the `Cursor`. + bookmarkCursor.close(); }); // `MultiChoiceModeListener` handles long clicks. bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { - // `moveBookmarkUpMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkUpMenuItem; - - // `moveBookmarkDownMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkDownMenuItem; - - // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. + // Instantiate the common variables. MenuItem editBookmarkMenuItem; - - // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`. + MenuItem deleteBookmarksMenuItem; MenuItem selectAllBookmarksMenuItem; + boolean deletingBookmarks; @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { @@ -189,15 +206,22 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma mode.setTitle(currentFolder); } - // Get a handle for MenuItems we need to selectively disable. + // Get a handle for `MenuItems` that need to be selectively disabled. moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up); moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down); editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark); + deleteBookmarksMenuItem = menu.findItem(R.id.delete_bookmark); selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks); - // Store `contextualActionMode` so we can close it programatically. + // Disable the delete bookmarks menu item if a delete is pending. + if (deletingBookmarks) { + deleteBookmarksMenuItem.setEnabled(false); + } + + // Store `contextualActionMode` so it can be closed programatically. contextualActionMode = mode; + // Make it so. return true; } @@ -212,22 +236,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Enable the move to folder menu item if at least one folder exists. moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0); - // `return true` indicates that the menu has been updated. + // Make it so. return true; } @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - // Get an array of the selected bookmarks. + // Get a long array of the selected bookmarks. long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); // Calculate the number of selected bookmarks. int numberOfSelectedBookmarks = selectedBookmarksLongArray.length; - // Adjust the `mode` and the menu for the number of selected bookmarks. - if (numberOfSelectedBookmarks == 0) { + // Adjust the `ActionMode` and the `Menu` according to the number of selected bookmarks. + if (numberOfSelectedBookmarks == 0) { // No bookmarks are selected. + // Close the `ActionMode`. mode.finish(); - } else if (numberOfSelectedBookmarks == 1) { + } else if (numberOfSelectedBookmarks == 1) { // One bookmark is selected. // List the number of selected bookmarks in the subtitle. mode.setSubtitle(getString(R.string.one_selected)); @@ -236,41 +261,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma moveBookmarkDownMenuItem.setVisible(true); editBookmarkMenuItem.setVisible(true); - // Get the database IDs for the bookmarks. - int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; - int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0); - // bookmarksListView is 0 indexed. - int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1); - - // Disable `moveBookmarkUpMenuItem` if the selected bookmark is at the top of the ListView. - if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { - moveBookmarkUpMenuItem.setEnabled(false); - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled); - } else { // Otherwise enable `moveBookmarkUpMenuItem`. - moveBookmarkUpMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark); - } else { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light); - } - } - - // Disable `moveBookmarkDownMenuItem` if the selected bookmark is at the bottom of the ListView. - if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { - moveBookmarkDownMenuItem.setEnabled(false); - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled); - } else { // Otherwise enable `moveBookmarkDownMenuItem`. - moveBookmarkDownMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark); - } else { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light); - } - } + // Update the enabled status of the move icons. + updateMoveIcons(); } else { // More than one bookmark is selected. // List the number of selected bookmarks in the subtitle. mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected)); @@ -295,20 +287,20 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma int menuItemId = item.getItemId(); // Instantiate the common variables. - int numberOfBookmarks; + int selectedBookmarkPosition; int selectedBookmarkNewPosition; - SparseBooleanArray bookmarkPositionSparseBooleanArray; + final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray; switch (menuItemId) { case R.id.move_bookmark_up: - // Get the array of checked bookmarks. - bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + // Get the array of checked bookmark positions. + selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - // Store the position of the selected bookmark. - selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); + // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_up` is enabled. + selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0); - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; + // Calculate the new position of the selected bookmark. + selectedBookmarkNewPosition = selectedBookmarkPosition - 1; // Iterate through the bookmarks. for (int i = 0; i < bookmarksListView.getCount(); i++) { @@ -319,7 +311,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark. // Move the current bookmark up one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1); - selectedBookmarkNewPosition = i - 1; } else if ((i + 1) == selectedBookmarkPosition){ // The current bookmark is immediately above the selected bookmark. // Move the current bookmark down one. bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1); @@ -334,26 +325,28 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } } - // Refresh the ListView. - updateBookmarksListView(currentFolder); + // Update `bookmarksCursor` with the current contents of the bookmarks database. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); - // Select the previously selected bookmark in the new location. - bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true); + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); - // Scroll `bookmarksListView` to five items above `selectedBookmarkNewPosition`. - bookmarksListView.setSelection(selectedBookmarkNewPosition - 5); + // Scroll with the bookmark. + scrollBookmarks(selectedBookmarkNewPosition); + // Update the enabled status of the move icons. + updateMoveIcons(); break; case R.id.move_bookmark_down: - // Get the array of checked bookmarks. - bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + // Get the array of checked bookmark positions. + selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - // Store the position of the selected bookmark. - selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); + // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_down` is enabled. + selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0); - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; + // Calculate the new position of the selected bookmark. + selectedBookmarkNewPosition = selectedBookmarkPosition + 1; // Iterate through the bookmarks. for (int i = 0; i { + // Do nothing because everything will be handled by `onDismissed()` below. }) .addCallback(new Snackbar.Callback() { @Override @@ -465,25 +459,32 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma switch (event) { // The user pushed the `Undo` button. case Snackbar.Callback.DISMISS_EVENT_ACTION: - // Refresh the ListView to show the rows again. - updateBookmarksListView(currentFolder); + // Update `bookmarksCursor` with the current contents of the bookmarks database, including the "deleted" bookmarks. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); + + // Re-select the previously selected bookmarks. + for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) { + bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true); + } - // Scroll to where the first deleted bookmark was located. - bookmarksListView.setSelection(selectedBookmarkPosition - 5); break; // The `Snackbar` was dismissed without the `Undo` button being pushed. default: - // Delete each selected row. - for (long databaseIdLong : selectedBookmarksLongArray) { + // Delete each selected bookmark. + for (long databaseIdLong : selectedBookmarksIdsLongArray) { // Convert `databaseIdLong` to an int. int databaseIdInt = (int) databaseIdLong; + // Delete the contents of the folder if the selected bookmark is a folder. if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) { deleteBookmarkFolderContents(databaseIdInt); } - // Delete `databaseIdInt`. + // Delete the selected bookmark. bookmarksDatabaseHelper.deleteBookmark(databaseIdInt); } @@ -500,43 +501,59 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i); } } - break; } + + // Enable the delete bookmarks menu item. + deleteBookmarksMenuItem.setEnabled(true); + + // Reset the deleting bookmarks flag. + deletingBookmarks = false; } }) + //Show the `SnackBar`. .show(); - // Close the contextual app bar. + // Close the `ActionBar`. mode.finish(); break; case R.id.context_menu_select_all_bookmarks: - numberOfBookmarks = bookmarksListView.getCount(); + // Get the total number of bookmarks. + int numberOfBookmarks = bookmarksListView.getCount(); + // Select them all. for (int i = 0; i < numberOfBookmarks; i++) { bookmarksListView.setItemChecked(i, true); } break; } + // Consume the click. return true; } @Override public void onDestroyActionMode(ActionMode mode) { - + // Do nothing. } }); - // Set a FloatingActionButton for creating new bookmarks. - FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab); - createBookmarkFAB.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - // Show the `CreateBookmarkDialog` `AlertDialog` and name the instance `@string/create_bookmark`. - AppCompatDialogFragment createBookmarkDialog = new CreateBookmarkDialog(); - createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark)); - } + // Get handles for the `FloatingActionButtons`. + FloatingActionButton createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab); + FloatingActionButton createBookmarkFab = findViewById(R.id.create_bookmark_fab); + + // Set the create new bookmark folder FAB to display the `AlertDialog`. + createBookmarkFolderFab.setOnClickListener(v -> { + // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`. + AppCompatDialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog(); + createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder)); + }); + + // Set the create new bookmark FAB to display the `AlertDialog`. + createBookmarkFab.setOnClickListener(view -> { + // Show the `CreateBookmarkDialog` `AlertDialog` and name the instance `@string/create_bookmark`. + AppCompatDialogFragment createBookmarkDialog = new CreateBookmarkDialog(); + createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark)); }); } @@ -556,26 +573,29 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma switch (menuItemId) { case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`. - if (currentFolder.isEmpty()) { // Exit BookmarksActivity if currently in the home folder. + if (currentFolder.isEmpty()) { // Currently in the home folder. + // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`. + MainWebViewActivity.currentBookmarksFolder = ""; + + // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`. + MainWebViewActivity.restartFromBookmarksActivity = true; + + // Return to `MainWebViewActivity`. NavUtils.navigateUpFromSameTask(this); - } else { // Navigate up one folder. + } else { // Currently in a subfolder. // Place the former parent folder in `currentFolder`. currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); - // Update `bookmarksListView`. - updateBookmarksListView(currentFolder); + // Load the new folder. + loadFolder(); } break; - case R.id.create_folder: - // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`. - AppCompatDialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog(); - createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder)); - break; - case R.id.options_menu_select_all_bookmarks: + // Get the total number of bookmarks. int numberOfBookmarks = bookmarksListView.getCount(); + // Select them all. for (int i = 0; i < numberOfBookmarks; i++) { bookmarksListView.setItemChecked(i, true); } @@ -592,30 +612,36 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void onBackPressed() { - if (currentFolder.isEmpty()) { // Exit BookmarksActivity if currently in the home folder. + if (currentFolder.isEmpty()) { // Currently in the home folder. + // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`. + MainWebViewActivity.currentBookmarksFolder = ""; + + // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`. + MainWebViewActivity.restartFromBookmarksActivity = true; + + // Exit `BookmarksActivity`. super.onBackPressed(); - } else { // Navigate up one folder. + } else { // Currently in a subfolder. // Place the former parent folder in `currentFolder`. currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); - // Reload the `ListView`. - updateBookmarksListView(currentFolder); + // Load the new folder. + loadFolder(); } } @Override public void onCreateBookmark(AppCompatDialogFragment dialogFragment) { // Get the `EditTexts` from the `dialogFragment`. - EditText createBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext); - EditText createBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext); + EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext); + EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext); // Extract the strings from the `EditTexts`. String bookmarkNameString = createBookmarkNameEditText.getText().toString(); String bookmarkUrlString = createBookmarkUrlEditText.getText().toString(); - // Convert the favoriteIcon Bitmap to a byte array. + // Convert the favoriteIcon Bitmap to a byte array. `0` is for lossless compression (the only option for a PNG). ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); - // `0` is for lossless compression (the only option for a PNG). MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream); byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray(); @@ -623,29 +649,36 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma int newBookmarkDisplayOrder = bookmarksListView.getCount(); // Create the bookmark. - bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, currentFolder, favoriteIconByteArray); + bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentFolder, newBookmarkDisplayOrder, favoriteIconByteArray); + + // Update `bookmarksCursor` with the current contents of this folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); - // Refresh the `ListView`. `setSelection` scrolls to the bottom of the list. - updateBookmarksListView(currentFolder); + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); + + // Scroll to the new bookmark. bookmarksListView.setSelection(newBookmarkDisplayOrder); } @Override public void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment) { - // Get `create_folder_name_edit_text` and extract the string. - EditText createFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext); + // Get handles for the views in `dialogFragment`. + EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext); + RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton); + ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon); + + // Get new folder name string. String folderNameString = createFolderNameEditText.getText().toString(); // Get the new folder icon `Bitmap`. - RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton); Bitmap folderIconBitmap; - if (defaultFolderIconRadioButton.isChecked()) { - // Get the default folder icon `ImageView` from the `Dialog` and convert it to a `Bitmap`. - ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon); + if (defaultFolderIconRadioButton.isChecked()) { // Use the default folder icon. + // Get the default folder icon and convert it to a `Bitmap`. Drawable folderIconDrawable = folderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); - } else { // Assign `favoriteIcon` from the `WebView`. + } else { // Use the `WebView` favorite icon. folderIconBitmap = MainWebViewActivity.favoriteIconBitmap; } @@ -660,31 +693,35 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarksDatabaseHelper.updateDisplayOrder(databaseId, i + 1); } - // Create the folder, placing it at the top of the ListView - bookmarksDatabaseHelper.createFolder(folderNameString, 0, currentFolder, folderIconByteArray); + // Create the folder, which will be placed at the top of the `ListView`. + bookmarksDatabaseHelper.createFolder(folderNameString, currentFolder, folderIconByteArray); + + // Update `bookmarksCursor` with the current contents of this folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); - // Refresh the ListView. - updateBookmarksListView(currentFolder); + // Scroll to the new folder. + bookmarksListView.setSelection(0); } @Override - public void onSaveEditBookmark(AppCompatDialogFragment dialogFragment) { - // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`. - long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); - int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; + public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) { + // Get handles for the views from `dialogFragment`. + EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); + EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext); + RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton); - // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings. - EditText editBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); + // Store the bookmark strings. String bookmarkNameString = editBookmarkNameEditText.getText().toString(); - EditText editBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext); String bookmarkUrlString = editBookmarkUrlEditText.getText().toString(); - // Get `edit_bookmark_current_icon_radiobutton`. - RadioButton currentBookmarkIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton); - + // Update the bookmark. if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon. bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString); } else { // Update the bookmark using the `WebView` favorite icon. + // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG). ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream(); MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream); byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray(); @@ -696,25 +733,24 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Close the contextual action mode. contextualActionMode.finish(); - // Refresh the `ListView`. `setSelection` scrolls to the position of the bookmark that was edited. - updateBookmarksListView(currentFolder); - bookmarksListView.setSelection(selectedBookmarkPosition); + // Update `bookmarksCursor` with the contents of the current folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); } @Override - public void onEditBookmarkFolder(AppCompatDialogFragment dialogFragment) { + public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) { + // Get handles for the views from `dialogFragment`. + RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton); + RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton); + ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview); + EditText editFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext); + // Get the new folder name. - EditText editFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext); String newFolderNameString = editFolderNameEditText.getText().toString(); - // Get a long array with the the database ID of the selected folder and convert it to an `int`. - long[] selectedFolderLongArray = bookmarksListView.getCheckedItemIds(); - int selectedFolderDatabaseId = (int) selectedFolderLongArray[0]; - - // Get the `RadioButtons` from the `Dialog`. - RadioButton currentFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton); - RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton); - // Check if the favorite icon has changed. if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed. // Update the name in the database. @@ -723,12 +759,11 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get the new folder icon `Bitmap`. Bitmap folderIconBitmap; if (defaultFolderIconRadioButton.isChecked()) { - // Get the default folder icon `ImageView` from the `Drawable` and convert it to a `Bitmap`. - ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon); - Drawable folderIconDrawable = folderIconImageView.getDrawable(); + // Get the default folder icon and convert it to a `Bitmap`. + Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); - } else { // Get the web page icon `ImageView` from the `Dialog`. + } else { // Use the `WebView` favorite icon. folderIconBitmap = MainWebViewActivity.favoriteIconBitmap; } @@ -740,15 +775,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Update the folder icon in the database. bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, folderIconByteArray); } else { // The folder icon and the name have changed. - // Get the new folder icon `Bitmap`. + // Instantiate the new folder icon `Bitmap`. Bitmap folderIconBitmap; + + // Populate the new folder icon bitmap. if (defaultFolderIconRadioButton.isChecked()) { - // Get the default folder icon `ImageView` from the `Drawable` and convert it to a `Bitmap`. - ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon); - Drawable folderIconDrawable = folderIconImageView.getDrawable(); + // Get the default folder icon and convert it to a `Bitmap`. + Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); - } else { // Get the web page icon `ImageView` from the `Dialog`. + } else { // Use the `WebView` favorite icon. folderIconBitmap = MainWebViewActivity.favoriteIconBitmap; } @@ -761,9 +797,11 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray); } - // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited. - updateBookmarksListView(currentFolder); - bookmarksListView.setSelection(selectedBookmarkPosition - 5); + // Update `bookmarksCursor` with the current contents of this folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); // Close the contextual action mode. contextualActionMode.finish(); @@ -771,16 +809,19 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void onMoveToFolder(AppCompatDialogFragment dialogFragment) { - // Get the new folder database id. - ListView folderListView = (ListView) dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview); + // Get a handle for the `ListView` from `dialogFragment`. + ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview); + + // Store a long array of the selected folders. long[] newFolderLongArray = folderListView.getCheckedItemIds(); - // Get the new folder database ID. + // Get the new folder database ID. Only one folder will be selected. int newFolderDatabaseId = (int) newFolderLongArray[0]; // Instantiate `newFolderName`. String newFolderName; + // Set the new folder name. if (newFolderDatabaseId == 0) { // The new folder is the home folder, represented as `""` in the database. newFolderName = ""; @@ -791,6 +832,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get a long array with the the database ID of the selected bookmarks. long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); + + // Move each of the selected bookmarks to the new folder. for (long databaseIdLong : selectedBookmarksLongArray) { // Get `databaseIdInt` for each selected bookmark. int databaseIdInt = (int) databaseIdLong; @@ -799,127 +842,156 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName); } - // Refresh the `ListView`. - updateBookmarksListView(currentFolder); + // Update `bookmarksCursor` with the current contents of this folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); + + // Update the `ListView`. + bookmarksCursorAdapter.changeCursor(bookmarksCursor); // Close the contextual app bar. contextualActionMode.finish(); } - private void updateBookmarksListView(String folderName) { - // Get a `Cursor` with the current contents of the bookmarks database. - bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName); + private void deleteBookmarkFolderContents(int databaseId) { + // Get the name of the folder. + String folderName = bookmarksDatabaseHelper.getFolderName(databaseId); - // Setup `bookmarksCursorAdapter` with `this` context. `false` disables `autoRequery`. - CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { - @Override - public View newView(Context context, Cursor cursor, ViewGroup parent) { - // Inflate the individual item layout. `false` does not attach it to the root. - return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false); - } + // Get the contents of the folder. + Cursor folderCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName); - @Override - public void bindView(View view, Context context, Cursor cursor) { - // Get the favorite icon byte array from the `Cursor`. - byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); + // Delete each of the bookmarks in the folder. + for (int i = 0; i < folderCursor.getCount(); i++) { + // Move `folderCursor` to the current row. + folderCursor.moveToPosition(i); - // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. - Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + // Get the database ID of the item. + int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID)); - // Display the bitmap in `bookmarkFavoriteIcon`. - ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); - bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); + // If this is a folder, recursively delete the contents first. + if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) { + deleteBookmarkFolderContents(itemDatabaseId); + } + // Delete the bookmark. + bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId); + } + } - // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. - String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); - bookmarkNameTextView.setText(bookmarkNameString); + private void updateMoveIcons() { + // Get a long array of the selected bookmarks. + long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); - // Make the font bold for folders. - if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { - bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD); - } else { // Reset the font to default for normal bookmarks. - bookmarkNameTextView.setTypeface(Typeface.DEFAULT); - } + // Get the database IDs for the first, last, and selected bookmarks. + int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; + int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0); + // bookmarksListView is 0 indexed. + int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1); + + // Update the move bookmark up `MenuItem`. + if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { // The selected bookmark is in the first position. + // Disable the move bookmark up `MenuItem`. + moveBookmarkUpMenuItem.setEnabled(false); + + // Set the move bookmark up icon to be ghosted. + moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled); + } else { // The selected bookmark is not in the first position. + // Enable the move bookmark up `MenuItem`. + moveBookmarkUpMenuItem.setEnabled(true); + + // Set the icon according to the theme. + if (MainWebViewActivity.darkTheme) { + moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark); + } else { + moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light); } - }; + } - // Update the ListView. - bookmarksListView.setAdapter(bookmarksCursorAdapter); + // Update the move bookmark down `MenuItem`. + if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { // The selected bookmark is in the last position. + // Disable the move bookmark down `MenuItem`. + moveBookmarkDownMenuItem.setEnabled(false); + + // Set the move bookmark down icon to be ghosted. + moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled); + } else { // The selected bookmark is not in the last position. + // Enable the move bookmark down `MenuItem`. + moveBookmarkDownMenuItem.setEnabled(true); + + // Set the icon according to the theme. + if (MainWebViewActivity.darkTheme) { + moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark); + } else { + moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light); + } + } + } - // Set the AppBar title. - if (currentFolder.isEmpty()) { - appBar.setTitle(R.string.bookmarks); - } else { - appBar.setTitle(currentFolder); + private void scrollBookmarks(int selectedBookmarkPosition) { + // Get the first and last visible bookmark positions. + int firstVisibleBookmarkPosition = bookmarksListView.getFirstVisiblePosition(); + int lastVisibleBookmarkPosition = bookmarksListView.getLastVisiblePosition(); + + // Calculate the number of bookmarks per screen. + int numberOfBookmarksPerScreen = lastVisibleBookmarkPosition - firstVisibleBookmarkPosition; + + // Scroll with the moved bookmark if necessary. + if (selectedBookmarkPosition <= firstVisibleBookmarkPosition) { // The selected bookmark position is at or above the top of the screen. + // Scroll to the selected bookmark position. + bookmarksListView.setSelection(selectedBookmarkPosition); + } 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`. + // 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`. + bookmarksListView.setSelection(selectedBookmarkPosition - numberOfBookmarksPerScreen + 1); } } - private void updateBookmarksListViewExcept(long[] exceptIdLongArray, String folderName) { - // Get a `Cursor` with the current contents of the bookmarks database except for the specified database IDs. - bookmarksCursor = bookmarksDatabaseHelper.getBookmarksCursorExcept(exceptIdLongArray, folderName); + private void loadFolder() { + // Update `bookmarksCursor` with the contents of the bookmarks database for the current folder. + bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); - // Setup `bookmarksCursorAdapter` with `this` context. `false` disables autoRequery. - CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + // Setup a `CursorAdapter`. `this` specifies the `Context`. `false` disables `autoRequery`. + bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate the individual item layout. `false` does not attach it to the root. - return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false); + return getLayoutInflater().inflate(R.layout.bookmarks_activity_item_linearlayout, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { - // Get the favorite icon byte array from the cursor. + // Get handles for the views. + ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmark_favorite_icon); + TextView bookmarkNameTextView = view.findViewById(R.id.bookmark_name); + + // Get the favorite icon byte array from the `Cursor`. byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); - // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. + // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last. Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); // Display the bitmap in `bookmarkFavoriteIcon`. - ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); - // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); bookmarkNameTextView.setText(bookmarkNameString); // Make the font bold for folders. if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { - // The first argument is `null` because we don't want to change the font. - bookmarkNameTextView.setTypeface(null, Typeface.BOLD); - } else { // Reset the font to default. + bookmarkNameTextView.setTypeface(Typeface.DEFAULT_BOLD); + } else { // Reset the font to default for normal bookmarks. bookmarkNameTextView.setTypeface(Typeface.DEFAULT); } } }; - // Update the `ListView`. + // Populate the `ListView` with the adapter. bookmarksListView.setAdapter(bookmarksCursorAdapter); - } - private void deleteBookmarkFolderContents(int databaseId) { - // Get the name of the folder. - String folderName = bookmarksDatabaseHelper.getFolderName(databaseId); - - // Get the contents of the folder. - Cursor folderCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName); - - for (int i = 0; i < folderCursor.getCount(); i++) { - // Move `folderCursor` to the current row. - folderCursor.moveToPosition(i); - - // Get the database ID of the item. - int itemDatabaseId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID)); - - // If this is a folder, delete the contents first. - if (bookmarksDatabaseHelper.isFolder(itemDatabaseId)) { - deleteBookmarkFolderContents(itemDatabaseId); - } - - bookmarksDatabaseHelper.deleteBookmark(itemDatabaseId); + // Set the `AppBar` title. + if (currentFolder.isEmpty()) { + appBar.setTitle(R.string.bookmarks); + } else { + appBar.setTitle(currentFolder); } } } \ No newline at end of file