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_AS_ARCHIVE = 2;
42 public static final int SAVE_AS_IMAGE = 3;
44 // The listener is used in `onAttach()` and `onCreateDialog()`.
45 private StoragePermissionDialogListener storagePermissionDialogListener;
47 // The public interface is used to send information back to the parent activity.
48 public interface StoragePermissionDialogListener {
49 void onCloseStoragePermissionDialog(int requestType);
53 public void onAttach(@NonNull Context context) {
54 // Run the default commands.
55 super.onAttach(context);
57 // Get a handle for the listener from the launching context.
58 storagePermissionDialogListener = (StoragePermissionDialogListener) context;
61 public static StoragePermissionDialog displayDialog(int requestType) {
62 // Create an arguments bundle.
63 Bundle argumentsBundle = new Bundle();
65 // Store the save type in the bundle.
66 argumentsBundle.putInt("request_type", requestType);
68 // Create a new instance of the storage permission dialog.
69 StoragePermissionDialog storagePermissionDialog = new StoragePermissionDialog();
71 // Add the arguments bundle to the new dialog.
72 storagePermissionDialog.setArguments(argumentsBundle);
74 // Return the new dialog.
75 return storagePermissionDialog;
80 public Dialog onCreateDialog(Bundle savedInstanceState) {
81 // Get a handle for the arguments.
82 Bundle arguments = getArguments();
84 // Remove the incorrect lint warning that the arguments might be null.
85 assert arguments != null;
88 int requestType = arguments.getInt("request_type");
90 // Use a builder to create the alert dialog.
91 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
93 // Get the current theme status.
94 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
96 // Set the icon according to the theme.
97 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
98 dialogBuilder.setIcon(R.drawable.import_export_night);
100 dialogBuilder.setIcon(R.drawable.import_export_day);
104 dialogBuilder.setTitle(R.string.storage_permission);
107 dialogBuilder.setMessage(R.string.storage_permission_message);
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);
115 // Create an alert dialog from the builder.
116 final AlertDialog alertDialog = dialogBuilder.create();
118 // Get a handle for the shared preferences.
119 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
121 // Get the screenshot preference.
122 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
124 // Disable screenshots if not allowed.
125 if (!allowScreenshots) {
126 // Remove the warning below that `getWindow()` might be null.
127 assert alertDialog.getWindow() != null;
129 // Disable screenshots.
130 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
133 // Return the alert dialog.