2 * Copyright © 2018-2020 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.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;
31 import androidx.annotation.NonNull;
32 import androidx.appcompat.app.AlertDialog;
33 import androidx.fragment.app.DialogFragment;
35 import com.stoutner.privacybrowser.R;
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;
45 // The listener is used in `onAttach()` and `onCreateDialog()`.
46 private StoragePermissionDialogListener storagePermissionDialogListener;
48 // The public interface is used to send information back to the parent activity.
49 public interface StoragePermissionDialogListener {
50 void onCloseStoragePermissionDialog(int requestType);
54 public void onAttach(@NonNull Context context) {
55 // Run the default commands.
56 super.onAttach(context);
58 // Get a handle for the listener from the launching context.
59 storagePermissionDialogListener = (StoragePermissionDialogListener) context;
62 public static StoragePermissionDialog displayDialog(int requestType) {
63 // Create an arguments bundle.
64 Bundle argumentsBundle = new Bundle();
66 // Store the save type in the bundle.
67 argumentsBundle.putInt("request_type", requestType);
69 // Create a new instance of the storage permission dialog.
70 StoragePermissionDialog storagePermissionDialog = new StoragePermissionDialog();
72 // Add the arguments bundle to the new dialog.
73 storagePermissionDialog.setArguments(argumentsBundle);
75 // Return the new dialog.
76 return storagePermissionDialog;
81 public Dialog onCreateDialog(Bundle savedInstanceState) {
82 // Get a handle for the arguments.
83 Bundle arguments = getArguments();
85 // Remove the incorrect lint warning that the arguments might be null.
86 assert arguments != null;
89 int requestType = arguments.getInt("request_type");
91 // Use a builder to create the alert dialog.
92 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
94 // Get the current theme status.
95 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
97 // Set the icon according to the theme.
98 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
99 dialogBuilder.setIcon(R.drawable.import_export_night);
101 dialogBuilder.setIcon(R.drawable.import_export_day);
105 dialogBuilder.setTitle(R.string.storage_permission);
108 dialogBuilder.setMessage(R.string.storage_permission_message);
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);
116 // Create an alert dialog from the builder.
117 final AlertDialog alertDialog = dialogBuilder.create();
119 // Get a handle for the shared preferences.
120 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
122 // Get the screenshot preference.
123 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
125 // Disable screenshots if not allowed.
126 if (!allowScreenshots) {
127 // Remove the warning below that `getWindow()` might be null.
128 assert alertDialog.getWindow() != null;
130 // Disable screenshots.
131 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
134 // Return the alert dialog.