]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadImageDialog.java
94f258685b3bab4c7d4a24b5f6b17832290115de
[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     // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
45     private DownloadImageListener downloadImageListener;
46
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);
50     }
51
52     // Check to make sure tha the parent activity implements the listener.
53     @Override
54     public void onAttach(Context context) {
55         // Run the default commands.
56         super.onAttach(context);
57
58         // Get a handle for `DownloadImageListener` from the launching context.
59         downloadImageListener = (DownloadImageListener) context;
60     }
61
62     public static DownloadImageDialog imageUrl(String imageUrlString) {
63         // Create an arguments bundle.
64         Bundle argumentsBundle = new Bundle();
65
66         // Create a variable for the image name string.
67         String imageNameString;
68
69         // Extract the image name string from the image URL.
70         Uri imageUri = Uri.parse(imageUrlString);
71         imageNameString = imageUri.getLastPathSegment();
72
73         // Store the variables in the bundle.
74         argumentsBundle.putString("URL", imageUrlString);
75         argumentsBundle.putString("Image_Name", imageNameString);
76
77         // Add the arguments bundle to this instance of `DownloadFileDialog`.
78         DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
79         thisDownloadFileDialog.setArguments(argumentsBundle);
80         return thisDownloadFileDialog;
81     }
82
83     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
84     @SuppressLint("InflateParams")
85     @Override
86     @NonNull
87     public Dialog onCreateDialog(Bundle savedInstanceState) {
88         // Remove the warning below that `getArguments()` might be null.
89         assert getArguments() != null;
90
91         // Get the strings from the bundle.
92         String imageUrl = getArguments().getString("URL");
93         String imageFileName = getArguments().getString("Image_Name");
94
95         // Remove the warning below that `.getActivity()` might be null.
96         assert getActivity() != null;
97
98         // Get the activity's layout inflater.
99         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
100
101         // Use and alert dialog builder to create the alert dialog.
102         AlertDialog.Builder dialogBuilder;
103
104         // Set the style according to the theme.
105         if (MainWebViewActivity.darkTheme) {
106             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
107         } else {
108             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
109         }
110
111         // Set the title.
112         dialogBuilder.setTitle(R.string.save_image_as);
113
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));
116
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.
120         });
121
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);
126         });
127
128
129         // Create an alert dialog from the alert dialog builder.
130         final AlertDialog alertDialog = dialogBuilder.create();
131
132         // Remove the warning below that `getWindow()` might be null.
133         assert alertDialog.getWindow() != null;
134
135         // Disable screenshots if not allowed.
136         if (!MainWebViewActivity.allowScreenshots) {
137             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
138         }
139
140         // Show the keyboard when `alertDialog` is displayed on the screen.
141         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
142
143         // The alert dialog must be shown before the contents can be modified.
144         alertDialog.show();
145
146         // Set the text for `downloadImageNameTextView`.
147         EditText downloadImageNameTextView = alertDialog.findViewById(R.id.download_image_name);
148         downloadImageNameTextView.setText(imageFileName);
149
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);
156
157                 // Manually dismiss the alert dialog.
158                 alertDialog.dismiss();
159
160                 // Consume the event.
161                 return true;
162             } else {  // If any other key was pressed, do not consume the event.
163                 return false;
164             }
165         });
166
167         // `onCreateDialog` requires the return of an `AlertDialog`.
168         return alertDialog;
169     }
170 }