2 * Copyright © 2018 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.support.annotation.NonNull;
29 import android.support.v7.app.AppCompatDialogFragment;
30 import android.view.View;
31 import android.view.WindowManager;
32 import android.widget.Button;
33 import android.widget.TextView;
35 import com.stoutner.privacybrowser.R;
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
38 public class ViewRequestDialog extends AppCompatDialogFragment {
39 // The public interface is used to send information back to the parent activity.
40 public interface ViewRequestListener {
41 void onPrevious(int id);
46 // `viewRequestListener` is used in `onAttach()` and `onCreateDialog()`.
47 private ViewRequestListener viewRequestListener;
49 public void onAttach(Context context) {
50 // Run the default commands.
51 super.onAttach(context);
53 // Get a handle for the listener from the launching context.
54 viewRequestListener = (ViewRequestListener) context;
57 public static ViewRequestDialog request(int id, boolean isLastRequest, String[] requestDetails) {
59 Bundle bundle = new Bundle();
61 // Store the request details.
62 bundle.putInt("ID", id);
63 bundle.putBoolean("Is Last Request", isLastRequest);
64 bundle.putStringArray("Request Details", requestDetails);
66 // Add the bundle to the dialog.
67 ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
68 viewRequestDialog.setArguments(bundle);
70 // Return the new dialog.
71 return viewRequestDialog;
76 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
77 @SuppressLint("InflateParams")
78 public Dialog onCreateDialog(Bundle savedInstanceState) {
79 // Remove the incorrect lint warning that `getInt()` might be null.
80 assert getArguments() != null;
82 // Get the info from the bundle.
83 int id = getArguments().getInt("ID");
84 boolean isLastRequest = getArguments().getBoolean("Is Last Request");
85 String[] requestDetails = getArguments().getStringArray("Request Details");
87 // Use an alert dialog builder to create the alert dialog.
88 AlertDialog.Builder dialogBuilder;
90 // Set the style and icon according to the theme.
91 if (MainWebViewActivity.darkTheme) {
92 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
93 dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
95 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
96 dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
99 // Create the dialog title.
100 String title = getResources().getString(R.string.request_details) + " - " + id;
103 dialogBuilder.setTitle(title);
105 // Remove the incorrect lint warnings about items being null.
106 assert requestDetails != null;
107 assert getActivity() != null;
109 // Set the view. The parent view is null because it will be assigned by the alert dialog.
110 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.view_request_dialog, null));
112 // Set the close button.
113 dialogBuilder.setNeutralButton(R.string.close, (DialogInterface dialog, int which) -> {
114 // Do nothing. The dialog will close automatically.
117 // Set the previous button.
118 dialogBuilder.setNegativeButton(R.string.previous, (DialogInterface dialog, int which) -> {
119 // Load the previous request.
120 viewRequestListener.onPrevious(id);
123 // Set the next button.
124 dialogBuilder.setPositiveButton(R.string.next, (DialogInterface dialog, int which) -> {
125 // Load the next request.
126 viewRequestListener.onNext(id);
129 // Create an alert dialog from the alert dialog builder.
130 final AlertDialog alertDialog = dialogBuilder.create();
132 // Disable screenshots if not allowed.
133 if (!MainWebViewActivity.allowScreenshots) {
134 // Remove the warning below that `getWindow()` might be null.
135 assert alertDialog.getWindow() != null;
137 // Disable screenshots.
138 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
141 //The alert dialog must be shown before the contents can be modified.
144 // Get handles for the dialog views.
145 TextView requestAction = alertDialog.findViewById(R.id.request_action);
146 TextView requestUrl = alertDialog.findViewById(R.id.request_url);
147 TextView requestBlockListLabel = alertDialog.findViewById(R.id.request_blocklist_label);
148 TextView requestBlockList = alertDialog.findViewById(R.id.request_blocklist);
149 TextView requestSubListLabel = alertDialog.findViewById(R.id.request_sublist_label);
150 TextView requestSubList = alertDialog.findViewById(R.id.request_sublist);
151 TextView requestBlockListEntriesLabel = alertDialog.findViewById(R.id.request_blocklist_entries_label);
152 TextView requestBlockListEntries = alertDialog.findViewById(R.id.request_blocklist_entries);
153 TextView requestBlockListOriginalEntryLabel = alertDialog.findViewById(R.id.request_blocklist_original_entry_label);
154 TextView requestBlockListOriginalEntry = alertDialog.findViewById(R.id.request_blocklist_original_entry);
155 Button previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
156 Button nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
158 // Disable the previous button if the first resource request is displayed.
159 previousButton.setEnabled(!(id == 1));
161 // Disable the next button if the last resource request is displayed.
162 nextButton.setEnabled(!isLastRequest);
164 // Set the request action text.
165 switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_DISPOSITION])) {
166 case MainWebViewActivity.REQUEST_DEFAULT:
168 requestAction.setText(R.string.default_allowed);
170 // Set the background color.
171 requestAction.setBackgroundColor(getResources().getColor(R.color.transparent));
174 case MainWebViewActivity.REQUEST_ALLOWED:
176 requestAction.setText(R.string.allowed);
178 // Set the background color.
179 if (MainWebViewActivity.darkTheme) {
180 requestAction.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
182 requestAction.setBackgroundColor(getResources().getColor(R.color.blue_100));
186 case MainWebViewActivity.REQUEST_BLOCKED:
188 requestAction.setText(R.string.blocked);
190 // Set the background color.
191 if (MainWebViewActivity.darkTheme) {
192 requestAction.setBackgroundColor(getResources().getColor(R.color.red_700_50));
194 requestAction.setBackgroundColor(getResources().getColor(R.color.red_100));
199 // Display the request URL.
200 requestUrl.setText(requestDetails[MainWebViewActivity.REQUEST_URL]);
202 // Modify the dialog based on the request action.
203 if (requestDetails.length == 2) { // A default request.
204 // Hide the unused views.
205 requestBlockListLabel.setVisibility(View.GONE);
206 requestBlockList.setVisibility(View.GONE);
207 requestSubListLabel.setVisibility(View.GONE);
208 requestSubList.setVisibility(View.GONE);
209 requestBlockListEntriesLabel.setVisibility(View.GONE);
210 requestBlockListEntries.setVisibility(View.GONE);
211 requestBlockListOriginalEntryLabel.setVisibility(View.GONE);
212 requestBlockListOriginalEntry.setVisibility(View.GONE);
213 } else { // A blocked or allowed request.
214 // Set the text on the text views.
215 requestBlockList.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST]);
216 requestBlockListEntries.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ENTRIES]);
217 requestBlockListOriginalEntry.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]);
219 // Set the sublist text.
220 switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_SUBLIST])) {
221 case MainWebViewActivity.MAIN_WHITELIST:
222 requestSubList.setText(R.string.main_whitelist);
225 case MainWebViewActivity.FINAL_WHITELIST:
226 requestSubList.setText(R.string.final_whitelist);
229 case MainWebViewActivity.DOMAIN_WHITELIST:
230 requestSubList.setText(R.string.domain_whitelist);
233 case MainWebViewActivity.DOMAIN_INITIAL_WHITELIST:
234 requestSubList.setText(R.string.domain_initial_whitelist);
237 case MainWebViewActivity.DOMAIN_FINAL_WHITELIST:
238 requestSubList.setText(R.string.domain_final_whitelist);
241 case MainWebViewActivity.THIRD_PARTY_WHITELIST:
242 requestSubList.setText(R.string.third_party_whitelist);
245 case MainWebViewActivity.THIRD_PARTY_DOMAIN_WHITELIST:
246 requestSubList.setText(R.string.third_party_domain_whitelist);
249 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
250 requestSubList.setText(R.string.third_party_domain_initial_whitelist);
253 case MainWebViewActivity.MAIN_BLACKLIST:
254 requestSubList.setText(R.string.main_blacklist);
257 case MainWebViewActivity.INITIAL_BLACKLIST:
258 requestSubList.setText(R.string.initial_blacklist);
261 case MainWebViewActivity.FINAL_BLACKLIST:
262 requestSubList.setText(R.string.final_blacklist);
265 case MainWebViewActivity.DOMAIN_BLACKLIST:
266 requestSubList.setText(R.string.domain_blacklist);
269 case MainWebViewActivity.DOMAIN_INITIAL_BLACKLIST:
270 requestSubList.setText(R.string.domain_initial_blacklist);
273 case MainWebViewActivity.DOMAIN_FINAL_BLACKLIST:
274 requestSubList.setText(R.string.domain_final_blacklist);
277 case MainWebViewActivity.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
278 requestSubList.setText(R.string.domain_regular_expression_blacklist);
281 case MainWebViewActivity.THIRD_PARTY_BLACKLIST:
282 requestSubList.setText(R.string.third_party_blacklist);
285 case MainWebViewActivity.THIRD_PARTY_DOMAIN_BLACKLIST:
286 requestSubList.setText(R.string.third_party_domain_blacklist);
289 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
290 requestSubList.setText(R.string.third_party_domain_initial_blacklist);
293 case MainWebViewActivity.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
294 requestSubList.setText(R.string.third_party_regular_expression_blacklist);
297 case MainWebViewActivity.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
298 requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
301 case MainWebViewActivity.REGULAR_EXPRESSION_BLACKLIST:
302 requestSubList.setText(R.string.regular_expression_blacklist);
307 // `onCreateDialog` requires the return of an alert dialog.