X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FDownloadFileDialog.java;h=5ff4a851a145d4eed0ee57ff9ae44555b31d3b8b;hb=0a5d2eabceeafb49a957598538aa74d4f11dfce0;hp=a496eb2075810042569d126019e307b644d59f29;hpb=5bcf4ca90f27512b94fb7aca4fad37b4e4774655;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 a496eb20..5ff4a851 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 Soren Stoutner . + * Copyright © 2016-2018 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -48,9 +48,10 @@ public class DownloadFileDialog extends AppCompatDialogFragment { private String fileSize; public static DownloadFileDialog fromUrl(String urlString, String contentDisposition, long contentLength) { - // Create `argumentsBundle`. + // Create an arguments bundle. Bundle argumentsBundle = new Bundle(); + // Create a variable for the file name string. String fileNameString; // Parse `filename` from `contentDisposition`. @@ -65,12 +66,12 @@ public class DownloadFileDialog extends AppCompatDialogFragment { fileNameString = downloadUri.getLastPathSegment(); } - // Store the variables in the `Bundle`. + // Store the variables in the bundle. argumentsBundle.putString("URL", urlString); argumentsBundle.putString("File_Name", fileNameString); argumentsBundle.putLong("File_Size", contentLength); - // Add `argumentsBundle` to this instance of `DownloadFileDialog`. + // Add the arguments bundle to this instance of `DownloadFileDialog`. DownloadFileDialog thisDownloadFileDialog = new DownloadFileDialog(); thisDownloadFileDialog.setArguments(argumentsBundle); return thisDownloadFileDialog; @@ -80,6 +81,9 @@ public class DownloadFileDialog extends AppCompatDialogFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(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"); @@ -120,10 +124,13 @@ public class DownloadFileDialog extends AppCompatDialogFragment { // `@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. @@ -139,58 +146,56 @@ 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. - dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Do nothing if `Cancel` is clicked. The `Dialog` will automatically close. - } + // 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 - dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity. - downloadFileListener.onDownloadFile(DownloadFileDialog.this, downloadUrl); - } + // 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. alertDialog.show(); // Set the text for `downloadFileSizeTextView`. - TextView downloadFileSizeTextView = (TextView) alertDialog.findViewById(R.id.download_file_size); + TextView downloadFileSizeTextView = alertDialog.findViewById(R.id.download_file_size); downloadFileSizeTextView.setText(fileSize); // Set the text for `downloadFileNameTextView`. - EditText downloadFileNameTextView = (EditText) alertDialog.findViewById(R.id.download_file_name); + EditText downloadFileNameTextView = alertDialog.findViewById(R.id.download_file_name); downloadFileNameTextView.setText(downloadFileName); // Allow the `enter` key on the keyboard to save the file from `downloadFileNameTextView`. - downloadFileNameTextView.setOnKeyListener(new View.OnKeyListener() { - @Override - public boolean onKey (View v, int keyCode, KeyEvent event) { - // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download. - if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { - // trigger `onDownloadFile()` and return the `DialogFragment` and the URL to the parent activity. - downloadFileListener.onDownloadFile(DownloadFileDialog.this, downloadUrl); - // Manually dismiss `alertDialog`. - alertDialog.dismiss(); - // Consume the event. - return true; - } else { // If any other key was pressed, do not consume the event. - return false; - } + downloadFileNameTextView.setOnKeyListener((View v, int keyCode, KeyEvent event) -> { + // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download. + if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { + // trigger `onDownloadFile()` and return the `DialogFragment` and the URL to the parent activity. + downloadFileListener.onDownloadFile(DownloadFileDialog.this, downloadUrl); + + // Manually dismiss the alert dialog. + alertDialog.dismiss(); + + // Consume the event. + return true; + } else { // If any other key was pressed, do not consume the event. + return false; } });