]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.java
Migrate five dialogs to Kotlin. https://redmine.stoutner.com/issues/604
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / MoveToFolderDialog.java
index 2a767d969cfaad375151882d11f34c66ed63e6c2..25701f7792fd4a76ec5fc1734d17b22e2cecd6f2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2017 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.dialogs;
 
 import android.annotation.SuppressLint;
-import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.MatrixCursor;
@@ -33,12 +33,10 @@ import android.graphics.BitmapFactory;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
-import android.support.annotation.NonNull;
-import android.support.v4.content.ContextCompat;
-// `AppCompatDialogFragment` must be used instead of `DialogFragment` or an error is produced on API <=22.
-import android.support.v7.app.AppCompatDialogFragment;
+import android.preference.PreferenceManager;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.widget.AdapterView;
 import android.widget.Button;
 import android.widget.CursorAdapter;
@@ -46,76 +44,86 @@ import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AlertDialog;
+import androidx.core.content.ContextCompat;
+import androidx.fragment.app.DialogFragment;
+
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.activities.BookmarksActivity;
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
 
 import java.io.ByteArrayOutputStream;
 
-public class MoveToFolderDialog extends AppCompatDialogFragment {
-    // Instantiate class variables.
+public class MoveToFolderDialog extends DialogFragment {
+    // Instantiate the class variables.
+    private MoveToFolderListener moveToFolderListener;
     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
     private StringBuilder exceptFolders;
 
     // The public interface is used to send information back to the parent activity.
     public interface MoveToFolderListener {
-        void onMoveToFolder(AppCompatDialogFragment dialogFragment);
+        void onMoveToFolder(DialogFragment dialogFragment);
     }
 
-    // `moveToFolderListener` is used in `onAttach()` and `onCreateDialog`.
-    private MoveToFolderListener moveToFolderListener;
-
-    public void onAttach(Context context) {
+    public void onAttach(@NonNull Context context) {
+        // Run the default commands.
         super.onAttach(context);
 
-        // Get a handle for `MoveToFolderListener` from `parentActivity`.
-        try {
-            moveToFolderListener = (MoveToFolderListener) context;
-        } catch(ClassCastException exception) {
-            throw new ClassCastException(context.toString() + " must implement EditBookmarkFolderListener.");
-        }
+        // Get a handle for `MoveToFolderListener` from the launching context.
+        moveToFolderListener = (MoveToFolderListener) context;
     }
 
-    // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
+    // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
     @SuppressLint("InflateParams")
     @Override
     @NonNull
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        // Initialize the database helper.  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`.
+        // Initialize the database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
 
-        // Use `AlertDialog.Builder` to create the `AlertDialog`.
-        AlertDialog.Builder dialogBuilder;
-
-        // Set the style according to the theme.
-        if (MainWebViewActivity.darkTheme) {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
-        } else {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
-        }
+        // Use an alert dialog builder to create the alert dialog.
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
 
         // Set the title.
         dialogBuilder.setTitle(R.string.move_to_folder);
 
+        // Remove the incorrect lint warning that `getActivity()` might be null.
+        assert getActivity() != null;
+
         // Set the view.  The parent view is `null` because it will be assigned by `AlertDialog`.
         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_dialog, null));
 
-        // Set an `onClick()` listener for the negative button.
+        // Set the listener for the negative button.
         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
             // Do nothing.  The `AlertDialog` will close automatically.
         });
 
-        // Set the `onClick()` listener fo the positive button.
+        // Set the listener fo the positive button.
         dialogBuilder.setPositiveButton(R.string.move, (DialogInterface dialog, int which) -> {
             // Return the `DialogFragment` to the parent activity on save.
             moveToFolderListener.onMoveToFolder(MoveToFolderDialog.this);
         });
 
-        // Create an `AlertDialog` from the `AlertDialog.Builder`.
+        // Create an alert dialog from the alert dialog builder.
         final AlertDialog alertDialog = dialogBuilder.create();
 
-        // Show the `AlertDialog` so the items in the layout can be modified.
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
+
+        // Get the screenshot preference.
+        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
+        // Disable screenshots if not allowed.
+        if (!allowScreenshots) {
+            // Remove the warning below that `getWindow()` might be null.
+            assert alertDialog.getWindow() != null;
+
+            // Disable screenshots.
+            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        }
+
+        // Show the alert dialog so the items in the layout can be modified.
         alertDialog.show();
 
         // Get a handle for the positive button.
