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

Delta Between Two Patch Sets: mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java

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

Powered by Google App Engine
This is Rietveld