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: fixes Created Oct. 4, 2017, 3:14 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,40 @@
* 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.ArrayList;
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 +54,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<>();
anton 2017/10/10 06:28:06 according to "programming to interface" i'd prefer
jens 2017/10/10 08:09:10 I did not introduce URL_TO_RES_ID_MAP but only cha
anton 2017/10/10 08:15:06 okay, then it's fine for now
diegocarloslima 2017/10/10 13:08:28 URL_TO_RES_ID_MAP is now only used here, isn't it?
jens 2017/10/10 13:56:40 As I have to make changes anyways, I'll change tha
jens 2017/10/10 13:56:41 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 +85,161 @@ 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());
+ refreshEntries();
+ }
+ }
- 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 refreshEntries()
+ {
+ final List<SubscriptionInfo> subs = 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 InputValidatorDialogPreference urlPreference = new InputValidatorDialogPreference(this.getContext());
+ urlPreference.setValidationType(InputValidatorDialogPreference.ValidationType.URL);
+ urlPreference.setTitle(R.string.add_another_list);
+ urlPreference.setDialogTitle(R.string.add_another_list);
+ urlPreference.getEditText().setHint(R.string.add_another_list_url_hint);
+ urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnInputReadyListener()
+ {
+ @Override
+ public void onInputReady(String input)
+ {
+ if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().startsWith("https://"))
+ {
+ input = "http://" + input;
+ }
+
+ try
+ {
+ engine.createAndAddSubscriptionFromUrl(input, 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;
+ }
+ });
+ }
+
+ private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions()
+ {
+ List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<>(5);
+ for (SubscriptionInfo sub : engine.getListedSubscriptions())
+ {
+ final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForUrl(sub.getUrl());
+ Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
+
+ if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
+ {
+ moreBlockingPreferenceSubscriptions.add(sub);
+ }
+
+ if (info != null && !info.isComplete() && sub.isEnabled())
+ {
+ moreBlockingPreferenceSubscriptions.add(sub);
+ }
+
+ if ((!(engine.isAcceptableAdsUrl(sub)) || sub.getTitle().startsWith("__"))
+ && resInt != null
+ && (info == null || info.getPrefixes().isEmpty() || sub.getType() != SubscriptionInfo.Type.ADS))
+ {
+ moreBlockingPreferenceSubscriptions.add(sub);
+ }
+ }
+
+ return moreBlockingPreferenceSubscriptions;
}
@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()
+ {
+ refreshEntries();
+ }
}

Powered by Google App Engine
This is Rietveld