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=e55b6711f0b757d4ee78a497ae313f29991160d0;hp=2f6738cd88a9064b02b6d9a405f411b90037d0bc;hb=70e33ff1c78812721732cd004efb9f8751215be7;hpb=897b2d7f61a0492f228e3c172d7c6a76e3c5f3ac 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 2f6738cd..e55b6711 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-2018 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -43,6 +43,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; import android.widget.AbsListView; import android.widget.CursorAdapter; import android.widget.EditText; @@ -65,22 +66,25 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma EditBookmarkFolderDialog.EditBookmarkFolderListener, MoveToFolderDialog.MoveToFolderListener { // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`. - // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `loadFolder()`. + // 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 `MoveToFolderDialog`. It is also used in `onCreate()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `updateMoveIcons()`. + // `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; - // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, - // and `loadFolder(). + // `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, + // `onMoveToFolder()`, `deleteBookmarkFolderContents()`, `loadFolder()`, and `onDestroy()`. private BookmarksDatabaseHelper bookmarksDatabaseHelper; - // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `updateMoveIcons()`, `scrollBookmarks()`, - // and `loadFolder()`. + // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, + // `updateMoveIcons()`, `scrollBookmarks()`, and `loadFolder()`. private ListView bookmarksListView; - // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder()`. + // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, + // `loadFolder()`, and `onDestroy()`. private Cursor bookmarksCursor; // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`. @@ -95,14 +99,25 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // `oldFolderName` is used in `onCreate()` and `onSaveBookmarkFolder()`. private String oldFolderNameString; - // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons`. + // `moveBookmarkUpMenuItem` is used in `onCreate()` and `updateMoveIcons()`. private MenuItem moveBookmarkUpMenuItem; - // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons`. + // `moveBookmarkDownMenuItem` is used in `onCreate()` and `updateMoveIcons()`. private MenuItem moveBookmarkDownMenuItem; + // `bookmarksDeletedSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`. + private Snackbar bookmarksDeletedSnackbar; + + // `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`. + private boolean closeActivityAfterDismissingSnackbar; + @Override protected void onCreate(Bundle savedInstanceState) { + // Disable screenshots if not allowed. + if (!MainWebViewActivity.allowScreenshots) { + getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + // Set the activity theme. if (MainWebViewActivity.darkTheme) { setTheme(R.style.PrivacyBrowserDark_SecondaryActivity); @@ -130,15 +145,15 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma final Toolbar bookmarksAppBar = findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); - // Get a handle for the activity, the app bar, and the `ListView`. + // Get a handle for the activity, the app bar, and the ListView. final Activity bookmarksActivity = this; appBar = getSupportActionBar(); bookmarksListView = findViewById(R.id.bookmarks_listview); - // This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. + // Remove the incorrect lint warning that `appBar` might be null. assert appBar != null; - // Display the home arrow on `SupportActionBar`. + // Display the home arrow on the app bar. appBar.setDisplayHomeAsUpEnabled(true); // Initialize the database helper. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. @@ -185,6 +200,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma bookmarkCursor.close(); }); + // Get a handle for the activity. + Activity activity = this; + // `MultiChoiceModeListener` handles long clicks. bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { // Instantiate the common variables. @@ -264,8 +282,24 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // 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)); + // List the number of selected bookmarks according to the language. + if (getString(R.string.android_asset_path).equals("ru")) { // The Russian translation is used. + // Convert the number of selected bookmarks to a string. + String numberOfSelectedBookmarksString = String.valueOf(numberOfSelectedBookmarks); + + // Russian follows rule #7 at . + if (numberOfSelectedBookmarksString.endsWith("1") && !numberOfSelectedBookmarksString.equals("11")) { // Ends in 1. + mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_ends_in_1)); + } else if ((numberOfSelectedBookmarksString.endsWith("2") || numberOfSelectedBookmarksString.endsWith("3") || numberOfSelectedBookmarksString.endsWith("4")) && + !numberOfSelectedBookmarksString.equals("12") && !numberOfSelectedBookmarksString.equals("13") && !numberOfSelectedBookmarksString.equals("14")) { // Ends in 2-4. + mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_ends_in_2)); + } else { // Everything else. + mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected_russian_everything_else)); + } + } else { // Another language is used. + // List the number of selected bookmarks in the subtitle. + mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected)); + } // Hide non-applicable `MenuItems`. moveBookmarkUpMenuItem.setVisible(false); @@ -441,15 +475,31 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Instantiate `snackbarMessage`. String snackbarMessage; - // Determine how many items are in the array and prepare an appropriate `Snackbar` message. + // 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 = selectedBookmarksIdsLongArray.length + " " + getString(R.string.bookmarks_deleted); + // Prepare a snackbar according to the language. + if (getString(R.string.android_asset_path).equals("ru")) { // The Russian translation is used. + // Convert the number of selected bookmarks to a string. + String numberOfBookmarksString = String.valueOf(selectedBookmarksIdsLongArray.length); + + // Russian follows rule #7 at . + if (numberOfBookmarksString.endsWith("1") && !numberOfBookmarksString.equals("11")) { // Ends in 1. + snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_ends_in_1); + } else if ((numberOfBookmarksString.endsWith("2") || numberOfBookmarksString.endsWith("3") || numberOfBookmarksString.endsWith("4")) && + !numberOfBookmarksString.equals("12") && !numberOfBookmarksString.equals("13") && !numberOfBookmarksString.equals("14")) { // Ends in 2-4. + snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_ends_in_2); + } else { // Everything else. + snackbarMessage = numberOfBookmarksString + " " + getString(R.string.bookmarks_deleted_russian_everything_else); + } + } else { // Another language is used. + snackbarMessage = selectedBookmarksIdsLongArray.length + " " + getString(R.string.bookmarks_deleted); + } } // Show a SnackBar. - Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG) + bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG) .setAction(R.string.undo, view -> { // Do nothing because everything will be handled by `onDismissed()` below. }) @@ -508,10 +558,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Reset the deleting bookmarks flag. deletingBookmarks = false; + + // Close the activity if back has been pressed. + if (closeActivityAfterDismissingSnackbar) { + // Update the bookmarks folder for the bookmarks drawer in the main WebView activity. + MainWebViewActivity.currentBookmarksFolder = currentFolder; + + // Close the bookmarks drawer and reload the bookmarks ListView when returning to the main WebView activity. + MainWebViewActivity.restartFromBookmarksActivity = true; + + // Return to `MainWebViewActivity`. + NavUtils.navigateUpFromSameTask(activity); + } } - }) - //Show the `SnackBar`. - .show(); + }); + + //Show the `SnackBar`. + bookmarksDeletedSnackbar.show(); // Close the `ActionBar`. mode.finish(); @@ -574,14 +637,23 @@ 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()) { // 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); + // Go home. + if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks delete snackbar before going home. + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; + + // Dismiss the snackbar. + bookmarksDeletedSnackbar.dismiss(); + } else { // Go home immediately. + // 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 { // Currently in a subfolder. // Place the former parent folder in `currentFolder`. currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); @@ -612,21 +684,22 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void onBackPressed() { - 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`. + // Check to see if a snackbar is currently displayed. If so, it must be closed before exiting so that a pending delete is completed before reloading the ListView in the bookmarks drawer. + if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks delete snackbar before going home. + // Set the close flag. + closeActivityAfterDismissingSnackbar = true; + + // Dismiss the snackbar. + bookmarksDeletedSnackbar.dismiss(); + } else { // Go home immediately. + // Update the bookmarks folder for the bookmarks drawer in the main WebView activity. + MainWebViewActivity.currentBookmarksFolder = currentFolder; + + // Close the bookmarks drawer and reload the bookmarks ListView when returning to the main WebView activity. MainWebViewActivity.restartFromBookmarksActivity = true; // Exit `BookmarksActivity`. super.onBackPressed(); - } else { // Currently in a subfolder. - // Place the former parent folder in `currentFolder`. - currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder); - - // Load the new folder. - loadFolder(); } } @@ -994,4 +1067,14 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma appBar.setTitle(currentFolder); } } + + @Override + public void onDestroy() { + // Close the bookmarks cursor and database. + bookmarksCursor.close(); + bookmarksDatabaseHelper.close(); + + // Run the default commands. + super.onDestroy(); + } } \ No newline at end of file