2 * Copyright © 2016-2018 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.support.annotation.NonNull;
30 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
31 import android.support.v7.app.AppCompatDialogFragment;
32 import android.view.KeyEvent;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.widget.EditText;
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
41 // `android.support.v7.app.AlertDialog` uses more of the horizontal screen real estate versus `android.app.AlertDialog's` smaller width.
42 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
43 public class DownloadImageDialog extends AppCompatDialogFragment {
44 // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
45 private DownloadImageListener downloadImageListener;
47 // The public interface is used to send information back to the parent activity.
48 public interface DownloadImageListener {
49 void onDownloadImage(AppCompatDialogFragment dialogFragment, String downloadUrl);
52 // Check to make sure tha the parent activity implements the listener.
54 public void onAttach(Context context) {
55 // Run the default commands.
56 super.onAttach(context);
58 // Get a handle for `DownloadImageListener` from the launching context.
59 downloadImageListener = (DownloadImageListener) context;
62 public static DownloadImageDialog imageUrl(String imageUrlString) {
63 // Create an arguments bundle.
64 Bundle argumentsBundle = new Bundle();
66 // Create a variable for the image name string.
67 String imageNameString;
69 // Extract the image name string from the image URL.
70 Uri imageUri = Uri.parse(imageUrlString);
71 imageNameString = imageUri.getLastPathSegment();
73 // Store the variables in the bundle.
74 argumentsBundle.putString("URL", imageUrlString);
75 argumentsBundle.putString("Image_Name", imageNameString);
77 // Add the arguments bundle to this instance of `DownloadFileDialog`.
78 DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
79 thisDownloadFileDialog.setArguments(argumentsBundle);
80 return thisDownloadFileDialog;
83 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
84 @SuppressLint("InflateParams")
87 public Dialog onCreateDialog(Bundle savedInstanceState) {
88 // Remove the warning below that `getArguments()` might be null.
89 assert getArguments() != null;
91 // Get the strings from the bundle.
92 String imageUrl = getArguments().getString("URL");
93 String imageFileName = getArguments().getString("Image_Name");
95 // Remove the warning below that `.getActivity()` might be null.
96 assert getActivity() != null;
98 // Get the activity's layout inflater.
99 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
101 // Use and alert dialog builder to create the alert dialog.
102 AlertDialog.Builder dialogBuilder;
104 // Set the style according to the theme.
105 if (MainWebViewActivity.darkTheme) {
106 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
108 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
112 dialogBuilder.setTitle(R.string.save_image_as);
114 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
115 dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
117 // Set an listener on the negative button.
118 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
119 // Do nothing if `Cancel` is clicked.
122 // Set an listener on the positive button
123 dialogBuilder.setPositiveButton(R.string.download, (DialogInterface dialog, int which) -> {
124 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
125 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
129 // Create an alert dialog from the alert dialog builder.
130 final AlertDialog alertDialog = dialogBuilder.create();
132 // Remove the warning below that `getWindow()` might be null.
133 assert alertDialog.getWindow() != null;
135 // Disable screenshots if not allowed.
136 if (!MainWebViewActivity.allowScreenshots) {
137 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
140 // Show the keyboard when `alertDialog` is displayed on the screen.
141 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
143 // The alert dialog must be shown before the contents can be modified.
146 // Set the text for `downloadImageNameTextView`.
147 EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
148 downloadImageNameTextView.setText(imageFileName);
150 // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
151 downloadImageNameTextView.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
152 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
153 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
154 // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
155 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
157 // Manually dismiss the alert dialog.
158 alertDialog.dismiss();
160 // Consume the event.
162 } else { // If any other key was pressed, do not consume the event.
167 // `onCreateDialog` requires the return of an `AlertDialog`.