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.app.Activity;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 // `android.support.v7.app.AlertDialog` uses more of the horizontal screen real estate versus `android.app.AlertDialog's` smaller width.
28 import android.support.v7.app.AlertDialog;
29 import android.view.KeyEvent;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.WindowManager;
33 import android.widget.EditText;
34 import android.widget.TextView;
36 import java.util.Locale;
38 public class DownloadFile extends DialogFragment {
40 private String downloadUrl;
41 private String downloadFileName;
42 private String fileSize;
44 public static DownloadFile fromUrl(String urlString, String contentDisposition, long contentLength) {
45 // Create `argumentsBundle`.
46 Bundle argumentsBundle = new Bundle();
48 // If `contentDisposition` is empty, use Android's standard string of `downloadfile.bin`.
49 String fileNameString;
50 if (contentDisposition.isEmpty()) {
51 fileNameString = "downloadfile.bin";
53 // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending one character before the end of `contentDisposition`.
54 fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=\"") + 10, contentDisposition.length() - 1);
57 // Convert `contentLength` to MB and store it in `fileSizeString`. `%.3g` displays the three most significant digits.
58 String fileSizeString = String.format(Locale.getDefault(), "%.3g", (float) contentLength / 1048576) + " MB";
60 // Store the variables in the `Bundle`.
61 argumentsBundle.putString("URL", urlString);
62 argumentsBundle.putString("File_Name", fileNameString);
63 argumentsBundle.putString("File_Size", fileSizeString);
65 // Add `argumentsBundle` to this instance of `DownloadFile`.
66 DownloadFile thisDownloadFileDialog = new DownloadFile();
67 thisDownloadFileDialog.setArguments(argumentsBundle);
68 return thisDownloadFileDialog;
72 public void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
75 // Store the strings in the local class variables.
76 downloadUrl = getArguments().getString("URL");
77 downloadFileName = getArguments().getString("File_Name");
78 fileSize = getArguments().getString("File_Size");
81 // The public interface is used to send information back to the parent activity.
82 public interface DownloadFileListener {
83 void onDownloadFile(DialogFragment dialogFragment, String downloadUrl);
86 // `downloadFileListener` is used in `onAttach()` and `onCreateDialog()`.
87 private DownloadFileListener downloadFileListener;
89 // Check to make sure tha the parent activity implements the listener.
90 public void onAttach(Activity parentActivity) {
91 super.onAttach(parentActivity);
93 downloadFileListener = (DownloadFileListener) parentActivity;
94 } catch (ClassCastException exception) {
95 throw new ClassCastException(parentActivity.toString() + " must implement DownloadFileListener.");
100 public Dialog onCreateDialog(Bundle savedInstanceState) {
101 // Get the activity's layout inflater.
102 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
104 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.lightAlertDialog` formats the color of the button text.
105 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
106 dialogBuilder.setTitle(R.string.file_download);
107 // The parent view is `null` because it will be assigned by `AlertDialog`.
108 dialogBuilder.setView(layoutInflater.inflate(R.layout.download_file_dialog, null));
110 // Set an `onClick()` listener on the negative button.
111 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
113 public void onClick(DialogInterface dialog, int which) {
114 // Do nothing if `Cancel` is clicked.
118 // Set an `onClick()` listener on the positive button
119 dialogBuilder.setPositiveButton(R.string.download, new DialogInterface.OnClickListener() {
121 public void onClick(DialogInterface dialog, int which) {
122 // trigger `onDownloadFile()` and return the `DialogFragment` and the download URL to the parent activity.
123 downloadFileListener.onDownloadFile(DownloadFile.this, downloadUrl);
128 // Create an `AlertDialog` from the `AlertDialog.Builder`.
129 final AlertDialog alertDialog = dialogBuilder.create();
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 `downloadFileSizeTextView`.
138 TextView downloadFileSizeTextView = (TextView) alertDialog.findViewById(R.id.download_file_size);
139 assert downloadFileSizeTextView != null; // Remove the warning of the following line that `downloadFileSizeTextView` might be null.
140 downloadFileSizeTextView.setText(fileSize);
142 // Set the text for `downloadFileNameTextView`.
143 EditText downloadFileNameTextView = (EditText) alertDialog.findViewById(R.id.download_file_name);
144 assert downloadFileNameTextView != null; // Remove the warning on the following line that `downloadFileNameTextView` might be null.
145 downloadFileNameTextView.setText(downloadFileName);
147 // Allow the `enter` key on the keyboard to save the file from `downloadFileNameTextView`.
148 downloadFileNameTextView.setOnKeyListener(new View.OnKeyListener() {
150 public boolean onKey (View v, int keyCode, KeyEvent event) {
151 // If the event is an `ACTION_DOWN` on the `enter` key, initiate the download.
152 if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
153 // trigger `onDownloadFile()` and return the `DialogFragment` and the URL to the parent activity.
154 downloadFileListener.onDownloadFile(DownloadFile.this, downloadUrl);
155 // Manually dismiss `alertDialog`.
156 alertDialog.dismiss();
157 // Consume the event.
159 } else { // If any other key was pressed, do not consume the event.
166 // `onCreateDialog` requires the return of an `AlertDialog`.