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.android.AdblockEngine; | 24 import org.adblockplus.libadblockplus.android.AdblockEngine; |
| 25 import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper; |
25 import org.adblockplus.libadblockplus.android.Utils; | 26 import org.adblockplus.libadblockplus.android.Utils; |
26 | 27 |
| 28 import java.io.File; |
| 29 import java.util.Map; |
27 import java.util.concurrent.CountDownLatch; | 30 import java.util.concurrent.CountDownLatch; |
28 import java.util.concurrent.atomic.AtomicInteger; | 31 import java.util.concurrent.atomic.AtomicInteger; |
29 | 32 |
30 /** | 33 /** |
31 * AdblockHelper shared resources | 34 * AdblockHelper shared resources |
32 * (singleton) | 35 * (singleton) |
33 */ | 36 */ |
34 public class AdblockHelper | 37 public class AdblockHelper |
35 { | 38 { |
36 private static final String TAG = Utils.getTag(AdblockHelper.class); | 39 private static final String TAG = Utils.getTag(AdblockHelper.class); |
37 | 40 |
38 /** | 41 /** |
39 * Suggested preference name | 42 * Suggested preference name to store settings |
40 */ | 43 */ |
41 public static final String PREFERENCE_NAME = "ADBLOCK"; | 44 public static final String PREFERENCE_NAME = "ADBLOCK"; |
| 45 |
| 46 /** |
| 47 * Suggested preference name to store intercepted subscription requests |
| 48 */ |
| 49 public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD"; |
42 private static AdblockHelper _instance; | 50 private static AdblockHelper _instance; |
43 | 51 |
44 private Context context; | 52 private Context context; |
45 private boolean developmentBuild; | 53 private boolean developmentBuild; |
46 private String preferenceName; | 54 private String settingsPreferenceName; |
| 55 private String preloadedPreferenceName; |
| 56 private Map<String, Integer> urlToResourceIdMap; |
47 private AdblockEngine engine; | 57 private AdblockEngine engine; |
48 private AdblockSettingsStorage storage; | 58 private AdblockSettingsStorage storage; |
49 private CountDownLatch engineCreated; | 59 private CountDownLatch engineCreated; |
50 | 60 |
51 /* | 61 /* |
52 Simple ARC management for AdblockEngine | 62 Simple ARC management for AdblockEngine |
53 Use `retain` and `release` | 63 Use `retain` and `release` |
54 */ | 64 */ |
55 | 65 |
56 private AtomicInteger referenceCounter = new AtomicInteger(0); | 66 private AtomicInteger referenceCounter = new AtomicInteger(0); |
(...skipping 25 matching lines...) Expand all Loading... |
82 | 92 |
83 public AdblockSettingsStorage getStorage() | 93 public AdblockSettingsStorage getStorage() |
84 { | 94 { |
85 return storage; | 95 return storage; |
86 } | 96 } |
87 | 97 |
88 /** | 98 /** |
89 * Init with context | 99 * Init with context |
90 * @param context application context | 100 * @param context application context |
91 * @param developmentBuild debug or release? | 101 * @param developmentBuild debug or release? |
92 * @param preferenceName Shared Preferences name | 102 * @param preferenceName Shared Preferences name to store adlock settings |
93 */ | 103 */ |
94 public void init(Context context, boolean developmentBuild, String preferenceN
ame) | 104 public AdblockHelper init(Context context, boolean developmentBuild, String pr
eferenceName) |
95 { | 105 { |
96 this.context = context.getApplicationContext(); | 106 this.context = context.getApplicationContext(); |
97 this.developmentBuild = developmentBuild; | 107 this.developmentBuild = developmentBuild; |
98 this.preferenceName = preferenceName; | 108 this.settingsPreferenceName = preferenceName; |
| 109 return this; |
| 110 } |
| 111 |
| 112 /** |
| 113 * Use preloaded subscriptions |
| 114 * @param preferenceName Shared Preferences name to store intercepted requests
stats |
| 115 * @param urlToResourceIdMap |
| 116 */ |
| 117 public void preloadSubscriptions(String preferenceName, Map<String, Integer> u
rlToResourceIdMap) |
| 118 { |
| 119 this.preloadedPreferenceName = preferenceName; |
| 120 this.urlToResourceIdMap = urlToResourceIdMap; |
99 } | 121 } |
100 | 122 |
101 private void createAdblock() | 123 private void createAdblock() |
102 { | 124 { |
103 Log.d(TAG, "Creating adblock engine ..."); | 125 Log.d(TAG, "Creating adblock engine ..."); |
104 | 126 |
105 // read and apply current settings | 127 // read and apply current settings |
106 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte
xt.MODE_PRIVATE); | 128 SharedPreferences settingsPrefs = context.getSharedPreferences( |
107 storage = new SharedPrefsStorage(prefs); | 129 settingsPreferenceName, |
| 130 Context.MODE_PRIVATE); |
| 131 storage = new SharedPrefsStorage(settingsPrefs); |
108 | 132 |
109 engine = AdblockEngine.create( | 133 File basePath = context.getDir(AdblockEngine.BASE_PATH_DIRECTORY, Context.MO
DE_PRIVATE); |
110 AdblockEngine.generateAppInfo(context, developmentBuild), | 134 AdblockEngine.Builder builder = AdblockEngine |
111 context.getCacheDir().getAbsolutePath(), | 135 .builder( |
112 true); // `true` as we need element hiding | 136 AdblockEngine.generateAppInfo(context, developmentBuild), |
| 137 basePath.getAbsolutePath()) |
| 138 .enableElementHiding(true); |
| 139 |
| 140 // if preloaded subscriptions provided |
| 141 if (preloadedPreferenceName != null) |
| 142 { |
| 143 SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferenc
es( |
| 144 preloadedPreferenceName, |
| 145 Context.MODE_PRIVATE); |
| 146 builder.preloadSubscriptions( |
| 147 context, |
| 148 urlToResourceIdMap, |
| 149 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri
ptionsPrefs)); |
| 150 } |
| 151 |
| 152 engine = builder.build(); |
| 153 |
113 Log.d(TAG, "AdblockHelper engine created"); | 154 Log.d(TAG, "AdblockHelper engine created"); |
114 | 155 |
115 AdblockSettings settings = storage.load(); | 156 AdblockSettings settings = storage.load(); |
116 if (settings != null) | 157 if (settings != null) |
117 { | 158 { |
118 Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 159 Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
119 // apply last saved settings to adblock engine | 160 // apply last saved settings to adblock engine |
120 | 161 |
121 // all the settings except `enabled` and whitelisted domains are saved by
adblock engine itself | 162 // all the settings except `enabled` and whitelisted domains are saved by
adblock engine itself |
122 engine.setEnabled(settings.isAdblockEnabled()); | 163 engine.setEnabled(settings.isAdblockEnabled()); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 engineCreated.countDown(); | 262 engineCreated.countDown(); |
222 engineCreated = null; | 263 engineCreated = null; |
223 } | 264 } |
224 else | 265 else |
225 { | 266 { |
226 disposeAdblock(); | 267 disposeAdblock(); |
227 } | 268 } |
228 } | 269 } |
229 } | 270 } |
230 } | 271 } |
OLD | NEW |