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