2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.dialogs;
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.view.KeyEvent;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.WindowManager;
33 import android.widget.EditText;
35 import androidx.annotation.NonNull;
36 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
41 public class DownloadImageDialog extends DialogFragment {
42 // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
43 private DownloadImageListener downloadImageListener;
45 // The public interface is used to send information back to the parent activity.
46 public interface DownloadImageListener {
47 void onDownloadImage(DialogFragment dialogFragment, String downloadUrl);
50 // Check to make sure tha the parent activity implements the listener.
52 public void onAttach(Context context) {
53 // Run the default commands.
54 super.onAttach(context);
56 // Get a handle for `DownloadImageListener` from the launching context.
57 downloadImageListener = (DownloadImageListener) context;
60 public static DownloadImageDialog imageUrl(String imageUrlString) {
61 // Create an arguments bundle.
62 Bundle argumentsBundle = new Bundle();
64 // Create a variable for the image name string.
65 String imageNameString;
67 // Extract the image name string from the image URL.
68 Uri imageUri = Uri.parse(imageUrlString);
69 imageNameString = imageUri.getLastPathSegment();
71 // Store the variables in the bundle.
72 argumentsBundle.putString("URL", imageUrlString);
73 argumentsBundle.putString("Image_Name", imageNameString);
75 // Add the arguments bundle to this instance of `DownloadFileDialog`.
76 DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
77 thisDownloadFileDialog.setArguments(argumentsBundle);
78 return thisDownloadFileDialog;
81 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
82 @SuppressLint("InflateParams")
85 public Dialog onCreateDialog(Bundle savedInstanceState) {
86 // Remove the warning below that `getArguments()` might be null.
87 assert getArguments() != null;
89 // Get the strings from the bundle.
90 String imageUrl = getArguments().getString("URL");
91 String imageFileName = getArguments().getString("Image_Name");
93 // Remove the warning below that `.getActivity()` might be null.
94 assert getActivity() != null;
96 // Get the activity's layout inflater.
97 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
99 // Use and alert dialog builder to create the alert dialog.
100 AlertDialog.Builder dialogBuilder;
102 // Set the style according to the theme.
103 if (MainWebViewActivity.darkTheme) {
104 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
106 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
110 dialogBuilder.setTitle(R.string.save_image_as);
112 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
113 dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
115 // Set an listener on the negative button.
116 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
117 // Do nothing if `Cancel` is clicked.
120 // Set an listener on the positive button
121 dialogBuilder.setPositiveButton(R.string.download, (DialogInterface dialog, int which) -> {
122 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
123 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
127 // Create an alert dialog from the alert dialog builder.
128 final AlertDialog alertDialog = dialogBuilder.create();
130 // Remove the warning below that `getWindow()` might be null.
131 assert alertDialog.getWindow() != null;
133 // Disable screenshots if not allowed.
134 if (!MainWebViewActivity.allowScreenshots) {
135 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
138 // Show the keyboard when `alertDialog` is displayed on the screen.
139 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
141 // The alert dialog must be shown before the contents can be modified.
144 // Set the text for `downloadImageNameTextView`.
145 EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
146 downloadImageNameTextView.setText(imageFileName);
148 // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
149 downloadImageNameTextView.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
150 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
151 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
152 // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
153 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
155 // Manually dismiss the alert dialog.
156 alertDialog.dismiss();
158 // Consume the event.
160 } else { // If any other key was pressed, do not consume the event.
165 // `onCreateDialog` requires the return of an `AlertDialog`.