]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadImageDialog.java
c6bcb6fbd5cfba16f2064dc3e0fd4199c66758ab
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadImageDialog.java
1 /*
2  * Copyright © 2016-2017 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 `argumentsBundle`.
50         Bundle argumentsBundle = new Bundle();
51
52         String imageNameString;
53
54         Uri imageUri = Uri.parse(imageUrlString);
55         imageNameString = imageUri.getLastPathSegment();
56
57         // Store the variables in the `Bundle`.
58         argumentsBundle.putString("URL", imageUrlString);
59         argumentsBundle.putString("Image_Name", imageNameString);
60
61         // Add `argumentsBundle` to this instance of `DownloadFileDialog`.
62         DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
63         thisDownloadFileDialog.setArguments(argumentsBundle);
64         return thisDownloadFileDialog;
65     }
66
67     @Override
68     public void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70
71         // Store the strings in the local class variables.
72         imageUrl = getArguments().getString("URL");
73         imageFileName = getArguments().getString("Image_Name");
74     }
75
76     // The public interface is used to send information back to the parent activity.
77     public interface DownloadImageListener {
78         void onDownloadImage(AppCompatDialogFragment dialogFragment, String downloadUrl);
79     }
80
81     // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
82     private DownloadImageListener downloadImageListener;
83
84     // Check to make sure tha the parent activity implements the listener.
85     @Override
86     public void onAttach(Context context) {
87         super.onAttach(context);
88         try {
89             downloadImageListener = (DownloadImageListener) context;
90         } catch (ClassCastException exception) {
91             throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
92         }
93     }
94
95     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
96     @SuppressLint("InflateParams")
97     @Override
98     @NonNull
99     public Dialog onCreateDialog(Bundle savedInstanceState) {
100         // Get the activity's layout inflater.
101         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
102
103         // Use `AlertDialog.Builder` to create the `AlertDialog`.
104         AlertDialog.Builder dialogBuilder;
105
106         // Set the style according to the theme.
107         if (MainWebViewActivity.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 view.  The parent view is `null` because it will be assigned by `AlertDialog`.
117         dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
118
119         // Set an `onClick()` listener on the negative button.
120         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
121             @Override
122             public void onClick(DialogInterface dialog, int which) {
123                 // Do nothing if `Cancel` is clicked.
124             }
125         });
126
127         // Set an `onClick()` listener on the positive button
128         dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
129             @Override
130             public void onClick(DialogInterface dialog, int which) {
131                 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
132                 downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
133             }
134         });
135
136
137         // Create an `AlertDialog` from the `AlertDialog.Builder`.
138         final AlertDialog alertDialog = dialogBuilder.create();
139
140         // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
141         assert alertDialog.getWindow() != null;
142
143         // Show the keyboard when `alertDialog` is displayed on the screen.
144         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
145
146         // We need to show `alertDialog` before we can modify the contents.
147         alertDialog.show();
148
149         // Set the text for `downloadImageNameTextView`.
150         EditText downloadImageNameTextView = (EditText) alertDialog.findViewById(R.id.download_image_name);
151         downloadImageNameTextView.setText(imageFileName);
152
153         // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
154         downloadImageNameTextView.setOnKeyListener(new View.OnKeyListener() {
155             @Override
156             public boolean onKey (View v, int keyCode, KeyEvent event) {
157                 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
158                 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
159                     // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
160                     downloadImageListener.onDownloadImage(DownloadImageDialog.this, imageUrl);
161                     // Manually dismiss `alertDialog`.
162                     alertDialog.dismiss();
163                     // Consume the event.
164                     return true;
165                 } else {  // If any other key was pressed, do not consume the event.
166                     return false;
167                 }
168             }
169         });
170
171
172         // `onCreateDialog` requires the return of an `AlertDialog`.
173         return alertDialog;
174     }
175 }