]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadLocationPermissionDialog.java
Add import and export of settings. https://redmine.stoutner.com/issues/23
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadLocationPermissionDialog.java
index 0ad43d1c7a69d15e5adde01cb5a86c818c671b0b..a232058886580ef5eea61260e648ad0f63233eba 100644 (file)
@@ -25,59 +25,51 @@ import android.app.DialogFragment;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.Bundle;
+import android.view.WindowManager;
 
 import com.stoutner.privacybrowser.R;
 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
 
 public class DownloadLocationPermissionDialog extends DialogFragment {
+    // The constants are used to differentiate between the two download types.
     public static final int DOWNLOAD_FILE = 1;
     public static final int DOWNLOAD_IMAGE = 2;
 
-    private int downloadType;
-
-    public static DownloadLocationPermissionDialog downloadType(int type) {
-        // Create an arguments bundle.
-        Bundle argumentsBundle = new Bundle();
-
-        // Store the download type in the bundle.
-        argumentsBundle.putInt("Download_Type", type);
-
-        // Add the arguments bundle to this instance of `DownloadLocationPermissionDialog`.
-        DownloadLocationPermissionDialog thisDownloadLocationPermissionDialog = new DownloadLocationPermissionDialog();
-        thisDownloadLocationPermissionDialog.setArguments(argumentsBundle);
-        return thisDownloadLocationPermissionDialog;
-    }
-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        // Store the download type in the local class variable.
-        downloadType = getArguments().getInt("Download_Type");
-    }
+    // The listener is used in `onAttach()` and `onCreateDialog()`.
+    private DownloadLocationPermissionDialogListener downloadLocationPermissionDialogListener;
 
     // The public interface is used to send information back to the parent activity.
     public interface DownloadLocationPermissionDialogListener {
         void onCloseDownloadLocationPermissionDialog(int downloadType);
     }
 
-    // `downloadLocationPermissionDialogListener` is used in `onAttach()` and `onCreateDialog()`.
-    private DownloadLocationPermissionDialogListener downloadLocationPermissionDialogListener;
-
     @Override
     public void onAttach(Context context) {
+        // Run the default commands.
         super.onAttach(context);
 
-        // Check to make sure the parent activity implements the listener.
-        try {
-            downloadLocationPermissionDialogListener = (DownloadLocationPermissionDialogListener) context;
-        } catch (ClassCastException exception) {
-            throw new ClassCastException(context.toString() + " must implement DownloadLocationPermissionDialogListener.");
-        }
+        // Get a handle for the listener from the launching context.
+        downloadLocationPermissionDialogListener = (DownloadLocationPermissionDialogListener) context;
+    }
+
+    public static DownloadLocationPermissionDialog downloadType(int type) {
+        // Create an arguments bundle.
+        Bundle argumentsBundle = new Bundle();
+
+        // Store the download type in the bundle.
+        argumentsBundle.putInt("download_type", type);
+
+        // Add the arguments bundle to this instance of the dialog.
+        DownloadLocationPermissionDialog thisDownloadLocationPermissionDialog = new DownloadLocationPermissionDialog();
+        thisDownloadLocationPermissionDialog.setArguments(argumentsBundle);
+        return thisDownloadLocationPermissionDialog;
     }
 
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Store the download type in a local variable.
+        int downloadType = getArguments().getInt("download_type");
+
         // Use a builder to create the alert dialog.
         AlertDialog.Builder dialogBuilder;
 
@@ -90,19 +82,31 @@ public class DownloadLocationPermissionDialog extends DialogFragment {
             dialogBuilder.setIcon(R.drawable.downloads_light);
         }
 
-        // Set an `onClick` listener on the negative button.  Using `null` as the listener closes the dialog without doing anything else.
+        // Set the title.
+        dialogBuilder.setTitle(R.string.download_location);
+
+        // Set the text.
+        dialogBuilder.setMessage(R.string.download_location_message);
+
+        // Set an `onClick` listener on the negative button.
         dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
             // Inform the parent activity that the dialog was closed.
             downloadLocationPermissionDialogListener.onCloseDownloadLocationPermissionDialog(downloadType);
         });
 
-        // Set the title.
-        dialogBuilder.setTitle(R.string.download_location);
+        // Create an alert dialog from the builder.
+        final AlertDialog alertDialog = dialogBuilder.create();
 
-        // Set the text.
-        dialogBuilder.setMessage(R.string.download_location_message);
+        // Disable screenshots if not allowed.
+        if (!MainWebViewActivity.allowScreenshots) {
+            // Remove the warning below that `getWindow()` might be null.
+            assert alertDialog.getWindow() != null;
+
+            // Disable screenshots.
+            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        }
 
-        // `onCreateDialog` requires the return of an `AlertDialog`.
-        return dialogBuilder.create();
+        // `onCreateDialog()` requires the return of an `AlertDialog`.
+        return alertDialog;
     }
 }
\ No newline at end of file