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=6ce84771d091e0de9ab067d16953bb7d6fd17532;hp=1fd2fd775cbbc78d9c6d1c0220dbb87824a474f1;hb=4d51aa9acb8daaec1326f14e5025fde6d1f0dcd8;hpb=ba40295dffd761ccdc95d3b46ca7acbad1f00d5e 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 1fd2fd77..6ce84771 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadFileDialog.java @@ -24,10 +24,11 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.view.KeyEvent; -import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.EditText; @@ -37,7 +38,6 @@ 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; @@ -51,7 +51,7 @@ public class DownloadFileDialog extends DialogFragment { } @Override - public void onAttach(Context context) { + public void onAttach(@NonNull Context context) { // Run the default commands. super.onAttach(context); @@ -66,14 +66,17 @@ public class DownloadFileDialog extends DialogFragment { // Create a variable for the file name string. String fileNameString; + // Get the index of the end of `filename=` from the file name string. + int fileNameIndex = contentDisposition.indexOf("filename=") + 9; + // 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 )) { + } else if (contentDisposition.contains("filename=") && ((contentDisposition.indexOf(";", fileNameIndex)) > 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)); + fileNameString = contentDisposition.substring(fileNameIndex, contentDisposition.indexOf(";", fileNameIndex)); } 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); + fileNameString = contentDisposition.substring(fileNameIndex); } else { // `contentDisposition` does not contain the filename, so use the last path segment of the URL. Uri downloadUri = Uri.parse(urlString); fileNameString = downloadUri.getLastPathSegment(); @@ -113,17 +116,18 @@ public class DownloadFileDialog extends DialogFragment { fileSize = String.format(Locale.getDefault(), "%.3g", (float) fileSizeLong / 1048576) + " MB"; } - // Remove the warning below that `getActivity()` might be null; - assert getActivity() != null; + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); - // Get the activity's layout inflater. - LayoutInflater layoutInflater = getActivity().getLayoutInflater(); + // Get the screenshot and theme preferences. + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); // Use an alert dialog builder to create the alert dialog. AlertDialog.Builder dialogBuilder; // Set the style according to the theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark); } else { dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight); @@ -132,8 +136,18 @@ public class DownloadFileDialog extends DialogFragment { // Set the title. dialogBuilder.setTitle(R.string.save_as); + // Set the icon according to the theme. + if (darkTheme) { + dialogBuilder.setIcon(R.drawable.save_dialog_dark); + } else { + dialogBuilder.setIcon(R.drawable.save_dialog_light); + } + + // Remove the warning below 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(layoutInflater.inflate(R.layout.download_file_dialog, null)); + dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.download_file_dialog, null)); // Set an listener on the negative button. dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> { @@ -153,7 +167,7 @@ public class DownloadFileDialog extends DialogFragment { assert alertDialog.getWindow() != null; // Disable screenshots if not allowed. - if (!MainWebViewActivity.allowScreenshots) { + if (!allowScreenshots) { alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); }