| 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, |
| 124 null, |
| 125 null, |
| 126 null); // `true` as we need element hiding |
| 127 Log.d(TAG, "Adblock engine created"); |
| 128 |
| 129 engine.getFilterEngine().setAllowedConnectionType("new_type"); |
| 130 String ct = engine.getFilterEngine().getAllowedConnectionType(); |
| 117 | 131 |
| 118 AdblockSettings settings = storage.load(); | 132 AdblockSettings settings = storage.load(); |
| 119 if (settings != null) | 133 if (settings != null) |
| 120 { | 134 { |
| 121 Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 135 Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
| 122 // apply last saved settings to adblock engine | 136 // apply last saved settings to adblock engine |
| 123 | 137 |
| 124 // all the settings except `enabled` and whitelisted domains are saved by
adblock engine itself | 138 // all the settings except `enabled`, whitelisted domains list |
| 139 // and allowed connection type are saved by adblock engine itself |
| 125 engine.setEnabled(settings.isAdblockEnabled()); | 140 engine.setEnabled(settings.isAdblockEnabled()); |
| 126 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); | 141 engine.setWhitelistedDomains(settings.getWhitelistedDomains()); |
| 142 |
| 143 String connectionType = (settings.getAllowedConnectionType() != null |
| 144 ? settings.getAllowedConnectionType().getValue() |
| 145 : null); |
| 146 engine.getFilterEngine().setAllowedConnectionType(connectionType); |
| 127 } | 147 } |
| 128 else | 148 else |
| 129 { | 149 { |
| 130 Log.w(TAG, "No saved adblock settings"); | 150 Log.w(TAG, "No saved adblock settings"); |
| 131 } | 151 } |
| 132 | 152 |
| 133 // unlock waiting client thread | 153 // unlock waiting client thread |
| 134 engineCreated.countDown(); | 154 engineCreated.countDown(); |
| 135 } | 155 } |
| 136 | 156 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 162 Log.w(TAG, "Disposing adblock engine"); | 182 Log.w(TAG, "Disposing adblock engine"); |
| 163 | 183 |
| 164 engine.dispose(); | 184 engine.dispose(); |
| 165 engine = null; | 185 engine = null; |
| 166 | 186 |
| 167 // to unlock waiting client in WaitForReady() | 187 // to unlock waiting client in WaitForReady() |
| 168 engineCreated.countDown(); | 188 engineCreated.countDown(); |
| 169 engineCreated = null; | 189 engineCreated = null; |
| 170 | 190 |
| 171 storage = null; | 191 storage = null; |
| 192 |
| 193 // callbacks |
| 194 this.isAllowedConnectionCallback.dispose(); |
| 195 this.isAllowedConnectionCallback = null; |
| 172 } | 196 } |
| 173 | 197 |
| 174 /** | 198 /** |
| 175 * Get registered clients count | 199 * Get registered clients count |
| 176 * @return registered clients count | 200 * @return registered clients count |
| 177 */ | 201 */ |
| 178 public int getCounter() | 202 public int getCounter() |
| 179 { | 203 { |
| 180 return referenceCounter.get(); | 204 return referenceCounter.get(); |
| 181 } | 205 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 */ | 237 */ |
| 214 public synchronized void release() | 238 public synchronized void release() |
| 215 { | 239 { |
| 216 if (referenceCounter.decrementAndGet() == 0) | 240 if (referenceCounter.decrementAndGet() == 0) |
| 217 { | 241 { |
| 218 waitForReady(); | 242 waitForReady(); |
| 219 disposeAdblock(); | 243 disposeAdblock(); |
| 220 } | 244 } |
| 221 } | 245 } |
| 222 } | 246 } |
| OLD | NEW |