]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ImportExportStoragePermissionDialog.java
d5b93900fb2cc796c463309457665963826aef42
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ImportExportStoragePermissionDialog.java
1 /*
2  * Copyright © 2018 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.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;
29
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
32
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;
37
38     // The listener is used in `onAttach()` and `onCreateDialog()`.
39     private ImportExportStoragePermissionDialogListener importExportStoragePermissionDialogListener;
40
41     // The public interface is used to send information back to the parent activity.
42     public interface ImportExportStoragePermissionDialogListener {
43         void onCloseImportExportStoragePermissionDialog(int type);
44     }
45
46     @Override
47     public void onAttach(Context context) {
48         // Run the default commands.
49         super.onAttach(context);
50
51         // Get a handle for the listener from the launching context.
52         importExportStoragePermissionDialogListener = (ImportExportStoragePermissionDialogListener) context;
53     }
54
55     public static ImportExportStoragePermissionDialog type(int type) {
56         // Create an arguments bundle.
57         Bundle argumentsBundle = new Bundle();
58
59         // Store the download type in the bundle.
60         argumentsBundle.putInt("type", type);
61
62         // Add the arguments bundle to this instance of the dialog.
63         ImportExportStoragePermissionDialog thisImportExportStoragePermissionDialog = new ImportExportStoragePermissionDialog();
64         thisImportExportStoragePermissionDialog.setArguments(argumentsBundle);
65         return thisImportExportStoragePermissionDialog;
66     }
67
68     @Override
69     public Dialog onCreateDialog(Bundle savedInstanceState) {
70         // Store the download type in a local variable.
71         int type = getArguments().getInt("type");
72
73         // Use a builder to create the alert dialog.
74         AlertDialog.Builder dialogBuilder;
75
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);
80         } else {
81             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
82             dialogBuilder.setIcon(R.drawable.import_export_light);
83         }
84
85         // Set the title.
86         dialogBuilder.setTitle(R.string.storage_permission);
87
88         // Set the text.
89         dialogBuilder.setMessage(R.string.storage_permission_message);
90
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);
95         });
96
97         // Create an alert dialog from the builder.
98         final AlertDialog alertDialog = dialogBuilder.create();
99
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;
104
105             // Disable screenshots.
106             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
107         }
108
109         // `onCreateDialog()` requires the return of an `AlertDialog`.
110         return alertDialog;
111     }
112 }