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