2 * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs;
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;
37 import com.stoutner.privacybrowser.R;
39 // `android.support.v7.app.AlertDialog` uses more of the horizontal screen real estate versus `android.app.AlertDialog's` smaller width.
40 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <=22.
41 public class DownloadImage extends AppCompatDialogFragment {
43 private String imageUrl;
44 private String imageFileName;
46 public static DownloadImage imageUrl(String imageUrlString) {
47 // Create `argumentsBundle`.
48 Bundle argumentsBundle = new Bundle();
50 String imageNameString;
52 Uri imageUri = Uri.parse(imageUrlString);
53 imageNameString = imageUri.getLastPathSegment();
55 // Store the variables in the `Bundle`.
56 argumentsBundle.putString("URL", imageUrlString);
57 argumentsBundle.putString("Image_Name", imageNameString);
59 // Add `argumentsBundle` to this instance of `DownloadFile`.
60 DownloadImage thisDownloadFileDialog = new DownloadImage();
61 thisDownloadFileDialog.setArguments(argumentsBundle);
62 return thisDownloadFileDialog;
66 public void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
69 // Store the strings in the local class variables.
70 imageUrl = getArguments().getString("URL");
71 imageFileName = getArguments().getString("Image_Name");
74 // The public interface is used to send information back to the parent activity.
75 public interface DownloadImageListener {
76 void onDownloadImage(AppCompatDialogFragment dialogFragment, String downloadUrl);
79 // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
80 private DownloadImageListener downloadImageListener;
82 // Check to make sure tha the parent activity implements the listener.
84 public void onAttach(Context context) {
85 super.onAttach(context);
87 downloadImageListener = (DownloadImageListener) context;
88 } catch (ClassCastException exception) {
89 throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
93 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
94 @SuppressLint("InflateParams")
97 public Dialog onCreateDialog(Bundle savedInstanceState) {
98 // Get the activity's layout inflater.
99 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
101 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.lightAlertDialog` formats the color of the button text.
102 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
103 dialogBuilder.setTitle(R.string.save_image_as);
104 // The parent view is `null` because it will be assigned by `AlertDialog`.
105 dialogBuilder.setView(layoutInflater.inflate(R.layout.download_image_dialog, null));
107 // Set an `onClick()` listener on the negative button.
108 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
110 public void onClick(DialogInterface dialog, int which) {
111 // Do nothing if `Cancel` is clicked.
115 // Set an `onClick()` listener on the positive button
116 dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
118 public void onClick(DialogInterface dialog, int which) {
119 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
120 downloadImageListener.onDownloadImage(DownloadImage.this, imageUrl);
125 // Create an `AlertDialog` from the `AlertDialog.Builder`.
126 final AlertDialog alertDialog = dialogBuilder.create();
128 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
129 assert alertDialog.getWindow() != null;
131 // Show the keyboard when `alertDialog` is displayed on the screen.
132 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
134 // We need to show `alertDialog` before we can modify the contents.
137 // Set the text for `downloadImageNameTextView`.
138 EditText downloadImageNameTextView = (EditText) alertDialog.findViewById(R.id.download_image_name);
139 assert downloadImageNameTextView != null; // Remove the warning on the following line that `downloadImageNameTextView` might be `null`.
140 downloadImageNameTextView.setText(imageFileName);
142 // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
143 downloadImageNameTextView.setOnKeyListener(new View.OnKeyListener() {
145 public boolean onKey (View v, int keyCode, KeyEvent event) {
146 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
147 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
148 // trigger `onDownloadImage()` and return the `DialogFragment` and the URL to the parent activity.
149 downloadImageListener.onDownloadImage(DownloadImage.this, imageUrl);
150 // Manually dismiss `alertDialog`.
151 alertDialog.dismiss();
152 // Consume the event.
154 } else { // If any other key was pressed, do not consume the event.
161 // `onCreateDialog` requires the return of an `AlertDialog`.