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