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

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

Issue 29376835: Issue 4769 - Supporting adding whitelisted websites on ABP for Samsung Internet (Closed)
Patch Set: Created Feb. 22, 2017, 10:40 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.sbrowser.contentblocker.engine; 18 package org.adblockplus.sbrowser.contentblocker.engine;
19 19
20 import java.io.BufferedReader; 20 import java.io.BufferedReader;
21 import java.io.BufferedWriter; 21 import java.io.BufferedWriter;
22 import java.io.File; 22 import java.io.File;
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;
30 import java.net.URISyntaxException;
29 import java.net.URL; 31 import java.net.URL;
30 import java.net.URLEncoder; 32 import java.net.URLEncoder;
33 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList; 34 import java.util.ArrayList;
35 import java.util.Collections;
32 import java.util.HashMap; 36 import java.util.HashMap;
33 import java.util.List; 37 import java.util.List;
34 import java.util.Map; 38 import java.util.Map;
39 import java.util.Set;
40 import java.util.TreeSet;
35 import java.util.concurrent.LinkedBlockingQueue; 41 import java.util.concurrent.LinkedBlockingQueue;
36 import java.util.concurrent.TimeUnit; 42 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.locks.ReentrantLock; 43 import java.util.concurrent.locks.ReentrantLock;
38 import java.util.regex.Pattern; 44 import java.util.regex.Pattern;
39 45
40 import org.adblockplus.adblockplussbrowser.R; 46 import org.adblockplus.adblockplussbrowser.R;
41 47
42 import android.content.Context; 48 import android.content.Context;
43 import android.content.Intent; 49 import android.content.Intent;
44 import android.content.SharedPreferences; 50 import android.content.SharedPreferences;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 { 152 {
147 return activityContext.getPackageManager() 153 return activityContext.getPackageManager()
148 .queryIntentActivities(new Intent(ACTION_OPEN_SETTINGS), 0).size() > 0 ; 154 .queryIntentActivities(new Intent(ACTION_OPEN_SETTINGS), 0).size() > 0 ;
149 } 155 }
150 catch (final Throwable t) 156 catch (final Throwable t)
151 { 157 {
152 return false; 158 return false;
153 } 159 }
154 } 160 }
155 161
156 void requestUpdateBroadcast() 162 public void requestUpdateBroadcast()
157 { 163 {
158 this.lock(); 164 this.lock();
159 try 165 try
160 { 166 {
161 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY_MILLIS; 167 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT ION_DELAY_MILLIS;
162 } 168 }
163 finally 169 finally
164 { 170 {
165 this.unlock(); 171 this.unlock();
166 } 172 }
167 } 173 }
168 174
169 private void sendUpdateBroadcast() 175 private void writeFileAndSendUpdateBroadcast()
170 { 176 {
171 createAndWriteFile(); 177 createAndWriteFile();
172 178
173 runOnUiThread(new Runnable() 179 runOnUiThread(new Runnable()
174 { 180 {
175 @Override 181 @Override
176 public void run() 182 public void run()
177 { 183 {
178 final Intent intent = new Intent(); 184 final Intent intent = new Intent();
179 intent.setAction(ACTION_UPDATE); 185 intent.setAction(ACTION_UPDATE);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 { 241 {
236 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled)); 242 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled));
237 } 243 }
238 244
239 void downloadFinished(final String id, final int responseCode, final String re sponse, 245 void downloadFinished(final String id, final int responseCode, final String re sponse,
240 final Map<String, String> headers) 246 final Map<String, String> headers)
241 { 247 {
242 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers)); 248 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response, headers));
243 } 249 }
244 250
245 public void createAndWriteFile() 251 private void createAndWriteFile()
246 { 252 {
247 this.lock(); 253 this.lock();
248 try 254 try
249 { 255 {
250 Log.d(TAG, "Writing filters..."); 256 Log.d(TAG, "Writing filters...");
251 final File filterFile = this.subscriptions.createAndWriteFile(); 257 final File filterFile = this.subscriptions.createAndWriteFile();
258 writeWhitelistedWebsites(this.serviceContext, filterFile);
252 259
253 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen ces(this.serviceContext); 260 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen ces(this.serviceContext);
254 final String key = this.serviceContext.getString(R.string.key_cached_filte r_path); 261 final String key = this.serviceContext.getString(R.string.key_cached_filte r_path);
255 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit(); 262 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit();
256 263
257 Log.d(TAG, "Cleaning up cache..."); 264 Log.d(TAG, "Cleaning up cache...");
258 final File dummyFile = getDummyFilterFile(this.serviceContext); 265 final File dummyFile = getDummyFilterFile(this.serviceContext);
259 final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi les(); 266 final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi les();
260 if (cacheDirFiles != null) 267 if (cacheDirFiles != null)
261 { 268 {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 } 425 }
419 } 426 }
420 427
421 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions"); 428 Log.d(TAG, "Added " + additional + " additional default/built-in subscript ions");
422 engine.subscriptions.persistSubscriptions(); 429 engine.subscriptions.persistSubscriptions();
423 } 430 }
424 431
425 final File cachedFilterFile = getCachedFilterFile(context); 432 final File cachedFilterFile = getCachedFilterFile(context);
426 if (cachedFilterFile == null || !cachedFilterFile.exists()) 433 if (cachedFilterFile == null || !cachedFilterFile.exists())
427 { 434 {
428 engine.sendUpdateBroadcast(); 435 engine.writeFileAndSendUpdateBroadcast();
429 } 436 }
430 437
431 engine.handlerThread = new Thread(new EventHandler(engine)); 438 engine.handlerThread = new Thread(new EventHandler(engine));
432 engine.handlerThread.setDaemon(true); 439 engine.handlerThread.setDaemon(true);
433 engine.handlerThread.start(); 440 engine.handlerThread.start();
434 441
435 engine.downloader = Downloader.create(context, engine); 442 engine.downloader = Downloader.create(context, engine);
436 443
437 return engine; 444 return engine;
438 } 445 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 } 494 }
488 return dummyFilterFile; 495 return dummyFilterFile;
489 } 496 }
490 497
491 public static void writeFilterHeaders(Writer writer) throws IOException 498 public static void writeFilterHeaders(Writer writer) throws IOException
492 { 499 {
493 writer.write("[Adblock Plus 2.0]\n"); 500 writer.write("[Adblock Plus 2.0]\n");
494 writer.write("! This file was automatically created.\n"); 501 writer.write("! This file was automatically created.\n");
495 } 502 }
496 503
504 private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException
505 {
506 Log.d(TAG, "Writing whitelisted websites...");
507 final SharedPreferences prefs =
508 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont ext());
509 final String key = context.getString(R.string.key_whitelisted_websites);
510
511 final Set<String> whitelistedWebsites = new TreeSet<>();
512 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty Set()));
513
514 try (final BufferedWriter w = new BufferedWriter(
515 new OutputStreamWriter(new FileOutputStream(filterFile, true), StandardC harsets.UTF_8)))
516 {
517 for (final String url : whitelistedWebsites)
518 {
519 try
520 {
521 final URI uri = new URI(url);
522 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h();
523 w.write("@@||" + host + "^$document");
524 w.write('\n');
525 }
526 catch (URISyntaxException e)
527 {
528 Log.w(TAG, "Failed to parse whitelisted website: " + url);
529 continue;
530 }
531 }
532 }
533 }
534
497 private static File getCachedFilterFile(Context context) 535 private static File getCachedFilterFile(Context context)
498 { 536 {
499 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); 537 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context);
500 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); 538 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null);
501 if (cachedFilterPath != null) 539 if (cachedFilterPath != null)
502 { 540 {
503 return new File(cachedFilterPath); 541 return new File(cachedFilterPath);
504 } 542 }
505 543
506 return null; 544 return null;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 { 657 {
620 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL_MINUTES * MI LLIS_PER_MINUTE; 658 nextUpdateCheck = currentTime + UPDATE_CHECK_INTERVAL_MINUTES * MI LLIS_PER_MINUTE;
621 659
622 this.engine.subscriptions.checkForUpdates(); 660 this.engine.subscriptions.checkForUpdates();
623 } 661 }
624 662
625 if (currentTime > this.engine.nextUpdateBroadcast) 663 if (currentTime > this.engine.nextUpdateBroadcast)
626 { 664 {
627 this.engine.nextUpdateBroadcast = Long.MAX_VALUE; 665 this.engine.nextUpdateBroadcast = Long.MAX_VALUE;
628 Log.d(TAG, "Sending update broadcast"); 666 Log.d(TAG, "Sending update broadcast");
629 this.engine.sendUpdateBroadcast(); 667 this.engine.writeFileAndSendUpdateBroadcast();
630 } 668 }
631 } 669 }
632 finally 670 finally
633 { 671 {
634 engine.unlock(); 672 engine.unlock();
635 } 673 }
636 } 674 }
637 catch (final InterruptedException e) 675 catch (final InterruptedException e)
638 { 676 {
639 Log.d(TAG, "Handler interrupted", e); 677 Log.d(TAG, "Handler interrupted", e);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 Log.d(TAG, headers.toString()); 762 Log.d(TAG, headers.toString());
725 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); 763 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers);
726 } 764 }
727 } 765 }
728 766
729 public void connectivityChanged() 767 public void connectivityChanged()
730 { 768 {
731 this.downloader.connectivityChanged(); 769 this.downloader.connectivityChanged();
732 } 770 }
733 } 771 }
OLDNEW

Powered by Google App Engine
This is Rietveld