]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadImageDialog.java
9c1afe67e91bb7f96f3cb69b56b66cbfd0c21c58
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadImageDialog.java
1 /*
2  * Copyright © 2016-2019 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.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;
35
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.
38
39 import com.stoutner.privacybrowser.R;
40
41 public class DownloadImageDialog extends DialogFragment {
42     // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
43     private DownloadImageListener downloadImageListener;
44
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);
48     }
49
50     // Check to make sure tha the parent activity implements the listener.
51     @Override
52     public void onAttach(@NonNull Context context) {
53         // Run the default commands.
54         super.onAttach(context);
55
56         // Get a handle for `DownloadImageListener` from the launching context.
57         downloadImageListener = (DownloadImageListener) context;
58     }
59
60     public static DownloadImageDialog imageUrl(String imageUrlString) {
61         // Create an arguments bundle.
62         Bundle argumentsBundle = new Bundle();
63
64         // Create a variable for the image name string.
65         String imageNameString;
66
67         // Extract the image name string from the image URL.
68         Uri imageUri = Uri.parse(imageUrlString);
69         imageNameString = imageUri.getLastPathSegment();
70
71         // Store the variables in the bundle.
72         argumentsBundle.putString("URL", imageUrlString);
73         argumentsBundle.putString("Image_Name", imageNameString);
74
75         // Add the arguments bundle to this instance of `DownloadFileDialog`.
76         DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
77         thisDownloadFileDialog.setArguments(argumentsBundle);
78         return thisDownloadFileDialog;
79     }
80
81     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
82     @SuppressLint("InflateParams")
83     @Override
84     @NonNull
85     public Dialog onCreateDialog(Bundle savedInstanceState) {
86         // Remove the warning below that `getArguments()` might be null.
87         assert getArguments() != null;
88
89         // Get the strings from the bundle.
90         String imageUrl = getArguments().getString("URL");
91         String imageFileName = getArguments().getString("Image_Name");
92
93         // Remove the warning below that `.getActivity()` might be null.
94         assert getActivity() != null;
95
96         // Get a handle for the shared preferences.
97         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
98
99         // Get the screenshot and theme preferences.
100         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
101         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
102
103         // Use and alert dialog builder to create the alert dialog.
104         AlertDialog.Builder dialogBuilder;
105
106         // Set the style according to the theme.
107         if (darkTheme) {
108             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
109         } else {
110             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
111         }
112
113         // Set the title.
114         dialogBuilder.setTitle(R.string.save_image_as);
115
116         // Set the icon according to the theme.
117         if (darkTheme) {
118             dialogBuilder.setIcon(R.drawable.images_enabled_dark);
119         } else {
120             dialogBuilder.setIcon(R.drawable.images_enabled_light);
121         }
122
123         // Remove the incorrect lint warning below that `getActivity() might be null.
124         assert getActivity() != null;
125
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));
128
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.
132         });
133
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);
138         });
139
140
141         // Create an alert dialog from the alert dialog builder.
142         final AlertDialog alertDialog = dialogBuilder.create();
143
144         // Remove the warning below that `getWindow()` might be null.
145         assert alertDialog.getWindow() != null;
146
147         // Disable screenshots if not allowed.
148         if (!allowScreenshots) {
149             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
150         }
151
152         // Show the keyboard when `alertDialog` is displayed on the screen.
153         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
154
155         // The alert dialog must be shown before the contents can be modified.
156         alertDialog.show();
157
158         // Set the text for `downloadImageNameTextView`.
159         EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
160         downloadImageNameTextView.setText(imageFileName);
161
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);
168
169                 // Manually dismiss the alert dialog.
170                 alertDialog.dismiss();
171
172                 // Consume the event.
173                 return true;
174             } else {  // If any other key was pressed, do not consume the event.
175                 return false;
176             }
177         });
178
179         // `onCreateDialog` requires the return of an `AlertDialog`.
180         return alertDialog;
181     }
182 }