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

Unified Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java

Issue 29524668: Issue 3916 - Supporting Adding filter lists via URL (Closed)
Patch Set: Issue 3916 - Supporting Adding filer lists via URL Created Aug. 23, 2017, 2:20 p.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: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java
===================================================================
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java
@@ -12,36 +12,38 @@
* 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.sbrowser.contentblocker;
+import java.io.IOException;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
-import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
import org.adblockplus.sbrowser.contentblocker.engine.Engine;
import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
import org.adblockplus.adblockplussbrowser.R;
import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPreference;
import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.Preference.OnPreferenceChangeListener;
import android.text.format.DateUtils;
import android.util.AttributeSet;
+import android.util.Log;
public class MoreBlockingPreferenceCategory extends PreferenceCategory implements
- EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener
+ EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.SubscriptionAddedCallback
{
private Engine engine = null;
private static final int[] WHITELISTED_LIST_TITLES =
{
R.string.subscription_disable_tracking,
R.string.subscription_disable_malware,
R.string.subscription_disable_anti_adblock,
R.string.subscription_disable_social_media
@@ -50,17 +52,17 @@ public class MoreBlockingPreferenceCateg
private static final String[] WHITELISTED_LIST_URLS =
{
"https://easylist-downloads.adblockplus.org/easyprivacy.txt",
"https://easylist-downloads.adblockplus.org/malwaredomains_full.txt",
"https://easylist-downloads.adblockplus.org/antiadblockfilters.txt",
"https://easylist-downloads.adblockplus.org/fanboy-social.txt"
};
- private static final HashMap<String, Integer> URL_TO_RES_ID_MAP = new HashMap<>();
+ public static final HashMap<String, Integer> URL_TO_RES_ID_MAP = new HashMap<>();
diegocarloslima 2017/09/08 15:06:07 Since this field is now used by Engine, we should
jens 2017/09/13 08:50:29 Acknowledged.
static
{
for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++)
{
URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i]);
}
}
@@ -81,68 +83,132 @@ public class MoreBlockingPreferenceCateg
EngineService.startService(this.getContext().getApplicationContext(), this);
super.onAttachedToActivity();
}
@Override
public void onEngineCreated(final Engine engine, final boolean success)
{
this.engine = engine;
- final String aaLink = engine.getPrefsDefault(Engine.SUBSCRIPTIONS_EXCEPTIONSURL);
if (success)
{
- final List<SubscriptionInfo> subs = engine.getListedSubscriptions();
- Collections.sort(subs);
- this.removeAll();
-
- for (final SubscriptionInfo sub : subs)
- {
- final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForUrl(sub.getUrl());
+ initialize();
+ }
+ }
- Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
- if (!(aaLink.equals(sub.getUrl()) || sub.getTitle().startsWith("__"))
- && resInt != null
- && (info == null || info.getPrefixes().isEmpty() || sub.getType() != SubscriptionInfo.Type.ADS))
- {
+ private void initialize()
+ {
+ final List<SubscriptionInfo> subs = engine.getMoreBlockingPreferenceSubscriptions();
+ sortSubscriptionsByRelevance(subs);
+ this.removeAll();
- final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(this.getContext());
+ for (final SubscriptionInfo sub : subs)
+ {
+ Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
+ final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(this.getContext());
- if (sub.isEnabled())
- {
- final StringBuilder sb = new StringBuilder();
- sb.append(this.getContext().getString(R.string.last_update));
- sb.append(' ');
- final long timestamp = sub.getLastUpdateTime();
- if (timestamp > 0)
- {
- sb.append(DateUtils.formatDateTime(this.getContext(), timestamp,
- DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
- }
- else
- {
- sb.append(this.getContext().getString(R.string.last_update_never));
- }
- cbp.setSummary(sb.toString());
- }
-
- cbp.setTitle(this.getContext().getString(resInt));
- cbp.setChecked(sub.isEnabled());
- cbp.setPersistent(false);
- cbp.setKey(sub.getId());
- cbp.setOnPreferenceChangeListener(this);
- this.addPreference(cbp);
+ if (sub.isEnabled())
+ {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(this.getContext().getString(R.string.last_update));
+ sb.append(' ');
+ final long timestamp = sub.getLastUpdateTime();
+ if (timestamp > 0)
+ {
+ sb.append(DateUtils.formatDateTime(this.getContext(), timestamp,
+ DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
+ }
+ else
+ {
+ sb.append(this.getContext().getString(R.string.last_update_never));
+ }
+ cbp.setSummary(sb.toString());
+ }
+ else
+ {
+ if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
+ {
+ engine.removeSubscriptionById(sub.getId());
+ continue;
}
}
+
+ cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resInt));
+ cbp.setChecked(sub.isEnabled());
+ cbp.setPersistent(false);
+ cbp.setKey(sub.getId());
+ cbp.setOnPreferenceChangeListener(this);
+ this.addPreference(cbp);
}
+
+ final UrlInputOpenerPreference urlPreference = new UrlInputOpenerPreference(this.getContext(), false);
+ urlPreference.setIcon(null);
+ urlPreference.setLayoutResource(R.layout.add_filter_by_url_pref);
+ urlPreference.setDialogTitle(R.string.add_other_list);
+ urlPreference.getEditText().setHint(R.string.add_other_list_url);
+ urlPreference.setOnUrlReadyListener(new UrlInputOpenerPreference.OnUrlReadyListener()
+ {
+ @Override
+ public void onUrlReady(String url)
+ {
+ if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://"))
+ {
+ url = "http://" + url;
+ }
+
+ try
+ {
+ engine.createAndAddSubscriptionFromUrl(url, MoreBlockingPreferenceCategory.this);
+ }
+ catch (IOException e)
+ {
+ Log.e(getClass().getSimpleName(), "Unable to add subscription from url", e);
+ }
+ }
+ });
+ this.addPreference(urlPreference);
+ }
+
+ private void sortSubscriptionsByRelevance(final List<SubscriptionInfo> moreBlockingPreferenceSubscriptions)
+ {
+ Collections.sort(moreBlockingPreferenceSubscriptions, new Comparator<SubscriptionInfo>()
+ {
+ @Override
+ public int compare(SubscriptionInfo o1, SubscriptionInfo o2)
+ {
+ if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.containsKey(o2.getUrl()))
+ {
+ return o1.getTitle().compareTo(o2.getTitle());
+ }
+
+ if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && !URL_TO_RES_ID_MAP.containsKey(o2.getUrl()))
+ {
+ return -1;
+ }
+
+ if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.containsKey(o2.getUrl()))
+ {
+ return 1;
+ }
+
+ return 0;
+ }
+ });
}
@Override
public boolean onPreferenceChange(final Preference preference, final Object newValue)
{
final String id = preference.getKey();
final boolean enabled = (Boolean) newValue;
this.engine.changeSubscriptionState(id, enabled);
return true;
}
+
+ @Override
+ public void subscriptionAdded()
+ {
+ initialize();
+ }
}

Powered by Google App Engine
This is Rietveld