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=42b1c0e1f0b06447c5334f251771c4b00f98cb6d;hb=70e33ff1c78812721732cd004efb9f8751215be7;hpb=a8669fa252bd4a278decc411912d6f8674ff483d 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 42b1c0e1..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 . * @@ -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; @@ -42,8 +43,8 @@ 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.AdapterView; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ImageView; @@ -64,26 +65,29 @@ 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 `MoveToFolderDialog`. It is also used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, - // `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `deleteBookmarkFolderContents()`, and `loadFolder(). - public static BookmarksDatabaseHelper bookmarksDatabaseHelper; - // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`. - // It is used in `onCreate`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `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()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `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; - // `bookmarksListView` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, `updateMoveIcons()`, `scrollBookmarks()`, - // 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()`. private ListView bookmarksListView; - // `bookmarksCursor` is used in `onCreate()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `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()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`. + // `bookmarksCursorAdapter` is used in `onCreate(), `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, `onMoveToFolder()`, and `onLoadFolder()`. private CursorAdapter bookmarksCursorAdapter; // `contextualActionMode` is used in `onCreate()`, `onSaveEditBookmark()`, `onSaveEditBookmarkFolder()` and `onMoveToFolder()`. @@ -92,17 +96,28 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // `appBar` is used in `onCreate()` and `loadFolder()`. private ActionBar appBar; - // `oldFolderName` is used in `onCreate()` and `onSaveEditBookmarkFolder()`. + // `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); @@ -116,25 +131,29 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get the intent that launched the activity. Intent launchingIntent = getIntent(); - // Get the current folder from the `Intent`. - currentFolder = launchingIntent.getStringExtra("Current Folder"); + // 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); - // 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 = (ListView) findViewById(R.id.bookmarks_listview); + 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`. @@ -145,42 +164,45 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma loadFolder(); // Set a listener so that tapping a list item loads the URL or folder. - 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; + 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; - // Get the bookmark `Cursor` for this ID and move it to the first row. - Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID); - bookmarkCursor.moveToFirst(); + // 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 `currentFolder`. - currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); + // 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`. `mainWebView` will automatically reload when `BookmarksActivity` closes. - MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); + // 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)); - // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`. - MainWebViewActivity.currentBookmarksFolder = currentFolder; + // Set `MainWebViewActivity` to load the new URL on restart. + MainWebViewActivity.loadUrlOnRestart = true; - // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`. - MainWebViewActivity.restartFromBookmarksActivity = true; + // Update the bookmarks folder for the bookmarks drawer in `MainWebViewActivity`. + MainWebViewActivity.currentBookmarksFolder = currentFolder; - // Return to `MainWebViewActivity`. - NavUtils.navigateUpFromSameTask(bookmarksActivity); - } + // Close the bookmarks drawer and reload the bookmarks `ListView` when returning to `MainWebViewActivity`. + MainWebViewActivity.restartFromBookmarksActivity = true; - // Close the `Cursor`. - bookmarkCursor.close(); + // Return to `MainWebViewActivity`. + NavUtils.navigateUpFromSameTask(bookmarksActivity); } + + // Close the `Cursor`. + bookmarkCursor.close(); }); + // Get a handle for the activity. + Activity activity = this; + // `MultiChoiceModeListener` handles long clicks. bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { // Instantiate the common variables. @@ -260,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); @@ -396,22 +434,23 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // 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 position 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); // 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, which is used in `onSaveEditBookmarkFolder()`. + // Save the current folder name, which is used in `onSaveBookmarkFolder()`. oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - // Show the edit bookmark folder `AlertDialog` and name the instance `@string/edit_folder`. + // Show the edit bookmark folder dialog. AppCompatDialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId); editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder)); } else { - // Show the edit bookmark `AlertDialog` and name the instance `@string/edit_bookmark`. + // Show the edit bookmark dialog. AppCompatDialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId); editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark)); } @@ -436,20 +475,33 @@ 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) - .setAction(R.string.undo, new View.OnClickListener() { - @Override - public void onClick(View view) { - // Do nothing because everything will be handled by `onDismissed()` below. - } + 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. }) .addCallback(new Snackbar.Callback() { @Override @@ -506,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(); @@ -537,27 +602,21 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma }); // Get handles for the `FloatingActionButtons`. - FloatingActionButton createBookmarkFolderFab = (FloatingActionButton) findViewById(R.id.create_bookmark_folder_fab); - FloatingActionButton createBookmarkFab = (FloatingActionButton) findViewById(R.id.create_bookmark_fab); + 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(new View.OnClickListener() { - @Override - public void onClick(View 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)); - } + 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(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)); - } + 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)); }); } @@ -578,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); @@ -616,29 +684,30 @@ 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(); } } @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(); @@ -653,7 +722,7 @@ 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); @@ -668,9 +737,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment) { // Get handles for the views in `dialogFragment`. - EditText createFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext); - RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton); - ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon); + 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(); @@ -697,8 +766,8 @@ 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); @@ -711,11 +780,11 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } @Override - public void onSaveEditBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) { + public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) { // Get handles for the views from `dialogFragment`. - EditText editBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); - EditText editBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext); - RadioButton currentBookmarkIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton); + 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); // Store the bookmark strings. String bookmarkNameString = editBookmarkNameEditText.getText().toString(); @@ -737,7 +806,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Close the contextual action mode. contextualActionMode.finish(); - // Update `bookmarksCursor` with the current contents of this folder. + // Update `bookmarksCursor` with the contents of the current folder. bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolder); // Update the `ListView`. @@ -745,12 +814,12 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } @Override - public void onSaveEditBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) { + public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) { // Get handles for the views from `dialogFragment`. - EditText editFolderNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext); - 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); - ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon); + 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. String newFolderNameString = editFolderNameEditText.getText().toString(); @@ -764,7 +833,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma Bitmap folderIconBitmap; if (defaultFolderIconRadioButton.isChecked()) { // Get the default folder icon and convert it to a `Bitmap`. - Drawable folderIconDrawable = folderIconImageView.getDrawable(); + Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); } else { // Use the `WebView` favorite icon. @@ -779,11 +848,13 @@ 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 and convert it to a `Bitmap`. - Drawable folderIconDrawable = folderIconImageView.getDrawable(); + Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable(); BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable; folderIconBitmap = folderIconBitmapDrawable.getBitmap(); } else { // Use the `WebView` favorite icon. @@ -812,7 +883,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void onMoveToFolder(AppCompatDialogFragment dialogFragment) { // Get a handle for the `ListView` from `dialogFragment`. - ListView folderListView = (ListView) dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview); + ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview); // Store a long array of the selected folders. long[] newFolderLongArray = folderListView.getCheckedItemIds(); @@ -961,8 +1032,8 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma @Override public void bindView(View view, Context context, Cursor cursor) { // Get handles for the views. - ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); - TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); + 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)); @@ -996,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