]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ProxyNotInstalledDialog.java
Fix a rare crash in `onBackPresses()`. https://redmine.stoutner.com/issues/651
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ProxyNotInstalledDialog.java
1 /*
2  * Copyright © 2019-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.view.WindowManager;
29
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.fragment.app.DialogFragment;
33 import androidx.preference.PreferenceManager;
34
35
36 import com.stoutner.privacybrowser.helpers.ProxyHelper;
37 import com.stoutner.privacybrowser.R;
38
39 public class ProxyNotInstalledDialog extends DialogFragment {
40     public static ProxyNotInstalledDialog displayDialog(String proxyMode) {
41         // Create an arguments bundle.
42         Bundle argumentsBundle = new Bundle();
43
44         // Store the proxy mode in the bundle.
45         argumentsBundle.putString("proxy_mode", proxyMode);
46
47         // Create a new instance of the dialog.
48         ProxyNotInstalledDialog proxyNotInstalledDialog = new ProxyNotInstalledDialog();
49
50         // Add the bundle to the dialog.
51         proxyNotInstalledDialog.setArguments(argumentsBundle);
52
53         // Return the new dialog.
54         return proxyNotInstalledDialog;
55     }
56
57     @Override
58     @NonNull
59     public Dialog onCreateDialog(Bundle savedInstanceState) {
60         // Get the context.
61         Context context = requireContext();
62
63         // Get the arguments.
64         Bundle arguments = getArguments();
65
66         // Remove the incorrect lint warning below that the arguments might be null.
67         assert arguments != null;
68
69         // Get the proxy mode from the arguments.
70         String proxyMode = arguments.getString("proxy_mode");
71
72         // Remove the incorrect lint warning below that the proxy mode might be null.
73         assert proxyMode != null;
74
75         // Use a builder to create the alert dialog.
76         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog);
77
78         // Get the current theme status.
79         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
80
81         // Set the icon according to the theme.
82         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
83             dialogBuilder.setIcon(R.drawable.proxy_enabled_night);
84         } else {
85             dialogBuilder.setIcon(R.drawable.proxy_enabled_day);
86         }
87
88         // Set the title and the message according to the proxy mode.
89         switch (proxyMode) {
90             case ProxyHelper.TOR:
91                 // Set the title.
92                 dialogBuilder.setTitle(R.string.orbot_not_installed_title);
93
94                 // Set the message.
95                 dialogBuilder.setMessage(R.string.orbot_not_installed_message);
96                 break;
97
98             case ProxyHelper.I2P:
99                 // Set the title.
100                 dialogBuilder.setTitle(R.string.i2p_not_installed_title);
101
102                 // Set the message.
103                 dialogBuilder.setMessage(R.string.i2p_not_installed_message);
104                 break;
105         }
106
107         // Set the positive button.
108         dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
109             // Do nothing.  The alert dialog will close automatically.
110         });
111
112         // Create an alert dialog from the alert dialog builder.
113         AlertDialog alertDialog = dialogBuilder.create();
114
115         // Get a handle for the shared preferences.
116         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
117
118         // Get the screenshot preference.
119         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
120
121         // Disable screenshots if not allowed.
122         if (!allowScreenshots) {
123             // Remove the warning below that `getWindows()` might be null.
124             assert alertDialog.getWindow() != null;
125
126             // Disable screenshots.
127             alertDialog.getWindow().addFlags((WindowManager.LayoutParams.FLAG_SECURE));
128         }
129
130         // Return the alert dialog.
131         return alertDialog;
132     }
133 }