| Left: | ||
| Right: |
| 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 |
| 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.libadblockplus.android.settings; | 18 package org.adblockplus.libadblockplus.android.settings; |
| 19 | 19 |
| 20 import android.content.Context; | 20 import android.content.Context; |
| 21 import android.content.SharedPreferences; | 21 import android.content.SharedPreferences; |
| 22 import android.util.Log; | 22 import android.util.Log; |
| 23 | 23 |
| 24 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; | |
| 25 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | |
| 24 import org.adblockplus.libadblockplus.android.AdblockEngine; | 26 import org.adblockplus.libadblockplus.android.AdblockEngine; |
| 25 import org.adblockplus.libadblockplus.android.Utils; | 27 import org.adblockplus.libadblockplus.android.Utils; |
| 26 | 28 |
| 27 import java.util.concurrent.CountDownLatch; | 29 import java.util.concurrent.CountDownLatch; |
| 28 import java.util.concurrent.atomic.AtomicInteger; | 30 import java.util.concurrent.atomic.AtomicInteger; |
| 29 | 31 |
| 30 /** | 32 /** |
| 31 * AdblockHelper shared resources | 33 * AdblockHelper shared resources |
| 32 * (singleton) | 34 * (singleton) |
| 33 */ | 35 */ |
| 34 public class AdblockHelper | 36 public class AdblockHelper |
| 35 { | 37 { |
| 36 private static final String TAG = Utils.getTag(AdblockHelper.class); | 38 private static final String TAG = Utils.getTag(AdblockHelper.class); |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Suggested preference name | 41 * Suggested preference name |
| 40 */ | 42 */ |
| 41 public static final String PREFERENCE_NAME = "ADBLOCK"; | 43 public static final String PREFERENCE_NAME = "ADBLOCK"; |
| 42 private static AdblockHelper _instance; | 44 private static AdblockHelper _instance; |
| 43 | 45 |
| 44 private Context context; | 46 private Context context; |
| 45 private boolean developmentBuild; | 47 private boolean developmentBuild; |
| 46 private String preferenceName; | 48 private String preferenceName; |
| 47 private AdblockEngine engine; | 49 private AdblockEngine engine; |
| 48 private AdblockSettingsStorage storage; | 50 private AdblockSettingsStorage storage; |
| 49 private CountDownLatch engineCreated; | 51 private CountDownLatch engineCreated; |
| 50 | 52 |
| 53 private IsAllowedConnectionCallback isAllowedConnectionCallback; | |
| 54 | |
| 51 /* | 55 /* |
| 52 Simple ARC management for AdblockEngine | 56 Simple ARC management for AdblockEngine |
| 53 Use `retain` and `release` | 57 Use `retain` and `release` |
| 54 */ | 58 */ |
| 55 | 59 |
| 56 private AtomicInteger referenceCounter = new AtomicInteger(0); | 60 private AtomicInteger referenceCounter = new AtomicInteger(0); |
| 57 | 61 |
| 58 // singleton | 62 // singleton |
| 59 protected AdblockHelper() | 63 protected AdblockHelper() |
| 60 { | 64 { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 */ | 97 */ |
| 94 public void init(Context context, boolean developmentBuild, String preferenceN ame) | 98 public void init(Context context, boolean developmentBuild, String preferenceN ame) |
| 95 { | 99 { |
| 96 this.context = context.getApplicationContext(); | 100 this.context = context.getApplicationContext(); |
| 97 this.developmentBuild = developmentBuild; | 101 this.developmentBuild = developmentBuild; |
| 98 this.preferenceName = preferenceName; | 102 this.preferenceName = preferenceName; |
| 99 } | 103 } |
| 100 | 104 |
| 101 private void createAdblock() | 105 private void createAdblock() |
| 102 { | 106 { |
| 107 this.isAllowedConnectionCallback = new IsAllowedConnectionCallbackImpl(conte xt); | |
| 108 | |
| 103 Log.d(TAG, "Creating adblock engine ..."); | 109 Log.d(TAG, "Creating adblock engine ..."); |
| 104 | 110 |
| 105 // read and apply current settings | 111 // read and apply current settings |
| 106 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte xt.MODE_PRIVATE); | 112 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte xt.MODE_PRIVATE); |
| 107 storage = new SharedPrefsStorage(prefs); | 113 storage = new SharedPrefsStorage(prefs); |
| 108 | 114 |
| 109 // latch is required for async (see `waitForReady()`) | 115 // latch is required for async (see `waitForReady()`) |
| 110 engineCreated = new CountDownLatch(1); | 116 engineCreated = new CountDownLatch(1); |
| 111 | 117 |
| 112 engine = AdblockEngine.create( | 118 engine = AdblockEngine.create( |
| 113 AdblockEngine.generateAppInfo(context, developmentBuild), | 119 AdblockEngine.generateAppInfo(context, developmentBuild), |
| 114 context.getCacheDir().getAbsolutePath(), | 120 context.getCacheDir().getAbsolutePath(), |
| 115 true); // `true` as we need element hiding | 121 true, |
| 116 Log.d(TAG, "AdblockHelper engine created"); | 122 isAllowedConnectionCallback, |
| 123 null, | |
|
Felix Dahlke
2017/03/24 07:37:24
How I saw it, there is a version of create where y
anton
2017/03/24 09:43:10
Actually you're right and i can just use the versi
| |
| 124 null, | |
| 125 null, | |
| 126 null); // `true` as we need element hiding | |
|
Felix Dahlke
2017/03/24 07:37:24
Nit: Seems like this comment should move up now, s
anton
2017/03/24 09:43:10
Acknowledged.
| |
| 127 Log.d(TAG, "Adblock engine created"); | |
| 117 | 128 |
| 118 AdblockSettings settings = storage.load(); | 129 AdblockSettings settings = storage.load(); |
| 119 if (settings != null) | 130 if (settings != null) |
| 120 { | 131 { |
| 121 Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 132 Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
| 122 // apply last saved settings to adblock engine | 133 // apply last saved settings to adblock engine |
| 123 | 134 |
| 124 // all the settings except `enabled` and whitelisted domains are saved by adblock engine itself | 135 // all the settings except `enabled`, whitelisted domains list |
| 136 // and allowed connection type are saved by adblock engine itself | |
| 125 engine.setEnabled(settings.isAdblockEnabled()); | 137 engine.setEnabled(settings.isAdblockEnabled()); |
| 126 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); | 138 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); |
| 139 | |
| 140 String connectionType = (settings.getAllowedConnectionType() != null | |
| 141 ? settings.getAllowedConnectionType().getValue() | |
| 142 : null); | |
| 143 engine.getFilterEngine().setAllowedConnectionType(connectionType); | |
| 127 } | 144 } |
| 128 else | 145 else |
| 129 { | 146 { |
| 130 Log.w(TAG, "No saved adblock settings"); | 147 Log.w(TAG, "No saved adblock settings"); |
| 131 } | 148 } |
| 132 | 149 |
| 133 // unlock waiting client thread | 150 // unlock waiting client thread |
| 134 engineCreated.countDown(); | 151 engineCreated.countDown(); |
| 135 } | 152 } |
| 136 | 153 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 157 } | 174 } |
| 158 } | 175 } |
| 159 | 176 |
| 160 private void disposeAdblock() | 177 private void disposeAdblock() |
| 161 { | 178 { |
| 162 Log.w(TAG, "Disposing adblock engine"); | 179 Log.w(TAG, "Disposing adblock engine"); |
| 163 | 180 |
| 164 engine.dispose(); | 181 engine.dispose(); |
| 165 engine = null; | 182 engine = null; |
| 166 | 183 |
| 167 // to unlock waiting client in WaitForReady() | 184 // to unlock waiting client in waitForReady() |
| 168 engineCreated.countDown(); | 185 engineCreated.countDown(); |
| 169 engineCreated = null; | 186 engineCreated = null; |
| 170 | 187 |
| 171 storage = null; | 188 storage = null; |
| 189 | |
| 190 // callbacks | |
| 191 this.isAllowedConnectionCallback.dispose(); | |
| 192 this.isAllowedConnectionCallback = null; | |
| 172 } | 193 } |
| 173 | 194 |
| 174 /** | 195 /** |
| 175 * Get registered clients count | 196 * Get registered clients count |
| 176 * @return registered clients count | 197 * @return registered clients count |
| 177 */ | 198 */ |
| 178 public int getCounter() | 199 public int getCounter() |
| 179 { | 200 { |
| 180 return referenceCounter.get(); | 201 return referenceCounter.get(); |
| 181 } | 202 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 */ | 234 */ |
| 214 public synchronized void release() | 235 public synchronized void release() |
| 215 { | 236 { |
| 216 if (referenceCounter.decrementAndGet() == 0) | 237 if (referenceCounter.decrementAndGet() == 0) |
| 217 { | 238 { |
| 218 waitForReady(); | 239 waitForReady(); |
| 219 disposeAdblock(); | 240 disposeAdblock(); |
| 220 } | 241 } |
| 221 } | 242 } |
| 222 } | 243 } |
| OLD | NEW |