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