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

Delta Between Two Patch Sets: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java

Issue 29524668: Issue 3916 - Supporting Adding filter lists via URL (Closed)
Left Patch Set: Issue 3916 - Supporting Adding filer lists via URL Created Aug. 23, 2017, 2:20 p.m.
Right Patch Set: Use TAG constant for log, fix in MoreBlockingPreferenceCategory Created Oct. 10, 2017, 1:57 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.sbrowser.contentblocker; 18 package org.adblockplus.sbrowser.contentblocker;
19 19
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.util.ArrayList;
21 import java.util.Collections; 22 import java.util.Collections;
22 import java.util.Comparator; 23 import java.util.Comparator;
23 import java.util.HashMap; 24 import java.util.HashMap;
24 import java.util.List; 25 import java.util.List;
25 26 import java.util.Map;
27
28 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
26 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 29 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
27 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 30 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
28 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; 31 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
29 import org.adblockplus.adblockplussbrowser.R; 32 import org.adblockplus.adblockplussbrowser.R;
30 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence; 33 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence;
31 34
32 import android.content.Context; 35 import android.content.Context;
33 import android.preference.Preference; 36 import android.preference.Preference;
34 import android.preference.PreferenceCategory; 37 import android.preference.PreferenceCategory;
35 import android.preference.Preference.OnPreferenceChangeListener; 38 import android.preference.Preference.OnPreferenceChangeListener;
(...skipping 14 matching lines...) Expand all
50 }; 53 };
51 54
52 private static final String[] WHITELISTED_LIST_URLS = 55 private static final String[] WHITELISTED_LIST_URLS =
53 { 56 {
54 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", 57 "https://easylist-downloads.adblockplus.org/easyprivacy.txt",
55 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", 58 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt",
56 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", 59 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt",
57 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 60 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
58 }; 61 };
59 62
60 public static final HashMap<String, Integer> URL_TO_RES_ID_MAP = new HashMap<> (); 63 private static final Map<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.
61 64
62 static 65 static
63 { 66 {
64 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++) 67 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++)
65 { 68 {
66 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] ); 69 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] );
67 } 70 }
68 } 71 }
69 72
70 public MoreBlockingPreferenceCategory(final Context context) 73 public MoreBlockingPreferenceCategory(final Context context)
(...skipping 13 matching lines...) Expand all
84 super.onAttachedToActivity(); 87 super.onAttachedToActivity();
85 } 88 }
86 89
87 @Override 90 @Override
88 public void onEngineCreated(final Engine engine, final boolean success) 91 public void onEngineCreated(final Engine engine, final boolean success)
89 { 92 {
90 this.engine = engine; 93 this.engine = engine;
91 94
92 if (success) 95 if (success)
93 { 96 {
94 initialize(); 97 refreshEntries();
95 } 98 }
96 } 99 }
97 100
98 private void initialize() 101 private void refreshEntries()
99 { 102 {
100 final List<SubscriptionInfo> subs = engine.getMoreBlockingPreferenceSubscrip tions(); 103 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions() ;
101 sortSubscriptionsByRelevance(subs); 104 sortSubscriptionsByRelevance(subs);
102 this.removeAll(); 105 this.removeAll();
103 106
104 for (final SubscriptionInfo sub : subs) 107 for (final SubscriptionInfo sub : subs)
105 { 108 {
106 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); 109 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
107 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext()); 110 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext());
108 111
109 if (sub.isEnabled()) 112 if (sub.isEnabled())
110 { 113 {
(...skipping 22 matching lines...) Expand all
133 } 136 }
134 137
135 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt)); 138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt));
136 cbp.setChecked(sub.isEnabled()); 139 cbp.setChecked(sub.isEnabled());
137 cbp.setPersistent(false); 140 cbp.setPersistent(false);
138 cbp.setKey(sub.getId()); 141 cbp.setKey(sub.getId());
139 cbp.setOnPreferenceChangeListener(this); 142 cbp.setOnPreferenceChangeListener(this);
140 this.addPreference(cbp); 143 this.addPreference(cbp);
141 } 144 }
142 145
143 final UrlInputOpenerPreference urlPreference = new UrlInputOpenerPreference( this.getContext(), false); 146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(this.getContext());
144 urlPreference.setIcon(null); 147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp e.URL);
145 urlPreference.setLayoutResource(R.layout.add_filter_by_url_pref); 148 urlPreference.setTitle(R.string.add_another_list);
146 urlPreference.setDialogTitle(R.string.add_other_list); 149 urlPreference.setDialogTitle(R.string.add_another_list);
147 urlPreference.getEditText().setHint(R.string.add_other_list_url); 150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint);
148 urlPreference.setOnUrlReadyListener(new UrlInputOpenerPreference.OnUrlReadyL istener() 151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener()
149 { 152 {
150 @Override 153 @Override
151 public void onUrlReady(String url) 154 public void onInputReady(String input)
152 { 155 {
153 if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().start sWith("https://")) 156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s tartsWith("https://"))
154 { 157 {
155 url = "http://" + url; 158 input = "http://" + input;
156 } 159 }
157 160
158 try 161 try
159 { 162 {
160 engine.createAndAddSubscriptionFromUrl(url, MoreBlockingPreferenceCate gory.this); 163 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa tegory.this);
161 } 164 }
162 catch (IOException e) 165 catch (IOException e)
163 { 166 {
164 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e); 167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e);
165 } 168 }
166 } 169 }
167 }); 170 });
168 this.addPreference(urlPreference); 171 this.addPreference(urlPreference);
169 } 172 }
170 173
(...skipping 17 matching lines...) Expand all
188 if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl())) 191 if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl()))
189 { 192 {
190 return 1; 193 return 1;
191 } 194 }
192 195
193 return 0; 196 return 0;
194 } 197 }
195 }); 198 });
196 } 199 }
197 200
201 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions()
202 {
203 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<> (5);
204 for (SubscriptionInfo sub : engine.getListedSubscriptions())
205 {
206 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU rl(sub.getUrl());
207 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
208
209 if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
210 {
211 moreBlockingPreferenceSubscriptions.add(sub);
212 continue;
213 }
214
215 if (info != null && !info.isComplete() && sub.isEnabled())
216 {
217 moreBlockingPreferenceSubscriptions.add(sub);
218 continue;
219 }
220
221 if ((!(engine.isAcceptableAdsUrl(sub)) || sub.getTitle().startsWith("__"))
222 && resInt != null
223 && (info == null || info.getPrefixes().isEmpty() || sub.getType() != S ubscriptionInfo.Type.ADS))
224 {
225 moreBlockingPreferenceSubscriptions.add(sub);
226 continue;
227 }
228 }
229
230 return moreBlockingPreferenceSubscriptions;
231 }
232
198 @Override 233 @Override
199 public boolean onPreferenceChange(final Preference preference, final Object ne wValue) 234 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
200 { 235 {
201 final String id = preference.getKey(); 236 final String id = preference.getKey();
202 final boolean enabled = (Boolean) newValue; 237 final boolean enabled = (Boolean) newValue;
203 238
204 this.engine.changeSubscriptionState(id, enabled); 239 this.engine.changeSubscriptionState(id, enabled);
205 240
206 return true; 241 return true;
207 } 242 }
208 243
209 @Override 244 @Override
210 public void subscriptionAdded() 245 public void subscriptionAdded()
211 { 246 {
212 initialize(); 247 refreshEntries();
213 } 248 }
214 } 249 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld