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