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: Renamed URLInputPreference and adjusted layout design Created Sept. 15, 2017, 12:36 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;
24 import java.util.HashMap;
23 import java.util.List; 25 import java.util.List;
24 26 import java.util.Map;
27
28 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
25 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 29 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
26 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 30 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
27 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; 31 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
28 import org.adblockplus.adblockplussbrowser.R; 32 import org.adblockplus.adblockplussbrowser.R;
29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence; 33 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence;
30 34
31 import android.content.Context; 35 import android.content.Context;
32 import android.preference.Preference; 36 import android.preference.Preference;
33 import android.preference.PreferenceCategory; 37 import android.preference.PreferenceCategory;
34 import android.preference.Preference.OnPreferenceChangeListener; 38 import android.preference.Preference.OnPreferenceChangeListener;
35 import android.text.format.DateUtils; 39 import android.text.format.DateUtils;
36 import android.util.AttributeSet; 40 import android.util.AttributeSet;
37 import android.util.Log; 41 import android.util.Log;
38 42
39 import static org.adblockplus.sbrowser.contentblocker.engine.Engine.URL_TO_RES_I D_MAP;
40
41 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s 43 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s
42 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su bscriptionAddedCallback 44 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su bscriptionAddedCallback
43 { 45 {
44 private Engine engine = null; 46 private Engine engine = null;
45 private static final int[] WHITELISTED_LIST_TITLES = 47 private static final int[] WHITELISTED_LIST_TITLES =
46 { 48 {
47 R.string.subscription_disable_tracking, 49 R.string.subscription_disable_tracking,
48 R.string.subscription_disable_malware, 50 R.string.subscription_disable_malware,
49 R.string.subscription_disable_anti_adblock, 51 R.string.subscription_disable_anti_adblock,
50 R.string.subscription_disable_social_media 52 R.string.subscription_disable_social_media
51 }; 53 };
52 54
53 private static final String[] WHITELISTED_LIST_URLS = 55 private static final String[] WHITELISTED_LIST_URLS =
54 { 56 {
55 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", 57 "https://easylist-downloads.adblockplus.org/easyprivacy.txt",
56 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", 58 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt",
57 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", 59 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt",
58 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 60 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
59 }; 61 };
60 62
63 private static final Map<String, Integer> URL_TO_RES_ID_MAP = new HashMap<>();
64
61 static 65 static
62 { 66 {
63 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++) 67 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++)
64 { 68 {
65 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] );
66 } 70 }
67 } 71 }
68 72
69 public MoreBlockingPreferenceCategory(final Context context) 73 public MoreBlockingPreferenceCategory(final Context context)
70 { 74 {
(...skipping 12 matching lines...) Expand all
83 super.onAttachedToActivity(); 87 super.onAttachedToActivity();
84 } 88 }
85 89
86 @Override 90 @Override
87 public void onEngineCreated(final Engine engine, final boolean success) 91 public void onEngineCreated(final Engine engine, final boolean success)
88 { 92 {
89 this.engine = engine; 93 this.engine = engine;
90 94
91 if (success) 95 if (success)
92 { 96 {
93 initialize(); 97 refreshEntries();
94 } 98 }
95 } 99 }
96 100
97 private void initialize() 101 private void refreshEntries()
98 { 102 {
99 final List<SubscriptionInfo> subs = engine.getMoreBlockingPreferenceSubscrip tions(); 103 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions() ;
100 sortSubscriptionsByRelevance(subs); 104 sortSubscriptionsByRelevance(subs);
101 this.removeAll(); 105 this.removeAll();
102 106
103 for (final SubscriptionInfo sub : subs) 107 for (final SubscriptionInfo sub : subs)
104 { 108 {
105 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); 109 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
106 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext()); 110 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext());
107 111
108 if (sub.isEnabled()) 112 if (sub.isEnabled())
109 { 113 {
(...skipping 22 matching lines...) Expand all
132 } 136 }
133 137
134 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt)); 138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt));
135 cbp.setChecked(sub.isEnabled()); 139 cbp.setChecked(sub.isEnabled());
136 cbp.setPersistent(false); 140 cbp.setPersistent(false);
137 cbp.setKey(sub.getId()); 141 cbp.setKey(sub.getId());
138 cbp.setOnPreferenceChangeListener(this); 142 cbp.setOnPreferenceChangeListener(this);
139 this.addPreference(cbp); 143 this.addPreference(cbp);
140 } 144 }
141 145
142 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference( 146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(this.getContext());
143 this.getContext(), InputValidatorDialogPreference.ValidationType.URL); 147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp e.URL);
144 urlPreference.setTitle(R.string.add_other_list); 148 urlPreference.setTitle(R.string.add_another_list);
145 urlPreference.setDialogTitle(R.string.add_other_list); 149 urlPreference.setDialogTitle(R.string.add_another_list);
146 urlPreference.getEditText().setHint(R.string.add_other_list_url_hint); 150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint);
147 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener() 151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener()
148 { 152 {
149 @Override 153 @Override
150 public void onInputReady(String url) 154 public void onInputReady(String input)
151 { 155 {
152 if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().start sWith("https://")) 156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s tartsWith("https://"))
153 { 157 {
154 url = "http://" + url; 158 input = "http://" + input;
155 } 159 }
156 160
157 try 161 try
158 { 162 {
159 engine.createAndAddSubscriptionFromUrl(url, MoreBlockingPreferenceCate gory.this); 163 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa tegory.this);
160 } 164 }
161 catch (IOException e) 165 catch (IOException e)
162 { 166 {
163 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e); 167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e);
164 } 168 }
165 } 169 }
166 }); 170 });
167 this.addPreference(urlPreference); 171 this.addPreference(urlPreference);
168 } 172 }
169 173
(...skipping 17 matching lines...) Expand all
187 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()))
188 { 192 {
189 return 1; 193 return 1;
190 } 194 }
191 195
192 return 0; 196 return 0;
193 } 197 }
194 }); 198 });
195 } 199 }
196 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
197 @Override 233 @Override
198 public boolean onPreferenceChange(final Preference preference, final Object ne wValue) 234 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
199 { 235 {
200 final String id = preference.getKey(); 236 final String id = preference.getKey();
201 final boolean enabled = (Boolean) newValue; 237 final boolean enabled = (Boolean) newValue;
202 238
203 this.engine.changeSubscriptionState(id, enabled); 239 this.engine.changeSubscriptionState(id, enabled);
204 240
205 return true; 241 return true;
206 } 242 }
207 243
208 @Override 244 @Override
209 public void subscriptionAdded() 245 public void subscriptionAdded()
210 { 246 {
211 initialize(); 247 refreshEntries();
diegocarloslima 2017/09/22 17:43:42 Maybe we should change the name of this method to
jens 2017/09/26 10:25:01 Acknowledged.
212 } 248 }
213 } 249 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld