Fix changes to Download with External App not applying to existing tabs. https:...
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BlockListHelper.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.helpers;
21
22 import android.content.res.AssetManager;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
30
31 public class BlockListHelper {
32     // Describe the schema of the string array in each entry of the resource requests array list.
33     public final static int REQUEST_DISPOSITION = 0;
34     public final static int REQUEST_URL = 1;
35     public final static int REQUEST_BLOCKLIST = 2;
36     public final static int REQUEST_SUBLIST = 3;
37     public final static int REQUEST_BLOCKLIST_ENTRIES = 4;
38     public final static int REQUEST_BLOCKLIST_ORIGINAL_ENTRY = 5;
39
40     // The request disposition options.
41     public final static String REQUEST_DEFAULT = "0";
42     public final static String REQUEST_ALLOWED = "1";
43     public final static String REQUEST_THIRD_PARTY = "2";
44     public final static String REQUEST_BLOCKED = "3";
45
46     // The whitelists.
47     public final static String MAIN_WHITELIST = "1";
48     public final static String FINAL_WHITELIST = "2";
49     public final static String DOMAIN_WHITELIST = "3";
50     public final static String DOMAIN_INITIAL_WHITELIST = "4";
51     public final static String DOMAIN_FINAL_WHITELIST = "5";
52     public final static String THIRD_PARTY_WHITELIST = "6";
53     public final static String THIRD_PARTY_DOMAIN_WHITELIST = "7";
54     public final static String THIRD_PARTY_DOMAIN_INITIAL_WHITELIST = "8";
55
56     // The blacklists.
57     public final static String MAIN_BLACKLIST = "9";
58     public final static String INITIAL_BLACKLIST = "10";
59     public final static String FINAL_BLACKLIST = "11";
60     public final static String DOMAIN_BLACKLIST = "12";
61     public final static String DOMAIN_INITIAL_BLACKLIST = "13";
62     public final static String DOMAIN_FINAL_BLACKLIST = "14";
63     public final static String DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "15";
64     public final static String THIRD_PARTY_BLACKLIST = "16";
65     public final static String THIRD_PARTY_INITIAL_BLACKLIST = "17";
66     public final static String THIRD_PARTY_DOMAIN_BLACKLIST = "18";
67     public final static String THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST = "19";
68     public final static String THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST = "20";
69     public final static String THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST = "21";
70     public final static String REGULAR_EXPRESSION_BLACKLIST = "22";
71
72     public ArrayList<List<String[]>> parseBlockList(AssetManager assets, String blockListName) {
73         // Initialize the header list.
74         List<String[]> headers = new ArrayList<>();  // 0.
75
76         // Initialize the whitelists.
77         List<String[]> mainWhiteList = new ArrayList<>();  // 1.
78         List<String[]> finalWhiteList = new ArrayList<>();  // 2.
79         List<String[]> domainWhiteList = new ArrayList<>();  // 3.
80         List<String[]> domainInitialWhiteList = new ArrayList<>();  // 4.
81         List<String[]> domainFinalWhiteList = new ArrayList<>();  // 5.
82         List<String[]> thirdPartyWhiteList = new ArrayList<>();  // 6.
83         List<String[]> thirdPartyDomainWhiteList = new ArrayList<>();  // 7.
84         List<String[]> thirdPartyDomainInitialWhiteList = new ArrayList<>();  // 8.
85
86         // Initialize the blacklists
87         List<String[]> mainBlackList = new ArrayList<>();  // 9.
88         List<String[]> initialBlackList = new ArrayList<>();  // 10.
89         List<String[]> finalBlackList = new ArrayList<>();  // 11.
90         List<String[]> domainBlackList = new ArrayList<>();  // 12.
91         List<String[]> domainInitialBlackList = new ArrayList<>();  // 13.
92         List<String[]> domainFinalBlackList = new ArrayList<>();  // 14.
93         List<String[]> domainRegularExpressionBlackList = new ArrayList<>();  // 15.
94         List<String[]> thirdPartyBlackList = new ArrayList<>();  // 16.
95         List<String[]> thirdPartyInitialBlackList = new ArrayList<>();  // 17.
96         List<String[]> thirdPartyDomainBlackList = new ArrayList<>();  // 18.
97         List<String[]> thirdPartyDomainInitialBlackList = new ArrayList<>();  // 19.
98         List<String[]> regularExpressionBlackList = new ArrayList<>();  // 20.
99         List<String[]> thirdPartyRegularExpressionBlackList = new ArrayList<>();  // 21.
100         List<String[]> thirdPartyDomainRegularExpressionBlackList = new ArrayList<>();  // 22.
101
102
103         // Populate the block lists.  The `try` is required by `InputStreamReader`.
104         try {
105             // Load the block list into a `BufferedReader`.
106             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(assets.open(blockListName)));
107
108             // Create a string for storing the block list entries.
109             String blockListEntry;
110
111             // Parse the block list.
112             while ((blockListEntry = bufferedReader.readLine()) != null) {
113                 // Store the original block list entry.
114                 String originalBlockListEntry = blockListEntry;
115
116                 // Remove any `^` from the block list entry.  Privacy Browser does not process them in the interest of efficiency.
117                 blockListEntry = blockListEntry.replace("^", "");
118
119                 //noinspection StatementWithEmptyBody
120                 if (blockListEntry.contains("##") || blockListEntry.contains("#?#") || blockListEntry.contains("#@#") || blockListEntry.startsWith("[")) {
121                     // Entries that contain `##`, `#?#`, and `#@#` are for hiding elements in the main page's HTML.  Entries that start with `[` describe the AdBlock compatibility level.
122                     // Do nothing.  Privacy Browser does not currently use these entries.
123
124                     //Log.i("BlockLists", "Not added: " + blockListEntry);
125                 } else //noinspection StatementWithEmptyBody
126                     if (blockListEntry.contains("$csp=script-src")) {  // Ignore entries that contain `$csp=script-src`.
127                         // Do nothing.  It is uncertain what this directive is even supposed to mean, and it is blocking entire websites like androidcentral.com.  https://redmine.stoutner.com/issues/306.
128
129                         //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
130                 } else //noinspection StatementWithEmptyBody
131                     if (blockListEntry.contains("$websocket") || blockListEntry.contains("$third-party,websocket") || blockListEntry.contains("$script,websocket")) {  // Ignore entries with `websocket`.
132                         // Do nothing.  Privacy Browser does not differentiate between websocket requests and other requests and these entries cause a lot of false positives.
133
134                         //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
135                 } else if (blockListEntry.startsWith("!")) {  //  Comment entries.
136                     if (blockListEntry.startsWith("! Version:")) {
137                         // Get the list version number.
138                         String[] listVersion = {blockListEntry.substring(11)};
139
140                         // Store the list version in the headers list.
141                         headers.add(listVersion);
142                     }
143
144                     if (blockListEntry.startsWith("! Title:")) {
145                         // Get the list title.
146                         String[] listTitle = {blockListEntry.substring(9)};
147
148                         // Store the list title in the headers list.
149                         headers.add(listTitle);
150                     }
151
152                     //Log.i("BlockLists", "Not added: " + blockListEntry);
153                 } else if (blockListEntry.startsWith("@@")) {  // Entries that begin with `@@` are whitelists.
154                     // Remove the `@@`
155                     blockListEntry = blockListEntry.substring(2);
156
157                     // Strip out any initial `||`.  Privacy Browser doesn't differentiate items that only match against the end of the domain name.
158                     if (blockListEntry.startsWith("||")) {
159                         blockListEntry = blockListEntry.substring(2);
160                     }
161
162                     if (blockListEntry.contains("$")) {  // Filter entries.
163                         //noinspection StatementWithEmptyBody
164                         if (blockListEntry.contains("~third-party")) {  // Ignore entries that contain `~third-party`.
165                             // Do nothing.
166
167                             //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
168                         } else if (blockListEntry.contains("third-party")) {  // Third-party white list entries.
169                             if (blockListEntry.contains("domain=")) {  // Third-party domain white list entries.
170                                 // Parse the entry.
171                                 String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
172                                 String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
173                                 String domains = filters.substring(filters.indexOf("domain=") + 7);
174
175                                 //noinspection StatementWithEmptyBody
176                                 if (domains.contains("~")) {  // It is uncertain what a `~` domain means inside an `@@` entry.
177                                     // Do Nothing
178
179                                     //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
180                                 } else if (blockListEntry.startsWith("|")) {  // Third-party domain initial white list entries.
181                                     // Strip out the initial `|`.
182                                     entry = entry.substring(1);
183
184                                     //noinspection StatementWithEmptyBody
185                                     if (entry.equals("http://") || entry.equals("https://")) {  // Ignore generic entries.
186                                         // Do nothing.  These entries are designed for filter options that Privacy Browser does not use.
187
188                                         //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
189                                     } else {  // Process third-party domain initial white list entries.
190                                         // Process each domain.
191                                         do {
192                                             // Create a string to keep track of the current domain.
193                                             String domain;
194
195                                             if (domains.contains("|")) {  // There is more than one domain in the list.
196                                                 // Get the first domain from the list.
197                                                 domain = domains.substring(0, domains.indexOf("|"));
198
199                                                 // Remove the first domain from the list.
200                                                 domains = domains.substring(domains.indexOf("|") + 1);
201                                             } else {  // There is only one domain in the list.
202                                                 domain = domains;
203                                             }
204
205                                             if (entry.contains("*")) {  // Process a third-party domain initial white list double entry.
206                                                 // Get the index of the wildcard.
207                                                 int wildcardIndex = entry.indexOf("*");
208
209                                                 // Split the entry into components.
210                                                 String firstEntry = entry.substring(0, wildcardIndex);
211                                                 String secondEntry = entry.substring(wildcardIndex + 1);
212
213                                                 // Create an entry string array.
214                                                 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
215
216                                                 // Add the entry to the white list.
217                                                 thirdPartyDomainInitialWhiteList.add(domainDoubleEntry);
218
219                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party domain initial white list added: " + domain + " , " + firstEntry + " , " + secondEntry +
220                                                 //        "  -  " + originalBlockListEntry);
221                                             } else {  // Process a third-party domain initial white list single entry.
222                                                 // Create a domain entry string array.
223                                                 String[] domainEntry = {domain, entry, originalBlockListEntry};
224
225                                                 // Add the entry to the third party domain initial white list.
226                                                 thirdPartyDomainInitialWhiteList.add(domainEntry);
227
228                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party domain initial white list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
229                                             }
230                                         } while (domains.contains("|"));
231                                     }
232                                 } else {  // Third-party domain entries.
233                                     // Process each domain.
234                                     do {
235                                         // Create a string to keep track of the current domain.
236                                         String domain;
237
238                                         if (domains.contains("|")) {  // three is more than one domain in the list.
239                                             // Get the first domain from the list.
240                                             domain = domains.substring(0, domains.indexOf("|"));
241
242                                             // Remove the first domain from the list.
243                                             domains = domains.substring(domains.indexOf("|") + 1);
244                                         } else {  // There is only one domain in the list.
245                                             domain = domains;
246                                         }
247
248                                         // Remove any trailing `*` from the entry.
249                                         if (entry.endsWith("*")) {
250                                             entry = entry.substring(0, entry.length() - 1);
251                                         }
252
253                                         if (entry.contains("*")) {  // Process a third-party domain double entry.
254                                             // Get the index of the wildcard.
255                                             int wildcardIndex = entry.indexOf("*");
256
257                                             // Split the entry into components.
258                                             String firstEntry = entry.substring(0, wildcardIndex);
259                                             String secondEntry = entry.substring(wildcardIndex + 1);
260
261                                             // Create an entry string array.
262                                             String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
263
264                                             // Add the entry to the white list.
265                                             thirdPartyDomainWhiteList.add(domainDoubleEntry);
266
267                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
268                                             //        originalBlockListEntry);
269                                         } else {  // Process a third-party domain single entry.
270                                             // Create an entry string array.
271                                             String[] domainEntry = {domain, entry, originalBlockListEntry};
272
273                                             // Add the entry to the white list.
274                                             thirdPartyDomainWhiteList.add(domainEntry);
275
276                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
277                                         }
278                                     } while (domains.contains("|"));
279                                 }
280                             } else {  // Process third-party white list entries.
281                                 // Parse the entry
282                                 String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
283
284                                 if (entry.contains("*")) {  // There are two or more entries.
285                                     // Get the index of the wildcard.
286                                     int wildcardIndex = entry.indexOf("*");
287
288                                     // Split the entry into components.
289                                     String firstEntry = entry.substring(0, wildcardIndex);
290                                     String secondEntry = entry.substring(wildcardIndex + 1);
291
292                                     if (secondEntry.contains("*")) {  // There are three or more entries.
293                                         // Get the index of the wildcard.
294                                         int secondWildcardIndex = secondEntry.indexOf("*");
295
296                                         // Split the entry into components.
297                                         String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
298                                         String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
299
300                                         if (thirdEntry.contains("*")) {  // There are four or more entries.
301                                             // Get the index of the wildcard.
302                                             int thirdWildcardIndex = thirdEntry.indexOf("*");
303
304                                             // Split the entry into components.
305                                             String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
306                                             String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
307
308                                             if (fourthEntry.contains("*")) {  // Process a third-party white list quintuple entry.
309                                                 // Get the index of the wildcard.
310                                                 int fourthWildcardIndex = fourthEntry.indexOf("*");
311
312                                                 // Split the entry into components.
313                                                 String realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex);
314                                                 String fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1);
315
316                                                 // Create an entry string array.
317                                                 String[] quintupleEntry = {firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlockListEntry};
318
319                                                 // Add the entry to the white list.
320                                                 thirdPartyWhiteList.add(quintupleEntry);
321
322                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
323                                                 //        realFourthEntry + " , " + fifthEntry + "  -  " + originalBlockListEntry);
324                                             } else {  // Process a third-party white list quadruple entry.
325                                                 // Create an entry string array.
326                                                 String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlockListEntry};
327
328                                                 // Add the entry to the white list.
329                                                 thirdPartyWhiteList.add(quadrupleEntry);
330
331                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
332                                                 //        fourthEntry + "  -  " + originalBlockListEntry);
333                                             }
334                                         } else {  // Process a third-party white list triple entry.
335                                             // Create an entry string array.
336                                             String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
337
338                                             // Add the entry to the white list.
339                                             thirdPartyWhiteList.add(tripleEntry);
340
341                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " +
342                                             //        originalBlockListEntry);
343                                         }
344                                     } else {  // Process a third-party white list double entry.
345                                         // Create an entry string array.
346                                         String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
347
348                                         // Add the entry to the white list.
349                                         thirdPartyWhiteList.add(doubleEntry);
350
351                                         //Log.i("BlockLists", headers.get(1)[0] + " third-party white list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
352                                     }
353                                 } else {  // Process a third-party white list single entry.
354                                     // Create an entry string array.
355                                     String[] singleEntry = {entry, originalBlockListEntry};
356
357                                     // Add the entry to the white list.
358                                     thirdPartyWhiteList.add(singleEntry);
359
360                                     //Log.i("BlockLists", headers.get(1)[0] + " third-party domain white list added: " + entry + "  -  " + originalBlockListEntry);
361                                 }
362                             }
363                         } else if (blockListEntry.contains("domain=")) {  // Process domain white list entries.
364                             // Parse the entry
365                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
366                             String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
367                             String domains = filters.substring(filters.indexOf("domain=") + 7);
368
369                             if (entry.startsWith("|")) {  // Initial domain white list entries.
370                                 // Strip the initial `|`.
371                                 entry = entry.substring(1);
372
373                                 //noinspection StatementWithEmptyBody
374                                 if (entry.equals("http://") || entry.equals("https://")) {  // Ignore generic entries.
375                                     // Do nothing.  These entries are designed for filter options that Privacy Browser does not use.
376
377                                     //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
378                                 } else {  // Initial domain white list entry.
379                                     // Process each domain.
380                                     do {
381                                         // Create a string to keep track of the current domain.
382                                         String domain;
383
384                                         if (domains.contains("|")) {  // There is more than one domain in the list.
385                                             // Get the first domain from the list.
386                                             domain = domains.substring(0, domains.indexOf("|"));
387
388                                             // Remove the first domain from the list.
389                                             domains = domains.substring(domains.indexOf("|") + 1);
390                                         } else {  // There is only one domain in the list.
391                                             domain = domains;
392                                         }
393
394                                         if (entry.contains("*")) {  // There are two or more entries.
395                                             // Get the index of the wildcard.
396                                             int wildcardIndex = entry.indexOf("*");
397
398                                             // Split the entry into components.
399                                             String firstEntry = entry.substring(0, wildcardIndex);
400                                             String secondEntry = entry.substring(wildcardIndex + 1);
401
402                                             if (secondEntry.contains("*")) {  // Process a domain initial triple entry.
403                                                 // Get the index of the wildcard.
404                                                 int secondWildcardIndex = secondEntry.indexOf("*");
405
406                                                 // Split the entry into components.
407                                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
408                                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
409
410                                                 // Create an entry string array.
411                                                 String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
412
413                                                 // Add the entry to the white list.
414                                                 domainInitialWhiteList.add(domainTripleEntry);
415
416                                                 //Log.i("BlockLists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
417                                                 //        thirdEntry + "  -  " + originalBlockListEntry);
418                                             } else {  // Process a domain initial double entry.
419                                                 // Create an entry string array.
420                                                 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
421
422                                                 // Add the entry to the white list.
423                                                 domainInitialWhiteList.add(domainDoubleEntry);
424
425                                                 //Log.i("BlockLists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
426                                                 //        originalBlockListEntry);
427                                             }
428                                         } else {  // Process a domain initial single entry.
429                                             // Create an entry string array.
430                                             String[] domainEntry = {domain, entry, originalBlockListEntry};
431
432                                             // Add the entry to the white list.
433                                             domainInitialWhiteList.add(domainEntry);
434
435                                             //Log.i("BlockLists", headers.get(1)[0] + " domain initial white list entry added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
436                                         }
437                                     } while (domains.contains("|"));
438                                 }
439                             } else if (entry.endsWith("|")) {  // Final domain white list entries.
440                                 // Strip the `|` from the end of the entry.
441                                 entry = entry.substring(0, entry.length() - 1);
442
443                                 // Process each domain.
444                                 do {
445                                     // Create a string to keep track of the current domain.
446                                     String domain;
447
448                                     if (domains.contains("|")) {  // There is more than one domain in the list.
449                                         // Get the first domain from the list.
450                                         domain = domains.substring(0, domains.indexOf("|"));
451
452                                         // Remove the first domain from the list.
453                                         domains = domains.substring(domains.indexOf("|") + 1);
454                                     } else {  // There is only one domain in the list.
455                                         domain = domains;
456                                     }
457
458                                     if (entry.contains("*")) {  // Process a domain final white list double entry.
459                                         // Get the index of the wildcard.
460                                         int wildcardIndex = entry.indexOf("*");
461
462                                         // Split the entry into components.
463                                         String firstEntry = entry.substring(0, wildcardIndex);
464                                         String secondEntry = entry.substring(wildcardIndex + 1);
465
466                                         // Create an entry string array.
467                                         String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
468
469                                         // Add the entry to the white list.
470                                         domainFinalWhiteList.add(domainDoubleEntry);
471
472                                         //Log.i("BlockLists", headers.get(1)[0] + " domain final white list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
473                                         //        originalBlockListEntry);
474                                     } else {  // Process a domain final white list single entry.
475                                         // create an entry string array.
476                                         String[] domainEntry = {domain, entry, originalBlockListEntry};
477
478                                         // Add the entry to the white list.
479                                         domainFinalWhiteList.add(domainEntry);
480
481                                         //Log.i("BlockLists", headers.get(1)[0] + " domain final white list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
482                                     }
483                                 } while (domains.contains("|"));
484
485                             } else {  // Standard domain white list entries with filters.
486                                 //noinspection StatementWithEmptyBody
487                                 if (domains.contains("~")) {  // It is uncertain what a `~` domain means inside an `@@` entry.
488                                     // Do Nothing
489
490                                     //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
491                                 } else {
492                                     // Process each domain.
493                                     do {
494                                         // Create a string to keep track of the current domain.
495                                         String domain;
496
497                                         if (domains.contains("|")) {  // There is more than one domain in the list.
498                                             // Get the first domain from the list.
499                                             domain = domains.substring(0, domains.indexOf("|"));
500
501                                             // Remove the first domain from the list.
502                                             domains = domains.substring(domains.indexOf("|") + 1);
503                                         } else {  // There is only one domain in the list.
504                                             domain = domains;
505                                         }
506
507                                         if (entry.contains("*")) {  // There are two or more entries.
508                                             // Get the index of the wildcard.
509                                             int wildcardIndex = entry.indexOf("*");
510
511                                             // Split the entry into components.
512                                             String firstEntry = entry.substring(0, wildcardIndex);
513                                             String secondEntry = entry.substring(wildcardIndex + 1);
514
515                                             if (secondEntry.contains("*")) {  // There are three or more entries.
516                                                 // Get the index of the wildcard.
517                                                 int secondWildcardIndex = secondEntry.indexOf("*");
518
519                                                 // Split the entry into components.
520                                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
521                                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
522
523                                                 if (thirdEntry.contains("*")) {  // Process a domain white list quadruple entry.
524                                                     // Get the index of the wildcard.
525                                                     int thirdWildcardIndex = thirdEntry.indexOf("*");
526
527                                                     // Split the entry into components.
528                                                     String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
529                                                     String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
530
531                                                     // Create an entry string array.
532                                                     String[] domainQuadrupleEntry = {domain, firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlockListEntry};
533
534                                                     // Add the entry to the white list.
535                                                     domainWhiteList.add(domainQuadrupleEntry);
536
537                                                     //Log.i("BlockLists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
538                                                     //        realThirdEntry + " , " + fourthEntry + "  -  " + originalBlockListEntry);
539                                                 } else {  // Process a domain white list triple entry.
540                                                     // Create an entry string array.
541                                                     String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
542
543                                                     // Add the entry to the white list.
544                                                     domainWhiteList.add(domainTripleEntry);
545
546                                                     //Log.i("BlockLists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " +
547                                                     //        thirdEntry + "  -  " + originalBlockListEntry);
548                                                 }
549                                             } else {  // Process a domain white list double entry.
550                                                 // Create an entry string array.
551                                                 String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
552
553                                                 // Add the entry to the white list.
554                                                 domainWhiteList.add(domainDoubleEntry);
555
556                                                 //Log.i("BlockLists", headers.get(1)[0] + " domain white list added : " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
557                                                 //        originalBlockListEntry);
558                                             }
559                                         } else {  // Process a domain white list single entry.
560                                             // Create an entry string array.
561                                             String[] domainEntry = {domain, entry, originalBlockListEntry};
562
563                                             // Add the entry to the white list.
564                                             domainWhiteList.add(domainEntry);
565
566                                             //Log.i("BlockLists", headers.get(1)[0] + " domain white list added : " + domain + " , " + entry + "  -  " + originalBlockListEntry);
567                                         }
568                                     } while (domains.contains("|"));
569                                 }
570                             }
571                         }  // Ignore all other filter entries.
572                     } else if (blockListEntry.endsWith("|")) {  // Final white list entries.
573                         // Remove the final `|` from the entry.
574                         String entry = blockListEntry.substring(0, blockListEntry.length() - 1);
575
576                         if (entry.contains("*")) {  // Process a final white list double entry
577                             // Get the index of the wildcard.
578                             int wildcardIndex = entry.indexOf("*");
579
580                             // split the entry into components.
581                             String firstEntry = entry.substring(0, wildcardIndex);
582                             String secondEntry = entry.substring(wildcardIndex + 1);
583
584                             // Create an entry string array.
585                             String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
586
587                             // Add the entry to the white list.
588                             finalWhiteList.add(doubleEntry);
589
590                             //Log.i("BlockLists", headers.get(1)[0] + " final white list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
591                         } else {  // Process a final white list single entry.
592                             // Create an entry string array.
593                             String[] singleEntry = {entry, originalBlockListEntry};
594
595                             // Add the entry to the white list.
596                             finalWhiteList.add(singleEntry);
597
598                             //Log.i("BlockLists", headers.get(1)[0] + " final white list added: " + entry + "  -  " + originalBlockListEntry);
599                         }
600                     } else {  // Main white list entries.
601                         if (blockListEntry.contains("*")) {  // There are two or more entries.
602                             // Get the index of the wildcard.
603                             int wildcardIndex = blockListEntry.indexOf("*");
604
605                             // Split the entry into components.
606                             String firstEntry = blockListEntry.substring(0, wildcardIndex);
607                             String secondEntry = blockListEntry.substring(wildcardIndex + 1);
608
609                             if (secondEntry.contains("*")) {  // Process a main white list triple entry.
610                                 // Get the index of the wildcard.
611                                 int secondWildcardIndex = secondEntry.indexOf("*");
612
613                                 // Split the entry into components.
614                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
615                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
616
617                                 // Create an entry string array.
618                                 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
619
620                                 // Add the entry to the white list.
621                                 mainWhiteList.add(tripleEntry);
622
623                                 //Log.i("BlockLists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " + originalBlockListEntry);
624                             } else {  // Process a main white list double entry.
625                                 // Create an entry string array.
626                                 String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
627
628                                 // Add the entry to the white list.
629                                 mainWhiteList.add(doubleEntry);
630
631                                 //Log.i("BlockLists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
632                             }
633                         } else {  // Process a main white list single entry.
634                             // Create an entry string array.
635                             String[] singleEntry = {blockListEntry, originalBlockListEntry};
636
637                             // Add the entry to the white list.
638                             mainWhiteList.add(singleEntry);
639
640                             //Log.i("BlockLists", headers.get(1)[0] + " main white list added: " + blockListEntry + "  -  " + originalBlockListEntry);
641                         }
642                     }
643                 } else if (blockListEntry.endsWith("|")) {  // Final black list entries.
644                     // Strip out the final "|"
645                     String entry = blockListEntry.substring(0, blockListEntry.length() - 1);
646
647                     // Strip out any initial `||`.  They are redundant in this case because the block list entry is being matched against the end of the URL.
648                     if (entry.startsWith("||")) {
649                         entry = entry.substring(2);
650                     }
651
652                     if (entry.contains("*")) {  // Process a final black list double entry.
653                         // Get the index of the wildcard.
654                         int wildcardIndex = entry.indexOf("*");
655
656                         // Split the entry into components.
657                         String firstEntry = entry.substring(0, wildcardIndex);
658                         String secondEntry = entry.substring(wildcardIndex + 1);
659
660                         // Create an entry string array.
661                         String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
662
663                         // Add the entry to the black list.
664                         finalBlackList.add(doubleEntry);
665
666                         //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
667                     } else {  // Process a final black list single entry.
668                         // create an entry string array.
669                         String[] singleEntry = {entry, originalBlockListEntry};
670
671                         // Add the entry to the black list.
672                         finalBlackList.add(singleEntry);
673
674                         //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + entry + "  -  " + originalBlockListEntry);
675                     }
676                 } else if (blockListEntry.contains("$")) {  // Entries with filter options.
677                     // Strip out any initial `||`.  These will be treated like any other entry.
678                     if (blockListEntry.startsWith("||")) {
679                         blockListEntry = blockListEntry.substring(2);
680                     }
681
682                     if (blockListEntry.contains("third-party")) {  // Third-party entries.
683                         //noinspection StatementWithEmptyBody
684                         if (blockListEntry.contains("~third-party")) {  // Third-party filter white list entries.
685                             // Do not process these white list entries.  They are designed to combine with block filters that Privacy Browser doesn't use, like `subdocument` and `xmlhttprequest`.
686
687                             //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
688                         } else if (blockListEntry.contains("domain=")) {  // Third-party domain entries.
689                             if (blockListEntry.startsWith("|")) {  // Third-party domain initial entries.
690                                 // Strip the initial `|`.
691                                 blockListEntry = blockListEntry.substring(1);
692
693                                 // Parse the entry
694                                 String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
695                                 String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
696                                 String domains = filters.substring(filters.indexOf("domain=") + 7);
697
698                                 //noinspection StatementWithEmptyBody
699                                 if (entry.equals("http:") || entry.equals("https:") || entry.equals("http://") || entry.equals("https://")) {  // Ignore generic entries.
700                                     // Do nothing.  These entries will almost entirely disable the website.
701                                     // Often the original entry blocks filter options like `$script`, which Privacy Browser does not differentiate.
702
703                                     //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
704                                 } else {  // Third-party domain initial entries.
705                                     // Process each domain.
706                                     do {
707                                         // Create a string to keep track of the current domain.
708                                         String domain;
709
710                                         if (domains.contains("|")) {  // There is more than one domain in the list.
711                                             // Get the first domain from the list.
712                                             domain = domains.substring(0, domains.indexOf("|"));
713
714                                             // Remove the first domain from the list.
715                                             domains = domains.substring(domains.indexOf("|") + 1);
716                                         } else {  // There is only one domain in the list.
717                                             domain = domains;
718                                         }
719
720                                         if (entry.contains("*")) {  // Three are two or more entries.
721                                             // Get the index of the wildcard.
722                                             int wildcardIndex = entry.indexOf("*");
723
724                                             // Split the entry into components.
725                                             String firstEntry = entry.substring(0, wildcardIndex);
726                                             String secondEntry = entry.substring(wildcardIndex + 1);
727
728                                             if (secondEntry.contains("*")) {  // Process a third-party domain initial black list triple entry.
729                                                 // Get the index of the wildcard.
730                                                 int secondWildcardIndex = secondEntry.indexOf("*");
731
732                                                 // Split the entry into components.
733                                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
734                                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
735
736                                                 // Create an entry string array.
737                                                 String[] tripleDomainEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
738
739                                                 // Add the entry to the black list.
740                                                 thirdPartyDomainInitialBlackList.add(tripleDomainEntry);
741
742                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + firstEntry + " , " + realSecondEntry +
743                                                 //        " , " + thirdEntry + "  -  " + originalBlockListEntry);
744                                             } else {  // Process a third-party domain initial black list double entry.
745                                                 // Create an entry string array.
746                                                 String[] doubleDomainEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
747
748                                                 // Add the entry to the black list.
749                                                 thirdPartyDomainInitialBlackList.add(doubleDomainEntry);
750
751                                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + firstEntry + " , " + secondEntry +
752                                                 //        "  -  " + originalBlockListEntry);
753                                             }
754                                         } else {  // Process a third-party domain initial black list single entry.
755                                             // Create an entry string array.
756                                             String[] singleEntry = {domain, entry, originalBlockListEntry};
757
758                                             // Add the entry to the black list.
759                                             thirdPartyDomainInitialBlackList.add(singleEntry);
760
761                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain initial black list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
762                                         }
763                                     } while (domains.contains("|"));
764                                 }
765                             } else if (blockListEntry.contains("\\")) {  // Process a third-party domain black list regular expression.
766                                 // Parse the entry.  At least one regular expression in this entry contains `$`, so the parser uses `/$`.
767                                 String entry = blockListEntry.substring(0, blockListEntry.indexOf("/$") + 1);
768                                 String filters = blockListEntry.substring(blockListEntry.indexOf("/$") + 2);
769                                 String domains = filters.substring(filters.indexOf("domain=") + 7);
770
771                                 // Process each domain.
772                                 do {
773                                     // Create a string to keep track of the current domain.
774                                     String domain;
775
776                                     if (domains.contains("|")) {  // There is more than one domain in the list.
777                                         // Get the first domain from the list.
778                                         domain = domains.substring(0, domains.indexOf("|"));
779
780                                         // Remove the first domain from the list.
781                                         domains = domains.substring(domains.indexOf("|") + 1);
782                                     } else {  // There is only one domain in the list.
783                                         domain = domains;
784                                     }
785
786                                     // Create an entry string array.
787                                     String[] domainEntry = {domain, entry, originalBlockListEntry};
788
789                                     // Add the entry to the black list.
790                                     thirdPartyDomainRegularExpressionBlackList.add(domainEntry);
791
792                                     //Log.i("BlockLists", headers.get(1)[0] + " third-party domain regular expression black list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
793                                 } while (domains.contains("|"));
794                             } else {  // Third-party domain entries.
795                                 // Parse the entry
796                                 String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
797                                 String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
798                                 String domains = filters.substring(filters.indexOf("domain=") + 7);
799
800                                 // Strip any trailing "*" from the entry.
801                                 if (entry.endsWith("*")) {
802                                     entry = entry.substring(0, entry.length() - 1);
803                                 }
804
805                                 // Track if any third-party white list filters are applied.
806                                 boolean whiteListDomain = false;
807
808                                 // Process each domain.
809                                 do {
810                                     // Create a string to keep track of the current domain.
811                                     String domain;
812
813                                     if (domains.contains("|")) {  // There is more than one domain in the list.
814                                         // Get the first domain from the list.
815                                         domain = domains.substring(0, domains.indexOf("|"));
816
817                                         // Remove the first domain from the list.
818                                         domains = domains.substring(domains.indexOf("|") + 1);
819                                     } else {  // The is only one domain in the list.
820                                         domain = domains;
821                                     }
822
823                                     // Differentiate between block list domains and white list domains.
824                                     if (domain.startsWith("~")) {  // White list third-party domain entry.
825                                         // Strip the initial `~`.
826                                         domain = domain.substring(1);
827
828                                         // Set the white list domain flag.
829                                         whiteListDomain = true;
830
831                                         if (entry.contains("*")) {  // Process a third-party domain white list double entry.
832                                             // Get the index of the wildcard.
833                                             int wildcardIndex = entry.indexOf("*");
834
835                                             // Split the entry into components.
836                                             String firstEntry = entry.substring(0, wildcardIndex);
837                                             String secondEntry = entry.substring(wildcardIndex + 1);
838
839                                             // Create an entry string array.
840                                             String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
841
842                                             // Add the entry to the white list.
843                                             thirdPartyDomainWhiteList.add(domainDoubleEntry);
844
845                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
846                                             //        originalBlockListEntry);
847                                         } else {  // Process a third-party domain white list single entry.
848                                             // Create an entry string array.
849                                             String[] domainEntry = {domain, entry, originalBlockListEntry};
850
851                                             // Add the entry to the white list.
852                                             thirdPartyDomainWhiteList.add(domainEntry);
853
854                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain white list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
855                                         }
856                                     } else {  // Third-party domain black list entries.
857                                         if (entry.contains("*")) {  // Process a third-party domain black list double entry.
858                                             // Get the index of the wildcard.
859                                             int wildcardIndex = entry.indexOf("*");
860
861                                             // Split the entry into components.
862                                             String firstEntry = entry.substring(0, wildcardIndex);
863                                             String secondEntry = entry.substring(wildcardIndex + 1);
864
865                                             // Create an entry string array.
866                                             String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
867
868                                             // Add the entry to the black list
869                                             thirdPartyDomainBlackList.add(domainDoubleEntry);
870
871                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain black list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
872                                             //        originalBlockListEntry);
873                                         } else {  // Process a third-party domain black list single entry.
874                                             // Create an entry string array.
875                                             String[] domainEntry = {domain, entry, originalBlockListEntry};
876
877                                             // Add the entry to the black list.
878                                             thirdPartyDomainBlackList.add(domainEntry);
879
880                                             //Log.i("BlockLists", headers.get(1)[0] + " third-party domain block list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
881                                         }
882                                     }
883                                 } while (domains.contains("|"));
884
885                                 // Add a third-party black list entry if a white list domain was processed.
886                                 if (whiteListDomain) {
887                                     if (entry.contains("*")) {  // Process a third-party black list double entry.
888                                         // Get the index of the wildcard.
889                                         int wildcardIndex = entry.indexOf("*");
890
891                                         // Split the entry into components.
892                                         String firstEntry = entry.substring(0, wildcardIndex);
893                                         String secondEntry = entry.substring(wildcardIndex + 1);
894
895                                         // Create an entry string array.
896                                         String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
897
898                                         // Add the entry to the black list.
899                                         thirdPartyBlackList.add(doubleEntry);
900
901                                         //Log.i("BlockLists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
902                                     } else {  // Process a third-party black list single entry.
903                                         // Create an entry string array.
904                                         String[] singleEntry = {entry, originalBlockListEntry};
905
906                                         // Add an entry to the black list.
907                                         thirdPartyBlackList.add(singleEntry);
908
909                                         //Log.i("BlockLists", headers.get(1)[0] + " third-party black list added: " + entry + "  -  " + originalBlockListEntry);
910                                     }
911                                 }
912                             }
913                         } else if (blockListEntry.startsWith("|")) {  // Third-party initial black list entries.
914                             // Strip the initial `|`.
915                             blockListEntry = blockListEntry.substring(1);
916
917                             // Get the entry.
918                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
919
920                             if (entry.contains("*")) {  // Process a third-party initial black list double entry.
921                                 // Get the index of the wildcard.
922                                 int wildcardIndex = entry.indexOf("*");
923
924                                 // Split the entry into components.
925                                 String firstEntry = entry.substring(0, wildcardIndex);
926                                 String secondEntry = entry.substring(wildcardIndex + 1);
927
928                                 // Create an entry string array.
929                                 String[] thirdPartyDoubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
930
931                                 // Add the entry to the black list.
932                                 thirdPartyInitialBlackList.add(thirdPartyDoubleEntry);
933
934                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party initial black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
935                             } else {  // Process a third-party initial black list single entry.
936                                 // Create an entry string array.
937                                 String[] singleEntry = {entry, originalBlockListEntry};
938
939                                 // Add the entry to the black list.
940                                 thirdPartyInitialBlackList.add(singleEntry);
941
942                                 //Log.i("BlockLists", headers.get(1)[0] + " third-party initial black list added: " + entry + "  -  " + originalBlockListEntry);
943                             }
944                         } else if (blockListEntry.contains("\\")) {  // Process a regular expression black list entry.
945                             // Prepare a string to hold the entry.
946                             String entry;
947
948                             // Get the entry.
949                             if (blockListEntry.contains("$/$")) {  // The first `$` is part of the regular expression.
950                                 entry = blockListEntry.substring(0, blockListEntry.indexOf("$/$") + 2);
951                             } else {  // The only `$` indicates the filter options.
952                                 entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
953                             }
954
955                             // Create an entry string array.
956                             String[] singleEntry = {entry, originalBlockListEntry};
957
958                             // Add the entry to the black list.
959                             thirdPartyRegularExpressionBlackList.add(singleEntry);
960
961                             //Log.i("BlockLists", headers.get(1)[0] + " third-party regular expression black list added: " + entry + "  -  " + originalBlockListEntry);
962                         } else if (blockListEntry.contains("*")) {  // Third-party and regular expression black list entries.
963                             // Get the entry.
964                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
965
966                             if (entry.endsWith("*")) {  // Process a third-party black list single entry.
967                                 // Strip the final `*`.
968                                 entry = entry.substring(0, entry.length() - 1);
969
970                                 // Create an entry string array.
971                                 String[] singleEntry = {entry, originalBlockListEntry};
972
973                                 // Add the entry to the black list.
974                                 thirdPartyBlackList.add(singleEntry);
975
976                                 //Log.i("BlockLists", headers.get(1)[0] + " third party black list added: " + entry + "  -  " + originalBlockListEntry);
977                             } else {  // There are two or more entries.
978                                 // Get the index of the wildcard.
979                                 int wildcardIndex = entry.indexOf("*");
980
981                                 // Split the entry into components.
982                                 String firstEntry = entry.substring(0, wildcardIndex);
983                                 String secondEntry = entry.substring(wildcardIndex + 1);
984
985                                 if (secondEntry.contains("*")) {  // There are three or more entries.
986                                     // Get the index of the wildcard.
987                                     int secondWildcardIndex = secondEntry.indexOf("*");
988
989                                     // Split the entry into components.
990                                     String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
991                                     String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
992
993                                     if (thirdEntry.contains("*")) {  // Process a third-party black list quadruple entry.
994                                         // Get the index of the wildcard.
995                                         int thirdWildcardIndex = thirdEntry.indexOf("*");
996
997                                         // Split the entry into components.
998                                         String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
999                                         String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
1000
1001                                         // Create an entry string array.
1002                                         String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlockListEntry};
1003
1004                                         // Add the entry to the black list.
1005                                         thirdPartyBlackList.add(quadrupleEntry);
1006
1007                                         //Log.i("BlockLists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1008                                         //        fourthEntry + "  -  " + originalBlockListEntry);
1009                                     } else {  // Process a third-party black list triple entry.
1010                                         // Create an entry string array.
1011                                         String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1012
1013                                         // Add the entry to the black list.
1014                                         thirdPartyBlackList.add(tripleEntry);
1015
1016                                         //Log.i("BlockLists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " +
1017                                         //        originalBlockListEntry);
1018                                     }
1019                                 } else {  // Process a third-party black list double entry.
1020                                     // Create an entry string array.
1021                                     String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1022
1023                                     // Add the entry to the black list.
1024                                     thirdPartyBlackList.add(doubleEntry);
1025
1026                                     //Log.i("BlockLists", headers.get(1)[0] + " third-party black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1027                                 }
1028                             }
1029                         } else {  // Process a third party black list single entry.
1030                             // Get the entry.
1031                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1032
1033                             // Create an entry string array.
1034                             String[] singleEntry = {entry, originalBlockListEntry};
1035
1036                             // Add the entry to the black list.
1037                             thirdPartyBlackList.add(singleEntry);
1038
1039                             //Log.i("BlockLists", headers.get(1)[0] + " third party black list added: " + entry + "  -  " + originalBlockListEntry);
1040                         }
1041                     } else if (blockListEntry.substring(blockListEntry.indexOf("$")).contains("domain=")) {  // Domain entries.
1042                         if (blockListEntry.contains("~")) {  // Domain white list entries.
1043                             // Separate the filters.
1044                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1045                             String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
1046                             String domains = filters.substring(filters.indexOf("domain=") + 7);
1047
1048                             // Strip any final `*` from the entry.  They are redundant.
1049                             if (entry.endsWith("*")) {
1050                                 entry = entry.substring(0, entry.length() - 1);
1051                             }
1052
1053                             // Process each domain.
1054                             do {
1055                                 // Create a string to keep track of the current domain.
1056                                 String domain;
1057
1058                                 if (domains.contains("|")) {  // There is more than one domain in the list.
1059                                     // Get the first domain from the list.
1060                                     domain = domains.substring(0, domains.indexOf("|"));
1061
1062                                     // Remove the first domain from the list.
1063                                     domains = domains.substring(domains.indexOf("|") + 1);
1064                                 } else {  // There is only one domain in the list.
1065                                     domain = domains;
1066                                 }
1067
1068                                 // Strip the initial `~`.
1069                                 domain = domain.substring(1);
1070
1071                                 if (entry.contains("*")) {  // There are two or more entries.
1072                                     // Get the index of the wildcard.
1073                                     int wildcardIndex = entry.indexOf("*");
1074
1075                                     // Split the entry into components.
1076                                     String firstEntry = entry.substring(0, wildcardIndex);
1077                                     String secondEntry = entry.substring(wildcardIndex + 1);
1078
1079                                     if (secondEntry.contains("*")) {  // Process a domain white list triple entry.
1080                                         // Get the index of the wildcard.
1081                                         int secondWildcardIndex = secondEntry.indexOf("*");
1082
1083                                         // Split the entry into components.
1084                                         String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1085                                         String thirdEntry = secondEntry.substring((secondWildcardIndex + 1));
1086
1087                                         // Create an entry string array.
1088                                         String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1089
1090                                         // Add the entry to the white list.
1091                                         domainWhiteList.add(domainTripleEntry);
1092
1093                                         //Log.i("BlockLists", headers.get(1)[0] + " domain white list added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1094                                         //        "  -  " + originalBlockListEntry);
1095                                     } else {  // Process a domain white list double entry.
1096                                         // Create an entry string array.
1097                                         String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
1098
1099                                         // Add the entry to the white list.
1100                                         domainWhiteList.add(domainDoubleEntry);
1101
1102                                         //Log.i("BlockLists", headers.get(1)[0] + " domain white list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1103                                     }
1104                                 } else {  // Process a domain white list single entry.
1105                                     // Create an entry string array.
1106                                     String[] domainEntry = {domain, entry, originalBlockListEntry};
1107
1108                                     // Add the entry to the white list.
1109                                     domainWhiteList.add(domainEntry);
1110
1111                                     //Log.i("BlockLists", headers.get(1)[0] + " domain white list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
1112                                 }
1113                             } while (domains.contains("|"));
1114                         } else {  // Domain black list entries.
1115                             // Separate the filters.
1116                             String entry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1117                             String filters = blockListEntry.substring(blockListEntry.indexOf("$") + 1);
1118                             String domains = filters.substring(filters.indexOf("domain=") + 7);
1119
1120                             // Only process the item if the entry is not null.  For example, some lines begin with `$websocket`, which create a null entry.
1121                             if (!entry.equals("")) {
1122                                 // Process each domain.
1123                                 do {
1124                                     // Create a string to keep track of the current domain.
1125                                     String domain;
1126
1127                                     if (domains.contains("|")) {  // There is more than one domain in the list.
1128                                         // Get the first domain from the list.
1129                                         domain = domains.substring(0, domains.indexOf("|"));
1130
1131                                         // Remove the first domain from the list.
1132                                         domains = domains.substring(domains.indexOf("|") + 1);
1133                                     } else {  // There is only one domain in the list.
1134                                         domain = domains;
1135                                     }
1136
1137                                     if (entry.startsWith("|")) {  // Domain initial black list entries.
1138                                         // Remove the initial `|`;
1139                                         String entryBase = entry.substring(1);
1140
1141                                         //noinspection StatementWithEmptyBody
1142                                         if (entryBase.equals("http://") || entryBase.equals("https://")) {
1143                                             // Do nothing.  These entries will entirely block the website.
1144                                             // Often the original entry blocks `$script` but Privacy Browser does not currently differentiate between scripts and other entries.
1145
1146                                             //Log.i("BlockLists", headers.get(1)[0] + " not added: " + originalBlockListEntry);
1147                                         } else {  // Process a domain initial black list entry
1148                                             // Create an entry string array.
1149                                             String[] domainEntry = {domain, entryBase, originalBlockListEntry};
1150
1151                                             // Add the entry to the black list.
1152                                             domainInitialBlackList.add(domainEntry);
1153
1154                                             //Log.i("BlockLists", headers.get(1)[0] + " domain initial black list added: " + domain + " , " + entryBase + "  -  " + originalBlockListEntry);
1155                                         }
1156                                     } else if (entry.endsWith("|")) {  // Domain final black list entries.
1157                                         // Remove the final `|`.
1158                                         String entryBase = entry.substring(0, entry.length() - 1);
1159
1160                                         if (entryBase.contains("*")) {  // Process a domain final black list double entry.
1161                                             // Get the index of the wildcard.
1162                                             int wildcardIndex = entry.indexOf("*");
1163
1164                                             // Split the entry into components.
1165                                             String firstEntry = entryBase.substring(0, wildcardIndex);
1166                                             String secondEntry = entryBase.substring(wildcardIndex + 1);
1167
1168                                             // Create an entry string array.
1169                                             String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
1170
1171                                             // Add the entry to the black list.
1172                                             domainFinalBlackList.add(domainDoubleEntry);
1173
1174                                             //Log.i("BlockLists", headers.get(1)[0] + " domain final black list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
1175                                             //        originalBlockListEntry);
1176                                         } else {  // Process a domain final black list single entry.
1177                                             // Create an entry string array.
1178                                             String[] domainEntry = {domain, entryBase, originalBlockListEntry};
1179
1180                                             // Add the entry to the black list.
1181                                             domainFinalBlackList.add(domainEntry);
1182
1183                                             //Log.i("BlockLists", headers.get(1)[0] + " domain final black list added: " + domain + " , " + entryBase + "  -  " + originalBlockListEntry);
1184                                         }
1185                                     } else if (entry.contains("\\")) {  // Process a domain regular expression black list entry.
1186                                         // Create an entry string array.
1187                                         String[] domainEntry = {domain, entry, originalBlockListEntry};
1188
1189                                         // Add the entry to the black list.
1190                                         domainRegularExpressionBlackList.add(domainEntry);
1191
1192                                         //Log.i("BlockLists", headers.get(1)[0] + " domain regular expression black list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
1193                                     } else if (entry.contains("*")) {  // There are two or more entries.
1194                                         // Get the index of the wildcard.
1195                                         int wildcardIndex = entry.indexOf("*");
1196
1197                                         // Split the entry into components.
1198                                         String firstEntry = entry.substring(0, wildcardIndex);
1199                                         String secondEntry = entry.substring(wildcardIndex + 1);
1200
1201                                         if (secondEntry.contains("*")) {  // Process a domain black list triple entry.
1202                                             // Get the index of the wildcard.
1203                                             int secondWildcardIndex = secondEntry.indexOf("*");
1204
1205                                             // Split the entry into components.
1206                                             String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1207                                             String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1208
1209                                             // Create an entry string array.
1210                                             String[] domainTripleEntry = {domain, firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1211
1212                                             // Add the entry to the black list.
1213                                             domainBlackList.add(domainTripleEntry);
1214
1215                                             //Log.i("BlockLists", headers.get(1)[0] + " domain black list added: " + domain + " , " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry +
1216                                             //        "  -  " + originalBlockListEntry);
1217                                         } else {  // Process a domain black list double entry.
1218                                             // Create an entry string array.
1219                                             String[] domainDoubleEntry = {domain, firstEntry, secondEntry, originalBlockListEntry};
1220
1221                                             // Add the entry to the black list.
1222                                             domainBlackList.add(domainDoubleEntry);
1223
1224                                             //Log.i("BlockLists", headers.get(1)[0] + " domain black list added: " + domain + " , " + firstEntry + " , " + secondEntry + "  -  " +
1225                                             //        originalBlockListEntry);
1226                                         }
1227                                     } else {  // Process a domain black list single entry.
1228                                         // Create an entry string array.
1229                                         String[] domainEntry = {domain, entry, originalBlockListEntry};
1230
1231                                         // Add the entry to the black list.
1232                                         domainBlackList.add(domainEntry);
1233
1234                                         //Log.i("BlockLists", headers.get(1)[0] + " domain black list added: " + domain + " , " + entry + "  -  " + originalBlockListEntry);
1235                                     }
1236                                 } while (domains.contains("|"));
1237                             }
1238                         }
1239                     } else if (blockListEntry.contains("~")) {  // White list entries.  Privacy Browser does not differentiate against these filter options, so they are just generally white listed.
1240                         // Remove the filter options.
1241                         blockListEntry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1242
1243                         // Strip any trailing `*`.
1244                         if (blockListEntry.endsWith("*")) {
1245                             blockListEntry = blockListEntry.substring(0, blockListEntry.length() - 1);
1246                         }
1247
1248                         if (blockListEntry.contains("*")) {  // Process a white list double entry.
1249                             // Get the index of the wildcard.
1250                             int wildcardIndex = blockListEntry.indexOf("*");
1251
1252                             // Split the entry into components.
1253                             String firstEntry = blockListEntry.substring(0, wildcardIndex);
1254                             String secondEntry = blockListEntry.substring(wildcardIndex + 1);
1255
1256                             // Create an entry string array.
1257                             String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1258
1259                             // Add the entry to the white list.
1260                             mainWhiteList.add(doubleEntry);
1261
1262                             //Log.i("BlockLists", headers.get(1)[0] + " main white list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1263                         } else {  // Process a white list single entry.
1264                             // Create an entry string array.
1265                             String[] singleEntry = {blockListEntry, originalBlockListEntry};
1266
1267                             // Add the entry to the white list.
1268                             mainWhiteList.add(singleEntry);
1269
1270                             //Log.i("BlockLists", headers.get(1)[0] + " main white list added: " + blockListEntry + "  -  + " + originalBlockListEntry);
1271                         }
1272                     } else if (blockListEntry.contains("\\")) {  // Process a regular expression black list entry.
1273                         // Remove the filter options.
1274                         blockListEntry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1275
1276                         // Create an entry string array.
1277                         String[] singleEntry = {blockListEntry, originalBlockListEntry};
1278
1279                         // Add the entry to the black list.
1280                         regularExpressionBlackList.add(singleEntry);
1281
1282                         //Log.i("BlockLists", headers.get(1)[0] + " regular expression black list added: " + blockListEntry + "  -  " + originalBlockListEntry);
1283                     } else {  // Black list entries.
1284                         // Remove the filter options.
1285                         if (!blockListEntry.contains("$file")) {  // EasyPrivacy contains an entry with `$file` that does not have filter options.
1286                             blockListEntry = blockListEntry.substring(0, blockListEntry.indexOf("$"));
1287                         }
1288
1289                         // Strip any trailing `*`.  These are redundant.
1290                         if (blockListEntry.endsWith("*")) {
1291                             blockListEntry = blockListEntry.substring(0, blockListEntry.length() - 1);
1292                         }
1293
1294                         if (blockListEntry.startsWith("|")) {  // Initial black list entries.
1295                             // Strip the initial `|`.
1296                             String entry = blockListEntry.substring(1);
1297
1298                             if (entry.contains("*")) {  // Process an initial black list double entry.
1299                                 // Get the index of the wildcard.
1300                                 int wildcardIndex = entry.indexOf("*");
1301
1302                                 // Split the entry into components.
1303                                 String firstEntry = entry.substring(0, wildcardIndex);
1304                                 String secondEntry = entry.substring(wildcardIndex + 1);
1305
1306                                 // Create an entry string array.
1307                                 String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1308
1309                                 // Add the entry to the black list.
1310                                 initialBlackList.add(doubleEntry);
1311
1312                                 //Log.i("BlockLists", headers.get(1)[0] + " initial black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1313                             } else {  // Process an initial black list single entry.
1314                                 // Create an entry string array.
1315                                 String[] singleEntry = {entry, originalBlockListEntry};
1316
1317                                 // Add the entry to the black list.
1318                                 initialBlackList.add(singleEntry);
1319
1320                                 //Log.i("BlockLists", headers.get(1)[0] + " initial black list added: " + entry + "  -  " + originalBlockListEntry);
1321                             }
1322                         } else if (blockListEntry.endsWith("|")) {  // Final black list entries.
1323                             // Ignore entries with `object` filters.  They can block entire websites and don't have any meaning in the context of Privacy Browser.
1324                             if (!originalBlockListEntry.contains("$object")) {
1325                                 // Strip the final `|`.
1326                                 String entry = blockListEntry.substring(0, blockListEntry.length() - 1);
1327
1328                                 if (entry.contains("*")) {  // There are two or more entries.
1329                                     // Get the index of the wildcard.
1330                                     int wildcardIndex = entry.indexOf("*");
1331
1332                                     // Split the entry into components.
1333                                     String firstEntry = entry.substring(0, wildcardIndex);
1334                                     String secondEntry = entry.substring(wildcardIndex + 1);
1335
1336                                     if (secondEntry.contains("*")) {  // Process a final black list triple entry.
1337                                         // Get the index of the wildcard.
1338                                         int secondWildcardIndex = secondEntry.indexOf("*");
1339
1340                                         // Split the entry into components.
1341                                         String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1342                                         String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1343
1344                                         // Create an entry string array.
1345                                         String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1346
1347                                         // Add the entry to the black list.
1348                                         finalBlackList.add(tripleEntry);
1349
1350                                         //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " +
1351                                         //        originalBlockListEntry);
1352                                     } else {  // Process a final black list double entry.
1353                                         // Create an entry string array.
1354                                         String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1355
1356                                         // Add the entry to the black list.
1357                                         finalBlackList.add(doubleEntry);
1358
1359                                         //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1360                                     }
1361                                 } else {  // Process a final black list single entry.
1362                                     // Create an entry sting array.
1363                                     String[] singleEntry = {entry, originalBlockListEntry};
1364
1365                                     // Add the entry to the black list.
1366                                     finalBlackList.add(singleEntry);
1367
1368                                     //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + entry + "  -  " + originalBlockListEntry);
1369                                 }
1370                             }
1371                         } else if (blockListEntry.contains("*")) {  // There are two or more entries.
1372                             // Get the index of the wildcard.
1373                             int wildcardIndex = blockListEntry.indexOf("*");
1374
1375                             // Split the entry into components.
1376                             String firstEntry = blockListEntry.substring(0, wildcardIndex);
1377                             String secondEntry = blockListEntry.substring(wildcardIndex + 1);
1378
1379                             if (secondEntry.contains("*")) {  // Process a main black list triple entry.
1380                                 // Get the index of the wildcard.
1381                                 int secondWildcardIndex = secondEntry.indexOf("*");
1382
1383                                 // Split the entry into components.
1384                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1385                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1386
1387                                 // Create an entry string array.
1388                                 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1389
1390                                 // Add the entry to the black list.
1391                                 mainBlackList.add(tripleEntry);
1392
1393                                 //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " + originalBlockListEntry);
1394                             } else {  // Process a main black list double entry.
1395                                 // Create an entry string array.
1396                                 String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1397
1398                                 // Add the entry to the black list.
1399                                 mainBlackList.add(doubleEntry);
1400
1401                                 //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1402                             }
1403                         } else {  // Process a main black list single entry.
1404                             // Create an entry string array.
1405                             String[] singleEntry = {blockListEntry, originalBlockListEntry};
1406
1407                             // Add the entry to the black list.
1408                             mainBlackList.add(singleEntry);
1409
1410                             //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + blockListEntry + "  -  " + originalBlockListEntry);
1411                         }
1412                     }
1413                 } else {  // Main black list entries
1414                     // Strip out any initial `||`.  These will be treated like any other entry.
1415                     if (blockListEntry.startsWith("||")) {
1416                         blockListEntry = blockListEntry.substring(2);
1417                     }
1418
1419                     // Strip out any initial `*`.
1420                     if (blockListEntry.startsWith("*")) {
1421                         blockListEntry = blockListEntry.substring(1);
1422                     }
1423
1424                     // Strip out any trailing `*`.
1425                     if (blockListEntry.endsWith("*")) {
1426                         blockListEntry = blockListEntry.substring(0, blockListEntry.length() - 1);
1427                     }
1428
1429                     if (blockListEntry.startsWith("|")) {  // Initial black list entries.
1430                         // Strip the initial `|`.
1431                         String entry = blockListEntry.substring(1);
1432
1433                         if (entry.contains("*")) {  // Process an initial black list double entry.
1434                             // Get the index of the wildcard.
1435                             int wildcardIndex = entry.indexOf("*");
1436
1437                             // Split the entry into components.
1438                             String firstEntry = entry.substring(0, wildcardIndex);
1439                             String secondEntry = entry.substring(wildcardIndex + 1);
1440
1441                             // Create an entry string array.
1442                             String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1443
1444                             // Add the entry to the black list.
1445                             initialBlackList.add(doubleEntry);
1446
1447                             //Log.i("BlockLists", headers.get(1)[0] + " initial black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1448                         } else {  // Process an initial black list single entry.
1449                             // Create an entry string array.
1450                             String[] singleEntry = {entry, originalBlockListEntry};
1451
1452                             // Add the entry to the black list.
1453                             initialBlackList.add(singleEntry);
1454
1455                             //Log.i("BlockLists", headers.get(1)[0] + " initial black list added: " + entry + "  -  " + originalBlockListEntry);
1456                         }
1457                     } else if (blockListEntry.endsWith("|")) {  // Final black list entries.
1458                         // Strip the final `|`.
1459                         String entry = blockListEntry.substring(0, blockListEntry.length() - 1);
1460
1461                         if (entry.contains("*")) {  // There are two or more entries.
1462                             // Get the index of the wildcard.
1463                             int wildcardIndex = entry.indexOf("*");
1464
1465                             // Split the entry into components.
1466                             String firstEntry = entry.substring(0, wildcardIndex);
1467                             String secondEntry = entry.substring(wildcardIndex + 1);
1468
1469                             if (secondEntry.contains("*")) {  // Process a final black list triple entry.
1470                                 // Get the index of the wildcard.
1471                                 int secondWildcardIndex = secondEntry.indexOf("*");
1472
1473                                 // Split the entry into components.
1474                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1475                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1476
1477                                 // Create an entry string array.
1478                                 String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1479
1480                                 // Add the entry to the black list.
1481                                 finalBlackList.add(tripleEntry);
1482
1483                                 //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " +
1484                                 //        originalBlockListEntry);
1485                             } else {  // Process a final black list double entry.
1486                                 // Create an entry string array.
1487                                 String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1488
1489                                 // Add the entry to the black list.
1490                                 finalBlackList.add(doubleEntry);
1491
1492                                 //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1493                             }
1494                         } else {  // Process a final black list single entry.
1495                             // Create an entry string array.
1496                             String[] singleEntry = {entry, originalBlockListEntry};
1497
1498                             // Add the entry to the black list.
1499                             finalBlackList.add(singleEntry);
1500
1501                             //Log.i("BlockLists", headers.get(1)[0] + " final black list added: " + entry + "  -  " + originalBlockListEntry);
1502                         }
1503                     } else {  // Main black list entries.
1504                         if (blockListEntry.contains("*")) {  // There are two or more entries.
1505                             // Get the index of the wildcard.
1506                             int wildcardIndex = blockListEntry.indexOf("*");
1507
1508                             // Split the entry into components.
1509                             String firstEntry = blockListEntry.substring(0, wildcardIndex);
1510                             String secondEntry = blockListEntry.substring(wildcardIndex + 1);
1511
1512                             if (secondEntry.contains("*")) {  // There are three or more entries.
1513                                 // Get the index of the wildcard.
1514                                 int secondWildcardIndex = secondEntry.indexOf("*");
1515
1516                                 // Split the entry into components.
1517                                 String realSecondEntry = secondEntry.substring(0, secondWildcardIndex);
1518                                 String thirdEntry = secondEntry.substring(secondWildcardIndex + 1);
1519
1520                                 if (thirdEntry.contains("*")) {  // There are four or more entries.
1521                                     // Get the index of the wildcard.
1522                                     int thirdWildcardIndex = thirdEntry.indexOf("*");
1523
1524                                     // Split the entry into components.
1525                                     String realThirdEntry = thirdEntry.substring(0, thirdWildcardIndex);
1526                                     String fourthEntry = thirdEntry.substring(thirdWildcardIndex + 1);
1527
1528                                     if (fourthEntry.contains("*")) {  // Process a main black list quintuple entry.
1529                                         // Get the index of the wildcard.
1530                                         int fourthWildcardIndex = fourthEntry.indexOf("*");
1531
1532                                         // Split the entry into components.
1533                                         String realFourthEntry = fourthEntry.substring(0, fourthWildcardIndex);
1534                                         String fifthEntry = fourthEntry.substring(fourthWildcardIndex + 1);
1535
1536                                         // Create an entry string array.
1537                                         String[] quintupleEntry = {firstEntry, realSecondEntry, realThirdEntry, realFourthEntry, fifthEntry, originalBlockListEntry};
1538
1539                                         // Add the entry to the black list.
1540                                         mainBlackList.add(quintupleEntry);
1541
1542                                         //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1543                                         //        realFourthEntry + " , " + fifthEntry + "  -  " + originalBlockListEntry);
1544                                     } else {  // Process a main black list quadruple entry.
1545                                         // Create an entry string array.
1546                                         String[] quadrupleEntry = {firstEntry, realSecondEntry, realThirdEntry, fourthEntry, originalBlockListEntry};
1547
1548                                         // Add the entry to the black list.
1549                                         mainBlackList.add(quadrupleEntry);
1550
1551                                         //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + realThirdEntry + " , " +
1552                                         //        fourthEntry + "  -  " + originalBlockListEntry);
1553                                     }
1554                                 } else {  // Process a main black list triple entry.
1555                                     // Create an entry string array.
1556                                     String[] tripleEntry = {firstEntry, realSecondEntry, thirdEntry, originalBlockListEntry};
1557
1558                                     // Add the entry to the black list.
1559                                     mainBlackList.add(tripleEntry);
1560
1561                                     //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + realSecondEntry + " , " + thirdEntry + "  -  " + originalBlockListEntry);
1562                                 }
1563                             } else {  // Process a main black list double entry.
1564                                 // Create an entry string array.
1565                                 String[] doubleEntry = {firstEntry, secondEntry, originalBlockListEntry};
1566
1567                                 // Add the entry to the black list.
1568                                 mainBlackList.add(doubleEntry);
1569
1570                                 //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + firstEntry + " , " + secondEntry + "  -  " + originalBlockListEntry);
1571                             }
1572                         } else {  // Process a main black list single entry.
1573                             // Create an entry string array.
1574                             String[] singleEntry = {blockListEntry, originalBlockListEntry};
1575
1576                             // Add the entry to the black list.
1577                             mainBlackList.add(singleEntry);
1578
1579                             //Log.i("BlockLists", headers.get(1)[0] + " main black list added: " + blockListEntry + "  -  " + originalBlockListEntry);
1580                         }
1581                     }
1582                 }
1583             }
1584             // Close `bufferedReader`.
1585             bufferedReader.close();
1586         } catch (IOException e) {
1587             // The asset exists, so the `IOException` will never be thrown.
1588         }
1589
1590         // Initialize the combined list.
1591         ArrayList<List<String[]>> combinedLists = new ArrayList<>();
1592
1593         // Add the headers (0).
1594         combinedLists.add(headers);  // 0.
1595
1596         // Add the white lists (1-8).
1597         combinedLists.add(mainWhiteList);  // 1.
1598         combinedLists.add(finalWhiteList);  // 2.
1599         combinedLists.add(domainWhiteList);  // 3.
1600         combinedLists.add(domainInitialWhiteList);  // 4.
1601         combinedLists.add(domainFinalWhiteList); // 5.
1602         combinedLists.add(thirdPartyWhiteList);  // 6.
1603         combinedLists.add(thirdPartyDomainWhiteList);  // 7.
1604         combinedLists.add(thirdPartyDomainInitialWhiteList);  // 8.
1605
1606         // Add the black lists (9-22).
1607         combinedLists.add(mainBlackList);  // 9.
1608         combinedLists.add(initialBlackList);  // 10.
1609         combinedLists.add(finalBlackList);  // 11.
1610         combinedLists.add(domainBlackList);  //  12.
1611         combinedLists.add(domainInitialBlackList);  // 13.
1612         combinedLists.add(domainFinalBlackList);  // 14.
1613         combinedLists.add(domainRegularExpressionBlackList);  // 15.
1614         combinedLists.add(thirdPartyBlackList);  // 16.
1615         combinedLists.add(thirdPartyInitialBlackList);  // 17.
1616         combinedLists.add(thirdPartyDomainBlackList);  // 18.
1617         combinedLists.add(thirdPartyDomainInitialBlackList);  // 19.
1618         combinedLists.add(thirdPartyRegularExpressionBlackList);  // 20.
1619         combinedLists.add(thirdPartyDomainRegularExpressionBlackList);  // 21.
1620         combinedLists.add(regularExpressionBlackList);  // 22.
1621
1622         return combinedLists;
1623     }
1624
1625     public String[] checkBlocklist(String currentDomain, String resourceUrl, boolean isThirdPartyRequest, ArrayList<List<String[]>> blockList) {
1626         // Get the blocklist name.
1627         String BLOCK_LIST_NAME_STRING = blockList.get(0).get(1)[0];
1628
1629         // Assert that currentDomain != null only if this is a third party request.  Apparently, lint can't tell that this isn't redundant.
1630         //noinspection RedundantIfStatement
1631         if (isThirdPartyRequest) {
1632             assert currentDomain != null;
1633         }
1634
1635         // Process the white lists.
1636         // Main white list.
1637         for (String[] whiteListEntry : blockList.get(Integer.valueOf(MAIN_WHITELIST))) {
1638             switch (whiteListEntry.length) {
1639                 case 2:  // There is one entry.
1640                     if (resourceUrl.contains(whiteListEntry[0])) {
1641                         // Return a whitelist match request allowed.
1642                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whiteListEntry[0], whiteListEntry[1]};
1643                     }
1644                     break;
1645
1646                 case 3:  // There are two entries.
1647                     if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1])) {
1648                         // Return a whitelist match request allowed.
1649                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1650                     }
1651                     break;
1652
1653                 case 4:  // There are three entries.
1654                     if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1655                         // Return a whitelist match request allowed.
1656                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2], whiteListEntry[3]};
1657                     }
1658                     break;
1659             }
1660         }
1661
1662         // Final white list.
1663         for (String[] whiteListEntry : blockList.get(Integer.valueOf(FINAL_WHITELIST))) {
1664             if (whiteListEntry.length == 2) {  // There is one entry.
1665                 if (resourceUrl.contains(whiteListEntry[0])) {
1666                     // Return a whitelist match request allowed.
1667                     return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_WHITELIST, whiteListEntry[0], whiteListEntry[1]};
1668                 }
1669             } else {  // There are two entries.
1670                 if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1])) {
1671                     // Return a whitelist match request allowed.
1672                     return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1673                 }
1674             }
1675         }
1676
1677         // Only check the domain lists if the current domain is not null (like `about:blank`).
1678         if (currentDomain != null) {
1679             // Domain white list.
1680             for (String[] whiteListEntry : blockList.get(Integer.valueOf(DOMAIN_WHITELIST))) {
1681                 switch (whiteListEntry.length) {
1682                     case 3:  // There is one entry.
1683                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1])) {
1684                             // Return a whitelist match request allowed.
1685                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1686                         }
1687                         break;
1688
1689                     case 4:  // There are two entries.
1690                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1691                             // Return a whitelist match request allowed.
1692                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1693                                     whiteListEntry[3]};
1694                         }
1695                         break;
1696
1697                     case 5:  // There are three entries.
1698                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2]) && resourceUrl.contains(whiteListEntry[3])) {
1699                             // Return a whitelist match request allowed.
1700                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2] + "\n" +
1701                                     whiteListEntry[3], whiteListEntry[4]};
1702                         }
1703                         break;
1704
1705                     case 6:  // There are four entries.
1706                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2]) && resourceUrl.contains(whiteListEntry[3]) &&
1707                                 resourceUrl.contains(whiteListEntry[4])) {
1708                             // Return a whitelist match request allowed.
1709                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2] + "\n" +
1710                                     whiteListEntry[3] + "\n" + whiteListEntry[4], whiteListEntry[5]};
1711                         }
1712                         break;
1713                 }
1714             }
1715
1716             // Domain initial white list.
1717             for (String[] whiteListEntry : blockList.get(Integer.valueOf(DOMAIN_INITIAL_WHITELIST))) {
1718                 switch (whiteListEntry.length) {
1719                     case 3:  // There is one entry.
1720                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.startsWith(whiteListEntry[1])) {
1721                             // Return a whitelist match request allowed.
1722                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1723                         }
1724                         break;
1725
1726                     case 4:  // There are two entries.
1727                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.startsWith(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1728                             // Return a whitelist match request allowed.
1729                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1730                                     whiteListEntry[3]};
1731                         }
1732                         break;
1733
1734                     case 5:  // There are three entries.
1735                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.startsWith(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2]) && resourceUrl.startsWith(whiteListEntry[3])) {
1736                             // Return a whitelist match request allowed.
1737                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2] + "\n" +
1738                                     whiteListEntry[3], whiteListEntry[4]};
1739                         }
1740                         break;
1741                 }
1742             }
1743
1744             // Domain final white list.
1745             for (String[] whiteListEntry : blockList.get(Integer.valueOf(DOMAIN_FINAL_WHITELIST))) {
1746                 switch (whiteListEntry.length) {
1747                     case 3:  // There is one entry;
1748                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.endsWith(whiteListEntry[1])) {
1749                             // Return a whitelist match request allowed.
1750                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1751                         }
1752                         break;
1753
1754                     case 4:  // There are two entries;
1755                         if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.endsWith(whiteListEntry[2])) {
1756                             // Return a whitelist match request allowed.
1757                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1758                                     whiteListEntry[3]};
1759                         }
1760                         break;
1761                 }
1762             }
1763         }
1764
1765         // Only check the third-party white lists if this is a third-party request.
1766         if (isThirdPartyRequest) {
1767             // Third-party white list.
1768             for (String[] whiteListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_WHITELIST))) {
1769                 switch (whiteListEntry.length) {
1770                     case 2:  // There is one entry
1771                         if (resourceUrl.contains(whiteListEntry[0])) {
1772                             // Return a whitelist match request allowed.
1773                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whiteListEntry[0], whiteListEntry[1]};
1774                         }
1775                         break;
1776
1777                     case 3:  // There are two entries.
1778                         if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1])) {
1779                             // Return a whitelist match request allowed.
1780                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1781                         }
1782                         break;
1783
1784                     case 4:  // There are three entries.
1785                         if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1786                             // Return a whitelist match request allowed.
1787                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1788                                     whiteListEntry[3]};
1789                         }
1790                         break;
1791
1792                     case 5:  // There are four entries.
1793                         if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2]) && resourceUrl.contains(whiteListEntry[3])) {
1794                             // Return a whitelist match request allowed.
1795                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2] + "\n" +
1796                                     whiteListEntry[3], whiteListEntry[4]};
1797                         }
1798                         break;
1799
1800                     case 6:  // There are five entries.
1801                         if (resourceUrl.contains(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2]) && resourceUrl.contains(whiteListEntry[3]) &&
1802                                 resourceUrl.contains(whiteListEntry[4])) {
1803                             // Return a whitelist match request allowed.
1804                             return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2] + "\n" +
1805                                     whiteListEntry[3] + "\n" + whiteListEntry[4], whiteListEntry[5]};
1806                         }
1807                         break;
1808                 }
1809             }
1810
1811             // Third-party domain white list.
1812             for (String[] whiteListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_DOMAIN_WHITELIST))) {
1813                 if (whiteListEntry.length == 3) {  // There is one entry.
1814                     if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1])) {
1815                         // Return a whitelist match request allowed.
1816                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1817                     }
1818                 } else {  // There are two entries.
1819                     if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.contains(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1820                         // Return a whitelist match request allowed.
1821                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1822                                 whiteListEntry[3]};
1823                     }
1824                 }
1825             }
1826
1827             // Third-party domain initial white list.
1828             for (String[] whiteListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_DOMAIN_INITIAL_WHITELIST))) {
1829                 if (whiteListEntry.length == 3) {  // There is one entry.
1830                     if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.startsWith(whiteListEntry[1])) {
1831                         // Return a whitelist match request allowed.
1832                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1], whiteListEntry[2]};
1833                     }
1834                 } else {  // There are two entries.
1835                     if (currentDomain.endsWith(whiteListEntry[0]) && resourceUrl.startsWith(whiteListEntry[1]) && resourceUrl.contains(whiteListEntry[2])) {
1836                         // Return a whitelist match request allowed.
1837                         return new String[] {REQUEST_ALLOWED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_WHITELIST, whiteListEntry[0] + "\n" + whiteListEntry[1] + "\n" + whiteListEntry[2],
1838                                 whiteListEntry[3]};
1839                     }
1840                 }
1841             }
1842         }
1843
1844         // Process the black lists.
1845         // Main black list.
1846         for (String[] blackListEntry : blockList.get(Integer.valueOf(MAIN_BLACKLIST))) {
1847             switch (blackListEntry.length) {
1848                 case 2:  // There is one entry.
1849                     if (resourceUrl.contains(blackListEntry[0])) {
1850                         // Return a blacklist match request blocked.
1851                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blackListEntry[0], blackListEntry[1]};
1852                     }
1853                     break;
1854
1855                 case 3:  // There are two entries.
1856                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
1857                         // Return a blacklist match request blocked.
1858                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1859                     }
1860                     break;
1861
1862                 case 4:  // There are three entries.
1863                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2])) {
1864                         // Return a blacklist match request blocked.
1865                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2], blackListEntry[3]};
1866                     }
1867                     break;
1868
1869                 case 5:  // There are four entries.
1870                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2]) && resourceUrl.contains(blackListEntry[3])) {
1871                         // Return a blacklist match request blocked.
1872                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2] + "\n" +
1873                                 blackListEntry[3], blackListEntry[4]};
1874                     }
1875                     break;
1876
1877                 case 6:  // There are five entries.
1878                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2]) && resourceUrl.contains(blackListEntry[3]) &&
1879                             resourceUrl.contains(blackListEntry[4])) {
1880                         // Return a blacklist match request blocked.
1881                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, MAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2] + "\n" +
1882                                 blackListEntry[3] + "\n" + blackListEntry[4], blackListEntry[5]};
1883                     }
1884                     break;
1885             }
1886         }
1887
1888         // Initial black list.
1889         for (String[] blackListEntry : blockList.get(Integer.valueOf(INITIAL_BLACKLIST))) {
1890             if (blackListEntry.length == 2) {  // There is one entry.
1891                 if (resourceUrl.startsWith(blackListEntry[0])) {
1892                     // Return a blacklist match request blocked.
1893                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, INITIAL_BLACKLIST, blackListEntry[0], blackListEntry[1]};
1894                 }
1895             } else {  // There are two entries
1896                 if (resourceUrl.startsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
1897                     // Return a blacklist match request blocked.
1898                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1899                 }
1900             }
1901         }
1902
1903         // Final black list.
1904         for (String[] blackListEntry : blockList.get(Integer.valueOf(FINAL_BLACKLIST))) {
1905             switch (blackListEntry.length) {
1906                 case 2:  // There is one entry.
1907                     if (resourceUrl.endsWith(blackListEntry[0])) {
1908                         // Return a blacklist match request blocked.
1909                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blackListEntry[0], blackListEntry[1]};
1910                     }
1911                     break;
1912
1913                 case 3:  // There are two entries.
1914                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.endsWith(blackListEntry[1])) {
1915                         // Return a blacklist match request blocked.
1916                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1917                     }
1918                     break;
1919
1920                 case 4:  // There are three entries.
1921                     if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.endsWith(blackListEntry[2])) {
1922                         // Return a blacklist match request blocked.
1923                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, FINAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2], blackListEntry[3]};
1924                     }
1925                     break;
1926             }
1927         }
1928
1929         // Only check the domain lists if the current domain is not null (like `about:blank`).
1930         if (currentDomain != null) {
1931             // Domain black list.
1932             for (String[] blackListEntry : blockList.get(Integer.valueOf(DOMAIN_BLACKLIST))) {
1933                 switch (blackListEntry.length) {
1934                     case 3:  // There is one entry.
1935                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
1936                             // Return a blacklist match request blocked.
1937                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1938                         }
1939                         break;
1940
1941                     case 4:  // There are two entries.
1942                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2])) {
1943                             // Return a blacklist match request blocked.
1944                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2],
1945                                     blackListEntry[3]};
1946                         }
1947                         break;
1948
1949                     case 5:  // There are three entries.
1950                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2]) && resourceUrl.contains(blackListEntry[3])) {
1951                             // Return a blacklist match request blocked.
1952                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2] + "\n" +
1953                                     blackListEntry[3], blackListEntry[4]};
1954                         }
1955                         break;
1956                 }
1957             }
1958
1959             // Domain initial black list.
1960             for (String[] blackListEntry : blockList.get(Integer.valueOf(DOMAIN_INITIAL_BLACKLIST))) {
1961                 // Store the entry in the resource request log.
1962                 if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.startsWith(blackListEntry[1])) {
1963                     // Return a blacklist match request blocked.
1964                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1965                 }
1966             }
1967
1968             // Domain final black list.
1969             for (String[] blackListEntry : blockList.get(Integer.valueOf(DOMAIN_FINAL_BLACKLIST))) {
1970                 switch (blackListEntry.length) {
1971                     case 3:  // There is one entry.
1972                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.endsWith(blackListEntry[1])) {
1973                             // Return a blacklist match request blocked.
1974                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1975                         }
1976                         break;
1977
1978                     case 4:  // There are two entries.
1979                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.endsWith(blackListEntry[2])) {
1980                             // Return a blacklist match request blocked.
1981                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_FINAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2],
1982                                     blackListEntry[3]};
1983                         }
1984                         break;
1985                 }
1986             }
1987
1988             // Domain regular expression black list.
1989             for (String[] blackListEntry : blockList.get(Integer.valueOf(DOMAIN_REGULAR_EXPRESSION_BLACKLIST))) {
1990                 if (currentDomain.endsWith(blackListEntry[0]) && Pattern.matches(blackListEntry[1], resourceUrl)) {
1991                     // Return a blacklist match request blocked.
1992                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, DOMAIN_REGULAR_EXPRESSION_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
1993                 }
1994             }
1995         }
1996
1997         // Only check the third-party black lists if this is a third-party request.
1998         if (isThirdPartyRequest) {
1999             // Third-party black list.
2000             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_BLACKLIST))) {
2001                 switch (blackListEntry.length) {
2002                     case 2:  // There is one entry.
2003                         if (resourceUrl.contains(blackListEntry[0])) {
2004                             // Return a blacklist match request blocked.
2005                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blackListEntry[0], blackListEntry[1]};
2006                         }
2007                         break;
2008
2009                     case 3:  // There are two entries.
2010                         if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
2011                             // Return a blacklist match request blocked.
2012                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
2013                         }
2014                         break;
2015
2016                     case 4:  // There are three entries.
2017                         if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2])) {
2018                             // Return a blacklist match request blocked.
2019                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2],
2020                                     blackListEntry[3]};
2021                         }
2022                         break;
2023
2024                     case 5:  // There are four entries.
2025                         if (resourceUrl.contains(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2]) && resourceUrl.contains(blackListEntry[3])) {
2026                             // Return a blacklist match request blocked.
2027                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2] + "\n" +
2028                                     blackListEntry[3], blackListEntry[4]};
2029                         }
2030                         break;
2031                 }
2032             }
2033
2034             // Third-party initial black list.
2035             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_INITIAL_BLACKLIST))) {
2036                 if (blackListEntry.length == 2) {  // There is one entry.
2037                     if (resourceUrl.startsWith(blackListEntry[0])) {
2038                         // Return a blacklist match request blocked.
2039                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_INITIAL_BLACKLIST, blackListEntry[0], blackListEntry[1]};
2040                     }
2041                 } else {  // There are two entries.
2042                     if (resourceUrl.startsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
2043                         // Return a blacklist match request blocked.
2044                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
2045                     }
2046                 }
2047             }
2048
2049             // Third-party domain black list.
2050             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_DOMAIN_BLACKLIST))) {
2051                 if (blackListEntry.length == 3) {  // There is one entry.
2052                     if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1])) {
2053                         // Return a blacklist match request blocked.
2054                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
2055                     }
2056                 } else { // There are two entries.
2057                     if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.contains(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2])) {
2058                         // Return a blacklist match request blocked.
2059                         return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" + blackListEntry[2],
2060                                 blackListEntry[3]};
2061                     }
2062                 }
2063             }
2064
2065             // Third-party domain initial black list.
2066             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST))) {
2067                 switch (blackListEntry.length) {
2068                     case 3:  // There is one entry.
2069                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.startsWith(blackListEntry[1])) {
2070                             // Return a blacklist match request blocked.
2071                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
2072                         }
2073                         break;
2074
2075                     case 4:  // There are two entries.
2076                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.startsWith(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2])) {
2077                             // Return a blacklist match request blocked.
2078                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" +
2079                                     blackListEntry[2], blackListEntry[3]};
2080                         }
2081                         break;
2082
2083                     case 5:  // There are three entries.
2084                         if (currentDomain.endsWith(blackListEntry[0]) && resourceUrl.startsWith(blackListEntry[1]) && resourceUrl.contains(blackListEntry[2]) && resourceUrl.contains(blackListEntry[3])) {
2085                             // Return a blacklist match request blocked.
2086                             return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1] + "\n" +
2087                                     blackListEntry[2] + "\n" + blackListEntry[3], blackListEntry[4]};
2088                         }
2089                         break;
2090                 }
2091             }
2092
2093             // Third-party regular expression black list.
2094             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST))) {
2095                 if (Pattern.matches(blackListEntry[0], resourceUrl)) {
2096                     // Return a blacklist match request blocked.
2097                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST, blackListEntry[0], blackListEntry[1]};
2098                 }
2099             }
2100
2101             // Third-party domain regular expression black list.
2102             for (String[] blackListEntry : blockList.get(Integer.valueOf(THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST))) {
2103                 if (currentDomain.endsWith(blackListEntry[0]) && Pattern.matches(blackListEntry[1], resourceUrl)) {
2104                     // Return a blacklist match request blocked.
2105                     return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST, blackListEntry[0] + "\n" + blackListEntry[1], blackListEntry[2]};
2106                 }
2107             }
2108         }
2109
2110         // Regular expression black list.
2111         for (String[] blackListEntry : blockList.get(Integer.valueOf(REGULAR_EXPRESSION_BLACKLIST))) {
2112             if (Pattern.matches(blackListEntry[0], resourceUrl)) {
2113                 // Return a blacklist match request blocked.
2114                 return new String[] {REQUEST_BLOCKED, resourceUrl, BLOCK_LIST_NAME_STRING, REGULAR_EXPRESSION_BLACKLIST, blackListEntry[0], blackListEntry[1]};
2115             }
2116         }
2117
2118         // Return a no match request default.
2119         return new String[] {REQUEST_DEFAULT};
2120     }
2121 }