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

Side by Side Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java

Issue 29453722: Noissue - Lint adjustments and optimizations (Closed)
Patch Set: Created June 1, 2017, 9:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 26 matching lines...) Expand all
37 import java.util.List; 37 import java.util.List;
38 import java.util.Map; 38 import java.util.Map;
39 import java.util.Set; 39 import java.util.Set;
40 import java.util.TreeSet; 40 import java.util.TreeSet;
41 import java.util.concurrent.LinkedBlockingQueue; 41 import java.util.concurrent.LinkedBlockingQueue;
42 import java.util.concurrent.TimeUnit; 42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.locks.ReentrantLock; 43 import java.util.concurrent.locks.ReentrantLock;
44 import java.util.regex.Pattern; 44 import java.util.regex.Pattern;
45 45
46 import org.adblockplus.adblockplussbrowser.R; 46 import org.adblockplus.adblockplussbrowser.R;
47 import org.adblockplus.sbrowser.contentblocker.MainPreferences; 47 import org.adblockplus.sbrowser.contentblocker.MainPreferences;
jens 2017/06/02 10:50:01 Am I wrong, or is this an unused import?
diegocarloslima 2017/06/02 21:03:42 Yes, but this will be removed by the other NoIssue
jens 2017/06/06 09:39:55 Acknowledged.
48 48
49 import android.content.Context; 49 import android.content.Context;
50 import android.content.Intent; 50 import android.content.Intent;
51 import android.content.SharedPreferences; 51 import android.content.SharedPreferences;
52 import android.content.pm.PackageInfo; 52 import android.content.pm.PackageInfo;
53 import android.content.pm.PackageManager; 53 import android.content.pm.PackageManager;
54 import android.content.pm.ResolveInfo; 54 import android.content.pm.ResolveInfo;
55 import android.net.ConnectivityManager; 55 import android.net.ConnectivityManager;
56 import android.net.NetworkInfo; 56 import android.net.NetworkInfo;
57 import android.net.Uri; 57 import android.net.Uri;
58 import android.os.Handler; 58 import android.os.Handler;
59 import android.os.Looper; 59 import android.os.Looper;
60 import android.preference.PreferenceManager; 60 import android.preference.PreferenceManager;
61 import android.text.TextUtils; 61 import android.text.TextUtils;
62 import android.text.format.DateUtils;
62 import android.util.Log; 63 import android.util.Log;
63 64
64 public final class Engine 65 public final class Engine
65 { 66 {
66 private static final String TAG = Engine.class.getSimpleName(); 67 private static final String TAG = Engine.class.getSimpleName();
67 68
68 // TODO make use of this regex's 69 // TODO make use of this regex's
69 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( 70 public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile(
70 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); 71 "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE);
71 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)"); 72 public static final Pattern RE_FILTER_META = Pattern.compile("^\\s*!\\s*(\\w+) \\s*:\\s*(.*)");
72 public static final Pattern RE_FILTER_ELEMHIDE = Pattern 73 public static final Pattern RE_FILTER_ELEMHIDE = Pattern
73 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$"); 74 .compile("^([^\\/\\*\\|\\@\"!]*?)#(\\@)?(?:([\\w\\-]+|\\*)((?:\\([\\w\\-]+ (?:[$^*]?=[^\\(\\)\"]*)?\\))*)|#([^{}]+))$");
74 public static final Pattern RE_FILTER_REGEXP = Pattern 75 public static final Pattern RE_FILTER_REGEXP = Pattern
75 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$"); 76 .compile("^(@@)?\\/.*\\/(?:\\$~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:= [^,\\s]+)?)*)?$");
76 public static final Pattern RE_FILTER_OPTIONS = Pattern 77 public static final Pattern RE_FILTER_OPTIONS = Pattern
77 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ; 78 .compile("\\$(~?[\\w\\-]+(?:=[^,\\s]+)?(?:,~?[\\w\\-]+(?:=[^,\\s]+)?)*)$") ;
78 public static final Pattern RE_FILTER_CSSPROPERTY = Pattern 79 public static final Pattern RE_FILTER_CSSPROPERTY = Pattern
79 .compile("\\[\\-abp\\-properties=([\"'])([^\"']+)\\1\\]"); 80 .compile("\\[\\-abp\\-properties=([\"'])([^\"']+)\\1\\]");
80 81
81 public static final String USER_FILTERS_TITLE = "__filters"; 82 public static final String USER_FILTERS_TITLE = "__filters";
82 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; 83 public static final String USER_EXCEPTIONS_TITLE = "__exceptions";
83 84
84 public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser"; 85 public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser";
85 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING"; 86 private static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrows er.contentBlocker.ACTION_SETTING";
86 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE"; 87 private static final String ACTION_UPDATE = "com.samsung.android.sbrowser.cont entBlocker.ACTION_UPDATE";
87 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt"; 88 private static final String EASYLIST_URL = "https://easylist-downloads.adblock plus.org/easylist.txt";
88 89
89 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl"; 90 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl";
90 91
91 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version "; 92 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version ";
92 93
93 // The value below specifies an interval of [x, 2*x[, where x = 94 // The value below specifies an interval of [x, 2*x[, where x =
94 // INITIAL_UPDATE_CHECK_DELAY_SECONDS 95 // INITIAL_UPDATE_CHECK_DELAY
95 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; 96 private static final long INITIAL_UPDATE_CHECK_DELAY = 5 * DateUtils.SECOND_IN _MILLIS;
96 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; 97 private static final long UPDATE_CHECK_INTERVAL = 30 * DateUtils.MINUTE_IN_MIL LIS;
97 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; 98 private static final long BROADCAST_COMBINATION_DELAY = 2500;
98
99 public static final long MILLIS_PER_SECOND = 1000;
100 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
101 public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
102 public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
103 99
104 private static final int NO_FLAG = 0; 100 private static final int NO_FLAG = 0;
105 private static final int OLDEST_SAMSUNG_INTERNET_5_VERSIONCODE = 500000000; 101 private static final int OLDEST_SAMSUNG_INTERNET_5_VERSIONCODE = 500000000;
106 102
107 private final ReentrantLock accessLock = new ReentrantLock(); 103 private final ReentrantLock accessLock = new ReentrantLock();
108 private DefaultSubscriptions defaultSubscriptions; 104 private DefaultSubscriptions defaultSubscriptions;
109 private Subscriptions subscriptions; 105 private Subscriptions subscriptions;
110 private JSONPrefs jsonPrefs; 106 private JSONPrefs jsonPrefs;
111 private AppInfo appInfo; 107 private AppInfo appInfo;
112 private LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueu e<>(); 108 private final LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlocki ngQueue<>();
113 private Thread handlerThread; 109 private Thread handlerThread;
114 private Downloader downloader; 110 private Downloader downloader;
115 private final Context serviceContext; 111 private final Context serviceContext;
116 private boolean wasFirstRun = false; 112 private boolean wasFirstRun = false;
117 private long nextUpdateBroadcast = Long.MAX_VALUE; 113 private long nextUpdateBroadcast = Long.MAX_VALUE;
118 114
119 private Engine(final Context context) 115 private Engine(final Context context)
120 { 116 {
121 this.serviceContext = context; 117 this.serviceContext = context;
122 } 118 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 Log.d(TAG, "No compatible Samsung Browser found.", e); 183 Log.d(TAG, "No compatible Samsung Browser found.", e);
188 return false; 184 return false;
189 } 185 }
190 } 186 }
191 187
192 public void requestUpdateBroadcast() 188 public void requestUpdateBroadcast()
193 { 189 {
194 this.lock(); 190 this.lock();
195 try 191 try
196 { 192 {
197 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY_MILLIS; 193 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY;
198 } 194 }
199 finally 195 finally
200 { 196 {
201 this.unlock(); 197 this.unlock();
202 } 198 }
203 } 199 }
204 200
205 private void writeFileAndSendUpdateBroadcast() 201 private void writeFileAndSendUpdateBroadcast()
206 { 202 {
207 createAndWriteFile(); 203 createAndWriteFile();
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 } 432 }
437 433
438 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions"); 434 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions");
439 engine.subscriptions.persistSubscriptions(); 435 engine.subscriptions.persistSubscriptions();
440 } 436 }
441 437
442 engine.handlerThread = new Thread(new EventHandler(engine)); 438 engine.handlerThread = new Thread(new EventHandler(engine));
443 engine.handlerThread.setDaemon(true); 439 engine.handlerThread.setDaemon(true);
444 engine.handlerThread.start(); 440 engine.handlerThread.start();
445 441
446 engine.downloader = Downloader.create(context, engine); 442 engine.downloader = Downloader.create(engine);
447 443
448 final File cachedFilterFile = getCachedFilterFile(context); 444 final File cachedFilterFile = getCachedFilterFile(context);
449 if (cachedFilterFile == null || !cachedFilterFile.exists()) 445 if (cachedFilterFile == null || !cachedFilterFile.exists())
450 { 446 {
451 engine.writeFileAndSendUpdateBroadcast(); 447 engine.writeFileAndSendUpdateBroadcast();
452 } 448 }
453 449
454 return engine; 450 return engine;
455 } 451 }
456 452
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 try 526 try
531 { 527 {
532 final URI uri = new URI(url); 528 final URI uri = new URI(url);
533 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h(); 529 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h();
534 w.write("@@||" + host + "^$document"); 530 w.write("@@||" + host + "^$document");
535 w.write('\n'); 531 w.write('\n');
536 } 532 }
537 catch (URISyntaxException e) 533 catch (URISyntaxException e)
538 { 534 {
539 Log.w(TAG, "Failed to parse whitelisted website: " + url); 535 Log.w(TAG, "Failed to parse whitelisted website: " + url);
540 continue; 536 continue;
jens 2017/06/02 10:50:01 I think this continue statement can be deleted.
diegocarloslima 2017/06/02 21:03:42 Yeah, this one will be removed in the cleanup Nois
jens 2017/06/06 09:39:55 Acknowledged.
541 } 537 }
542 } 538 }
543 } 539 }
544 } 540 }
545 541
546 private static File getCachedFilterFile(Context context) 542 private static File getCachedFilterFile(Context context)
547 { 543 {
548 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); 544 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context);
549 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); 545 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null);
550 if (cachedFilterPath != null) 546 if (cachedFilterPath != null)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 { 617 {
622 this.engine = engine; 618 this.engine = engine;
623 } 619 }
624 620
625 @Override 621 @Override
626 public void run() 622 public void run()
627 { 623 {
628 Log.d(TAG, "Handler thread started"); 624 Log.d(TAG, "Handler thread started");
629 boolean interrupted = false; 625 boolean interrupted = false;
630 long nextUpdateCheck = System.currentTimeMillis() 626 long nextUpdateCheck = System.currentTimeMillis()
631 + (long) ((1 + Math.random()) * INITIAL_UPDATE_CHECK_DELAY_SECONDS * M ILLIS_PER_SECOND); 627 + (long) ((1 + Math.random()) * INITIAL_UPDATE_CHECK_DELAY);
632 while (!interrupted) 628 while (!interrupted)
633 { 629 {
634 try 630 try
635 { 631 {
636 final EngineEvent event = this.engine.engineEvents.poll(100, TimeUnit. MILLISECONDS); 632 final EngineEvent event = this.engine.engineEvents.poll(100, TimeUnit. MILLISECONDS);
637 engine.lock(); 633 engine.lock();
638 try 634 try
639 { 635 {
640 if (event != null) 636 if (event != null)
641 { 637 {
(...skipping 17 matching lines...) Expand all
659 } 655 }
660 default: 656 default:
661 Log.d(TAG, "Unhandled type: " + event.getType()); 657 Log.d(TAG, "Unhandled type: " + event.getType());
662 break; 658 break;
663 } 659 }
664 } 660 }
665 661
666 final long currentTime = System.currentTimeMillis(); 662 final long currentTime = System.currentTimeMillis();
667 if (currentTime > nextUpdateCheck) 663 if (currentTime > nextUpdateCheck)
668 { 664 {
669 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL_MINUTES * MI LLIS_PER_MINUTE; 665 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL;
670 666
671 this.engine.subscriptions.checkForUpdates(); 667 this.engine.subscriptions.checkForUpdates();
672 } 668 }
673 669
674 if (currentTime > this.engine.nextUpdateBroadcast) 670 if (currentTime > this.engine.nextUpdateBroadcast)
675 { 671 {
676 this.engine.nextUpdateBroadcast = Long.MAX_VALUE; 672 this.engine.nextUpdateBroadcast = Long.MAX_VALUE;
677 Log.d(TAG, "Sending update broadcast"); 673 Log.d(TAG, "Sending update broadcast");
678 this.engine.writeFileAndSendUpdateBroadcast(); 674 this.engine.writeFileAndSendUpdateBroadcast();
679 } 675 }
(...skipping 21 matching lines...) Expand all
701 { 697 {
702 public enum EngineEventType 698 public enum EngineEventType
703 { 699 {
704 CHANGE_ENABLED_STATE, 700 CHANGE_ENABLED_STATE,
705 FORCE_DOWNLOAD, 701 FORCE_DOWNLOAD,
706 DOWNLOAD_FINISHED 702 DOWNLOAD_FINISHED
707 } 703 }
708 704
709 private final EngineEventType type; 705 private final EngineEventType type;
710 706
711 protected EngineEvent(final EngineEventType type) 707 EngineEvent(final EngineEventType type)
712 { 708 {
713 this.type = type; 709 this.type = type;
714 } 710 }
715 711
716 public EngineEventType getType() 712 public EngineEventType getType()
717 { 713 {
718 return this.type; 714 return this.type;
719 } 715 }
720 } 716 }
721 717
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 Log.d(TAG, headers.toString()); 772 Log.d(TAG, headers.toString());
777 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); 773 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
778 } 774 }
779 } 775 }
780 776
781 public void connectivityChanged() 777 public void connectivityChanged()
782 { 778 {
783 this.downloader.connectivityChanged(); 779 this.downloader.connectivityChanged();
784 } 780 }
785 } 781 }
OLDNEW

Powered by Google App Engine
This is Rietveld