]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ProxyNotInstalledDialog.java
Add support for I2P and custom proxies. https://redmine.stoutner.com/issues/355
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ProxyNotInstalledDialog.java
1 /*
2  * Copyright © 2019 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.Activity;
23 import android.app.Dialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.SharedPreferences;
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 and the activity.
61         Context context = getContext();
62         Activity activity = getActivity();
63
64         // Remove the incorrect lint warnings below that the context or the activity might be null.
65         assert context != null;
66         assert activity != null;
67
68         // Get the arguments.
69         Bundle arguments = getArguments();
70
71         // Remove the incorrect lint warning below that the arguments might be null.
72         assert arguments != null;
73
74         // Get the proxy mode from the arguments.
75         String proxyMode = arguments.getString("proxy_mode");
76
77         // Remove the incorrect lint warning below tha tth eproxy mode migth be null.
78         assert proxyMode != null;
79
80         // Get a handle for the shared preferences.
81         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
82
83         // Get the screenshot and the theme preferences.
84         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
85         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
86
87         // Use a builder to create the alert dialog.
88         AlertDialog.Builder dialogBuilder;
89
90         // Set the style and the icon according to the theme.
91         if (darkTheme) {
92             // Set the style.
93             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
94
95             // Set the icon.
96             dialogBuilder.setIcon(R.drawable.proxy_enabled_dark);
97         } else {
98             // Set the style.
99             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
100
101             // Set the icon.
102             dialogBuilder.setIcon(R.drawable.proxy_enabled_light);
103         }
104
105         // Set the title and the message according to the proxy mode.
106         switch (proxyMode) {
107             case ProxyHelper.TOR:
108                 // Set the title.
109                 dialogBuilder.setTitle(R.string.orbot_not_installed_title);
110
111                 // Set the message.
112                 dialogBuilder.setMessage(R.string.orbot_not_installed_message);
113                 break;
114
115             case ProxyHelper.I2P:
116                 // Set the title.
117                 dialogBuilder.setTitle(R.string.i2p_not_installed_title);
118
119                 // Set the message.
120                 dialogBuilder.setMessage(R.string.i2p_not_installed_message);
121                 break;
122         }
123
124         // Set the positive button.
125         dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
126             // Do nothing.  The alert dialog will close automatically.
127         });
128
129         // Create an alert dialog from the alert dialog builder.
130         AlertDialog alertDialog = dialogBuilder.create();
131
132         // Disable screenshots if not allowed.
133         if (!allowScreenshots) {
134             // Remove the warning below that `getWindows()` might be null.
135             assert alertDialog.getWindow() != null;
136
137             // Disable screenshots.
138             alertDialog.getWindow().addFlags((WindowManager.LayoutParams.FLAG_SECURE));
139         }
140
141         // Return the alert dialog.
142         return alertDialog;
143     }
144 }