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.content.SharedPreferences;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.preference.PreferenceManager;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.EditText;
36 import androidx.annotation.NonNull;
37 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
39 import com.stoutner.privacybrowser.R;
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 a handle for the shared preferences.
97 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
99 // Get the screenshot and theme preferences.
100 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
101 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
103 // Use and alert dialog builder to create the alert dialog.
104 AlertDialog.Builder dialogBuilder;
106 // Set the style according to the theme.
108 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
110 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
114 dialogBuilder.setTitle(R.string.save_image_as);
116 // Set the icon according to the theme.
118 dialogBuilder.setIcon(R.drawable.images_enabled_dark);
120 dialogBuilder.setIcon(R.drawable.images_enabled_light);
123 // Remove the incorrect lint warning below that `getActivity() might be null.
124 assert getActivity() != null;
126 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
127 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.download_image_dialog, null));
129 // Set an listener on the negative button.
130 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
131 // Do nothing if `Cancel` is clicked.
134 // Set an listener on the positive button
135 dialogBuilder.setPositiveButton(R.string.download, (DialogInterface dialog, int which) -> {
136 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
137 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
141 // Create an alert dialog from the alert dialog builder.
142 final AlertDialog alertDialog = dialogBuilder.create();
144 // Remove the warning below that `getWindow()` might be null.
145 assert alertDialog.getWindow() != null;
147 // Disable screenshots if not allowed.
148 if (!allowScreenshots) {
149 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
152 // Show the keyboard when `alertDialog` is displayed on the screen.
153 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
155 // The alert dialog must be shown before the contents can be modified.
158 // Set the text for `downloadImageNameTextView`.
159 EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
160 downloadImageNameTextView.setText(imageFileName);
162 // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
163 downloadImageNameTextView.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
164 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
165 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
166 // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
167 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
169 // Manually dismiss the alert dialog.
170 alertDialog.dismiss();
172 // Consume the event.
174 } else { // If any other key was pressed, do not consume the event.
179 // `onCreateDialog` requires the return of an `AlertDialog`.