| 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, // `true` as we need element hiding |
| 116 Log.d(TAG, "AdblockHelper engine created"); | 122 isAllowedConnectionCallback); |
| 123 Log.d(TAG, "Adblock 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`, whitelisted domains list |
| 132 // and allowed connection type are saved by adblock engine itself |
| 125 engine.setEnabled(settings.isAdblockEnabled()); | 133 engine.setEnabled(settings.isAdblockEnabled()); |
| 126 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); | 134 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); |
| 135 |
| 136 String connectionType = (settings.getAllowedConnectionType() != null |
| 137 ? settings.getAllowedConnectionType().getValue() |
| 138 : null); |
| 139 engine.getFilterEngine().setAllowedConnectionType(connectionType); |
| 127 } | 140 } |
| 128 else | 141 else |
| 129 { | 142 { |
| 130 Log.w(TAG, "No saved adblock settings"); | 143 Log.w(TAG, "No saved adblock settings"); |
| 131 } | 144 } |
| 132 | 145 |
| 133 // unlock waiting client thread | 146 // unlock waiting client thread |
| 134 engineCreated.countDown(); | 147 engineCreated.countDown(); |
| 135 } | 148 } |
| 136 | 149 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 157 } | 170 } |
| 158 } | 171 } |
| 159 | 172 |
| 160 private void disposeAdblock() | 173 private void disposeAdblock() |
| 161 { | 174 { |
| 162 Log.w(TAG, "Disposing adblock engine"); | 175 Log.w(TAG, "Disposing adblock engine"); |
| 163 | 176 |
| 164 engine.dispose(); | 177 engine.dispose(); |
| 165 engine = null; | 178 engine = null; |
| 166 | 179 |
| 167 // to unlock waiting client in WaitForReady() | 180 // to unlock waiting client in waitForReady() |
| 168 engineCreated.countDown(); | 181 engineCreated.countDown(); |
| 169 engineCreated = null; | 182 engineCreated = null; |
| 170 | 183 |
| 171 storage = null; | 184 storage = null; |
| 185 |
| 186 // callbacks |
| 187 this.isAllowedConnectionCallback.dispose(); |
| 188 this.isAllowedConnectionCallback = null; |
| 172 } | 189 } |
| 173 | 190 |
| 174 /** | 191 /** |
| 175 * Get registered clients count | 192 * Get registered clients count |
| 176 * @return registered clients count | 193 * @return registered clients count |
| 177 */ | 194 */ |
| 178 public int getCounter() | 195 public int getCounter() |
| 179 { | 196 { |
| 180 return referenceCounter.get(); | 197 return referenceCounter.get(); |
| 181 } | 198 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 */ | 230 */ |
| 214 public synchronized void release() | 231 public synchronized void release() |
| 215 { | 232 { |
| 216 if (referenceCounter.decrementAndGet() == 0) | 233 if (referenceCounter.decrementAndGet() == 0) |
| 217 { | 234 { |
| 218 waitForReady(); | 235 waitForReady(); |
| 219 disposeAdblock(); | 236 disposeAdblock(); |
| 220 } | 237 } |
| 221 } | 238 } |
| 222 } | 239 } |
| OLD | NEW |