2 * Copyright © 2018 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.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.os.Bundle;
28 import android.view.WindowManager;
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33 public class DownloadLocationPermissionDialog extends DialogFragment {
34 public static final int DOWNLOAD_FILE = 1;
35 public static final int DOWNLOAD_IMAGE = 2;
37 // `downloadLocationPermissionDialogListener` is used in `onAttach()` and `onCreateDialog()`.
38 private DownloadLocationPermissionDialogListener downloadLocationPermissionDialogListener;
40 // The public interface is used to send information back to the parent activity.
41 public interface DownloadLocationPermissionDialogListener {
42 void onCloseDownloadLocationPermissionDialog(int downloadType);
46 public void onAttach(Context context) {
47 // Run the default commands.
48 super.onAttach(context);
50 // Get a handle for `DownloadLocationPermissionDialogListener` from the launching context.
51 downloadLocationPermissionDialogListener = (DownloadLocationPermissionDialogListener) context;
54 public static DownloadLocationPermissionDialog downloadType(int type) {
55 // Create an arguments bundle.
56 Bundle argumentsBundle = new Bundle();
58 // Store the download type in the bundle.
59 argumentsBundle.putInt("Download_Type", type);
61 // Add the arguments bundle to this instance of `DownloadLocationPermissionDialog`.
62 DownloadLocationPermissionDialog thisDownloadLocationPermissionDialog = new DownloadLocationPermissionDialog();
63 thisDownloadLocationPermissionDialog.setArguments(argumentsBundle);
64 return thisDownloadLocationPermissionDialog;
68 public Dialog onCreateDialog(Bundle savedInstanceState) {
69 // Store the download type in the local class variable.
70 int downloadType = getArguments().getInt("Download_Type");
72 // Use a builder to create the alert dialog.
73 AlertDialog.Builder dialogBuilder;
75 // Set the style and the icon according to the theme.
76 if (MainWebViewActivity.darkTheme) {
77 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
78 dialogBuilder.setIcon(R.drawable.downloads_dark);
80 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
81 dialogBuilder.setIcon(R.drawable.downloads_light);
84 // Set an `onClick` listener on the negative button. Using `null` as the listener closes the dialog without doing anything else.
85 dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
86 // Inform the parent activity that the dialog was closed.
87 downloadLocationPermissionDialogListener.onCloseDownloadLocationPermissionDialog(downloadType);
91 dialogBuilder.setTitle(R.string.download_location);
94 dialogBuilder.setMessage(R.string.download_location_message);
96 // Create an alert dialog from the alert dialog builder.
97 final AlertDialog alertDialog = dialogBuilder.create();
99 // Disable screenshots if not allowed.
100 if (!MainWebViewActivity.allowScreenshots) {
101 // Remove the warning below that `getWindow()` might be null.
102 assert alertDialog.getWindow() != null;
104 // Disable screenshots.
105 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
108 // `onCreateDialog` requires the return of an `AlertDialog`.