2 * Copyright © 2018-2019 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.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.os.Bundle;
28 import android.view.View;
29 import android.view.WindowManager;
30 import android.widget.Button;
31 import android.widget.TextView;
33 import androidx.annotation.NonNull;
34 import androidx.fragment.app.DialogFragment;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39 public class ViewRequestDialog extends DialogFragment {
40 // The public interface is used to send information back to the parent activity.
41 public interface ViewRequestListener {
42 void onPrevious(int id);
47 // `viewRequestListener` is used in `onAttach()` and `onCreateDialog()`.
48 private ViewRequestListener viewRequestListener;
50 public void onAttach(Context context) {
51 // Run the default commands.
52 super.onAttach(context);
54 // Get a handle for the listener from the launching context.
55 viewRequestListener = (ViewRequestListener) context;
58 public static ViewRequestDialog request(int id, boolean isLastRequest, String[] requestDetails) {
60 Bundle bundle = new Bundle();
62 // Store the request details.
63 bundle.putInt("ID", id);
64 bundle.putBoolean("Is Last Request", isLastRequest);
65 bundle.putStringArray("Request Details", requestDetails);
67 // Add the bundle to the dialog.
68 ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
69 viewRequestDialog.setArguments(bundle);
71 // Return the new dialog.
72 return viewRequestDialog;
77 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
78 @SuppressLint("InflateParams")
79 public Dialog onCreateDialog(Bundle savedInstanceState) {
80 // Remove the incorrect lint warning that `getInt()` might be null.
81 assert getArguments() != null;
83 // Get the info from the bundle.
84 int id = getArguments().getInt("ID");
85 boolean isLastRequest = getArguments().getBoolean("Is Last Request");
86 String[] requestDetails = getArguments().getStringArray("Request Details");
88 // Use an alert dialog builder to create the alert dialog.
89 AlertDialog.Builder dialogBuilder;
91 // Set the style and icon according to the theme.
92 if (MainWebViewActivity.darkTheme) {
93 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
94 dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
96 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
97 dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
100 // Create the dialog title.
101 String title = getResources().getString(R.string.request_details) + " - " + id;
104 dialogBuilder.setTitle(title);
106 // Remove the incorrect lint warnings about items being null.
107 assert requestDetails != null;
108 assert getActivity() != null;
110 // Set the view. The parent view is null because it will be assigned by the alert dialog.
111 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.view_request_dialog, null));
113 // Set the close button.
114 dialogBuilder.setNeutralButton(R.string.close, (DialogInterface dialog, int which) -> {
115 // Do nothing. The dialog will close automatically.
118 // Set the previous button.
119 dialogBuilder.setNegativeButton(R.string.previous, (DialogInterface dialog, int which) -> {
120 // Load the previous request.
121 viewRequestListener.onPrevious(id);
124 // Set the next button.
125 dialogBuilder.setPositiveButton(R.string.next, (DialogInterface dialog, int which) -> {
126 // Load the next request.
127 viewRequestListener.onNext(id);
130 // Create an alert dialog from the alert dialog builder.
131 final AlertDialog alertDialog = dialogBuilder.create();
133 // Disable screenshots if not allowed.
134 if (!MainWebViewActivity.allowScreenshots) {
135 // Remove the warning below that `getWindow()` might be null.
136 assert alertDialog.getWindow() != null;
138 // Disable screenshots.
139 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
142 //The alert dialog must be shown before the contents can be modified.
145 // Get handles for the dialog views.
146 TextView requestDisposition = alertDialog.findViewById(R.id.request_disposition);
147 TextView requestUrl = alertDialog.findViewById(R.id.request_url);
148 TextView requestBlockListLabel = alertDialog.findViewById(R.id.request_blocklist_label);
149 TextView requestBlockList = alertDialog.findViewById(R.id.request_blocklist);
150 TextView requestSubListLabel = alertDialog.findViewById(R.id.request_sublist_label);
151 TextView requestSubList = alertDialog.findViewById(R.id.request_sublist);
152 TextView requestBlockListEntriesLabel = alertDialog.findViewById(R.id.request_blocklist_entries_label);
153 TextView requestBlockListEntries = alertDialog.findViewById(R.id.request_blocklist_entries);
154 TextView requestBlockListOriginalEntryLabel = alertDialog.findViewById(R.id.request_blocklist_original_entry_label);
155 TextView requestBlockListOriginalEntry = alertDialog.findViewById(R.id.request_blocklist_original_entry);
156 Button previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
157 Button nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
159 // Disable the previous button if the first resource request is displayed.
160 previousButton.setEnabled(!(id == 1));
162 // Disable the next button if the last resource request is displayed.
163 nextButton.setEnabled(!isLastRequest);
165 // Set the request action text.
166 switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_DISPOSITION])) {
167 case MainWebViewActivity.REQUEST_DEFAULT:
169 requestDisposition.setText(R.string.default_allowed);
171 // Set the background color.
172 requestDisposition.setBackgroundColor(getResources().getColor(R.color.transparent));
175 case MainWebViewActivity.REQUEST_ALLOWED:
177 requestDisposition.setText(R.string.allowed);
179 // Set the background color.
180 if (MainWebViewActivity.darkTheme) {
181 requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
183 requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_100));
187 case MainWebViewActivity.REQUEST_THIRD_PARTY:
189 requestDisposition.setText(R.string.third_party_blocked);
191 // Set the background color.
192 if (MainWebViewActivity.darkTheme) {
193 requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_700_50));
195 requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_100));
199 case MainWebViewActivity.REQUEST_BLOCKED:
201 requestDisposition.setText(R.string.blocked);
203 // Set the background color.
204 if (MainWebViewActivity.darkTheme) {
205 requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_700_40));
207 requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_100));
212 // Display the request URL.
213 requestUrl.setText(requestDetails[MainWebViewActivity.REQUEST_URL]);
215 // Modify the dialog based on the request action.
216 if (requestDetails.length == 2) { // A default request.
217 // Hide the unused views.
218 requestBlockListLabel.setVisibility(View.GONE);
219 requestBlockList.setVisibility(View.GONE);
220 requestSubListLabel.setVisibility(View.GONE);
221 requestSubList.setVisibility(View.GONE);
222 requestBlockListEntriesLabel.setVisibility(View.GONE);
223 requestBlockListEntries.setVisibility(View.GONE);
224 requestBlockListOriginalEntryLabel.setVisibility(View.GONE);
225 requestBlockListOriginalEntry.setVisibility(View.GONE);
226 } else { // A blocked or allowed request.
227 // Set the text on the text views.
228 requestBlockList.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST]);
229 requestBlockListEntries.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ENTRIES]);
230 requestBlockListOriginalEntry.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]);
232 // Set the sublist text.
233 switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_SUBLIST])) {
234 case MainWebViewActivity.MAIN_WHITELIST:
235 requestSubList.setText(R.string.main_whitelist);
238 case MainWebViewActivity.FINAL_WHITELIST:
239 requestSubList.setText(R.string.final_whitelist);
242 case MainWebViewActivity.DOMAIN_WHITELIST:
243 requestSubList.setText(R.string.domain_whitelist);
246 case MainWebViewActivity.DOMAIN_INITIAL_WHITELIST:
247 requestSubList.setText(R.string.domain_initial_whitelist);
250 case MainWebViewActivity.DOMAIN_FINAL_WHITELIST:
251 requestSubList.setText(R.string.domain_final_whitelist);
254 case MainWebViewActivity.THIRD_PARTY_WHITELIST:
255 requestSubList.setText(R.string.third_party_whitelist);
258 case MainWebViewActivity.THIRD_PARTY_DOMAIN_WHITELIST:
259 requestSubList.setText(R.string.third_party_domain_whitelist);
262 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
263 requestSubList.setText(R.string.third_party_domain_initial_whitelist);
266 case MainWebViewActivity.MAIN_BLACKLIST:
267 requestSubList.setText(R.string.main_blacklist);
270 case MainWebViewActivity.INITIAL_BLACKLIST:
271 requestSubList.setText(R.string.initial_blacklist);
274 case MainWebViewActivity.FINAL_BLACKLIST:
275 requestSubList.setText(R.string.final_blacklist);
278 case MainWebViewActivity.DOMAIN_BLACKLIST:
279 requestSubList.setText(R.string.domain_blacklist);
282 case MainWebViewActivity.DOMAIN_INITIAL_BLACKLIST:
283 requestSubList.setText(R.string.domain_initial_blacklist);
286 case MainWebViewActivity.DOMAIN_FINAL_BLACKLIST:
287 requestSubList.setText(R.string.domain_final_blacklist);
290 case MainWebViewActivity.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
291 requestSubList.setText(R.string.domain_regular_expression_blacklist);
294 case MainWebViewActivity.THIRD_PARTY_BLACKLIST:
295 requestSubList.setText(R.string.third_party_blacklist);
298 case MainWebViewActivity.THIRD_PARTY_DOMAIN_BLACKLIST:
299 requestSubList.setText(R.string.third_party_domain_blacklist);
302 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
303 requestSubList.setText(R.string.third_party_domain_initial_blacklist);
306 case MainWebViewActivity.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
307 requestSubList.setText(R.string.third_party_regular_expression_blacklist);
310 case MainWebViewActivity.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
311 requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
314 case MainWebViewActivity.REGULAR_EXPRESSION_BLACKLIST:
315 requestSubList.setText(R.string.regular_expression_blacklist);
320 // `onCreateDialog` requires the return of an alert dialog.