2 * Copyright 2016-2017 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.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;
38 import com.stoutner.privacybrowser.R;
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 DownloadImageDialog extends AppCompatDialogFragment {
44 private String imageUrl;
45 private String imageFileName;
47 public static DownloadImageDialog imageUrl(String imageUrlString) {
48 // Create `argumentsBundle`.
49 Bundle argumentsBundle = new Bundle();
51 String imageNameString;
53 Uri imageUri = Uri.parse(imageUrlString);
54 imageNameString = imageUri.getLastPathSegment();
56 // Store the variables in the `Bundle`.
57 argumentsBundle.putString("URL", imageUrlString);
58 argumentsBundle.putString("Image_Name", imageNameString);
60 // Add `argumentsBundle` to this instance of `DownloadFileDialog`.
61 DownloadImageDialog thisDownloadFileDialog = new DownloadImageDialog();
62 thisDownloadFileDialog.setArguments(argumentsBundle);
63 return thisDownloadFileDialog;
67 public void onCreate(Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
70 // Store the strings in the local class variables.
71 imageUrl = getArguments().getString("URL");
72 imageFileName = getArguments().getString("Image_Name");
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);
80 // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
81 private DownloadImageListener downloadImageListener;
83 // Check to make sure tha the parent activity implements the listener.
85 public void onAttach(Context context) {
86 super.onAttach(context);
88 downloadImageListener = (DownloadImageListener) context;
89 } catch (ClassCastException exception) {
90 throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
94 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
95 @SuppressLint("InflateParams")
98 public Dialog onCreateDialog(Bundle savedInstanceState) {
99 // Get the activity's layout inflater.
100 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
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));
108 // Set an `onClick()` listener on the negative button.
109 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
111 public void onClick(DialogInterface dialog, int which) {
112 // Do nothing if `Cancel` is clicked.
116 // Set an `onClick()` listener on the positive button
117 dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
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(DownloadImageDialog.this, imageUrl);
126 // Create an `AlertDialog` from the `AlertDialog.Builder`.
127 final AlertDialog alertDialog = dialogBuilder.create();
129 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
130 assert alertDialog.getWindow() != null;
132 // Show the keyboard when `alertDialog` is displayed on the screen.
133 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
135 // We need to show `alertDialog` before we can modify the contents.
138 // Set the text for `downloadImageNameTextView`.
139 EditText downloadImageNameTextView = (EditText) alertDialog.findViewById(R.id.download_image_name);
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(DownloadImageDialog.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`.