Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java

Issue 29376835: Issue 4769 - Supporting adding whitelisted websites on ABP for Samsung Internet (Closed)
Left Patch Set: Created Feb. 22, 2017, 10:40 p.m.
Right Patch Set: Adjusting Russian string Created March 15, 2017, 4:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 12 matching lines...) Expand all
23 import java.io.FileOutputStream; 23 import java.io.FileOutputStream;
24 import java.io.IOException; 24 import java.io.IOException;
25 import java.io.InputStream; 25 import java.io.InputStream;
26 import java.io.InputStreamReader; 26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter; 27 import java.io.OutputStreamWriter;
28 import java.io.Writer; 28 import java.io.Writer;
29 import java.net.URI; 29 import java.net.URI;
30 import java.net.URISyntaxException; 30 import java.net.URISyntaxException;
31 import java.net.URL; 31 import java.net.URL;
32 import java.net.URLEncoder; 32 import java.net.URLEncoder;
33 import java.nio.charset.StandardCharsets;
34 import java.util.ArrayList; 33 import java.util.ArrayList;
35 import java.util.Collections; 34 import java.util.Collections;
36 import java.util.HashMap; 35 import java.util.HashMap;
37 import java.util.List; 36 import java.util.List;
38 import java.util.Map; 37 import java.util.Map;
39 import java.util.Set; 38 import java.util.Set;
40 import java.util.TreeSet; 39 import java.util.TreeSet;
41 import java.util.concurrent.LinkedBlockingQueue; 40 import java.util.concurrent.LinkedBlockingQueue;
42 import java.util.concurrent.TimeUnit; 41 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.locks.ReentrantLock; 42 import java.util.concurrent.locks.ReentrantLock;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 75
77 public static final String USER_FILTERS_TITLE = "__filters"; 76 public static final String USER_FILTERS_TITLE = "__filters";
78 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; 77 public static final String USER_EXCEPTIONS_TITLE = "__exceptions";
79 78
80 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING"; 79 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING";
81 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE"; 80 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE";
82 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt"; 81 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt";
83 82
84 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl"; 83 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl";
85 84
86 private static final String URL_ENCODE_CHARSET = "UTF-8"; 85 public static final String CHARSET_UTF_8 = "UTF-8";
87 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version "; 86 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version ";
88 87
89 // The value below specifies an interval of [x, 2*x[, where x = 88 // The value below specifies an interval of [x, 2*x[, where x =
90 // INITIAL_UPDATE_CHECK_DELAY_SECONDS 89 // INITIAL_UPDATE_CHECK_DELAY_SECONDS
91 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; 90 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5;
92 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; 91 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30;
93 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; 92 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500;
94 93
95 public static final long MILLIS_PER_SECOND = 1000; 94 public static final long MILLIS_PER_SECOND = 1000;
96 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; 95 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 engine.handlerThread.start(); 439 engine.handlerThread.start();
441 440
442 engine.downloader = Downloader.create(context, engine); 441 engine.downloader = Downloader.create(context, engine);
443 442
444 return engine; 443 return engine;
445 } 444 }
446 445
447 public static String readFileAsString(InputStream instream) throws IOException 446 public static String readFileAsString(InputStream instream) throws IOException
448 { 447 {
449 final StringBuilder sb = new StringBuilder(); 448 final StringBuilder sb = new StringBuilder();
450 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 449 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
451 for (int ch = r.read(); ch != -1; ch = r.read()) 450 for (int ch = r.read(); ch != -1; ch = r.read())
452 { 451 {
453 sb.append((char) ch); 452 sb.append((char) ch);
454 } 453 }
455 return sb.toString(); 454 return sb.toString();
456 } 455 }
457 456
458 public static List<String> readLines(InputStream instream) throws IOException 457 public static List<String> readLines(InputStream instream) throws IOException
459 { 458 {
460 final ArrayList<String> list = new ArrayList<String>(); 459 final ArrayList<String> list = new ArrayList<String>();
461 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 460 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8));
462 for (String line = r.readLine(); line != null; line = r.readLine()) 461 for (String line = r.readLine(); line != null; line = r.readLine())
463 { 462 {
464 list.add(line); 463 list.add(line);
465 } 464 }
466 return list; 465 return list;
467 } 466 }
468 467
469 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion 468 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion
470 { 469 {
471 final File cachedFilterFile = getCachedFilterFile(context); 470 final File cachedFilterFile = getCachedFilterFile(context);
472 if (cachedFilterFile != null && cachedFilterFile.exists()) 471 if (cachedFilterFile != null && cachedFilterFile.exists())
473 { 472 {
474 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); 473 Log.d(TAG, "Cached filter file found: " + cachedFilterFile);
475 return cachedFilterFile; 474 return cachedFilterFile;
476 } 475 }
477 476
478 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); 477 Log.d(TAG, "Cached filter file not found. Using dummy filter file");
479 final File dummyFilterFile = getDummyFilterFile(context); 478 final File dummyFilterFile = getDummyFilterFile(context);
480 if (!dummyFilterFile.exists()) 479 if (!dummyFilterFile.exists())
481 { 480 {
482 Log.d(TAG, "Creating dummy filter file..."); 481 Log.d(TAG, "Creating dummy filter file...");
483 dummyFilterFile.getParentFile().mkdirs(); 482 dummyFilterFile.getParentFile().mkdirs();
484 final BufferedWriter writer = new BufferedWriter( 483 final BufferedWriter writer = new BufferedWriter(
485 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), "UTF-8") ); 484 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), CHARSET_ UTF_8));
486 try 485 try
487 { 486 {
488 writeFilterHeaders(writer); 487 writeFilterHeaders(writer);
489 } 488 }
490 finally 489 finally
491 { 490 {
492 writer.close(); 491 writer.close();
493 } 492 }
494 } 493 }
495 return dummyFilterFile; 494 return dummyFilterFile;
496 } 495 }
497 496
498 public static void writeFilterHeaders(Writer writer) throws IOException 497 public static void writeFilterHeaders(Writer writer) throws IOException
499 { 498 {
500 writer.write("[Adblock Plus 2.0]\n"); 499 writer.write("[Adblock Plus 2.0]\n");
501 writer.write("! This file was automatically created.\n"); 500 writer.write("! This file was automatically created.\n");
502 } 501 }
503 502
504 private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException 503 private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException
505 { 504 {
506 Log.d(TAG, "Writing whitelisted websites..."); 505 Log.d(TAG, "Writing whitelisted websites...");
507 final SharedPreferences prefs = 506 final SharedPreferences prefs =
508 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont ext()); 507 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont ext());
509 final String key = context.getString(R.string.key_whitelisted_websites); 508 final String key = context.getString(R.string.key_whitelisted_websites);
510 509
511 final Set<String> whitelistedWebsites = new TreeSet<>(); 510 final Set<String> whitelistedWebsites = new TreeSet<String>();
512 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty Set())); 511 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty Set()));
513 512
514 try (final BufferedWriter w = new BufferedWriter( 513 final BufferedWriter w = new BufferedWriter(
515 new OutputStreamWriter(new FileOutputStream(filterFile, true), StandardC harsets.UTF_8))) 514 new OutputStreamWriter(new FileOutputStream(filterFile, true), CHARSET_U TF_8));
515 try
516 { 516 {
517 for (final String url : whitelistedWebsites) 517 for (final String url : whitelistedWebsites)
518 { 518 {
519 try 519 try
520 { 520 {
521 final URI uri = new URI(url); 521 final URI uri = new URI(url);
522 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h(); 522 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h();
523 w.write("@@||" + host + "^$document"); 523 w.write("@@||" + host + "^$document");
524 w.write('\n'); 524 w.write('\n');
525 } 525 }
526 catch (URISyntaxException e) 526 catch (URISyntaxException e)
527 { 527 {
528 Log.w(TAG, "Failed to parse whitelisted website: " + url); 528 Log.w(TAG, "Failed to parse whitelisted website: " + url);
529 continue; 529 continue;
530 } 530 }
531 } 531 }
532 } 532 }
533 finally
534 {
535 w.close();
536 }
533 } 537 }
534 538
535 private static File getCachedFilterFile(Context context) 539 private static File getCachedFilterFile(Context context)
536 { 540 {
537 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); 541 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context);
538 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); 542 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null);
539 if (cachedFilterPath != null) 543 if (cachedFilterPath != null)
540 { 544 {
541 return new File(cachedFilterPath); 545 return new File(cachedFilterPath);
542 } 546 }
(...skipping 24 matching lines...) Expand all
567 if (sub.getURL().getQuery() != null) 571 if (sub.getURL().getQuery() != null)
568 { 572 {
569 sb.append('&'); 573 sb.append('&');
570 } 574 }
571 else 575 else
572 { 576 {
573 sb.append('?'); 577 sb.append('?');
574 } 578 }
575 579
576 sb.append("addonName="); 580 sb.append("addonName=");
577 sb.append(URLEncoder.encode(this.appInfo.addonName, URL_ENCODE_CHARSET)); 581 sb.append(URLEncoder.encode(this.appInfo.addonName, CHARSET_UTF_8));
578 sb.append("&addonVersion="); 582 sb.append("&addonVersion=");
579 sb.append(URLEncoder.encode(this.appInfo.addonVersion, URL_ENCODE_CHARSET)); 583 sb.append(URLEncoder.encode(this.appInfo.addonVersion, CHARSET_UTF_8));
580 sb.append("&application="); 584 sb.append("&application=");
581 sb.append(URLEncoder.encode(this.appInfo.application, URL_ENCODE_CHARSET)); 585 sb.append(URLEncoder.encode(this.appInfo.application, CHARSET_UTF_8));
582 sb.append("&applicationVersion="); 586 sb.append("&applicationVersion=");
583 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, URL_ENCODE_CHAR SET)); 587 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, CHARSET_UTF_8)) ;
584 sb.append("&platform="); 588 sb.append("&platform=");
585 sb.append(URLEncoder.encode(this.appInfo.platform, URL_ENCODE_CHARSET)); 589 sb.append(URLEncoder.encode(this.appInfo.platform, CHARSET_UTF_8));
586 sb.append("&platformVersion="); 590 sb.append("&platformVersion=");
587 sb.append(URLEncoder.encode(this.appInfo.platformVersion, URL_ENCODE_CHARSET )); 591 sb.append(URLEncoder.encode(this.appInfo.platformVersion, CHARSET_UTF_8));
588 sb.append("&lastVersion="); 592 sb.append("&lastVersion=");
589 sb.append(sub.getVersion()); 593 sb.append(sub.getVersion());
590 sb.append("&downloadCount="); 594 sb.append("&downloadCount=");
591 final long downloadCount = sub.getDownloadCount(); 595 final long downloadCount = sub.getDownloadCount();
592 if (downloadCount < 5) 596 if (downloadCount < 5)
593 { 597 {
594 sb.append(downloadCount); 598 sb.append(downloadCount);
595 } 599 }
596 else 600 else
597 { 601 {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 Log.d(TAG, headers.toString()); 766 Log.d(TAG, headers.toString());
763 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); 767 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
764 } 768 }
765 } 769 }
766 770
767 public void connectivityChanged() 771 public void connectivityChanged()
768 { 772 {
769 this.downloader.connectivityChanged(); 773 this.downloader.connectivityChanged();
770 } 774 }
771 } 775 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld