| 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 24 matching lines...) Expand all Loading... |
| 35 { | 35 { |
| 36 private static final String TAG = Utils.getTag(AdblockHelper.class); | 36 private static final String TAG = Utils.getTag(AdblockHelper.class); |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Suggested preference name | 39 * Suggested preference name |
| 40 */ | 40 */ |
| 41 public static final String PREFERENCE_NAME = "ADBLOCK"; | 41 public static final String PREFERENCE_NAME = "ADBLOCK"; |
| 42 private static AdblockHelper _instance; | 42 private static AdblockHelper _instance; |
| 43 | 43 |
| 44 private Context context; | 44 private Context context; |
| 45 private String basePath; |
| 45 private boolean developmentBuild; | 46 private boolean developmentBuild; |
| 46 private String preferenceName; | 47 private String preferenceName; |
| 47 private AdblockEngine engine; | 48 private AdblockEngine engine; |
| 48 private AdblockSettingsStorage storage; | 49 private AdblockSettingsStorage storage; |
| 49 private CountDownLatch engineCreated; | 50 private CountDownLatch engineCreated; |
| 50 | 51 |
| 51 /* | 52 /* |
| 52 Simple ARC management for AdblockEngine | 53 Simple ARC management for AdblockEngine |
| 53 Use `retain` and `release` | 54 Use `retain` and `release` |
| 54 */ | 55 */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 } | 82 } |
| 82 | 83 |
| 83 public AdblockSettingsStorage getStorage() | 84 public AdblockSettingsStorage getStorage() |
| 84 { | 85 { |
| 85 return storage; | 86 return storage; |
| 86 } | 87 } |
| 87 | 88 |
| 88 /** | 89 /** |
| 89 * Init with context | 90 * Init with context |
| 90 * @param context application context | 91 * @param context application context |
| 92 * @param basePath file system root to store files |
| 93 * |
| 94 * Adblock Plus library will download subscription files and s
tore them on |
| 95 * the path passed. The path should exist and the directory co
ntent should not be |
| 96 * cleared out occasionally. Using `context.getCacheDir().getA
bsolutePath()` is not |
| 97 * recommended because it can be cleared by the system. |
| 91 * @param developmentBuild debug or release? | 98 * @param developmentBuild debug or release? |
| 92 * @param preferenceName Shared Preferences name | 99 * @param preferenceName Shared Preferences name |
| 93 */ | 100 */ |
| 94 public void init(Context context, boolean developmentBuild, String preferenceN
ame) | 101 public void init(Context context, String basePath, boolean developmentBuild, S
tring preferenceName) |
| 95 { | 102 { |
| 96 this.context = context.getApplicationContext(); | 103 this.context = context.getApplicationContext(); |
| 104 this.basePath = basePath; |
| 97 this.developmentBuild = developmentBuild; | 105 this.developmentBuild = developmentBuild; |
| 98 this.preferenceName = preferenceName; | 106 this.preferenceName = preferenceName; |
| 99 } | 107 } |
| 100 | 108 |
| 101 private void createAdblock() | 109 private void createAdblock() |
| 102 { | 110 { |
| 103 Log.d(TAG, "Creating adblock engine ..."); | 111 Log.d(TAG, "Creating adblock engine ..."); |
| 104 | 112 |
| 105 // read and apply current settings | 113 // read and apply current settings |
| 106 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte
xt.MODE_PRIVATE); | 114 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte
xt.MODE_PRIVATE); |
| 107 storage = new SharedPrefsStorage(prefs); | 115 storage = new SharedPrefsStorage(prefs); |
| 108 | 116 |
| 109 // latch is required for async (see `waitForReady()`) | 117 // latch is required for async (see `waitForReady()`) |
| 110 engineCreated = new CountDownLatch(1); | 118 engineCreated = new CountDownLatch(1); |
| 111 | 119 |
| 112 engine = AdblockEngine.create( | 120 engine = AdblockEngine.create( |
| 113 AdblockEngine.generateAppInfo(context, developmentBuild), | 121 AdblockEngine.generateAppInfo(context, developmentBuild), |
| 114 context.getCacheDir().getAbsolutePath(), | 122 basePath, true); // `true` as we need element hiding |
| 115 true); // `true` as we need element hiding | |
| 116 Log.d(TAG, "AdblockHelper engine created"); | 123 Log.d(TAG, "AdblockHelper engine created"); |
| 117 | 124 |
| 118 AdblockSettings settings = storage.load(); | 125 AdblockSettings settings = storage.load(); |
| 119 if (settings != null) | 126 if (settings != null) |
| 120 { | 127 { |
| 121 Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 128 Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
| 122 // apply last saved settings to adblock engine | 129 // apply last saved settings to adblock engine |
| 123 | 130 |
| 124 // all the settings except `enabled` and whitelisted domains are saved by
adblock engine itself | 131 // all the settings except `enabled` and whitelisted domains are saved by
adblock engine itself |
| 125 engine.setEnabled(settings.isAdblockEnabled()); | 132 engine.setEnabled(settings.isAdblockEnabled()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 */ | 220 */ |
| 214 public synchronized void release() | 221 public synchronized void release() |
| 215 { | 222 { |
| 216 if (referenceCounter.decrementAndGet() == 0) | 223 if (referenceCounter.decrementAndGet() == 0) |
| 217 { | 224 { |
| 218 waitForReady(); | 225 waitForReady(); |
| 219 disposeAdblock(); | 226 disposeAdblock(); |
| 220 } | 227 } |
| 221 } | 228 } |
| 222 } | 229 } |
| OLD | NEW |