| OLD | NEW | 
|---|
| 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 27 matching lines...) Expand all  Loading... | 
| 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; | 
|  | 48 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; | 
| 48 | 49 | 
| 49 import android.content.Context; | 50 import android.content.Context; | 
| 50 import android.content.Intent; | 51 import android.content.Intent; | 
| 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; |  | 
| 61 import android.text.TextUtils; | 60 import android.text.TextUtils; | 
| 62 import android.util.Log; | 61 import android.util.Log; | 
| 63 | 62 | 
| 64 public final class Engine | 63 public final class Engine | 
| 65 { | 64 { | 
| 66   private static final String TAG = Engine.class.getSimpleName(); | 65   private static final String TAG = Engine.class.getSimpleName(); | 
| 67 | 66 | 
| 68   // TODO make use of this regex's | 67   // TODO make use of this regex's | 
| 69   public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( | 68   public static final Pattern RE_SUBSCRIPTION_HEADER = Pattern.compile( | 
| 70       "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); | 69       "\\[Adblock(?:\\s*Plus\\s*([\\d\\.]+)?)?\\]", Pattern.CASE_INSENSITIVE); | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 81   public static final String USER_FILTERS_TITLE = "__filters"; | 80   public static final String USER_FILTERS_TITLE = "__filters"; | 
| 82   public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; | 81   public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; | 
| 83 | 82 | 
| 84   public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser"; | 83   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"; | 84   public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse
     r.contentBlocker.ACTION_SETTING"; | 
| 86   public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte
     ntBlocker.ACTION_UPDATE"; | 85   public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte
     ntBlocker.ACTION_UPDATE"; | 
| 87   public static final String EASYLIST_URL = "https://easylist-downloads.adblockp
     lus.org/easylist.txt"; | 86   public static final String EASYLIST_URL = "https://easylist-downloads.adblockp
     lus.org/easylist.txt"; | 
| 88 | 87 | 
| 89   public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except
     ionsurl"; | 88   public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except
     ionsurl"; | 
| 90 | 89 | 
| 91   private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version
     "; |  | 
| 92 |  | 
| 93   // The value below specifies an interval of [x, 2*x[, where x = | 90   // The value below specifies an interval of [x, 2*x[, where x = | 
| 94   // INITIAL_UPDATE_CHECK_DELAY_SECONDS | 91   // INITIAL_UPDATE_CHECK_DELAY_SECONDS | 
| 95   private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; | 92   private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; | 
| 96   private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; | 93   private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; | 
| 97   private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; | 94   private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; | 
| 98 | 95 | 
| 99   public static final long MILLIS_PER_SECOND = 1000; | 96   public static final long MILLIS_PER_SECOND = 1000; | 
| 100   public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; | 97   public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; | 
| 101   public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; | 98   public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; | 
| 102   public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; | 99   public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; | 
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 222   boolean canUseInternet() | 219   boolean canUseInternet() | 
| 223   { | 220   { | 
| 224     final ConnectivityManager connManager = (ConnectivityManager) this.serviceCo
     ntext | 221     final ConnectivityManager connManager = (ConnectivityManager) this.serviceCo
     ntext | 
| 225         .getSystemService(Context.CONNECTIVITY_SERVICE); | 222         .getSystemService(Context.CONNECTIVITY_SERVICE); | 
| 226     final NetworkInfo current = connManager.getActiveNetworkInfo(); | 223     final NetworkInfo current = connManager.getActiveNetworkInfo(); | 
| 227     if (current == null) | 224     if (current == null) | 
| 228     { | 225     { | 
| 229       return false; | 226       return false; | 
| 230     } | 227     } | 
| 231 | 228 | 
| 232     final SharedPreferences prefs = PreferenceManager | 229     final boolean wifiOnly = "1".equals(SharedPrefsUtils.getString( | 
| 233         .getDefaultSharedPreferences(this.serviceContext); | 230         this.serviceContext, R.string.key_automatic_updates , "1")); | 
| 234     final boolean wifiOnly = prefs.getString( |  | 
| 235         this.serviceContext.getString(R.string.key_automatic_updates), "1").equa
     ls("1"); |  | 
| 236 | 231 | 
| 237     if (wifiOnly) | 232     if (wifiOnly) | 
| 238     { | 233     { | 
| 239       if (current.isConnected() && !current.isRoaming()) | 234       if (current.isConnected() && !current.isRoaming()) | 
| 240       { | 235       { | 
| 241         switch (current.getType()) | 236         switch (current.getType()) | 
| 242         { | 237         { | 
| 243           case ConnectivityManager.TYPE_BLUETOOTH: | 238           case ConnectivityManager.TYPE_BLUETOOTH: | 
| 244           case ConnectivityManager.TYPE_ETHERNET: | 239           case ConnectivityManager.TYPE_ETHERNET: | 
| 245           case ConnectivityManager.TYPE_WIFI: | 240           case ConnectivityManager.TYPE_WIFI: | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 280 | 275 | 
| 281   private void createAndWriteFile() | 276   private void createAndWriteFile() | 
| 282   { | 277   { | 
| 283     this.lock(); | 278     this.lock(); | 
| 284     try | 279     try | 
| 285     { | 280     { | 
| 286       Log.d(TAG, "Writing filters..."); | 281       Log.d(TAG, "Writing filters..."); | 
| 287       final File filterFile = this.subscriptions.createAndWriteFile(); | 282       final File filterFile = this.subscriptions.createAndWriteFile(); | 
| 288       writeWhitelistedWebsites(this.serviceContext, filterFile); | 283       writeWhitelistedWebsites(this.serviceContext, filterFile); | 
| 289 | 284 | 
| 290       final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen
     ces(this.serviceContext); | 285       SharedPrefsUtils.putString( | 
| 291       final String key = this.serviceContext.getString(R.string.key_cached_filte
     r_path); | 286           this.serviceContext, R.string.key_cached_filter_path, filterFile.getAb
     solutePath()); | 
| 292       prefs.edit().putString(key, filterFile.getAbsolutePath()).commit(); |  | 
| 293 | 287 | 
| 294       Log.d(TAG, "Cleaning up cache..."); | 288       Log.d(TAG, "Cleaning up cache..."); | 
| 295       final File dummyFile = getDummyFilterFile(this.serviceContext); | 289       final File dummyFile = getDummyFilterFile(this.serviceContext); | 
| 296       final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi
     les(); | 290       final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFi
     les(); | 
| 297       if (cacheDirFiles != null) | 291       if (cacheDirFiles != null) | 
| 298       { | 292       { | 
| 299         for (final File file : cacheDirFiles) | 293         for (final File file : cacheDirFiles) | 
| 300         { | 294         { | 
| 301           if (!file.equals(dummyFile) && !file.equals(filterFile)) | 295           if (!file.equals(dummyFile) && !file.equals(filterFile)) | 
| 302           { | 296           { | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 344   { | 338   { | 
| 345     return this.wasFirstRun; | 339     return this.wasFirstRun; | 
| 346   } | 340   } | 
| 347 | 341 | 
| 348   private void migrateFromPreviousVersion(final Context context) | 342   private void migrateFromPreviousVersion(final Context context) | 
| 349   { | 343   { | 
| 350     try | 344     try | 
| 351     { | 345     { | 
| 352       final int versionCode = context.getPackageManager().getPackageInfo(context
     .getPackageName(), | 346       final int versionCode = context.getPackageManager().getPackageInfo(context
     .getPackageName(), | 
| 353           0).versionCode; | 347           0).versionCode; | 
| 354       final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen
     ces(context); | 348 | 
| 355       int previous = prefs.getInt(PREFS_KEY_PREVIOUS_VERSION, 0); | 349       final int previousVersionCode = SharedPrefsUtils.getInt( | 
| 356       if (versionCode > previous) | 350           context, R.string.key_previous_version_code, 0); | 
|  | 351 | 
|  | 352       if (versionCode > previousVersionCode) | 
| 357       { | 353       { | 
| 358         if (previous > 0) | 354         if (previousVersionCode > 0) | 
| 359         { | 355         { | 
| 360           // We can do possible migration stuff here | 356           // We can do possible migration stuff here | 
| 361           // Currently we only persist the new version code | 357           // Currently we only persist the new version code | 
| 362         } | 358         } | 
| 363         prefs.edit().putInt(PREFS_KEY_PREVIOUS_VERSION, versionCode).commit(); | 359         SharedPrefsUtils.putInt(context, R.string.key_previous_version_code, ver
     sionCode); | 
| 364       } | 360       } | 
| 365     } | 361     } | 
| 366     catch (final Throwable t) | 362     catch (final Throwable t) | 
| 367     { | 363     { | 
| 368       Log.e(TAG, "Failed on migration, please clear all application data", t); | 364       Log.e(TAG, "Failed on migration, please clear all application data", t); | 
| 369     } | 365     } | 
| 370   } | 366   } | 
| 371 | 367 | 
| 372   static Engine create(final Context context) throws IOException | 368   static Engine create(final Context context) throws IOException | 
| 373   { | 369   { | 
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 508 | 504 | 
| 509   public static void writeFilterHeaders(Writer writer) throws IOException | 505   public static void writeFilterHeaders(Writer writer) throws IOException | 
| 510   { | 506   { | 
| 511     writer.write("[Adblock Plus 2.0]\n"); | 507     writer.write("[Adblock Plus 2.0]\n"); | 
| 512     writer.write("! This file was automatically created.\n"); | 508     writer.write("! This file was automatically created.\n"); | 
| 513   } | 509   } | 
| 514 | 510 | 
| 515   private static void writeWhitelistedWebsites(Context context, File filterFile)
      throws IOException | 511   private static void writeWhitelistedWebsites(Context context, File filterFile)
      throws IOException | 
| 516   { | 512   { | 
| 517     Log.d(TAG, "Writing whitelisted websites..."); | 513     Log.d(TAG, "Writing whitelisted websites..."); | 
| 518     final SharedPreferences prefs = |  | 
| 519         PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont
     ext()); |  | 
| 520     final String key = context.getString(R.string.key_whitelisted_websites); |  | 
| 521 |  | 
| 522     final Set<String> whitelistedWebsites = new TreeSet<>(); | 514     final Set<String> whitelistedWebsites = new TreeSet<>(); | 
| 523     whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty
     Set())); | 515     whitelistedWebsites.addAll(SharedPrefsUtils.getStringSet( | 
|  | 516         context, R.string.key_whitelisted_websites, Collections.<String>emptySet
     ())); | 
| 524 | 517 | 
| 525     try (final BufferedWriter w = new BufferedWriter( new OutputStreamWriter( | 518     try (final BufferedWriter w = new BufferedWriter( new OutputStreamWriter( | 
| 526         new FileOutputStream(filterFile, true), StandardCharsets.UTF_8))) | 519         new FileOutputStream(filterFile, true), StandardCharsets.UTF_8))) | 
| 527     { | 520     { | 
| 528       for (final String url : whitelistedWebsites) | 521       for (final String url : whitelistedWebsites) | 
| 529       { | 522       { | 
| 530         try | 523         try | 
| 531         { | 524         { | 
| 532           final URI uri = new URI(url); | 525           final URI uri = new URI(url); | 
| 533           final String host = uri.getHost() != null ? uri.getHost() : uri.getPat
     h(); | 526           final String host = uri.getHost() != null ? uri.getHost() : uri.getPat
     h(); | 
| 534           w.write("@@||" + host + "^$document"); | 527           w.write("@@||" + host + "^$document"); | 
| 535           w.write('\n'); | 528           w.write('\n'); | 
| 536         } | 529         } | 
| 537         catch (URISyntaxException e) | 530         catch (URISyntaxException e) | 
| 538         { | 531         { | 
| 539           Log.w(TAG, "Failed to parse whitelisted website: " + url); | 532           Log.w(TAG, "Failed to parse whitelisted website: " + url); | 
| 540           continue; | 533           continue; | 
| 541         } | 534         } | 
| 542       } | 535       } | 
| 543     } | 536     } | 
| 544   } | 537   } | 
| 545 | 538 | 
| 546   private static File getCachedFilterFile(Context context) | 539   private static File getCachedFilterFile(Context context) | 
| 547   { | 540   { | 
| 548     final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference
     s(context); | 541     final String cachedFilterPath = SharedPrefsUtils.getString( | 
| 549     final String cachedFilterPath = prefs.getString(context.getString(R.string.k
     ey_cached_filter_path), null); | 542         context, R.string.key_cached_filter_path, null); | 
|  | 543 | 
| 550     if (cachedFilterPath != null) | 544     if (cachedFilterPath != null) | 
| 551     { | 545     { | 
| 552       return new File(cachedFilterPath); | 546       return new File(cachedFilterPath); | 
| 553     } | 547     } | 
| 554 | 548 | 
| 555     return null; | 549     return null; | 
| 556   } | 550   } | 
| 557 | 551 | 
| 558   private static File getDummyFilterFile(Context context) | 552   private static File getDummyFilterFile(Context context) | 
| 559   { | 553   { | 
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 776       Log.d(TAG, headers.toString()); | 770       Log.d(TAG, headers.toString()); | 
| 777       this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), 
     headers); | 771       this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), 
     headers); | 
| 778     } | 772     } | 
| 779   } | 773   } | 
| 780 | 774 | 
| 781   public void connectivityChanged() | 775   public void connectivityChanged() | 
| 782   { | 776   { | 
| 783     this.downloader.connectivityChanged(); | 777     this.downloader.connectivityChanged(); | 
| 784   } | 778   } | 
| 785 } | 779 } | 
| OLD | NEW | 
|---|