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