]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java
Migrate five dialogs to Kotlin. https://redmine.stoutner.com/issues/543
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksDatabaseViewActivity.java
index 4645f3dc7031ba2d4f4ea6a58e4c0d224d5df29e..9844f184ed188613001501cbd7c2adeba16f1164 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
 package com.stoutner.privacybrowser.activities;
 
 import android.annotation.SuppressLint;
+import android.app.Dialog;
 import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.database.MergeCursor;
@@ -30,6 +33,7 @@ import android.graphics.Typeface;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.preference.PreferenceManager;
 import android.util.SparseBooleanArray;
 import android.view.ActionMode;
 import android.view.Menu;
@@ -47,6 +51,7 @@ import android.widget.ResourceCursorAdapter;
 import android.widget.Spinner;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
 import androidx.appcompat.app.ActionBar;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;  // The AndroidX toolbar must be used until the minimum API is >= 21.
@@ -68,7 +73,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
         EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
     // Instantiate the constants.
     private static final int ALL_FOLDERS_DATABASE_ID = -2;
-    private static final int HOME_FOLDER_DATABASE_ID = -1;
+    public static final int HOME_FOLDER_DATABASE_ID = -1;
 
     // `bookmarksDatabaseHelper` is used in `onCreate()`, `updateBookmarksListView()`, `selectAllBookmarksInFolder()`, and `onDestroy()`.
     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
@@ -99,13 +104,20 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
+        // Get the theme and screenshot preferences.
+        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
         // Disable screenshots if not allowed.
-        if (!MainWebViewActivity.allowScreenshots) {
+        if (!allowScreenshots) {
             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
         }
 
         // Set the activity theme.
-        if (MainWebViewActivity.darkTheme) {
+        if (darkTheme) {
             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
         } else {
             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
@@ -114,6 +126,18 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
         // Run the default commands.
         super.onCreate(savedInstanceState);
 
+        // Get the intent that launched the activity.
+        Intent launchingIntent = getIntent();
+
+        // Get the favorite icon byte array.
+        byte[] favoriteIconByteArray = launchingIntent.getByteArrayExtra("favorite_icon_byte_array");
+
+        // Remove the incorrect lint warning below that the favorite icon byte array might be null.
+        assert favoriteIconByteArray != null;
+
+        // Convert the favorite icon byte array to a bitmap and store it in a class variable.
+        Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
+
         // Set the content view.
         setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
 
@@ -121,7 +145,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
         Toolbar toolbar = findViewById(R.id.bookmarks_databaseview_toolbar);
         setSupportActionBar(toolbar);
 
-        // Get a handle for the `AppBar`.
+        // Get a handle for the action bar.
         ActionBar actionBar = getSupportActionBar();
 
         // Remove the incorrect lint warning that the action bar might be null.
@@ -311,7 +335,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                     bookmarkParentFolderTextView.setText(bookmarkParentFolder);
 
                     // Set the text color according to the theme.
-                    if (MainWebViewActivity.darkTheme) {
+                    if (darkTheme) {
                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
                     } else {
                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
@@ -337,11 +361,11 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
 
                 // Show the edit bookmark folder dialog.
-                DialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
+                DialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId, favoriteIconBitmap);
                 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
             } else {
                 // Show the edit bookmark dialog.
-                DialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
+                DialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId, favoriteIconBitmap);
                 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
             }
         });
@@ -497,7 +521,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                         // Update the list view.
                         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
 
