]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewRequestDialog.java
Release 3.7.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewRequestDialog.java
1 /*
2  * Copyright © 2018-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.annotation.SuppressLint;
23 import android.app.Dialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.SharedPreferences;
27 import android.content.res.Configuration;
28 import android.os.Bundle;
29 import android.preference.PreferenceManager;
30 import android.view.View;
31 import android.view.WindowManager;
32 import android.widget.Button;
33 import android.widget.TextView;
34
35 import androidx.annotation.NonNull;
36 import androidx.appcompat.app.AlertDialog;
37 import androidx.fragment.app.DialogFragment;
38
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
41
42 public class ViewRequestDialog extends DialogFragment {
43     // The public interface is used to send information back to the parent activity.
44     public interface ViewRequestListener {
45         void onPrevious(int id);
46
47         void onNext(int id);
48     }
49
50     // The view request listener is used in `onAttach()` and `onCreateDialog()`.
51     private ViewRequestListener viewRequestListener;
52
53     public void onAttach(@NonNull Context context) {
54         // Run the default commands.
55         super.onAttach(context);
56
57         // Get a handle for the listener from the launching context.
58         viewRequestListener = (ViewRequestListener) context;
59     }
60
61     public static ViewRequestDialog request(int id, boolean isLastRequest, String[] requestDetails) {
62         // Create a bundle.
63         Bundle bundle = new Bundle();
64
65         // Store the request details.
66         bundle.putInt("id", id);
67         bundle.putBoolean("is_last_request", isLastRequest);
68         bundle.putStringArray("request_details", requestDetails);
69
70         // Add the bundle to the dialog.
71         ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
72         viewRequestDialog.setArguments(bundle);
73
74         // Return the new dialog.
75         return viewRequestDialog;
76     }
77
78     @Override
79     @NonNull
80     // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
81     @SuppressLint("InflateParams")
82     public Dialog onCreateDialog(Bundle savedInstanceState) {
83         // Remove the incorrect lint warning that `getInt()` might be null.
84         assert getArguments() != null;
85
86         // Get the info from the bundle.
87         int id = getArguments().getInt("id");
88         boolean isLastRequest = getArguments().getBoolean("is_last_request");
89         String[] requestDetails = getArguments().getStringArray("request_details");
90
91         // Use an alert dialog builder to create the alert dialog.
92         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
93
94         // Get the current theme status.
95         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
96
97         // Set the icon according to the theme.
98         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
99             dialogBuilder.setIcon(R.drawable.block_ads_enabled_night);
100         } else {
101             dialogBuilder.setIcon(R.drawable.block_ads_enabled_day);
102         }
103
104         // Create the dialog title.
105         String title = getResources().getString(R.string.request_details) + " - " + id;
106
107         // Set the title.
108         dialogBuilder.setTitle(title);
109
110         // Remove the incorrect lint warnings about items being null.
111         assert requestDetails != null;
112         assert getActivity() != null;
113
114         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
115         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.view_request_dialog, null));
116
117         // Set the close button.
118         dialogBuilder.setNeutralButton(R.string.close, (DialogInterface dialog, int which) -> {
119             // Do nothing.  The dialog will close automatically.
120         });
121
122         // Set the previous button.
123         dialogBuilder.setNegativeButton(R.string.previous, (DialogInterface dialog, int which) -> {
124             // Load the previous request.
125             viewRequestListener.onPrevious(id);
126         });
127
128         // Set the next button.
129         dialogBuilder.setPositiveButton(R.string.next, (DialogInterface dialog, int which) -> {
130             // Load the next request.
131             viewRequestListener.onNext(id);
132         });
133
134         // Create an alert dialog from the alert dialog builder.
135         final AlertDialog alertDialog = dialogBuilder.create();
136
137         // Get a handle for the shared preferences.
138         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
139
140         // Get the screenshot preference.
141         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
142
143         // Disable screenshots if not allowed.
144         if (!allowScreenshots) {
145             // Remove the warning below that `getWindow()` might be null.
146             assert alertDialog.getWindow() != null;
147
148             // Disable screenshots.
149             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
150         }
151
152         //The alert dialog must be shown before the contents can be modified.
153         alertDialog.show();
154
155         // Get handles for the dialog views.
156         TextView requestDisposition = alertDialog.findViewById(R.id.request_disposition);
157         TextView requestUrl = alertDialog.findViewById(R.id.request_url);
158         TextView requestBlockListLabel = alertDialog.findViewById(R.id.request_blocklist_label);
159         TextView requestBlockList = alertDialog.findViewById(R.id.request_blocklist);
160         TextView requestSubListLabel = alertDialog.findViewById(R.id.request_sublist_label);
161         TextView requestSubList = alertDialog.findViewById(R.id.request_sublist);
162         TextView requestBlockListEntriesLabel = alertDialog.findViewById(R.id.request_blocklist_entries_label);
163         TextView requestBlockListEntries = alertDialog.findViewById(R.id.request_blocklist_entries);
164         TextView requestBlockListOriginalEntryLabel = alertDialog.findViewById(R.id.request_blocklist_original_entry_label);
165         TextView requestBlockListOriginalEntry = alertDialog.findViewById(R.id.request_blocklist_original_entry);
166         Button previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
167         Button nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
168
169         // Remove the incorrect lint warnings below that the views might be null.
170         assert requestDisposition != null;
171         assert requestUrl != null;
172         assert requestBlockListLabel != null;
173         assert requestBlockList != null;
174         assert requestSubListLabel != null;
175         assert requestSubList != null;
176         assert requestBlockListEntriesLabel != null;
177         assert requestBlockListEntries != null;
178         assert requestBlockListOriginalEntryLabel != null;
179         assert requestBlockListOriginalEntry != null;
180
181         // Disable the previous button if the first resource request is displayed.
182         previousButton.setEnabled(!(id == 1));
183
184         // Disable the next button if the last resource request is displayed.
185         nextButton.setEnabled(!isLastRequest);
186
187         // Set the request action text.
188         switch (requestDetails[BlocklistHelper.REQUEST_DISPOSITION]) {
189             case BlocklistHelper.REQUEST_DEFAULT:
190                 // Set the text.
191                 requestDisposition.setText(R.string.default_allowed);
192
193                 // Set the background color.
194                 requestDisposition.setBackgroundColor(getResources().getColor(R.color.transparent));
195                 break;
196
197             case BlocklistHelper.REQUEST_ALLOWED:
198                 // Set the text.
199                 requestDisposition.setText(R.string.allowed);
200
201                 // Set the background color.
202                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
203                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
204                 } else {
205                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_100));
206                 }
207                 break;
208
209             case BlocklistHelper.REQUEST_THIRD_PARTY:
210                 // Set the text.
211                 requestDisposition.setText(R.string.third_party_blocked);
212
213                 // Set the background color.
214                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
215                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_700_50));
216                 } else {
217                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_100));
218                 }
219                 break;
220
221             case BlocklistHelper.REQUEST_BLOCKED:
222                 // Set the text.
223                 requestDisposition.setText(R.string.blocked);
224
225                 // Set the background color.
226                 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
227                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_700_40));
228                 } else {
229                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_100));
230                 }
231                 break;
232         }
233
234         // Display the request URL.
235         requestUrl.setText(requestDetails[BlocklistHelper.REQUEST_URL]);
236
237         // Modify the dialog based on the request action.
238         if (requestDetails.length == 2) {  // A default request.
239             // Hide the unused views.
240             requestBlockListLabel.setVisibility(View.GONE);
241             requestBlockList.setVisibility(View.GONE);
242             requestSubListLabel.setVisibility(View.GONE);
243             requestSubList.setVisibility(View.GONE);
244             requestBlockListEntriesLabel.setVisibility(View.GONE);
245             requestBlockListEntries.setVisibility(View.GONE);
246             requestBlockListOriginalEntryLabel.setVisibility(View.GONE);
247             requestBlockListOriginalEntry.setVisibility(View.GONE);
248         } else {  // A blocked or allowed request.
249             // Set the text on the text views.
250             requestBlockList.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST]);
251             requestBlockListEntries.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ENTRIES]);
252             requestBlockListOriginalEntry.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]);
253
254             // Set the sublist text.
255             switch (requestDetails[BlocklistHelper.REQUEST_SUBLIST]) {
256                 case BlocklistHelper.MAIN_WHITELIST:
257                     requestSubList.setText(R.string.main_whitelist);
258                     break;
259
260                 case BlocklistHelper.FINAL_WHITELIST:
261                     requestSubList.setText(R.string.final_whitelist);
262                     break;
263
264                 case BlocklistHelper.DOMAIN_WHITELIST:
265                     requestSubList.setText(R.string.domain_whitelist);
266                     break;
267
268                 case BlocklistHelper.DOMAIN_INITIAL_WHITELIST:
269                     requestSubList.setText(R.string.domain_initial_whitelist);
270                     break;
271
272                 case BlocklistHelper.DOMAIN_FINAL_WHITELIST:
273                     requestSubList.setText(R.string.domain_final_whitelist);
274                     break;
275
276                 case BlocklistHelper.THIRD_PARTY_WHITELIST:
277                     requestSubList.setText(R.string.third_party_whitelist);
278                     break;
279
280                 case BlocklistHelper.THIRD_PARTY_DOMAIN_WHITELIST:
281                     requestSubList.setText(R.string.third_party_domain_whitelist);
282                     break;
283
284                 case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
285                     requestSubList.setText(R.string.third_party_domain_initial_whitelist);
286                     break;
287
288                 case BlocklistHelper.MAIN_BLACKLIST:
289                     requestSubList.setText(R.string.main_blacklist);
290                     break;
291
292                 case BlocklistHelper.INITIAL_BLACKLIST:
293                     requestSubList.setText(R.string.initial_blacklist);
294                     break;
295
296                 case BlocklistHelper.FINAL_BLACKLIST:
297                     requestSubList.setText(R.string.final_blacklist);
298                     break;
299
300                 case BlocklistHelper.DOMAIN_BLACKLIST:
301                     requestSubList.setText(R.string.domain_blacklist);
302                     break;
303
304                 case BlocklistHelper.DOMAIN_INITIAL_BLACKLIST:
305                     requestSubList.setText(R.string.domain_initial_blacklist);
306                     break;
307
308                 case BlocklistHelper.DOMAIN_FINAL_BLACKLIST:
309                     requestSubList.setText(R.string.domain_final_blacklist);
310                     break;
311
312                 case BlocklistHelper.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
313                     requestSubList.setText(R.string.domain_regular_expression_blacklist);
314                     break;
315
316                 case BlocklistHelper.THIRD_PARTY_BLACKLIST:
317                     requestSubList.setText(R.string.third_party_blacklist);
318                     break;
319
320                 case BlocklistHelper.THIRD_PARTY_INITIAL_BLACKLIST:
321                     requestSubList.setText(R.string.third_party_initial_blacklist);
322                     break;
323
324                 case BlocklistHelper.THIRD_PARTY_DOMAIN_BLACKLIST:
325                     requestSubList.setText(R.string.third_party_domain_blacklist);
326                     break;
327
328                 case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
329                     requestSubList.setText(R.string.third_party_domain_initial_blacklist);
330                     break;
331
332                 case BlocklistHelper.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
333                     requestSubList.setText(R.string.third_party_regular_expression_blacklist);
334                     break;
335
336                 case BlocklistHelper.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
337                     requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
338                     break;
339
340                 case BlocklistHelper.REGULAR_EXPRESSION_BLACKLIST:
341                     requestSubList.setText(R.string.regular_expression_blacklist);
342                     break;
343             }
344         }
345
346         // `onCreateDialog` requires the return of an alert dialog.
347         return alertDialog;
348     }
349 }