X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FDownloadFileDialog.java;h=cf9481249e12c804dc2b74b05fe13c83c48db4c1;hb=012e5595c82d6e8d0b8a46f1ef18a02a56341182;hp=cbf08390254a854a4023c234e402117e279dfeff;hpb=d420aa6be32a78b27905074edc3881a6e71d2263;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java index cbf08390..cf948124 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java @@ -42,10 +42,22 @@ import com.stoutner.privacybrowser.activities.MainWebViewActivity; import java.util.Locale; public class DownloadFileDialog extends AppCompatDialogFragment { + // `downloadFileListener` is used in `onAttach()` and `onCreateDialog()`. + private DownloadFileListener downloadFileListener; - private String downloadUrl; - private String downloadFileName; - private String fileSize; + // The public interface is used to send information back to the parent activity. + public interface DownloadFileListener { + void onDownloadFile(AppCompatDialogFragment dialogFragment, String downloadUrl); + } + + @Override + public void onAttach(Context context) { + // Run the default commands. + super.onAttach(context); + + // Get a handle for `DownloadFileListener` from the launching context. + downloadFileListener = (DownloadFileListener) context; + } public static DownloadFileDialog fromUrl(String urlString, String contentDisposition, long contentLength) { // Create an arguments bundle. @@ -78,59 +90,35 @@ public class DownloadFileDialog extends AppCompatDialogFragment { } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - + @NonNull + // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. + @SuppressLint("InflateParams") + public Dialog onCreateDialog(Bundle savedInstanceState) { // Remove the warning below that `getArguments()` might be null. assert getArguments() != null; - // Store the strings in the local class variables. - downloadUrl = getArguments().getString("URL"); - downloadFileName = getArguments().getString("File_Name"); - - // Get the `File_Size`. + // Store the variables from the bundle. + String downloadUrl = getArguments().getString("URL"); + String downloadFileName = getArguments().getString("File_Name"); long fileSizeLong = getArguments().getLong("File_Size"); + // Initialize the file size string. + String fileSize; + // Convert `fileSizeLong` to a String. if (fileSizeLong == -1) { // We don't know the file size. fileSize = getString(R.string.unknown_size); } else { // Convert `fileSize` to MB and store it in `fileSizeString`. `%.3g` displays the three most significant digits. fileSize = String.format(Locale.getDefault(), "%.3g", (float) fileSizeLong / 1048576) + " MB"; } - } - - // The public interface is used to send information back to the parent activity. - public interface DownloadFileListener { - void onDownloadFile(AppCompatDialogFragment dialogFragment, String downloadUrl); - } - - // `downloadFileListener` is used in `onAttach()` and `onCreateDialog()`. - private DownloadFileListener downloadFileListener; - - @Override - public void onAttach(Context context) { - super.onAttach(context); - - // Check to make sure the parent activity implements the listener. - try { - downloadFileListener = (DownloadFileListener) context; - } catch (ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement DownloadFileListener."); - } - } - @Override - @NonNull - // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. - @SuppressLint("InflateParams") - public Dialog onCreateDialog(Bundle savedInstanceState) { // Remove the warning below that `getActivity()` might be null; assert getActivity() != null; // Get the activity's layout inflater. LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - // Use `AlertDialog.Builder` to create the `AlertDialog`. + // Use an alert dialog builder to create the alert dialog. AlertDialog.Builder dialogBuilder; // Set the style according to the theme. @@ -146,24 +134,29 @@ public class DownloadFileDialog extends AppCompatDialogFragment { // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`. dialogBuilder.setView(layoutInflater.inflate(R.layout.download_file_dialog, null)); - // Set an `onClick()` listener on the negative button. + // Set an listener on the negative button. dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> { // Do nothing if `Cancel` is clicked. The `Dialog` will automatically close. }); - // Set an `onClick()` listener on the positive button + // Set an listener on the positive button dialogBuilder.setPositiveButton(R.string.download, (DialogInterface dialog, int which) -> { // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity. downloadFileListener.onDownloadFile(DownloadFileDialog.this, downloadUrl); }); - // Create an `AlertDialog` from the `AlertDialog.Builder`. + // Create an alert dialog from the alert dialog builder`. final AlertDialog alertDialog = dialogBuilder.create(); - // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`. + // Remove the warning below that `getWindow()` might be null. assert alertDialog.getWindow() != null; - // Show the keyboard when `alertDialog` is displayed on the screen. + // Disable screenshots if not allowed. + if (!MainWebViewActivity.allowScreenshots) { + alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + + // Show the keyboard when alert dialog is displayed on the screen. alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); // We need to show `alertDialog` before we can modify the contents.