Add a requests activity. https://redmine.stoutner.com/issues/170
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewRequestDialog.java
1 /*
2  * Copyright © 2018 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.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;
34
35 import com.stoutner.privacybrowser.R;
36 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
37
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);
42
43         void onNext(int id);
44     }
45
46     // `viewRequestListener` is used in `onAttach()` and `onCreateDialog()`.
47     private ViewRequestListener viewRequestListener;
48
49     public void onAttach(Context context) {
50         // Run the default commands.
51         super.onAttach(context);
52
53         // Get a handle for the listener from the launching context.
54         viewRequestListener = (ViewRequestListener) context;
55     }
56
57     public static ViewRequestDialog request(int id, boolean isLastRequest, String[] requestDetails) {
58         // Create a bundle.
59         Bundle bundle = new Bundle();
60
61         // Store the request details.
62         bundle.putInt("ID", id);
63         bundle.putBoolean("Is Last Request", isLastRequest);
64         bundle.putStringArray("Request Details", requestDetails);
65
66         // Add the bundle to the dialog.
67         ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
68         viewRequestDialog.setArguments(bundle);
69
70         // Return the new dialog.
71         return viewRequestDialog;
72     }
73
74     @Override
75     @NonNull
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;
81
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");
86
87         // Use an alert dialog builder to create the alert dialog.
88         AlertDialog.Builder dialogBuilder;
89
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);
94         } else {
95             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
96             dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
97         }
98
99         // Create the dialog title.
100         String title = getResources().getString(R.string.request_details) + " - " + id;
101
102         // Set the title.
103         dialogBuilder.setTitle(title);
104
105         // Remove the incorrect lint warnings about items being null.
106         assert requestDetails != null;
107         assert getActivity() != null;
108
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));
111
112         // Set the close button.
113         dialogBuilder.setNeutralButton(R.string.close, (DialogInterface dialog, int which) -> {
114             // Do nothing.  The dialog will close automatically.
115         });
116
117         // Set the previous button.
118         dialogBuilder.setNegativeButton(R.string.previous, (DialogInterface dialog, int which) -> {
119             // Load the previous request.
120             viewRequestListener.onPrevious(id);
121         });
122
123         // Set the next button.
124         dialogBuilder.setPositiveButton(R.string.next, (DialogInterface dialog, int which) -> {
125             // Load the next request.
126             viewRequestListener.onNext(id);
127         });
128
129         // Create an alert dialog from the alert dialog builder.
130         final AlertDialog alertDialog = dialogBuilder.create();
131
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;
136
137             // Disable screenshots.
138             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
139         }
140
141         //The alert dialog must be shown before the contents can be modified.
142         alertDialog.show();
143
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);
157
158         // Disable the previous button if the first resource request is displayed.
159         previousButton.setEnabled(!(id == 1));
160
161         // Disable the next button if the last resource request is displayed.
162         nextButton.setEnabled(!isLastRequest);
163
164         // Set the request action text.
165         switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_DISPOSITION])) {
166             case MainWebViewActivity.REQUEST_DEFAULT:
167                 // Set the text.
168                 requestAction.setText(R.string.default_allowed);
169
170                 // Set the background color.
171                 requestAction.setBackgroundColor(getResources().getColor(R.color.transparent));
172                 break;
173
174             case MainWebViewActivity.REQUEST_ALLOWED:
175                 // Set the text.
176                 requestAction.setText(R.string.allowed);
177
178                 // Set the background color.
179                 if (MainWebViewActivity.darkTheme) {
180                     requestAction.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
181                 } else {
182                     requestAction.setBackgroundColor(getResources().getColor(R.color.blue_100));
183                 }
184                 break;
185
186             case MainWebViewActivity.REQUEST_BLOCKED:
187                 // Set the text.
188                 requestAction.setText(R.string.blocked);
189
190                 // Set the background color.
191                 if (MainWebViewActivity.darkTheme) {
192                     requestAction.setBackgroundColor(getResources().getColor(R.color.red_700_50));
193                 } else {
194                     requestAction.setBackgroundColor(getResources().getColor(R.color.red_100));
195                 }
196                 break;
197         }
198
199         // Display the request URL.
200         requestUrl.setText(requestDetails[MainWebViewActivity.REQUEST_URL]);
201
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]);
218
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);
223                     break;
224
225                 case MainWebViewActivity.FINAL_WHITELIST:
226                     requestSubList.setText(R.string.final_whitelist);
227                     break;
228
229                 case MainWebViewActivity.DOMAIN_WHITELIST:
230                     requestSubList.setText(R.string.domain_whitelist);
231                     break;
232
233                 case MainWebViewActivity.DOMAIN_INITIAL_WHITELIST:
234                     requestSubList.setText(R.string.domain_initial_whitelist);
235                     break;
236
237                 case MainWebViewActivity.DOMAIN_FINAL_WHITELIST:
238                     requestSubList.setText(R.string.domain_final_whitelist);
239                     break;
240
241                 case MainWebViewActivity.THIRD_PARTY_WHITELIST:
242                     requestSubList.setText(R.string.third_party_whiteist);
243                     break;
244
245                 case MainWebViewActivity.THIRD_PARTY_DOMAIN_WHITELIST:
246                     requestSubList.setText(R.string.third_party_domain_whitelist);
247                     break;
248
249                 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
250                     requestSubList.setText(R.string.third_party_domain_initial_whitelist);
251                     break;
252
253                 case MainWebViewActivity.MAIN_BLACKLIST:
254                     requestSubList.setText(R.string.main_blacklist);
255                     break;
256
257                 case MainWebViewActivity.INITIAL_BLACKLIST:
258                     requestSubList.setText(R.string.initial_blacklist);
259                     break;
260
261                 case MainWebViewActivity.FINAL_BLACKLIST:
262                     requestSubList.setText(R.string.final_blacklist);
263                     break;
264
265                 case MainWebViewActivity.DOMAIN_BLACKLIST:
266                     requestSubList.setText(R.string.domain_blacklist);
267                     break;
268
269                 case MainWebViewActivity.DOMAIN_INITIAL_BLACKLIST:
270                     requestSubList.setText(R.string.domain_initial_blacklist);
271                     break;
272
273                 case MainWebViewActivity.DOMAIN_FINAL_BLACKLIST:
274                     requestSubList.setText(R.string.domain_final_blacklist);
275                     break;
276
277                 case MainWebViewActivity.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
278                     requestSubList.setText(R.string.domain_regular_expression_blacklist);
279                     break;
280
281                 case MainWebViewActivity.THIRD_PARTY_BLACKLIST:
282                     requestSubList.setText(R.string.third_party_blacklist);
283                     break;
284
285                 case MainWebViewActivity.THIRD_PARTY_DOMAIN_BLACKLIST:
286                     requestSubList.setText(R.string.third_party_domain_blacklist);
287                     break;
288
289                 case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
290                     requestSubList.setText(R.string.third_party_domain_initial_blacklist);
291                     break;
292
293                 case MainWebViewActivity.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
294                     requestSubList.setText(R.string.third_party_regular_expression_blacklist);
295                     break;
296
297                 case MainWebViewActivity.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
298                     requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
299                     break;
300
301                 case MainWebViewActivity.REGULAR_EXPRESSION_BLACKLIST:
302                     requestSubList.setText(R.string.regular_expression_blacklist);
303                     break;
304             }
305         }
306
307         // `onCreateDialog` requires the return of an alert dialog.
308         return alertDialog;
309     }
310 }