X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksActivity.java;h=5e9d9fafe657201d25b3a9f663b82a541d389e02;hb=398b393f577ac8fb0f08c4ce770d6e064592afd7;hp=dcb603ff715678d25e6c5511ebe4ad3397804baa;hpb=b3b4105e9acd9cf8e202abef3b811d49c6c36bec;p=PrivacyBrowserAndroid.git 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 dcb603ff..5e9d9faf 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2017 Soren Stoutner . + * Copyright © 2016-2017 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -97,10 +97,20 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override protected void onCreate(Bundle savedInstanceState) { + // Set the activity theme. + if (MainWebViewActivity.darkTheme) { + setTheme(R.style.PrivacyBrowserDark_SecondaryActivity); + } else { + setTheme(R.style.PrivacyBrowserLight_SecondaryActivity); + } + + // Run the default commands. super.onCreate(savedInstanceState); + + // 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. + // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); @@ -185,7 +195,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark); selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks); - // Get a handle for `contextualActionMode` so we can close it programatically. + // Store `contextualActionMode` so we can close it programatically. contextualActionMode = mode; return true; @@ -225,19 +235,31 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Disable `moveBookmarkUpMenuItem` if the selected bookmark is at the top of the ListView. if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { moveBookmarkUpMenuItem.setEnabled(false); - moveBookmarkUpMenuItem.setIcon(R.drawable.move_bookmark_up_disabled); + moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled); } else { // Otherwise enable `moveBookmarkUpMenuItem`. moveBookmarkUpMenuItem.setEnabled(true); - moveBookmarkUpMenuItem.setIcon(R.drawable.move_bookmark_up_enabled); + + // 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_bookmark_down_disabled); + moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled); } else { // Otherwise enable `moveBookmarkDownMenuItem`. moveBookmarkDownMenuItem.setEnabled(true); - moveBookmarkDownMenuItem.setIcon(R.drawable.move_bookmark_down_enabled); + + // 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); + } } } else { // More than one bookmark is selected. // List the number of selected bookmarks in the subtitle. @@ -259,45 +281,46 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @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`. + // Instantiate the common variables. 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`. int selectedBookmarkNewPosition; - // `bookmarkPositionSparseBooleanArray` is used in `R.id.edit_bookmark` and `R.id.delete_bookmark`. SparseBooleanArray bookmarkPositionSparseBooleanArray; 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 bookmarks. + bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + + // Store the position of the selected bookmark. + selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); // Initialize `selectedBookmarkNewPosition`. selectedBookmarkNewPosition = 0; + // 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.updateBookmarkDisplayOrder(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.updateBookmarkDisplayOrder(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.updateBookmarkDisplayOrder(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); } } @@ -313,29 +336,36 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma 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 bookmarks. + bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); + + // Store the position of the selected bookmark. + selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); // Initialize `selectedBookmarkNewPosition`. selectedBookmarkNewPosition = 0; + // Iterate through the bookmarks. for (int i = 0; i