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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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.AdblockEngineProvider; | 25 import org.adblockplus.libadblockplus.android.AdblockEngineProvider; |
25 import org.adblockplus.libadblockplus.android.SingletonEngineProvider; | 26 import org.adblockplus.libadblockplus.android.SingleInstanceEngineProvider; |
26 import org.adblockplus.libadblockplus.android.Utils; | 27 import org.adblockplus.libadblockplus.android.Utils; |
27 | 28 |
28 /** | 29 /** |
29 * AdblockHelper shared resources | 30 * AdblockHelper shared resources |
30 * (singleton) | 31 * (singleton) |
31 */ | 32 */ |
32 public class AdblockHelper | 33 public class AdblockHelper |
33 { | 34 { |
34 private static final String TAG = Utils.getTag(AdblockHelper.class); | 35 private static final String TAG = Utils.getTag(AdblockHelper.class); |
35 | 36 |
36 /** | 37 /** |
37 * Suggested preference name to store settings | 38 * Suggested preference name to store settings |
38 */ | 39 */ |
39 public static final String PREFERENCE_NAME = "ADBLOCK"; | 40 public static final String PREFERENCE_NAME = "ADBLOCK"; |
40 | 41 |
41 /** | 42 /** |
42 * Suggested preference name to store intercepted subscription requests | 43 * Suggested preference name to store intercepted subscription requests |
43 */ | 44 */ |
44 public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD"; | 45 public static final String PRELOAD_PREFERENCE_NAME = "ADBLOCK_PRELOAD"; |
45 private static AdblockHelper _instance; | 46 private static AdblockHelper _instance; |
46 | 47 |
47 private SingletonEngineProvider adblockEngineProvider; | 48 private SingleInstanceEngineProvider provider; |
48 private AdblockSettingsStorage storage; | 49 private AdblockSettingsStorage storage; |
49 private Runnable engineCreatedCallback = new Runnable() | 50 |
51 private final Runnable engineCreatedCallback = new Runnable() | |
50 { | 52 { |
51 @Override | 53 @Override |
52 public void run() | 54 public void run() |
53 { | 55 { |
54 AdblockSettings settings = storage.load(); | 56 AdblockSettings settings = storage.load(); |
55 if (settings != null) | 57 if (settings != null) |
56 { | 58 { |
57 Log.d(TAG, "Applying saved adblock settings to adblock engine"); | 59 Log.d(TAG, "Applying saved adblock settings to adblock engine"); |
58 // apply last saved settings to adblock engine | 60 // apply last saved settings to adblock engine. |
59 | |
60 // all the settings except `enabled` and whitelisted domains list | 61 // all the settings except `enabled` and whitelisted domains list |
61 // are saved by adblock engine itself | 62 // are saved by adblock engine itself |
62 adblockEngineProvider.getAdblockEngine().setEnabled(settings.isAdblockEn abled()); | 63 provider.getEngine().setEnabled(settings.isAdblockEnabled()); |
63 adblockEngineProvider.getAdblockEngine().setWhitelistedDomains(settings. getWhitelistedDomains()); | 64 provider.getEngine().setWhitelistedDomains(settings.getWhitelistedDomain s()); |
64 | 65 |
65 // allowed connection type is saved by filter engine but we need to over ride it | 66 // allowed connection type is saved by filter engine but we need to over ride it |
66 // as filter engine can be not created when changing | 67 // as filter engine can be not created when changing |
67 String connectionType = (settings.getAllowedConnectionType() != null | 68 String connectionType = (settings.getAllowedConnectionType() != null |
68 ? settings.getAllowedConnectionType().getValue() | 69 ? settings.getAllowedConnectionType().getValue() |
69 : null); | 70 : null); |
70 adblockEngineProvider.getAdblockEngine().getFilterEngine().setAllowedCon nectionType(connectionType); | 71 provider.getEngine().getFilterEngine().setAllowedConnectionType(connecti onType); |
71 } | 72 } |
72 else | 73 else |
73 { | 74 { |
74 Log.w(TAG, "No saved adblock settings"); | 75 Log.w(TAG, "No saved adblock settings"); |
75 } | 76 } |
76 } | 77 } |
77 }; | 78 }; |
78 | 79 |
79 private Runnable engineDisposedCallback = new Runnable() | 80 private final Runnable engineDisposedCallback = new Runnable() |
80 { | 81 { |
81 @Override | 82 @Override |
82 public void run() | 83 public void run() |
83 { | 84 { |
84 Log.d(TAG, "Releasing adblock settings"); | 85 Log.d(TAG, "Releasing adblock settings storage"); |
85 storage = null; | 86 storage = null; |
86 } | 87 } |
87 }; | 88 }; |
88 | 89 |
89 // singleton | 90 // singleton |
90 protected AdblockHelper() | 91 protected AdblockHelper() |
91 { | 92 { |
92 // prevents instantiation | 93 // prevents instantiation |
93 } | 94 } |
94 | 95 |
95 /** | 96 /** |
96 * Use to get AdblockHelper instance | 97 * Use to get AdblockHelper instance |
97 * @return adblock instance | 98 * @return adblock instance |
98 */ | 99 */ |
99 public static synchronized AdblockHelper get() | 100 public static synchronized AdblockHelper get() |
100 { | 101 { |
101 if (_instance == null) | 102 if (_instance == null) |
102 { | 103 { |
103 _instance = new AdblockHelper(); | 104 _instance = new AdblockHelper(); |
104 } | 105 } |
105 | 106 |
106 return _instance; | 107 return _instance; |
107 } | 108 } |
108 | 109 |
109 public AdblockEngineProvider getAdblockEngineProvider() | 110 public AdblockEngineProvider getProvider() |
110 { | 111 { |
111 return adblockEngineProvider; | 112 if (provider == null) |
113 { | |
114 throw new IllegalStateException("Usage exception: call init(...) first"); | |
115 } | |
116 return provider; | |
112 } | 117 } |
113 | 118 |
114 public AdblockSettingsStorage getStorage() | 119 public AdblockSettingsStorage getStorage() |
115 { | 120 { |
121 if (storage == null) | |
122 { | |
123 throw new IllegalStateException("Usage exception: call init(...) first"); | |
124 } | |
116 return storage; | 125 return storage; |
117 } | 126 } |
118 | 127 |
119 /** | 128 /** |
120 * Init with context | 129 * Init with context |
121 * @param context application context | 130 * @param context application context |
122 * @param basePath file system root to store files | 131 * @param basePath file system root to store files |
123 * | 132 * |
124 * Adblock Plus library will download subscription files and s tore them on | 133 * Adblock Plus library will download subscription files and s tore them on |
125 * the path passed. The path should exist and the directory co ntent should not be | 134 * the path passed. The path should exist and the directory co ntent should not be |
126 * cleared out occasionally. Using `context.getCacheDir().getA bsolutePath()` is not | 135 * cleared out occasionally. Using `context.getCacheDir().getA bsolutePath()` is not |
127 * recommended because it can be cleared by the system. | 136 * recommended because it can be cleared by the system. |
128 * @param developmentBuild debug or release? | 137 * @param developmentBuild debug or release? |
129 * @param preferenceName Shared Preferences name to store adblock settings | 138 * @param preferenceName Shared Preferences name to store adblock settings |
130 */ | 139 */ |
131 public SingletonEngineProvider init(Context context, String basePath, | 140 public SingleInstanceEngineProvider init(Context context, String basePath, |
132 boolean developmentBuild, String preferenc eName) | 141 boolean developmentBuild, String pref erenceName) |
133 { | 142 { |
134 initProvider(context, basePath, developmentBuild); | 143 initProvider(context, basePath, developmentBuild); |
135 initStorage(context, preferenceName); | 144 initStorage(context, preferenceName); |
136 return adblockEngineProvider; | 145 return provider; |
137 } | 146 } |
138 | 147 |
139 private void initProvider(Context context, String basePath, boolean developmen tBuild) | 148 private void initProvider(Context context, String basePath, boolean developmen tBuild) |
140 { | 149 { |
141 this.adblockEngineProvider = new SingletonEngineProvider(context, basePath, developmentBuild); | 150 provider = new SingleInstanceEngineProvider(context, basePath, developmentBu ild); |
142 this.adblockEngineProvider.setEngineCreatedCallback(engineCreatedCallback); | 151 provider.setEngineCreatedCallback(engineCreatedCallback); |
143 this.adblockEngineProvider.setEngineDisposedCallback(engineDisposedCallback) ; | 152 provider.setEngineDisposedCallback(engineDisposedCallback); |
144 } | 153 } |
145 | 154 |
146 private void initStorage(Context context, String settingsPreferenceName) | 155 private void initStorage(Context context, String settingsPreferenceName) |
147 { | 156 { |
148 // read and apply current settings | 157 // read and apply current settings |
149 SharedPreferences settingsPrefs = context.getSharedPreferences( | 158 SharedPreferences settingsPrefs = context.getSharedPreferences( |
150 settingsPreferenceName, | 159 settingsPreferenceName, |
151 Context.MODE_PRIVATE); | 160 Context.MODE_PRIVATE); |
152 | 161 |
153 this.storage = new SharedPrefsStorage(settingsPrefs); | 162 storage = new SharedPrefsStorage(settingsPrefs); |
163 } | |
164 | |
165 /** | |
166 * The method is deprecated: use .getProvider().retain() instead | |
diegocarloslima
2018/01/19 12:59:19
It would be also good to add @deprecated in the ja
| |
167 */ | |
168 @Deprecated public boolean retain(boolean asynchronous) | |
diegocarloslima
2018/01/19 12:59:19
I would prefer this annotation as it was before, a
| |
169 { | |
170 return provider.retain(asynchronous); | |
171 } | |
172 | |
173 /** | |
174 * The method is deprecated: use .getProvider().waitForReady() instead | |
175 */ | |
176 @Deprecated public void waitForReady() | |
177 { | |
178 provider.waitForReady(); | |
179 } | |
180 | |
181 /** | |
182 * The method is deprecated: use .getProvider().getEngine() instead | |
183 */ | |
184 @Deprecated public AdblockEngine getEngine() | |
185 { | |
186 return provider.getEngine(); | |
187 } | |
188 | |
189 /** | |
190 * The method is deprecated: use .getProvider().release() instead | |
191 */ | |
192 @Deprecated public boolean release() | |
193 { | |
194 return provider.release(); | |
195 } | |
196 | |
197 /** | |
198 * The method is deprecated: use .getProvider().getCounter() instead | |
199 */ | |
200 @Deprecated public int getCounter() | |
201 { | |
202 return provider.getCounter(); | |
154 } | 203 } |
155 } | 204 } |
OLD | NEW |