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;
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 // `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 {
41 private String imageUrl;
42 private String imageFileName;
44 public static DownloadImage imageUrl(String imageUrlString) {
45 // Create `argumentsBundle`.
46 Bundle argumentsBundle = new Bundle();
48 String imageNameString;
50 Uri imageUri = Uri.parse(imageUrlString);
51 imageNameString = imageUri.getLastPathSegment();
53 // Store the variables in the `Bundle`.
54 argumentsBundle.putString("URL", imageUrlString);
55 argumentsBundle.putString("Image_Name", imageNameString);
57 // Add `argumentsBundle` to this instance of `DownloadFile`.
58 DownloadImage thisDownloadFileDialog = new DownloadImage();
59 thisDownloadFileDialog.setArguments(argumentsBundle);
60 return thisDownloadFileDialog;
64 public void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
67 // Store the strings in the local class variables.
68 imageUrl = getArguments().getString("URL");
69 imageFileName = getArguments().getString("Image_Name");
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);
77 // `downloadImageListener` is used in `onAttach()` and `onCreateDialog()`.
78 private DownloadImageListener downloadImageListener;
80 // Check to make sure tha the parent activity implements the listener.
82 public void onAttach(Context context) {
83 super.onAttach(context);
85 downloadImageListener = (DownloadImageListener) context;
86 } catch (ClassCastException exception) {
87 throw new ClassCastException(context.toString() + " must implement DownloadImageListener.");
91 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
92 @SuppressLint("InflateParams")
95 public Dialog onCreateDialog(Bundle savedInstanceState) {
96 // Get the activity's layout inflater.
97 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
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));
105 // Set an `onClick()` listener on the negative button.
106 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
108 public void onClick(DialogInterface dialog, int which) {
109 // Do nothing if `Cancel` is clicked.
113 // Set an `onClick()` listener on the positive button
114 dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
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);
123 // Create an `AlertDialog` from the `AlertDialog.Builder`.
124 final AlertDialog alertDialog = dialogBuilder.create();
126 // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`.
127 assert alertDialog.getWindow() != null;
129 // Show the keyboard when `alertDialog` is displayed on the screen.
130 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
132 // We need to show `alertDialog` before we can modify the contents.
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);
140 // Allow the `enter` key on the keyboard to save the file from `downloadImageNameTextView`.
141 downloadImageNameTextView.setOnKeyListener(new View.OnKeyListener() {
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.
152 } else { // If any other key was pressed, do not consume the event.
159 // `onCreateDialog` requires the return of an `AlertDialog`.