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

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

Issue 29322610: Issue 2720 - [Adblocking settings] Add the other filter lists category (Closed)
Patch Set: Merged Issue-2750 Created July 30, 2015, 11:13 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java
diff --git a/mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java b/mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java
index c23794d3b98db77c2e46e6be7e61ab3dfb8a8ba3..353bb68f8e0110b27f197438973d80e0fc559b0a 100644
--- a/mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java
+++ b/mobile/android/thirdparty/org/adblockplus/browser/SubscriptionPreferenceCategory.java
@@ -17,49 +17,25 @@
package org.adblockplus.browser;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Semaphore;
import org.mozilla.gecko.R;
-import org.mozilla.gecko.util.NativeJSObject;
import org.mozilla.gecko.util.ThreadUtils;
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
import android.app.ProgressDialog;
import android.content.Context;
-import android.os.Handler;
-import android.os.HandlerThread;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
-import android.util.Log;
-import android.util.Xml;
public class SubscriptionPreferenceCategory extends PreferenceCategory
{
- private static final HandlerThread HANDLER_THREAD;
- private static final Handler HANDLER;
-
- private volatile static SubscriptionContainer subscriptionContainer = null;
+ volatile static SubscriptionContainer subscriptionContainer = null;
private final CheckBoxChangeListener checkBoxChangeListener = new CheckBoxChangeListener();
private boolean isEnabledList = false;
private ProgressDialog progressDialog;
- static
- {
- HANDLER_THREAD = new HandlerThread("subscription-preference-background");
- HANDLER_THREAD.setDaemon(true);
- HANDLER_THREAD.start();
- HANDLER = new Handler(HANDLER_THREAD.getLooper());
- }
-
public SubscriptionPreferenceCategory(Context context)
{
super(context);
@@ -103,7 +79,7 @@ public class SubscriptionPreferenceCategory extends PreferenceCategory
this.progressDialog.setMessage(this.getContext().getString(R.string.abb_adblocking_waiting));
this.progressDialog.show();
- HANDLER.post(new Runnable()
+ AddOnBridge.handlerPost(new Runnable()
{
@Override
public void run()
@@ -200,244 +176,5 @@ public class SubscriptionPreferenceCategory extends PreferenceCategory
return true;
}
}
-
- private final static class SubscriptionContainer implements AdblockPlusApiCallback
- {
- private static final String TAG = SubscriptionContainer.class.getName();
- private final ConcurrentHashMap<String, Boolean> enableState = new ConcurrentHashMap<String, Boolean>();
- private final Semaphore entriesReady = new Semaphore(0);
- private final List<Subscription> entries = new ArrayList<Subscription>();
- private final HashMap<String, Subscription> urlMap = new HashMap<String, Subscription>();
-
- private SubscriptionContainer()
- {
- // prevent external instantiation
- }
-
- public final static SubscriptionContainer create()
- {
- final SubscriptionContainer sc = new SubscriptionContainer();
- AddOnBridge.queryValue(sc, "subscriptionsXml");
- sc.entriesReady.acquireUninterruptibly();
-
- for (final Subscription e : sc.entries)
- {
- sc.urlMap.put(e.url, e);
- sc.enableState.put(e.url, Boolean.FALSE);
- }
-
- sc.refresh();
-
- return sc;
- }
-
- public void refresh()
- {
- if (!this.entries.isEmpty())
- {
- for (int i = 0; i < this.entries.size(); i++)
- {
- SubscriptionChangeAction.post(this.entries.get(i),
- this,
- SubscriptionChangeAction.Mode.QUERY_SUBSCRIPTION_ENABLED);
- }
-
- this.entriesReady.acquireUninterruptibly(this.entries.size());
- }
- }
-
- public List<Subscription> getSubscriptions(boolean enabled)
- {
- final List<Subscription> ret = new ArrayList<Subscription>();
- for (final Subscription e : this.entries)
- {
- if (this.isSubscriptionListed(e.url) == enabled)
- {
- ret.add(e);
- }
- }
- return ret;
- }
-
- public boolean isSubscriptionListed(final String url)
- {
- return this.enableState.containsKey(url) && this.enableState.get(url).booleanValue();
- }
-
- public void changeSubscriptionState(final String url, final boolean enable)
- {
- final Subscription e = this.urlMap.get(url);
- if (e != null)
- {
- if (enable)
- {
- SubscriptionChangeAction.post(e, subscriptionContainer,
- SubscriptionChangeAction.Mode.ENABLE_SUBSCRIPTION);
- }
- else
- {
- SubscriptionChangeAction.post(e, subscriptionContainer,
- SubscriptionChangeAction.Mode.DISABLE_SUBSCRIPTION);
- }
- }
- }
-
- @Override
- public void onApiRequestSucceeded(NativeJSObject jsObject)
- {
- final XmlPullParser parser = Xml.newPullParser();
- try
- {
- parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
- parser.setInput(new StringReader(AddOnBridge.getStringFromJsObject(jsObject, "value", "")));
- parser.nextTag();
- parser.require(XmlPullParser.START_TAG, null, "subscriptions");
- while (parser.next() != XmlPullParser.END_TAG)
- {
- if (parser.getEventType() != XmlPullParser.START_TAG)
- {
- continue;
- }
- if ("subscription".equals(parser.getName()))
- {
- final String title = parser.getAttributeValue(null, "title");
- final String specialization = parser.getAttributeValue(null, "specialization");
- final String url = parser.getAttributeValue(null, "url");
- this.entries.add(new Subscription(title, specialization, url));
- }
- parser.next();
- }
- }
- catch (XmlPullParserException e)
- {
- Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e);
- }
- catch (IOException e)
- {
- Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e);
- }
- finally
- {
- this.entriesReady.release();
- }
- }
-
- @Override
- public void onApiRequestFailed(String errorMessage)
- {
- Log.e(TAG, "Error: " + errorMessage);
- this.entriesReady.release();
- }
-
- private static class SubscriptionChangeAction implements AdblockPlusApiCallback
- {
- private static final String TAG = SubscriptionChangeAction.class.getName();
-
- private final Subscription subscription;
- private final SubscriptionContainer parent;
- private final Mode mode;
-
- public enum Mode
- {
- QUERY_SUBSCRIPTION_ENABLED,
- ENABLE_SUBSCRIPTION,
- DISABLE_SUBSCRIPTION,
- }
-
- public SubscriptionChangeAction(final Subscription subscription,
- final SubscriptionContainer parent,
- final Mode mode)
- {
- this.subscription = subscription;
- this.parent = parent;
- this.mode = mode;
- }
-
- public static SubscriptionChangeAction post(final Subscription subscription,
- final SubscriptionContainer parent,
- final Mode mode)
- {
- return new SubscriptionChangeAction(subscription, parent, mode).post();
- }
-
- public SubscriptionChangeAction post()
- {
- switch (this.mode)
- {
- case QUERY_SUBSCRIPTION_ENABLED:
- AddOnBridge.querySubscriptionListStatus(this, this.subscription.url);
- break;
- case ENABLE_SUBSCRIPTION:
- AddOnBridge.addSubscription(this, this.subscription.url, this.subscription.title);
- break;
- case DISABLE_SUBSCRIPTION:
- AddOnBridge.removeSubscription(this, this.subscription.url);
- break;
- default:
- break;
- }
- return this;
- }
-
- @Override
- public void onApiRequestSucceeded(NativeJSObject jsObject)
- {
- switch (this.mode)
- {
- case QUERY_SUBSCRIPTION_ENABLED:
- try
- {
- this.parent.enableState.put(this.subscription.url,
- Boolean.valueOf(AddOnBridge.getBooleanFromJsObject(jsObject, "value", false)));
- }
- finally
- {
- this.parent.entriesReady.release();
- }
- break;
- default:
- break;
- }
- }
-
- @Override
- public void onApiRequestFailed(String errorMessage)
- {
- switch (this.mode)
- {
- case QUERY_SUBSCRIPTION_ENABLED:
- this.parent.enableState.put(this.subscription.url, Boolean.FALSE);
- this.parent.entriesReady.release();
- break;
- default:
- break;
- }
-
- Log.e(TAG, "Error for '" + this.subscription.url
- + "', mode: " + this.mode + ": "
- + errorMessage);
- }
- }
-
- public static class Subscription
- {
- public final String title;
- public final String specialization;
- public final String url;
-
- public Subscription(final String title, final String specialization, final String url)
- {
- this.title = title;
- this.specialization = specialization;
- this.url = url;
- }
-
- @Override
- public String toString()
- {
- return this.specialization + " (" + this.title + ") @ " + this.url;
- }
- }
- }
}

Powered by Google App Engine
This is Rietveld