2 * Copyright © 2018-2020 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.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;
35 import androidx.annotation.NonNull;
36 import androidx.appcompat.app.AlertDialog;
37 import androidx.fragment.app.DialogFragment;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.helpers.BlocklistHelper;
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);
50 // The view request listener is used in `onAttach()` and `onCreateDialog()`.
51 private ViewRequestListener viewRequestListener;
53 public void onAttach(@NonNull Context context) {
54 // Run the default commands.
55 super.onAttach(context);
57 // Get a handle for the listener from the launching context.
58 viewRequestListener = (ViewRequestListener) context;
61 public static ViewRequestDialog request(int id, boolean isLastRequest, String[] requestDetails) {
63 Bundle bundle = new Bundle();
65 // Store the request details.
66 bundle.putInt("id", id);
67 bundle.putBoolean("is_last_request", isLastRequest);
68 bundle.putStringArray("request_details", requestDetails);
70 // Add the bundle to the dialog.
71 ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
72 viewRequestDialog.setArguments(bundle);
74 // Return the new dialog.
75 return viewRequestDialog;
80 // `@SuppressLing("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;
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");
91 // Use an alert dialog builder to create the alert dialog.
92 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
94 // Get the current theme status.
95 int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
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);
101 dialogBuilder.setIcon(R.drawable.block_ads_enabled_day);
104 // Create the dialog title.
105 String title = getResources().getString(R.string.request_details) + " - " + id;
108 dialogBuilder.setTitle(title);
110 // Remove the incorrect lint warnings about items being null.
111 assert requestDetails != null;
112 assert getActivity() != null;
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));
117 // Set the close button.
118 dialogBuilder.setNeutralButton(R.string.close, (DialogInterface dialog, int which) -> {
119 // Do nothing. The dialog will close automatically.
122 // Set the previous button.
123 dialogBuilder.setNegativeButton(R.string.previous, (DialogInterface dialog, int which) -> {
124 // Load the previous request.
125 viewRequestListener.onPrevious(id);
128 // Set the next button.
129 dialogBuilder.setPositiveButton(R.string.next, (DialogInterface dialog, int which) -> {
130 // Load the next request.
131 viewRequestListener.onNext(id);
134 // Create an alert dialog from the alert dialog builder.
135 final AlertDialog alertDialog = dialogBuilder.create();
137 // Get a handle for the shared preferences.
138 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
140 // Get the screenshot preference.
141 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
143 // Disable screenshots if not allowed.
144 if (!allowScreenshots) {
145 // Remove the warning below that `getWindow()` might be null.
146 assert alertDialog.getWindow() != null;
148 // Disable screenshots.
149 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
152 //The alert dialog must be shown before the contents can be modified.
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);
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;
181 // Disable the previous button if the first resource request is displayed.
182 previousButton.setEnabled(!(id == 1));
184 // Disable the next button if the last resource request is displayed.
185 nextButton.setEnabled(!isLastRequest);
187 // Set the request action text.
188 switch (requestDetails[BlocklistHelper.REQUEST_DISPOSITION]) {
189 case BlocklistHelper.REQUEST_DEFAULT:
191 requestDisposition.setText(R.string.default_allowed);
193 // Set the background color.
194 requestDisposition.setBackgroundColor(getResources().getColor(R.color.transparent));
197 case BlocklistHelper.REQUEST_ALLOWED:
199 requestDisposition.setText(R.string.allowed);
201 // Set the background color.
202 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
203 requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
205 requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_100));
209 case BlocklistHelper.REQUEST_THIRD_PARTY:
211 requestDisposition.setText(R.string.third_party_blocked);
213 // Set the background color.
214 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
215 requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_700_50));
217 requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_100));
221 case BlocklistHelper.REQUEST_BLOCKED:
223 requestDisposition.setText(R.string.blocked);
225 // Set the background color.
226 if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
227 requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_700_40));
229 requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_100));
234 // Display the request URL.
235 requestUrl.setText(requestDetails[BlocklistHelper.REQUEST_URL]);
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]);
254 // Set the sublist text.
255 switch (requestDetails[BlocklistHelper.REQUEST_SUBLIST]) {
256 case BlocklistHelper.MAIN_WHITELIST:
257 requestSubList.setText(R.string.main_whitelist);
260 case BlocklistHelper.FINAL_WHITELIST:
261 requestSubList.setText(R.string.final_whitelist);
264 case BlocklistHelper.DOMAIN_WHITELIST:
265 requestSubList.setText(R.string.domain_whitelist);
268 case BlocklistHelper.DOMAIN_INITIAL_WHITELIST:
269 requestSubList.setText(R.string.domain_initial_whitelist);
272 case BlocklistHelper.DOMAIN_FINAL_WHITELIST:
273 requestSubList.setText(R.string.domain_final_whitelist);
276 case BlocklistHelper.THIRD_PARTY_WHITELIST:
277 requestSubList.setText(R.string.third_party_whitelist);
280 case BlocklistHelper.THIRD_PARTY_DOMAIN_WHITELIST:
281 requestSubList.setText(R.string.third_party_domain_whitelist);
284 case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
285 requestSubList.setText(R.string.third_party_domain_initial_whitelist);
288 case BlocklistHelper.MAIN_BLACKLIST:
289 requestSubList.setText(R.string.main_blacklist);
292 case BlocklistHelper.INITIAL_BLACKLIST:
293 requestSubList.setText(R.string.initial_blacklist);
296 case BlocklistHelper.FINAL_BLACKLIST:
297 requestSubList.setText(R.string.final_blacklist);
300 case BlocklistHelper.DOMAIN_BLACKLIST:
301 requestSubList.setText(R.string.domain_blacklist);
304 case BlocklistHelper.DOMAIN_INITIAL_BLACKLIST:
305 requestSubList.setText(R.string.domain_initial_blacklist);
308 case BlocklistHelper.DOMAIN_FINAL_BLACKLIST:
309 requestSubList.setText(R.string.domain_final_blacklist);
312 case BlocklistHelper.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
313 requestSubList.setText(R.string.domain_regular_expression_blacklist);
316 case BlocklistHelper.THIRD_PARTY_BLACKLIST:
317 requestSubList.setText(R.string.third_party_blacklist);
320 case BlocklistHelper.THIRD_PARTY_INITIAL_BLACKLIST:
321 requestSubList.setText(R.string.third_party_initial_blacklist);
324 case BlocklistHelper.THIRD_PARTY_DOMAIN_BLACKLIST:
325 requestSubList.setText(R.string.third_party_domain_blacklist);
328 case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
329 requestSubList.setText(R.string.third_party_domain_initial_blacklist);
332 case BlocklistHelper.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
333 requestSubList.setText(R.string.third_party_regular_expression_blacklist);
336 case BlocklistHelper.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
337 requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
340 case BlocklistHelper.REGULAR_EXPRESSION_BLACKLIST:
341 requestSubList.setText(R.string.regular_expression_blacklist);
346 // `onCreateDialog` requires the return of an alert dialog.