X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FDownloadFileDialog.java;h=1fd2fd775cbbc78d9c6d1c0220dbb87824a474f1;hp=5ff4a851a145d4eed0ee57ff9ae44555b31d3b8b;hb=ba40295dffd761ccdc95d3b46ca7acbad1f00d5e;hpb=0a5d2eabceeafb49a957598538aa74d4f11dfce0 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 5ff4a851..1fd2fd77 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-2018 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -26,9 +26,6 @@ import android.content.Context; import android.content.DialogInterface; import android.net.Uri; import android.os.Bundle; -import android.support.annotation.NonNull; -// We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22. -import android.support.v7.app.AppCompatDialogFragment; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -36,16 +33,31 @@ import android.view.WindowManager; import android.widget.EditText; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22. + import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.MainWebViewActivity; import java.util.Locale; -public class DownloadFileDialog extends AppCompatDialogFragment { +public class DownloadFileDialog extends DialogFragment { + // `downloadFileListener` is used in `onAttach()` and `onCreateDialog()`. + private DownloadFileListener downloadFileListener; + + // The public interface is used to send information back to the parent activity. + public interface DownloadFileListener { + void onDownloadFile(DialogFragment dialogFragment, String downloadUrl); + } + + @Override + public void onAttach(Context context) { + // Run the default commands. + super.onAttach(context); - private String downloadUrl; - private String downloadFileName; - private String fileSize; + // 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. @@ -54,13 +66,14 @@ public class DownloadFileDialog extends AppCompatDialogFragment { // Create a variable for the file name string. String fileNameString; - // Parse `filename` from `contentDisposition`. + // Parse the filename from `contentDisposition`. if (contentDisposition.contains("filename=\"")) { // The file name is contained in a string surrounded by `""`. fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=\"") + 10, contentDisposition.indexOf("\"", contentDisposition.indexOf("filename=\"") + 10)); - } else if (contentDisposition.contains("filename=") && ((contentDisposition.indexOf(";", contentDisposition.indexOf("filename=") + 9)) > 0 )) { // The file name is contained in a string beginning with `filename=` and ending with `;`. + } else if (contentDisposition.contains("filename=") && ((contentDisposition.indexOf(";", contentDisposition.indexOf("filename=") + 9)) > 0 )) { + // The file name is contained in a string beginning with `filename=` and ending with `;`. fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9, contentDisposition.indexOf(";", contentDisposition.indexOf("filename=") + 9)); } else if (contentDisposition.contains("filename=")) { // The file name is contained in a string beginning with `filename=` and proceeding to the end of `contentDisposition`. - fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9, contentDisposition.length()); + fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9); } else { // `contentDisposition` does not contain the filename, so use the last path segment of the URL. Uri downloadUri = Uri.parse(urlString); fileNameString = downloadUri.getLastPathSegment(); @@ -78,52 +91,28 @@ 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;