]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/StoragePermissionDialog.java
472a518481e20fcfffa0e3a8481de932ba27dc11
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / StoragePermissionDialog.java
1 /*
2  * Copyright © 2018-2020 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.Dialog;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.SharedPreferences;
26 import android.content.res.Configuration;
27 import android.os.Bundle;
28 import android.preference.PreferenceManager;
29 import android.view.WindowManager;
30
31 import androidx.annotation.NonNull;
32 import androidx.appcompat.app.AlertDialog;
33 import androidx.fragment.app.DialogFragment;
34
35 import com.stoutner.privacybrowser.R;
36
37 public class StoragePermissionDialog extends DialogFragment {
38     // Define the save type constants.
39     public static final int OPEN = 0;
40     public static final int SAVE_URL = 1;
41     public static final int SAVE_AS_ARCHIVE = 2;
42     public static final int SAVE_AS_IMAGE = 3;
43
44     // The listener is used in `onAttach()` and `onCreateDialog()`.
45     private StoragePermissionDialogListener storagePermissionDialogListener;
46
47     // The public interface is used to send information back to the parent activity.
48     public interface StoragePermissionDialogListener {
49         void onCloseStoragePermissionDialog(int requestType);
50     }
51
52     @Override
53     public void onAttach(@NonNull Context context) {
54         // Run the default commands.
55         super.onAttach(context);
56
57         // Get a handle for the listener from the launching context.
58         storagePermissionDialogListener = (StoragePermissionDialogListener) context;
59     }
60
61     public static StoragePermissionDialog displayDialog(int requestType) {
62         // Create an arguments bundle.
63         Bundle argumentsBundle = new Bundle();
64
65         // Store the save type in the bundle.
66         argumentsBundle.putInt("request_type", requestType);
67
68         // Create a new instance of the storage permission dialog.
69         StoragePermissionDialog storagePermissionDialog = new StoragePermissionDialog();
70
71         // Add the arguments bundle to the new dialog.
72         storagePermissionDialog.setArguments(argumentsBundle);
73
74         // Return the new dialog.
75         return storagePermissionDialog;
76     }
77
78     @NonNull
79     @Override
80     public Dialog onCreateDialog(Bundle savedInstanceState) {
81         // Get a handle for the arguments.
82         Bundle arguments = getArguments();
83
84         // Remove the incorrect lint warning that the arguments might be null.
85         assert arguments != null;
86
87         // Get the save type.
88         int requestType = arguments.getInt("request_type");
89
90         // Use a builder to create the alert dialog.
91         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
92
93         // Get the current theme status.
94         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
95
96         // Set the icon according to the theme.
97         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
98             dialogBuilder.setIcon(R.drawable.import_export_night);
99         } else {
100             dialogBuilder.setIcon(R.drawable.import_export_day);
101         }
102
103         // Set the title.
104         dialogBuilder.setTitle(R.string.storage_permission);
105
106         // Set the text.
107         dialogBuilder.setMessage(R.string.storage_permission_message);
108
109         // Set an listener on the OK button.
110         dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
111             // Inform the parent activity that the dialog was closed.
112             storagePermissionDialogListener.onCloseStoragePermissionDialog(requestType);
113         });
114
115         // Create an alert dialog from the builder.
116         final AlertDialog alertDialog = dialogBuilder.create();
117
118         // Get a handle for the shared preferences.
119         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
120
121         // Get the screenshot preference.
122         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
123
124         // Disable screenshots if not allowed.
125         if (!allowScreenshots) {
126             // Remove the warning below that `getWindow()` might be null.
127             assert alertDialog.getWindow() != null;
128
129             // Disable screenshots.
130             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
131         }
132
133         // Return the alert dialog.
134         return alertDialog;
135     }
136 }