2 * Copyright © 2018 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.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;
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33 public class ImportExportStoragePermissionDialog extends DialogFragment {
34 // The constants are used to differentiate between the two commands.
35 public static final int EXPORT_SETTINGS = 1;
36 public static final int IMPORT_SETTINGS = 2;
38 // The listener is used in `onAttach()` and `onCreateDialog()`.
39 private ImportExportStoragePermissionDialogListener importExportStoragePermissionDialogListener;
41 // The public interface is used to send information back to the parent activity.
42 public interface ImportExportStoragePermissionDialogListener {
43 void onCloseImportExportStoragePermissionDialog(int type);
47 public void onAttach(Context context) {
48 // Run the default commands.
49 super.onAttach(context);
51 // Get a handle for the listener from the launching context.
52 importExportStoragePermissionDialogListener = (ImportExportStoragePermissionDialogListener) context;
55 public static ImportExportStoragePermissionDialog type(int type) {
56 // Create an arguments bundle.
57 Bundle argumentsBundle = new Bundle();
59 // Store the download type in the bundle.
60 argumentsBundle.putInt("type", type);
62 // Add the arguments bundle to this instance of the dialog.
63 ImportExportStoragePermissionDialog thisImportExportStoragePermissionDialog = new ImportExportStoragePermissionDialog();
64 thisImportExportStoragePermissionDialog.setArguments(argumentsBundle);
65 return thisImportExportStoragePermissionDialog;
69 public Dialog onCreateDialog(Bundle savedInstanceState) {
70 // Store the download type in a local variable.
71 int type = getArguments().getInt("type");
73 // Use a builder to create the alert dialog.
74 AlertDialog.Builder dialogBuilder;
76 // Set the style and the icon according to the theme.
77 if (MainWebViewActivity.darkTheme) {
78 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
79 dialogBuilder.setIcon(R.drawable.import_export_dark);
81 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
82 dialogBuilder.setIcon(R.drawable.import_export_light);
86 dialogBuilder.setTitle(R.string.storage_permission);
89 dialogBuilder.setMessage(R.string.storage_permission_message);
91 // Set an `onClick` listener on the negative button.
92 dialogBuilder.setNegativeButton(R.string.ok, (DialogInterface dialog, int which) -> {
93 // Inform the parent activity that the dialog was closed.
94 importExportStoragePermissionDialogListener.onCloseImportExportStoragePermissionDialog(type);
97 // Create an alert dialog from the builder.
98 final AlertDialog alertDialog = dialogBuilder.create();
100 // Disable screenshots if not allowed.
101 if (!MainWebViewActivity.allowScreenshots) {
102 // Remove the warning below that `getWindow()` might be null.
103 assert alertDialog.getWindow() != null;
105 // Disable screenshots.
106 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
109 // `onCreateDialog()` requires the return of an `AlertDialog`.