Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java

Issue 29322610: Issue 2720 - [Adblocking settings] Add the other filter lists category (Closed)
Patch Set: Last naming nit Created July 31, 2015, 9:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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.browser; 18 package org.adblockplus.browser;
19 19
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List; 20 import java.util.List;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.Semaphore;
27 21
28 import org.mozilla.gecko.R; 22 import org.mozilla.gecko.R;
29 import org.mozilla.gecko.util.NativeJSObject;
30 import org.mozilla.gecko.util.ThreadUtils; 23 import org.mozilla.gecko.util.ThreadUtils;
31 import org.xmlpull.v1.XmlPullParser;
32 import org.xmlpull.v1.XmlPullParserException;
33 24
34 import android.app.ProgressDialog; 25 import android.app.ProgressDialog;
35 import android.content.Context; 26 import android.content.Context;
36 import android.os.Handler;
37 import android.os.HandlerThread;
38 import android.preference.CheckBoxPreference; 27 import android.preference.CheckBoxPreference;
39 import android.preference.Preference; 28 import android.preference.Preference;
40 import android.preference.PreferenceCategory; 29 import android.preference.PreferenceCategory;
41 import android.util.AttributeSet; 30 import android.util.AttributeSet;
42 import android.util.Log;
43 import android.util.Xml;
44 31
45 public class SubscriptionPreferenceCategory extends PreferenceCategory 32 public class SubscriptionPreferenceCategory extends PreferenceCategory
46 { 33 {
47 private static final HandlerThread HANDLER_THREAD; 34 volatile static SubscriptionContainer subscriptionContainer = null;
48 private static final Handler HANDLER;
49
50 private volatile static SubscriptionContainer subscriptionContainer = null;
51 private final CheckBoxChangeListener checkBoxChangeListener = new CheckBoxChan geListener(); 35 private final CheckBoxChangeListener checkBoxChangeListener = new CheckBoxChan geListener();
52 private boolean isEnabledList = false; 36 private boolean isEnabledList = false;
53 private ProgressDialog progressDialog; 37 private ProgressDialog progressDialog;
54 38
55 static
56 {
57 HANDLER_THREAD = new HandlerThread("subscription-preference-background");
58 HANDLER_THREAD.setDaemon(true);
59 HANDLER_THREAD.start();
60 HANDLER = new Handler(HANDLER_THREAD.getLooper());
61 }
62
63 public SubscriptionPreferenceCategory(Context context) 39 public SubscriptionPreferenceCategory(Context context)
64 { 40 {
65 super(context); 41 super(context);
66 } 42 }
67 43
68 public SubscriptionPreferenceCategory(Context context, AttributeSet attrs) 44 public SubscriptionPreferenceCategory(Context context, AttributeSet attrs)
69 { 45 {
70 super(context, attrs); 46 super(context, attrs);
71 } 47 }
72 48
(...skipping 23 matching lines...) Expand all
96 72
97 this.setEnabled(false); 73 this.setEnabled(false);
98 this.setShouldDisableView(true); 74 this.setShouldDisableView(true);
99 75
100 super.onAttachedToActivity(); 76 super.onAttachedToActivity();
101 77
102 this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 78 this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
103 this.progressDialog.setMessage(this.getContext().getString(R.string.abb_adbl ocking_waiting)); 79 this.progressDialog.setMessage(this.getContext().getString(R.string.abb_adbl ocking_waiting));
104 this.progressDialog.show(); 80 this.progressDialog.show();
105 81
106 HANDLER.post(new Runnable() 82 AddOnBridge.postToHandler(new Runnable()
107 { 83 {
108 @Override 84 @Override
109 public void run() 85 public void run()
110 { 86 {
111 initSubscriptions(); 87 initSubscriptions();
112 ThreadUtils.postToUiThread(new Runnable() 88 ThreadUtils.postToUiThread(new Runnable()
113 { 89 {
114 @Override 90 @Override
115 public void run() 91 public void run()
116 { 92 {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 { 169 {
194 final CheckBoxPreference cbp = (CheckBoxPreference) preference; 170 final CheckBoxPreference cbp = (CheckBoxPreference) preference;
195 final boolean enable = ((Boolean) newValue).booleanValue(); 171 final boolean enable = ((Boolean) newValue).booleanValue();
196 SubscriptionPreferenceCategory.subscriptionContainer.changeSubscriptionS tate( 172 SubscriptionPreferenceCategory.subscriptionContainer.changeSubscriptionS tate(
197 cbp.getKey(), 173 cbp.getKey(),
198 enable); 174 enable);
199 } 175 }
200 return true; 176 return true;
201 } 177 }
202 } 178 }
203
204 private final static class SubscriptionContainer implements AdblockPlusApiCall back
205 {
206 private static final String TAG = SubscriptionContainer.class.getName();
207 private final ConcurrentHashMap<String, Boolean> enableState = new Concurren tHashMap<String, Boolean>();
208 private final Semaphore entriesReady = new Semaphore(0);
209 private final List<Subscription> entries = new ArrayList<Subscription>();
210 private final HashMap<String, Subscription> urlMap = new HashMap<String, Sub scription>();
211
212 private SubscriptionContainer()
213 {
214 // prevent external instantiation
215 }
216
217 public final static SubscriptionContainer create()
218 {
219 final SubscriptionContainer sc = new SubscriptionContainer();
220 AddOnBridge.queryValue(sc, "subscriptionsXml");
221 sc.entriesReady.acquireUninterruptibly();
222
223 for (final Subscription e : sc.entries)
224 {
225 sc.urlMap.put(e.url, e);
226 sc.enableState.put(e.url, Boolean.FALSE);
227 }
228
229 sc.refresh();
230
231 return sc;
232 }
233
234 public void refresh()
235 {
236 if (!this.entries.isEmpty())
237 {
238 for (int i = 0; i < this.entries.size(); i++)
239 {
240 SubscriptionChangeAction.post(this.entries.get(i),
241 this,
242 SubscriptionChangeAction.Mode.QUERY_SUBSCRIPTION_ENABLED);
243 }
244
245 this.entriesReady.acquireUninterruptibly(this.entries.size());
246 }
247 }
248
249 public List<Subscription> getSubscriptions(boolean enabled)
250 {
251 final List<Subscription> ret = new ArrayList<Subscription>();
252 for (final Subscription e : this.entries)
253 {
254 if (this.isSubscriptionListed(e.url) == enabled)
255 {
256 ret.add(e);
257 }
258 }
259 return ret;
260 }
261
262 public boolean isSubscriptionListed(final String url)
263 {
264 return this.enableState.containsKey(url) && this.enableState.get(url).bool eanValue();
265 }
266
267 public void changeSubscriptionState(final String url, final boolean enable)
268 {
269 final Subscription e = this.urlMap.get(url);
270 if (e != null)
271 {
272 if (enable)
273 {
274 SubscriptionChangeAction.post(e, subscriptionContainer,
275 SubscriptionChangeAction.Mode.ENABLE_SUBSCRIPTION);
276 }
277 else
278 {
279 SubscriptionChangeAction.post(e, subscriptionContainer,
280 SubscriptionChangeAction.Mode.DISABLE_SUBSCRIPTION);
281 }
282 }
283 }
284
285 @Override
286 public void onApiRequestSucceeded(NativeJSObject jsObject)
287 {
288 final XmlPullParser parser = Xml.newPullParser();
289 try
290 {
291 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
292 parser.setInput(new StringReader(AddOnBridge.getStringFromJsObject(jsObj ect, "value", "")));
293 parser.nextTag();
294 parser.require(XmlPullParser.START_TAG, null, "subscriptions");
295 while (parser.next() != XmlPullParser.END_TAG)
296 {
297 if (parser.getEventType() != XmlPullParser.START_TAG)
298 {
299 continue;
300 }
301 if ("subscription".equals(parser.getName()))
302 {
303 final String title = parser.getAttributeValue(null, "title");
304 final String specialization = parser.getAttributeValue(null, "specia lization");
305 final String url = parser.getAttributeValue(null, "url");
306 this.entries.add(new Subscription(title, specialization, url));
307 }
308 parser.next();
309 }
310 }
311 catch (XmlPullParserException e)
312 {
313 Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e);
314 }
315 catch (IOException e)
316 {
317 Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e);
318 }
319 finally
320 {
321 this.entriesReady.release();
322 }
323 }
324
325 @Override
326 public void onApiRequestFailed(String errorMessage)
327 {
328 Log.e(TAG, "Error: " + errorMessage);
329 this.entriesReady.release();
330 }
331
332 private static class SubscriptionChangeAction implements AdblockPlusApiCallb ack
333 {
334 private static final String TAG = SubscriptionChangeAction.class.getName() ;
335
336 private final Subscription subscription;
337 private final SubscriptionContainer parent;
338 private final Mode mode;
339
340 public enum Mode
341 {
342 QUERY_SUBSCRIPTION_ENABLED,
343 ENABLE_SUBSCRIPTION,
344 DISABLE_SUBSCRIPTION,
345 }
346
347 public SubscriptionChangeAction(final Subscription subscription,
348 final SubscriptionContainer parent,
349 final Mode mode)
350 {
351 this.subscription = subscription;
352 this.parent = parent;
353 this.mode = mode;
354 }
355
356 public static SubscriptionChangeAction post(final Subscription subscriptio n,
357 final SubscriptionContainer parent,
358 final Mode mode)
359 {
360 return new SubscriptionChangeAction(subscription, parent, mode).post();
361 }
362
363 public SubscriptionChangeAction post()
364 {
365 switch (this.mode)
366 {
367 case QUERY_SUBSCRIPTION_ENABLED:
368 AddOnBridge.querySubscriptionListStatus(this, this.subscription.url) ;
369 break;
370 case ENABLE_SUBSCRIPTION:
371 AddOnBridge.addSubscription(this, this.subscription.url, this.subscr iption.title);
372 break;
373 case DISABLE_SUBSCRIPTION:
374 AddOnBridge.removeSubscription(this, this.subscription.url);
375 break;
376 default:
377 break;
378 }
379 return this;
380 }
381
382 @Override
383 public void onApiRequestSucceeded(NativeJSObject jsObject)
384 {
385 switch (this.mode)
386 {
387 case QUERY_SUBSCRIPTION_ENABLED:
388 try
389 {
390 this.parent.enableState.put(this.subscription.url,
391 Boolean.valueOf(AddOnBridge.getBooleanFromJsObject(jsObject, " value", false)));
392 }
393 finally
394 {
395 this.parent.entriesReady.release();
396 }
397 break;
398 default:
399 break;
400 }
401 }
402
403 @Override
404 public void onApiRequestFailed(String errorMessage)
405 {
406 switch (this.mode)
407 {
408 case QUERY_SUBSCRIPTION_ENABLED:
409 this.parent.enableState.put(this.subscription.url, Boolean.FALSE);
410 this.parent.entriesReady.release();
411 break;
412 default:
413 break;
414 }
415
416 Log.e(TAG, "Error for '" + this.subscription.url
417 + "', mode: " + this.mode + ": "
418 + errorMessage);
419 }
420 }
421
422 public static class Subscription
423 {
424 public final String title;
425 public final String specialization;
426 public final String url;
427
428 public Subscription(final String title, final String specialization, final String url)
429 {
430 this.title = title;
431 this.specialization = specialization;
432 this.url = url;
433 }
434
435 @Override
436 public String toString()
437 {
438 return this.specialization + " (" + this.title + ") @ " + this.url;
439 }
440 }
441 }
442 } 179 }
443 180
OLDNEW

Powered by Google App Engine
This is Rietveld