]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/DownloadLocationPermissionDialog.java
Add setting to disable screenshots. https://redmine.stoutner.com/issues/266
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / DownloadLocationPermissionDialog.java
1 /*
2  * Copyright © 2018 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
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;
29
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
32
33 public class DownloadLocationPermissionDialog extends DialogFragment {
34     public static final int DOWNLOAD_FILE = 1;
35     public static final int DOWNLOAD_IMAGE = 2;
36
37     private int downloadType;
38
39     public static DownloadLocationPermissionDialog downloadType(int type) {
40         // Create an arguments bundle.
41         Bundle argumentsBundle = new Bundle();
42
43         // Store the download type in the bundle.
44         argumentsBundle.putInt("Download_Type", type);
45
46         // Add the arguments bundle to this instance of `DownloadLocationPermissionDialog`.
47         DownloadLocationPermissionDialog thisDownloadLocationPermissionDialog = new DownloadLocationPermissionDialog();
48         thisDownloadLocationPermissionDialog.setArguments(argumentsBundle);
49         return thisDownloadLocationPermissionDialog;
50     }
51
52     @Override
53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55
56         // Store the download type in the local class variable.
57         downloadType = getArguments().getInt("Download_Type");
58     }
59
60     // The public interface is used to send information back to the parent activity.
61     public interface DownloadLocationPermissionDialogListener {
62         void onCloseDownloadLocationPermissionDialog(int downloadType);
63     }
64
65     // `downloadLocationPermissionDialogListener` is used in `onAttach()` and `onCreateDialog()`.
66     private DownloadLocationPermissionDialogListener downloadLocationPermissionDialogListener;
67
68     @Override
69     public void onAttach(Context context) {
70         super.onAttach(context);
71
72         // Check to make sure the parent activity implements the listener.
73         try {
74             downloadLocationPermissionDialogListener = (DownloadLocationPermissionDialogListener) context;
75         } catch (ClassCastException exception) {
76             throw new ClassCastException(context.toString() + " must implement DownloadLocationPermissionDialogListener.");
77         }
78     }
79
80     @Override
81     public Dialog onCreateDialog(Bundle savedInstanceState) {
82         // Use a builder to create the alert dialog.
83         AlertDialog.Builder dialogBuilder;
84
85         // Set the style and the icon according to the theme.
86         if (MainWebViewActivity.darkTheme) {
87             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
88             dialogBuilder.setIcon(R.drawable.downloads_dark);
89         } else {
90             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
91             dialogBuilder.setIcon(R.drawable.downloads_light);
92         }
93
94         // Set an `onClick` listener on the negative button.  Using `null` as the listener closes the dialog without doing anything else.
95         dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
96             // Inform the parent activity that the dialog was closed.
97             downloadLocationPermissionDialogListener.onCloseDownloadLocationPermissionDialog(downloadType);
98         });
99
100         // Set the title.
101         dialogBuilder.setTitle(R.string.download_location);
102
103         // Set the text.
104         dialogBuilder.setMessage(R.string.download_location_message);
105
106         // Create an alert dialog from the alert dialog builder.
107         final AlertDialog alertDialog = dialogBuilder.create();
108
109         // Disable screenshots if not allowed.
110         if (!MainWebViewActivity.allowScreenshots) {
111             // Remove the warning below that `getWindow()` might be null.
112             assert alertDialog.getWindow() != null;
113
114             // Disable screenshots.
115             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
116         }
117
118         // `onCreateDialog` requires the return of an `AlertDialog`.
119         return alertDialog;
120     }
121 }