Index: mobile/android/thirdparty/org/adblockplus/browser/OtherPreferenceGroup.java |
diff --git a/mobile/android/thirdparty/org/adblockplus/browser/OtherPreferenceGroup.java b/mobile/android/thirdparty/org/adblockplus/browser/OtherPreferenceGroup.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2ee1f61ed908b57cbc905f2bde8222325d5c2025 |
--- /dev/null |
+++ b/mobile/android/thirdparty/org/adblockplus/browser/OtherPreferenceGroup.java |
@@ -0,0 +1,365 @@ |
+/* |
+ * 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.adblockplus.browser.SubscriptionPreferenceCategory.SubscriptionContainer; |
Felix Dahlke
2015/07/26 17:06:22
May be non-trivial, but I think it'd make more sen
René Jeschke
2015/07/28 10:55:26
Done.
|
+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 OtherPreferenceGroup extends PreferenceGroup implements |
Felix Dahlke
2015/07/26 17:06:22
Nit: `OtherPreferenceGroup` sounds pretty generic,
René Jeschke
2015/07/28 10:55:26
Done.
|
+ 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_disable_tracking, |
+ R.string.abb_disable_malware, |
+ R.string.abb_disable_anti_adblock, |
+ R.string.abb_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" |
+ }; |
+ |
+ private static final String[] OTHER_BUILTINS = |
Felix Dahlke
2015/07/26 17:06:22
Nit: Considering how they're used, `IGNORED_BUILTI
René Jeschke
2015/07/28 10:55:26
Done.
|
+ { |
+ "https://easylist-downloads.adblockplus.org/exceptionrules.txt" |
+ }; |
+ |
+ static |
+ { |
+ for (int i = 0; i < BUILTIN_TITLES.length; i++) |
+ { |
+ BUILTIN_URL_TO_INDEX.put(BUILTIN_LISTS[i * 2 + 1], Integer.valueOf(i)); |
+ } |
+ |
+ for (int i = 0; i < OTHER_BUILTINS.length; i++) |
+ { |
+ IGNORED_URLS.add(OTHER_BUILTINS[i]); |
+ } |
+ } |
+ |
+ 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 OtherPreferenceGroup(final Context context, final AttributeSet attrs) |
+ { |
+ super(context, attrs); |
+ } |
+ |
+ public OtherPreferenceGroup(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.handlerPost(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ initRecommendedSubscriptions(); |
+ OtherPreferenceGroup.this.activeSubscriptions.refresh(); |
+ |
+ ThreadUtils.postToUiThread(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ OtherPreferenceGroup.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()) |
Felix Dahlke
2015/07/26 17:06:22
What about disabled subscriptions? Would it make s
René Jeschke
2015/07/28 10:55:26
As ABP does not store custom, removed subscription
|
+ { |
+ 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_block_list_adding)); |
+ this.progressDialog.show(); |
+ |
+ AddOnBridge.handlerPost(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(); |
+ |
+ OtherPreferenceGroup.this.activeSubscriptions.refresh(); |
+ |
+ ThreadUtils.postToUiThread(new Runnable() |
+ { |
+ @Override |
+ public void run() |
+ { |
+ OtherPreferenceGroup.this.initEntries(); |
+ } |
+ }); |
+ } |
+ catch (Throwable t) |
+ { |
+ if (OtherPreferenceGroup.this.progressDialog != null) |
+ { |
+ OtherPreferenceGroup.this.progressDialog.dismiss(); |
+ OtherPreferenceGroup.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 |
+ } |
+ } |
+} |