Index: mobile/android/thirdparty/org/adblockplus/browser/MoreSubscriptionsPreferenceGroup.java |
diff --git a/mobile/android/thirdparty/org/adblockplus/browser/MoreSubscriptionsPreferenceGroup.java b/mobile/android/thirdparty/org/adblockplus/browser/MoreSubscriptionsPreferenceGroup.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..068ab30c337765a75a0e6626c5cb165e7fe9f3d0 |
--- /dev/null |
+++ b/mobile/android/thirdparty/org/adblockplus/browser/MoreSubscriptionsPreferenceGroup.java |
@@ -0,0 +1,357 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.browser; |
+ |
+import java.util.HashMap; |
+import java.util.HashSet; |
+import java.util.Map.Entry; |
+import java.util.concurrent.Semaphore; |
+ |
+import org.mozilla.gecko.R; |
+import org.mozilla.gecko.util.NativeJSObject; |
+import org.mozilla.gecko.util.ThreadUtils; |
+ |
+import android.app.ProgressDialog; |
+import android.content.Context; |
+import android.os.Build; |
+import android.preference.CheckBoxPreference; |
+import android.preference.Preference; |
+import android.preference.PreferenceGroup; |
+import android.util.AttributeSet; |
+import android.util.Log; |
+import android.view.View; |
+import android.view.ViewGroup; |
+ |
+public class MoreSubscriptionsPreferenceGroup extends PreferenceGroup implements |
+ UrlInputDialog.UrlReadyCallback |
+{ |
+ private static final String TAG = "AdblockBrowser.OtherPreferenceGroup"; |
+ private static final HashMap<String, Integer> BUILTIN_URL_TO_INDEX = new HashMap<String, Integer>(); |
+ private static final HashSet<String> IGNORED_URLS = new HashSet<String>(); |
+ private static SubscriptionContainer recommendedSubscriptions = null; |
+ |
+ private final CheckBoxChangeListener checkBoxChangeListener = new CheckBoxChangeListener(); |
+ private final ActiveSubscriptionContainer activeSubscriptions = new ActiveSubscriptionContainer(); |
+ private ProgressDialog progressDialog; |
+ |
+ private static final int[] BUILTIN_TITLES = |
+ { |
+ R.string.abb_subscription_disable_tracking, |
+ R.string.abb_subscription_disable_malware, |
+ R.string.abb_subscription_disable_anti_adblock, |
+ R.string.abb_subscription_disable_social_media |
+ }; |
+ |
+ private static final String[] BUILTIN_LISTS = |
+ { |
+ "EasyPrivacy", |
+ "https://easylist-downloads.adblockplus.org/easyprivacy.txt", |
+ "Malware Domains", |
+ "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", |
+ "Adblock Warning Removal List", |
+ "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", |
+ "Fanboy's Social Blocking List", |
+ "https://easylist-downloads.adblockplus.org/fanboy-social.txt" |
+ }; |
+ |
+ static |
+ { |
+ for (int i = 0; i < BUILTIN_TITLES.length; i++) |
+ { |
+ BUILTIN_URL_TO_INDEX.put(BUILTIN_LISTS[i * 2 + 1], Integer.valueOf(i)); |
+ } |
+ |
+ IGNORED_URLS.add("https://easylist-downloads.adblockplus.org/exceptionrules.txt"); |
+ } |
+ |
+ private synchronized static void initRecommendedSubscriptions() |
+ { |
+ if (recommendedSubscriptions == null) |
+ { |
+ recommendedSubscriptions = SubscriptionContainer.create(false); |
+ |
+ for (SubscriptionContainer.Subscription s : recommendedSubscriptions.getSubscriptions(false)) |
+ { |
+ IGNORED_URLS.add(s.url); |
+ } |
+ } |
+ } |
+ |
+ public MoreSubscriptionsPreferenceGroup(final Context context, final AttributeSet attrs) |
+ { |
+ super(context, attrs); |
+ } |
+ |
+ public MoreSubscriptionsPreferenceGroup(final Context context, final AttributeSet attrs, |
+ final int defStyleAttr) |
+ { |
+ super(context, attrs, defStyleAttr); |
+ } |
+ |
+ @Override |
+ protected View onCreateView(final ViewGroup parent) |
+ { |
+ this.setLayoutResource(R.layout.abb_minimal_widget); |
+ return super.onCreateView(parent); |
+ } |
+ |
+ public static Preference createCheckBoxOrSwitch(final Context context) |
+ { |
+ if (Build.VERSION.SDK_INT < 14) |
+ { |
+ return new CheckBoxPreference(context); |
+ } |
+ try |
+ { |
+ return (Preference) Class.forName("android.preference.SwitchPreference") |
+ .getConstructor(Context.class) |
+ .newInstance(context); |
+ } |
+ catch (Exception e) |
+ { |
+ Log.e(TAG, "Failed to create SwitchPreference, falling back to CheckBoxPreference", e); |
+ return new CheckBoxPreference(context); |
+ } |
+ } |
+ |
+ @Override |
+ protected void onAttachedToActivity() |
+ { |
+ this.setEnabled(false); |
+ this.setShouldDisableView(true); |
+ |
+ super.onAttachedToActivity(); |
+ |
+ this.progressDialog = new ProgressDialog(this.getContext()); |
+ this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); |
+ this.progressDialog.setMessage(this.getContext().getString(R.string.abb_adblocking_waiting)); |
+ this.progressDialog.show(); |
+ |
+ UrlInputOpenerPreference.setRedirectUrlReadyCallback(this); |
+ |
+ AddOnBridge.postToHandler(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ initRecommendedSubscriptions(); |
+ MoreSubscriptionsPreferenceGroup.this.activeSubscriptions.refresh(); |
+ |
+ ThreadUtils.postToUiThread(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ MoreSubscriptionsPreferenceGroup.this.initEntries(); |
+ } |
+ }); |
+ } |
+ }); |
+ } |
+ |
+ private void initEntries() |
+ { |
+ this.removeAll(); |
+ int i = 0; |
+ for (; i < BUILTIN_TITLES.length; i++) |
+ { |
+ final CheckBoxPreference cbp = new CheckBoxPreference(this.getContext()); |
+ final String url = BUILTIN_LISTS[i * 2 + 1]; |
+ cbp.setOrder(i); |
+ cbp.setTitle(BUILTIN_TITLES[i]); |
+ cbp.setKey(url); |
+ cbp.setChecked(this.activeSubscriptions.enabledSubscriptions.containsKey(url)); |
+ cbp.setOnPreferenceChangeListener(this.checkBoxChangeListener); |
+ cbp.setPersistent(false); |
+ this.addPreference(cbp); |
+ } |
+ |
+ for (Entry<String, String> e : this.activeSubscriptions.enabledSubscriptions.entrySet()) |
+ { |
+ if (!BUILTIN_URL_TO_INDEX.containsKey(e.getKey())) |
+ { |
+ final CheckBoxPreference cbp = new CheckBoxPreference(this.getContext()); |
+ cbp.setOrder(i++); |
+ cbp.setTitle(e.getValue()); |
+ cbp.setKey(e.getKey()); |
+ cbp.setChecked(true); |
+ cbp.setOnPreferenceChangeListener(this.checkBoxChangeListener); |
+ cbp.setPersistent(false); |
+ this.addPreference(cbp); |
+ } |
+ } |
+ |
+ this.setEnabled(true); |
+ this.setShouldDisableView(false); |
+ if (this.progressDialog != null) |
+ { |
+ this.progressDialog.dismiss(); |
+ this.progressDialog = null; |
+ } |
+ } |
+ |
+ private void addNewSubscription(final String url) |
+ { |
+ this.progressDialog = new ProgressDialog(this.getContext()); |
+ this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); |
+ this.progressDialog |
+ .setMessage(this.getContext().getString(R.string.abb_add_subscription_adding)); |
+ this.progressDialog.show(); |
+ |
+ AddOnBridge.postToHandler(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ try |
+ { |
+ final Semaphore finished = new Semaphore(0); |
+ |
+ AddOnBridge.addSubscription(new AdblockPlusApiCallback() |
+ { |
+ @Override |
+ public void onApiRequestSucceeded(NativeJSObject jsObject) |
+ { |
+ finished.release(); |
+ } |
+ |
+ @Override |
+ public void onApiRequestFailed(String errorMessage) |
+ { |
+ finished.release(); |
+ } |
+ }, url, null); |
+ |
+ finished.acquireUninterruptibly(); |
+ |
+ MoreSubscriptionsPreferenceGroup.this.activeSubscriptions.refresh(); |
+ |
+ ThreadUtils.postToUiThread(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ MoreSubscriptionsPreferenceGroup.this.initEntries(); |
+ } |
+ }); |
+ } |
+ catch (Throwable t) |
+ { |
+ if (MoreSubscriptionsPreferenceGroup.this.progressDialog != null) |
+ { |
+ MoreSubscriptionsPreferenceGroup.this.progressDialog.dismiss(); |
+ MoreSubscriptionsPreferenceGroup.this.progressDialog = null; |
+ } |
+ } |
+ } |
+ }); |
+ } |
+ |
+ @Override |
+ public void callback(final String url) |
+ { |
+ if (url == null) |
+ { |
+ return; |
+ } |
+ |
+ Log.d(TAG, "Adding: " + url); |
+ this.addNewSubscription(url); |
+ } |
+ |
+ private static class ActiveSubscriptionContainer implements AdblockPlusApiCallback |
+ { |
+ public final HashMap<String, String> enabledSubscriptions = new HashMap<String, String>(); |
+ private final Semaphore entriesReady = new Semaphore(0); |
+ |
+ public void refresh() |
+ { |
+ AddOnBridge.queryActiveSubscriptions(this); |
+ this.entriesReady.acquireUninterruptibly(); |
+ } |
+ |
+ @Override |
+ public void onApiRequestSucceeded(NativeJSObject jsObject) |
+ { |
+ try |
+ { |
+ this.enabledSubscriptions.clear(); |
+ if (jsObject.getBoolean("success")) |
+ { |
+ NativeJSObject[] subs = jsObject.getObjectArray("value"); |
+ for (int i = 0; i < subs.length; i++) |
+ { |
+ final String url = subs[i].getString("url"); |
+ final String title = subs[i].has("title") ? subs[i].getString("title") : url; |
+ if (!IGNORED_URLS.contains(url)) |
+ { |
+ Log.d(TAG, "Adding: " + url + ", " + title); |
+ this.enabledSubscriptions.put(url, title); |
+ } |
+ } |
+ } |
+ } |
+ finally |
+ { |
+ this.entriesReady.release(); |
+ } |
+ } |
+ |
+ @Override |
+ public void onApiRequestFailed(String errorMessage) |
+ { |
+ this.entriesReady.release(); |
+ } |
+ } |
+ |
+ private static class CheckBoxChangeListener implements OnPreferenceChangeListener, |
+ AdblockPlusApiCallback |
+ { |
+ @Override |
+ public boolean onPreferenceChange(Preference preference, Object newValue) |
+ { |
+ if (preference instanceof CheckBoxPreference && newValue instanceof Boolean) |
+ { |
+ final CheckBoxPreference cbp = (CheckBoxPreference) preference; |
+ final boolean enable = ((Boolean) newValue).booleanValue(); |
+ |
+ if (enable) |
+ { |
+ AddOnBridge.addSubscription(this, cbp.getKey(), null); |
+ } |
+ else |
+ { |
+ AddOnBridge.removeSubscription(this, cbp.getKey()); |
+ } |
+ } |
+ return true; |
+ } |
+ |
+ @Override |
+ public void onApiRequestSucceeded(NativeJSObject jsObject) |
+ { |
+ // ignored |
+ } |
+ |
+ @Override |
+ public void onApiRequestFailed(String errorMessage) |
+ { |
+ // ignored |
+ } |
+ } |
+} |