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;
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;
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 `EditBookmarkDialog` 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.
// 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);
- // We need to use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
- final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar);
+ // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21.
+ 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;
+ // Display the home arrow on `SupportActionBar`.
+ appBar.setDisplayHomeAsUpEnabled(true);
- // Initialize the database handler and the `ListView`. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
+ // 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();
- // Close the `Cursor`.
- bookmarkCursor.close();
+ // Act upon the bookmark according to the type.
+ if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
+ // Update `currentFolder`.
+ 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`. `mainWebView` will automatically reload when `BookmarksActivity` closes.
+ MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
+
+ // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`.
+ MainWebViewActivity.currentBookmarksFolder = currentFolder;
+
+ // 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) {
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;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return false;
+ // Get a handle for the move to folder menu item.
+ MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder);
+
+ // Get a `Cursor` with all of the folders.
+ Cursor folderCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
+
+ // Enable the move to folder menu item if at least one folder exists.
+ moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0);
+
+ // 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));
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));
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
+ // Get the menu item ID.
int menuItemId = item.getItemId();
- // `numberOfBookmarks` is used in `R.id.move_bookmark_up_enabled`, `R.id.move_bookmark_down_enabled`, and `R.id.context_menu_select_all_bookmarks`.
- int numberOfBookmarks;
-
- // `selectedBookmarkLongArray` is used in `R.id.move_bookmark_up`, `R.id.move_bookmark_down`, and `R.id.edit_bookmark`.
- long[]selectedBookmarkLongArray;
- // `selectedBookmarkDatabaseId` is used in `R.id.move_bookmark_up`, `R.id.move_bookmark_down`, and `R.id.edit_bookmark`.
- int selectedBookmarkDatabaseId;
- // `selectedBookmarkNewPosition` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`.
+ // Instantiate the common variables.
+ int selectedBookmarkPosition;
int selectedBookmarkNewPosition;
- // `bookmarkPositionSparseBooleanArray` is used in `R.id.edit_bookmark` and `R.id.delete_bookmark`.
- SparseBooleanArray bookmarkPositionSparseBooleanArray;
+ final SparseBooleanArray selectedBookmarksPositionsSparseBooleanArray;
switch (menuItemId) {
case R.id.move_bookmark_up:
- // Get the selected bookmark database ID.
- selectedBookmarkLongArray = bookmarksListView.getCheckedItemIds();
- selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
+ // Get the array of checked bookmark positions.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
+
+ // 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++) {
- int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
- int nextBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i + 1);
-
- if (databaseId == selectedBookmarkDatabaseId || nextBookmarkDatabaseId == selectedBookmarkDatabaseId) {
- if (databaseId == selectedBookmarkDatabaseId) {
- // Move the selected bookmark up one and store the new bookmark position.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i - 1);
- selectedBookmarkNewPosition = i - 1;
- } else { // Move the bookmark above the selected bookmark down one.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i + 1);
+ // Get the database ID for the current bookmark.
+ int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
+
+ // Update the display order for the current bookmark.
+ if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
+ // Move the current bookmark up one.
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, 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);
+ } else { // The current bookmark is not changing positions.
+ // Move `bookmarksCursor` to the current bookmark position.
+ bookmarksCursor.moveToPosition(i);
+
+ // Update the display order only if it is not correct in the database.
+ if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
}
- } else {
- // Reset the rest of the bookmarks' DISPLAY_ORDER to match the position in the ListView.
- // This isn't necessary, but it clears out any stray values that might have crept into the database.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i);
}
}
- // 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 selected bookmark database ID.
- selectedBookmarkLongArray = bookmarksListView.getCheckedItemIds();
- selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
+ // Get the array of checked bookmark positions.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
- // Initialize `selectedBookmarkNewPosition`.
- selectedBookmarkNewPosition = 0;
+ // Store the position of the selected bookmark. Only one bookmark is selected when `move_bookmark_down` is enabled.
+ selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
+ // Calculate the new position of the selected bookmark.
+ selectedBookmarkNewPosition = selectedBookmarkPosition + 1;
+
+ // Iterate through the bookmarks.
for (int i = 0; i <bookmarksListView.getCount(); i++) {
- int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
- int previousBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i - 1);
-
- if (databaseId == selectedBookmarkDatabaseId || previousBookmarkDatabaseId == selectedBookmarkDatabaseId) {
- if (databaseId == selectedBookmarkDatabaseId) {
- // Move the selected bookmark down one and store the new bookmark position.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i + 1);
- selectedBookmarkNewPosition = i + 1;
- } else { // Move the bookmark below the selected bookmark up one.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i - 1);
+ // Get the database ID for the current bookmark.
+ int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
+
+ // Update the display order for the current bookmark.
+ if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark.
+ // Move the current bookmark down one.
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1);
+ } else if ((i - 1) == selectedBookmarkPosition) { // The current bookmark is immediately below the selected bookmark.
+ // Move the bookmark below the selected bookmark up one.
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1);
+ } else { // The current bookmark is not changing positions.
+ // Move `bookmarksCursor` to the current bookmark position.
+ bookmarksCursor.moveToPosition(i);
+
+ // Update the display order only if it is not correct in the database.
+ if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
}
- } else {
- // Reset the rest of the bookmark' DISPLAY_ORDER to match the position in the ListView.
- // This isn't necessary, but it clears out any stray values that might have crept into the database.
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i);
}
}
- // Refresh the ListView.
- updateBookmarksListView(currentFolder);
+ // Update `bookmarksCursor` with the current contents of the bookmarks database.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
+
+ // Update the `ListView`.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
- // Select the previously selected bookmark in the new location.
- bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true);
+ // Scroll with the bookmark.
+ scrollBookmarks(selectedBookmarkNewPosition);
- // Scroll `bookmarksListView` to five items above `selectedBookmarkNewPosition`.
- bookmarksListView.setSelection(selectedBookmarkNewPosition - 5);
+ // Update the enabled status of the move icons.
+ updateMoveIcons();
break;
case R.id.move_to_folder:
break;
case R.id.edit_bookmark:
- // Get a handle for `selectedBookmarkPosition` so we can scroll to it after refreshing the ListView.
- bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
- for (int i = 0; i < bookmarkPositionSparseBooleanArray.size(); i++) {
- // Find the bookmark that is selected and save the position to `selectedBookmarkPosition`.
- if (bookmarkPositionSparseBooleanArray.valueAt(i))
- selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(i);
- }
+ // Get the array of checked bookmark positions.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
+
+ // Get the position of the selected bookmark. Only one bookmark is selected when `edit_bookmark_down` is enabled.
+ selectedBookmarkPosition = selectedBookmarksPositionsSparseBooleanArray.keyAt(0);
- // Move to the selected database ID and find out if it is a folder.
+ // Move the `Cursor` to the selected position and find out if it is a folder.
bookmarksCursor.moveToPosition(selectedBookmarkPosition);
boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
- // Store `checkedItemIds` for use by the `AlertDialog`.
- checkedItemIds = bookmarksListView.getCheckedItemIds();
+ // Get the selected bookmark database ID.
+ int databaseId = bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID));
+ // Show the edit bookmark or edit bookmark folder dialog.
if (isFolder) {
- // Save the current folder name.
+ // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
- // Show the `EditBookmarkFolderDialog` `AlertDialog` and name the instance `@string/edit_folder`.
- AppCompatDialogFragment editFolderDialog = new EditBookmarkFolderDialog();
+ // Show the edit bookmark folder dialog.
+ AppCompatDialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
} else {
- // Show the `EditBookmarkDialog` `AlertDialog` and name the instance `@string/edit_bookmark`.
- AppCompatDialogFragment editBookmarkDialog = new EditBookmarkDialog();
+ // Show the edit bookmark dialog.
+ AppCompatDialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
}
break;
case R.id.delete_bookmark:
- // Get an array of the selected rows.
- final long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
-
- // Get a handle for `selectedBookmarkPosition` so we can scroll to it after refreshing the ListView.
- bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
- for (int i = 0; i < bookmarkPositionSparseBooleanArray.size(); i++) {
- // Find the bookmark that is selected and save the position to `selectedBookmarkPosition`.
- if (bookmarkPositionSparseBooleanArray.valueAt(i))
- selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(i);
- }
+ // Set the deleting bookmarks flag, which prevents the delete menu item from being enabled until the current process finishes.
+ deletingBookmarks = true;
+
+ // Get an array of the selected row IDs.
+ final long[] selectedBookmarksIdsLongArray = bookmarksListView.getCheckedItemIds();
- updateBookmarksListViewExcept(selectedBookmarksLongArray, currentFolder);
+ // 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.
+ selectedBookmarksPositionsSparseBooleanArray = bookmarksListView.getCheckedItemPositions().clone();
- // Scroll to where the deleted bookmark was located.
- bookmarksListView.setSelection(selectedBookmarkPosition - 5);
+ // Update `bookmarksCursor` with the current contents of the bookmarks database except for the specified database IDs.
+ bookmarksCursor = bookmarksDatabaseHelper.getBookmarksCursorExcept(selectedBookmarksIdsLongArray, currentFolder);
- // Initialize `snackbarMessage`.
+ // Update the `ListView`.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // Instantiate `snackbarMessage`.
String snackbarMessage;
- // Determine how many items are in the array and prepare an appropriate Snackbar message.
- if (selectedBookmarksLongArray.length == 1) {
+ // Determine how many items are in the array and prepare an appropriate `Snackbar` message.
+ if (selectedBookmarksIdsLongArray.length == 1) {
snackbarMessage = getString(R.string.one_bookmark_deleted);
} else {
- snackbarMessage = selectedBookmarksLongArray.length + " " + getString(R.string.bookmarks_deleted);
+ snackbarMessage = selectedBookmarksIdsLongArray.length + " " + getString(R.string.bookmarks_deleted);
}
// Show a SnackBar.
Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
- .setAction(R.string.undo, new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // Do nothing because everything will be handled by `onDismissed()` below.
- }
+ .setAction(R.string.undo, view -> {
+ // Do nothing because everything will be handled by `onDismissed()` below.
})
.addCallback(new Snackbar.Callback() {
@Override
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 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);
}
- break;
+
+ // Update the display order.
+ for (int i = 0; i < bookmarksListView.getCount(); i++) {
+ // Get the database ID for the current bookmark.
+ int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i);
+
+ // Move `bookmarksCursor` to the current bookmark position.
+ bookmarksCursor.moveToPosition(i);
+
+ // Update the display order only if it is not correct in the database.
+ if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) {
+ bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i);
+ }
+ }
}
+
+ // 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));
});
}
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);
}
@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();
int newBookmarkDisplayOrder = bookmarksListView.getCount();
// Create the bookmark.
- bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, currentFolder, favoriteIconByteArray);
+ bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, currentFolder, newBookmarkDisplayOrder, favoriteIconByteArray);
- // Refresh the `ListView`. `setSelection` scrolls to the bottom of the list.
- updateBookmarksListView(currentFolder);
+ // Update `bookmarksCursor` with the current contents of this folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(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();
- // Check to see if the folder already exists.
- Cursor bookmarkFolderCursor = bookmarksDatabaseHelper.getFolderCursor(folderNameString);
- int existingFoldersWithNewName = bookmarkFolderCursor.getCount();
- bookmarkFolderCursor.close();
- if (folderNameString.isEmpty() || (existingFoldersWithNewName > 0)) {
- String cannotCreateFolder = getResources().getString(R.string.cannot_create_folder) + " \"" + folderNameString + "\"";
- Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannotCreateFolder, Snackbar.LENGTH_INDEFINITE).show();
- } else { // Create the folder.
- // 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);
- Drawable folderIconDrawable = folderIconImageView.getDrawable();
- BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
- folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Assign `favoriteIcon` from the `WebView`.
- folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
- }
+ // Get the new folder icon `Bitmap`.
+ Bitmap folderIconBitmap;
+ 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 { // Use the `WebView` favorite icon.
+ folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ }
- // Convert `folderIconBitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
- ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
- folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
- byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
+ // Convert `folderIconBitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
+ ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
+ byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
- // Move all the bookmarks down one in the display order.
- for (int i = 0; i < bookmarksListView.getCount(); i++) {
- int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
- bookmarksDatabaseHelper.updateBookmarkDisplayOrder(databaseId, i + 1);
- }
+ // Move all the bookmarks down one in the display order.
+ for (int i = 0; i < bookmarksListView.getCount(); i++) {
+ int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
+ 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);
- // Refresh the ListView.
- updateBookmarksListView(currentFolder);
- }
+ // Update `bookmarksCursor` with the current contents of this folder.
+ bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder);
+
+ // Update the `ListView`.
+ bookmarksCursorAdapter.changeCursor(bookmarksCursor);
+
+ // 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();
// 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 onSaveEditBookmarkFolder(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();
- // Check to see if the new folder name is unique.
- Cursor bookmarkFolderCursor = bookmarksDatabaseHelper.getFolderCursor(newFolderNameString);
- int existingFoldersWithNewName = bookmarkFolderCursor.getCount();
- bookmarkFolderCursor.close();
- if ( ((existingFoldersWithNewName == 0) || newFolderNameString.equals(oldFolderNameString)) && !newFolderNameString.isEmpty()) {
- // 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()) {
- // Update the folder name if it has changed without modifying the favorite icon.
- if (!newFolderNameString.equals(oldFolderNameString)) {
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
-
- // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited.
- updateBookmarksListView(currentFolder);
- bookmarksListView.setSelection(selectedBookmarkPosition);
- }
- } else { // Update the folder icon.
- // 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();
- BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
- folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Get the web page icon `ImageView` from the `Dialog`.
- folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
- }
+ // Check if the favorite icon has changed.
+ if (currentFolderIconRadioButton.isChecked()) { // Only the name has changed.
+ // Update the name in the database.
+ bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
+ } else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
+ // Get the new folder icon `Bitmap`.
+ Bitmap folderIconBitmap;
+ if (defaultFolderIconRadioButton.isChecked()) {
+ // Get the default folder icon and convert it to a `Bitmap`.
+ Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
+ BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+ folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+ } else { // Use the `WebView` favorite icon.
+ folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ }
- // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
- ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
- folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
- byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
+ // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
+ ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
+ byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray);
+ // Update the folder icon in the database.
+ bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, folderIconByteArray);
+ } else { // The folder icon and the name have changed.
+ // Instantiate the new folder icon `Bitmap`.
+ Bitmap folderIconBitmap;
- // Refresh the `ListView`. `setSelection` scrolls to the position of the folder that was edited.
- updateBookmarksListView(currentFolder);
- bookmarksListView.setSelection(selectedBookmarkPosition);
+ // Populate the new folder icon bitmap.
+ if (defaultFolderIconRadioButton.isChecked()) {
+ // Get the default folder icon and convert it to a `Bitmap`.
+ Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
+ BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+ folderIconBitmap = folderIconBitmapDrawable.getBitmap();
+ } else { // Use the `WebView` favorite icon.
+ folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
}
- } else { // Don't edit the folder because the new name is not unique.
- String cannot_rename_folder = getResources().getString(R.string.cannot_save_folder) + " \"" + newFolderNameString + "\"";
- Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannot_rename_folder, Snackbar.LENGTH_INDEFINITE).show();
+
+ // Convert the folder `Bitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
+ ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
+ byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
+
+ // Update the folder name and icon in the database.
+ bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray);
}
+ // 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();
}
@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();
- if (newFolderLongArray.length == 0) { // No new folder was selected.
- Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.cannot_move_bookmarks), Snackbar.LENGTH_INDEFINITE).show();
- } else { // Move the selected bookmarks.
- // Get the new folder database ID.
- int newFolderDatabaseId = (int) newFolderLongArray[0];
+ // Get the new folder database ID. Only one folder will be selected.
+ int newFolderDatabaseId = (int) newFolderLongArray[0];
- // Instantiate `newFolderName`.
- String newFolderName;
+ // Instantiate `newFolderName`.
+ String newFolderName;
- if (newFolderDatabaseId == 0) {
- // The new folder is the home folder, represented as `""` in the database.
- newFolderName = "";
- } else {
- // Get the new folder name from the database.
- newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
- }
-
- // Get a long array with the the database ID of the selected bookmarks.
- long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
- for (long databaseIdLong : selectedBookmarksLongArray) {
- // Get `databaseIdInt` for each selected bookmark.
- int databaseIdInt = (int) databaseIdLong;
+ // Set the new folder name.
+ if (newFolderDatabaseId == 0) {
+ // The new folder is the home folder, represented as `""` in the database.
+ newFolderName = "";
+ } else {
+ // Get the new folder name from the database.
+ newFolderName = bookmarksDatabaseHelper.getFolderName(newFolderDatabaseId);
+ }
- // Move the selected bookmark to the new folder.
- bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
- }
+ // Get a long array with the the database ID of the selected bookmarks.
+ long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
- // Refresh the `ListView`.
- updateBookmarksListView(currentFolder);
+ // Move each of the selected bookmarks to the new folder.
+ for (long databaseIdLong : selectedBookmarksLongArray) {
+ // Get `databaseIdInt` for each selected bookmark.
+ int databaseIdInt = (int) databaseIdLong;
- // Close the contextual app bar.
- contextualActionMode.finish();
+ // Move the selected bookmark to the new folder.
+ bookmarksDatabaseHelper.moveToFolder(databaseIdInt, newFolderName);
}
+
+ // 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 AppBar title.
- if (currentFolder.isEmpty()) {
- appBar.setTitle(R.string.bookmarks);
- } else {
- appBar.setTitle(currentFolder);
+ // 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);
+ }
+ }
+ }
+
+ 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