-                        // Show a Snackbar with the number of deleted bookmarks.
+                        // Create a Snackbar with the number of deleted bookmarks.
                         bookmarksDeletedSnackbar = Snackbar.make(findViewById(R.id.bookmarks_databaseview_coordinatorlayout),
                                 getString(R.string.bookmarks_deleted) + "  " + selectedBookmarksIdsLongArray.length, Snackbar.LENGTH_LONG)
                                 .setAction(R.string.undo, view -> {
@@ -507,28 +531,23 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                                     @SuppressLint("SwitchIntDef")  // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
                                     @Override
                                     public void onDismissed(Snackbar snackbar, int event) {
-                                        switch (event) {
-                                            // The user pushed the `Undo` button.
-                                            case Snackbar.Callback.DISMISS_EVENT_ACTION:
-                                                // Update the bookmarks list view with the current contents of the bookmarks database, including the "deleted bookmarks.
-                                                updateBookmarksListView();
-
-                                                // Re-select the previously selected bookmarks.
-                                                for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
-                                                    bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
-                                                }
-                                                break;
-
-                                                // The Snackbar was dismissed without the `Undo` button being pushed.
-                                            default:
-                                                // Delete each selected bookmark.
-                                                for (long databaseIdLong : selectedBookmarksIdsLongArray) {
-                                                    // Convert `databaseIdLong` to an int.
-                                                    int databaseIdInt = (int) databaseIdLong;
-
-                                                    // Delete the selected bookmark.
-                                                    bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
-                                                }
+                                        if (event == Snackbar.Callback.DISMISS_EVENT_ACTION) {  // The user pushed the undo button.
+                                            // Update the bookmarks list view with the current contents of the bookmarks database, including the "deleted bookmarks.
+                                            updateBookmarksListView();
+
+                                            // Re-select the previously selected bookmarks.
+                                            for (int i = 0; i < selectedBookmarksPositionsSparseBooleanArray.size(); i++) {
+                                                bookmarksListView.setItemChecked(selectedBookmarksPositionsSparseBooleanArray.keyAt(i), true);
+                                            }
+                                        } else {  // The Snackbar was dismissed without the undo button being pushed.
+                                            // Delete each selected bookmark.
+                                            for (long databaseIdLong : selectedBookmarksIdsLongArray) {
+                                                // Convert `databaseIdLong` to an int.
+                                                int databaseIdInt = (int) databaseIdLong;
+
+                                                // Delete the selected bookmark.
+                                                bookmarksDatabaseHelper.deleteBookmark(databaseIdInt);
+                                            }
                                         }
 
                                         // Reset the deleting bookmarks flag.
@@ -571,9 +590,16 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
 
     @Override
     public boolean onOptionsItemSelected(MenuItem menuItem) {
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+
+        // Get the theme preferences.
+        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+
         // Get the ID of the menu item that was selected.
         int menuItemId = menuItem.getItemId();
 
+        // Run the command that corresponds to the selected menu item.
         switch (menuItemId) {
             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
                 // Exit the activity.
@@ -590,7 +616,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                 // Update the icon and display a snackbar.
                 if (sortByDisplayOrder) {  // Sort by display order.
                     // Update the icon according to the theme.
-                    if (MainWebViewActivity.darkTheme) {
+                    if (darkTheme) {
                         menuItem.setIcon(R.drawable.sort_selected_dark);
                     } else {
                         menuItem.setIcon(R.drawable.sort_selected_light);
@@ -600,7 +626,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                     Snackbar.make(bookmarksListView, R.string.sorted_by_display_order, Snackbar.LENGTH_SHORT).show();
                 } else {  // Sort by database id.
                     // Update the icon according to the theme.
-                    if (MainWebViewActivity.darkTheme) {
+                    if (darkTheme) {
                         menuItem.setIcon(R.drawable.sort_dark);
                     } else {
                         menuItem.setIcon(R.drawable.sort_light);
@@ -628,20 +654,12 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
             bookmarksDeletedSnackbar.dismiss();
         } else {  // Go home immediately.
             // Update the current folder in the bookmarks activity.
-            switch (currentFolderDatabaseId) {
-                case ALL_FOLDERS_DATABASE_ID:
-                    // Load the home folder.
-                    BookmarksActivity.currentFolder = "";
-                    break;
-
-                case HOME_FOLDER_DATABASE_ID:
-                    // Load the home folder.
-                    BookmarksActivity.currentFolder = "";
-                    break;
-
-                default:
-                    // Load the current folder.
-                    BookmarksActivity.currentFolder = currentFolderName;
+            if ((currentFolderDatabaseId == ALL_FOLDERS_DATABASE_ID) || (currentFolderDatabaseId == HOME_FOLDER_DATABASE_ID)) {  // All folders or the the home folder are currently displayed.
+                // Load the home folder.
+                BookmarksActivity.currentFolder = "";
+            } else {  // A subfolder is currently displayed.
+                // Load the current folder.
+                BookmarksActivity.currentFolder = currentFolderName;
             }
 
             // Reload the bookmarks list view when returning to the bookmarks activity.
@@ -736,25 +754,31 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
     }
 
     @Override
-    public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
+    public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, @NonNull Bitmap favoriteIconBitmap) {
+        // Get the dialog from the dialog fragment.
+        Dialog dialog = dialogFragment.getDialog();
+
+        // Remove the incorrect lint warning below that the dialog might be null.
+        assert dialog != null;
+
         // Get handles for the views from dialog fragment.
-        RadioButton currentBookmarkIconRadioButton = 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);
-        Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
-        EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
+        RadioButton currentBookmarkIconRadioButton = dialog.findViewById(R.id.edit_bookmark_current_icon_radiobutton);
+        EditText editBookmarkNameEditText = dialog.findViewById(R.id.edit_bookmark_name_edittext);
+        EditText editBookmarkUrlEditText = dialog.findViewById(R.id.edit_bookmark_url_edittext);
+        Spinner folderSpinner = dialog.findViewById(R.id.edit_bookmark_folder_spinner);
+        EditText displayOrderEditText = dialog.findViewById(R.id.edit_bookmark_display_order_edittext);
 
         // Extract the bookmark information.
         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
         int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
-        int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
+        int displayOrderInt = Integer.parseInt(displayOrderEditText.getText().toString());
 
         // Instantiate the parent folder name `String`.
         String parentFolderNameString;
 
         // Set the parent folder name.
-        if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
+        if (folderDatabaseId == HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
             parentFolderNameString = "";
         } else {  // Get the parent folder name from the database.
             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
@@ -764,14 +788,6 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
         } else {  // Update the bookmark using the `WebView` favorite icon.
-            // Get a copy of the favorite icon bitmap.
-            Bitmap favoriteIconBitmap = MainWebViewActivity.favoriteIconBitmap;
-
-            // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
-            if ((favoriteIconBitmap.getHeight() > 256) || (favoriteIconBitmap.getWidth() > 256)) {
-                favoriteIconBitmap = Bitmap.createScaledBitmap(favoriteIconBitmap, 256, 256, true);
-            }
-
             // Create a favorite icon byte array output stream.
             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
 
@@ -790,25 +806,31 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
     }
 
     @Override
-    public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
+    public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) {
+        // Get the dialog from the dialog fragment.
+        Dialog dialog = dialogFragment.getDialog();
+
+        // Remove the incorrect lint warning below that the dialog might be null.
+        assert dialog != null;
+
         // Get handles for the views from dialog fragment.
-        RadioButton currentBookmarkIconRadioButton = 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 editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
-        Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
-        EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
+        RadioButton currentBookmarkIconRadioButton = dialog.findViewById(R.id.edit_folder_current_icon_radiobutton);
+        RadioButton defaultFolderIconRadioButton = dialog.findViewById(R.id.edit_folder_default_icon_radiobutton);
+        ImageView defaultFolderIconImageView = dialog.findViewById(R.id.edit_folder_default_icon_imageview);
+        EditText editBookmarkNameEditText = dialog.findViewById(R.id.edit_folder_name_edittext);
+        Spinner parentFolderSpinner = dialog.findViewById(R.id.edit_folder_parent_folder_spinner);
+        EditText displayOrderEditText = dialog.findViewById(R.id.edit_folder_display_order_edittext);
 
         // Extract the folder information.
         String newFolderNameString = editBookmarkNameEditText.getText().toString();
         int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
-        int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
+        int displayOrderInt = Integer.parseInt(displayOrderEditText.getText().toString());
 
         // Instantiate the parent folder name `String`.
         String parentFolderNameString;
 
         // Set the parent folder name.
-        if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
+        if (parentFolderDatabaseId == HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
             parentFolderNameString = "";
         } else {  // Get the parent folder name from the database.
             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
@@ -833,12 +855,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements
                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
             } else {  // Use the `WebView` favorite icon.
                 // Get a copy of the favorite icon bitmap.
-                folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
-
-                // Scale the folder icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
-                if ((folderIconBitmap.getHeight() > 256) || (folderIconBitmap.getWidth() > 256)) {
-                    folderIconBitmap = Bitmap.createScaledBitmap(folderIconBitmap, 256, 256, true);
-                }
+                folderIconBitmap = favoriteIconBitmap;
             }
 
             // Create a folder icon byte array output stream.