@@ -124,15 +132,13 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
         // Initially disable the positive button.
         moveButton.setEnabled(false);
 
-        // Initialize the `Cursor` and `CursorAdapter` variables.
+        // Initialize the variables.
         Cursor foldersCursor;
         CursorAdapter foldersCursorAdapter;
+        exceptFolders = new StringBuilder();
 
         // Check to see if we are in the `Home Folder`.
         if (BookmarksActivity.currentFolder.isEmpty()) {  // Don't display `Home Folder` at the top of the `ListView`.
-            // Initialize `exceptFolders`.
-            exceptFolders = new StringBuilder();
-
             // If a folder is selected, add it and all children to the list of folders not to display.
             long[] selectedBookmarksLongArray = BookmarksActivity.checkedItemIds;
             for (long databaseIdLong : selectedBookmarksLongArray) {
@@ -159,13 +165,16 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
                 }
             }
 
-            // Get a `Cursor` containing the folders to display.
-            foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
+            // Get a cursor containing the folders to display.
+            foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(exceptFolders.toString());
 
             // Setup `foldersCursorAdaptor` with `this` context.  `false` disables autoRequery.
             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersCursor, false) {
                 @Override
                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
+                    // Remove the incorrect lint warning that `.getLayoutInflater()` might be false.
+                    assert getActivity() != null;
+
                     // Inflate the individual item layout.  `false` does not attach it to the root.
                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
                 }
@@ -187,10 +196,12 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
                 }
             };
         } else {  // Display `Home Folder` at the top of the `ListView`.
-            // Get the home folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
+            // Get the home folder icon drawable and convert it to a `Bitmap`.
             Drawable homeFolderIconDrawable = ContextCompat.getDrawable(getActivity().getApplicationContext(), R.drawable.folder_gray_bitmap);
             BitmapDrawable homeFolderIconBitmapDrawable = (BitmapDrawable) homeFolderIconDrawable;
+            assert homeFolderIconDrawable != null;
             Bitmap homeFolderIconBitmap = homeFolderIconBitmapDrawable.getBitmap();
+
             // Convert the folder `Bitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
             ByteArrayOutputStream homeFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
             homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream);
@@ -225,7 +236,7 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
             }
 
             // Get a `Cursor` containing the folders to display.
-            foldersCursor = bookmarksDatabaseHelper.getFoldersCursorExcept(exceptFolders.toString());
+            foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(exceptFolders.toString());
 
             // Combine `homeFolderMatrixCursor` and `foldersCursor`.
             MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{homeFolderMatrixCursor, foldersCursor});
@@ -234,6 +245,9 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
             foldersCursorAdapter = new CursorAdapter(alertDialog.getContext(), foldersMergeCursor, false) {
                 @Override
                 public View newView(Context context, Cursor cursor, ViewGroup parent) {
+                    // Remove the incorrect lint warning that `.getLayoutInflater()` might be false.
+                    assert getActivity() != null;
+
                     // Inflate the individual item layout.  `false` does not attach it to the root.
                     return getActivity().getLayoutInflater().inflate(R.layout.move_to_folder_item_linearlayout, parent, false);
                 }
@@ -256,8 +270,13 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
             };
         }
 
-        // Display the ListView
+        // Get a handle for the folders list view.
         ListView foldersListView = alertDialog.findViewById(R.id.move_to_folder_listview);
+
+        // Remove the incorrect lint warning below that the view might be null.
+        assert foldersListView != null;
+
+        // Set the folder list view adapter.
         foldersListView.setAdapter(foldersCursorAdapter);
 
         // Enable the move button when a folder is selected.
@@ -272,7 +291,7 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
 
     private void addSubfoldersToExceptFolders(String folderName) {
         // Get a `Cursor` will all the immediate subfolders.
-        Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfoldersCursor(folderName);
+        Cursor subfoldersCursor = bookmarksDatabaseHelper.getSubfolders(folderName);
 
         for (int i = 0; i < subfoldersCursor.getCount(); i++) {
             // Move `subfolderCursor` to the current item.
@@ -289,4 +308,4 @@ public class MoveToFolderDialog extends AppCompatDialogFragment {
             addSubfoldersToExceptFolders(subfolderName);
         }
     }
-}
+}
\ No newline at end of file