]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadImageDialog.java
Add setting to disable screenshots. https://redmine.stoutner.com/issues/266
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadImageDialog.java
1 /*
2  * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
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;
37
38 import com.stoutner.privacybrowser.R;
39 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
40
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
45     private String imageUrl;
46     private String imageFileName;
47
48     public static DownloadImageDialog imageUrl(String imageUrlString) {
49         // Create an arguments bundle.
50         Bundle argumentsBundle = new Bundle();
51
52         // Create a variable for the image name string.
53         String imageNameString;
54
55         // Extract the image name string from the image URL.
56         Uri imageUri = Uri.parse(imageUrlString);
57         imageNameString = imageUri.getLastPathSegment();
58
59         // Store the variables in the bundle.
60         argumentsBundle.putString("URL", imageUrlString);
61         argumentsBundle.putString("Image_Name", imageNameString);
62
63         // Add the arguments bundle to this instance of `DownloadFileDialog`.
64         DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
65         thisDownloadFileDialog.setArguments(argumentsBundle);
66         return thisDownloadFileDialog;
67     }
68
69     @Override
70     public void onCreate(Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72
73         // Remove the warning below that `getArguments()` might be null.
74         assert getArguments() != null;
75
76         // Store the strings in the local class variables.
77         imageUrl = getArguments().getString("URL");
78         imageFileName = getArguments().getString("Image_Name");
79     }
80
81     // The public interface is used to send information back to the parent activity.
82     public interface DownloadImageListener {
83         void onDownloadImage(AppCompatDialogFragment dialogFragment, String downloadUrl);
84     }
85
86     // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
87     private DownloadImageListener downloadImageListener;
88
89     // Check to make sure tha the parent activity implements the listener.
90     @Override
91     public void onAttach(Context context) {
92         super.onAttach(context);
93         try {
94             downloadImageListener = (DownloadImageListener) context;
95         } catch (ClassCastException exception) {
96             throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
97         }
98     }
99
100     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
101     @SuppressLint("InflateParams")
102     @Override
103     @NonNull
104     public Dialog onCreateDialog(Bundle savedInstanceState) {
105         // Remove the warning below that `.getActivity()` might be null.
106         assert getActivity() != null;
107
108         // Get the activity's layout inflater.
109         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
110
111         // Use and alert dialog builder to create the alert dialog.
112         AlertDialog.Builder dialogBuilder;
113
114         // Set the style according to the theme.
115         if (MainWebViewActivity.darkTheme) {
116             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
117         } else {
118             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
119         }
120
121         // Set the title.
122         dialogBuilder.setTitle(R.string.save_image_as);
123
124         // Set the view.  The parent view is `null` because it will be assigned by `AlertDialog`.
125         dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
126
127         // Set an listener on the negative button.
128         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
129             // Do nothing if `Cancel` is clicked.
130         });
131
132         // Set an listener on the positive button
133         dialogBuilder.setPositiveButton(R.string.download, (DialogInterface dialog, int which) -> {
134             // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
135             downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
136         });
137
138
139         // Create an alert dialog from the alert dialog builder.
140         final AlertDialog alertDialog = dialogBuilder.create();
141
142         // Remove the warning below that `getWindow()` might be null.
143         assert alertDialog.getWindow() != null;
144
145         // Disable screenshots if not allowed.
146         if (!MainWebViewActivity.allowScreenshots) {
147             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
148         }
149
150         // Show the keyboard when `alertDialog` is displayed on the screen.
151         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
152
153         // The alert dialog must be shown before the contents can be modified.
154         alertDialog.show();
155
156         // Set the text for `downloadImageNameTextView`.
157         EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
158         downloadImageNameTextView.setText(imageFileName);
159
160         // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
161         downloadImageNameTextView.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
162             // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
163             if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
164                 // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
165                 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
166
167                 // Manually dismiss the alert dialog.
168                 alertDialog.dismiss();
169
170                 // Consume the event.
171                 return true;
172             } else {  // If any other key was pressed, do not consume the event.
173                 return false;
174             }
175         });
176
177         // `onCreateDialog` requires the return of an `AlertDialog`.
178         return alertDialog;
179     }
180 }