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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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;
21 import java.util.ArrayList;
20 import java.util.Collections; 22 import java.util.Collections;
23 import java.util.Comparator;
21 import java.util.HashMap; 24 import java.util.HashMap;
22 import java.util.List; 25 import java.util.List;
23 26
24 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo; 27 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
25 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 28 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
26 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 29 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
27 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; 30 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
28 import org.adblockplus.adblockplussbrowser.R; 31 import org.adblockplus.adblockplussbrowser.R;
29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence; 32 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence;
30 33
31 import android.content.Context; 34 import android.content.Context;
32 import android.preference.Preference; 35 import android.preference.Preference;
33 import android.preference.PreferenceCategory; 36 import android.preference.PreferenceCategory;
34 import android.preference.Preference.OnPreferenceChangeListener; 37 import android.preference.Preference.OnPreferenceChangeListener;
35 import android.text.format.DateUtils; 38 import android.text.format.DateUtils;
36 import android.util.AttributeSet; 39 import android.util.AttributeSet;
40 import android.util.Log;
37 41
38 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s 42 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s
39 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener 43 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su bscriptionAddedCallback
40 { 44 {
41 private Engine engine = null; 45 private Engine engine = null;
42 private static final int[] WHITELISTED_LIST_TITLES = 46 private static final int[] WHITELISTED_LIST_TITLES =
43 { 47 {
44 R.string.subscription_disable_tracking, 48 R.string.subscription_disable_tracking,
45 R.string.subscription_disable_malware, 49 R.string.subscription_disable_malware,
46 R.string.subscription_disable_anti_adblock, 50 R.string.subscription_disable_anti_adblock,
47 R.string.subscription_disable_social_media 51 R.string.subscription_disable_social_media
48 }; 52 };
49 53
50 private static final String[] WHITELISTED_LIST_URLS = 54 private static final String[] WHITELISTED_LIST_URLS =
51 { 55 {
52 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", 56 "https://easylist-downloads.adblockplus.org/easyprivacy.txt",
53 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", 57 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt",
54 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", 58 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt",
55 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 59 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
56 }; 60 };
57 61
58 private static final HashMap<String, Integer> URL_TO_RES_ID_MAP = new HashMap< >(); 62 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.
59 63
60 static 64 static
61 { 65 {
62 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++) 66 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++)
63 { 67 {
64 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] ); 68 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] );
65 } 69 }
66 } 70 }
67 71
68 public MoreBlockingPreferenceCategory(final Context context) 72 public MoreBlockingPreferenceCategory(final Context context)
(...skipping 10 matching lines...) Expand all
79 protected void onAttachedToActivity() 83 protected void onAttachedToActivity()
80 { 84 {
81 EngineService.startService(this.getContext().getApplicationContext(), this); 85 EngineService.startService(this.getContext().getApplicationContext(), this);
82 super.onAttachedToActivity(); 86 super.onAttachedToActivity();
83 } 87 }
84 88
85 @Override 89 @Override
86 public void onEngineCreated(final Engine engine, final boolean success) 90 public void onEngineCreated(final Engine engine, final boolean success)
87 { 91 {
88 this.engine = engine; 92 this.engine = engine;
89 final String aaLink = engine.getPrefsDefault(Engine.SUBSCRIPTIONS_EXCEPTIONS URL);
90 93
91 if (success) 94 if (success)
92 { 95 {
93 final List<SubscriptionInfo> subs = engine.getListedSubscriptions(); 96 refreshEntries();
94 Collections.sort(subs); 97 }
95 this.removeAll(); 98 }
96 99
97 for (final SubscriptionInfo sub : subs) 100 private void refreshEntries()
101 {
102 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions() ;
103 sortSubscriptionsByRelevance(subs);
104 this.removeAll();
105
106 for (final SubscriptionInfo sub : subs)
107 {
108 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
109 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext());
110
111 if (sub.isEnabled())
98 { 112 {
99 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoFo rUrl(sub.getUrl()); 113 final StringBuilder sb = new StringBuilder();
100 114 sb.append(this.getContext().getString(R.string.last_update));
101 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); 115 sb.append(' ');
102 if (!(aaLink.equals(sub.getUrl()) || sub.getTitle().startsWith("__")) 116 final long timestamp = sub.getLastUpdateTime();
103 && resInt != null 117 if (timestamp > 0)
104 && (info == null || info.getPrefixes().isEmpty() || sub.getType() != SubscriptionInfo.Type.ADS))
105 { 118 {
106 119 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp,
107 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreferenc e(this.getContext()); 120 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
108 121 }
109 if (sub.isEnabled()) 122 else
110 { 123 {
111 final StringBuilder sb = new StringBuilder(); 124 sb.append(this.getContext().getString(R.string.last_update_never));
112 sb.append(this.getContext().getString(R.string.last_update)); 125 }
113 sb.append(' '); 126 cbp.setSummary(sb.toString());
114 final long timestamp = sub.getLastUpdateTime(); 127 }
115 if (timestamp > 0) 128 else
116 { 129 {
117 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp, 130 if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
118 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)); 131 {
119 } 132 engine.removeSubscriptionById(sub.getId());
120 else 133 continue;
121 {
122 sb.append(this.getContext().getString(R.string.last_update_never)) ;
123 }
124 cbp.setSummary(sb.toString());
125 }
126
127 cbp.setTitle(this.getContext().getString(resInt));
128 cbp.setChecked(sub.isEnabled());
129 cbp.setPersistent(false);
130 cbp.setKey(sub.getId());
131 cbp.setOnPreferenceChangeListener(this);
132 this.addPreference(cbp);
133 } 134 }
134 } 135 }
136
137 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt));
138 cbp.setChecked(sub.isEnabled());
139 cbp.setPersistent(false);
140 cbp.setKey(sub.getId());
141 cbp.setOnPreferenceChangeListener(this);
142 this.addPreference(cbp);
135 } 143 }
144
145 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(this.getContext());
146 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp e.URL);
147 urlPreference.setTitle(R.string.add_another_list);
148 urlPreference.setDialogTitle(R.string.add_another_list);
149 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint);
150 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener()
151 {
152 @Override
153 public void onInputReady(String input)
154 {
155 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s tartsWith("https://"))
156 {
157 input = "http://" + input;
158 }
159
160 try
161 {
162 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa tegory.this);
163 }
164 catch (IOException e)
165 {
166 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e);
167 }
168 }
169 });
170 this.addPreference(urlPreference);
171 }
172
173 private void sortSubscriptionsByRelevance(final List<SubscriptionInfo> moreBlo ckingPreferenceSubscriptions)
174 {
175 Collections.sort(moreBlockingPreferenceSubscriptions, new Comparator<Subscri ptionInfo>()
176 {
177 @Override
178 public int compare(SubscriptionInfo o1, SubscriptionInfo o2)
179 {
180 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.cont ainsKey(o2.getUrl()))
181 {
182 return o1.getTitle().compareTo(o2.getTitle());
183 }
184
185 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && !URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl()))
186 {
187 return -1;
188 }
189
190 if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl()))
191 {
192 return 1;
193 }
194
195 return 0;
196 }
197 });
198 }
199
200 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions()
201 {
202 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<> (5);
203 for (SubscriptionInfo sub : engine.getListedSubscriptions())
204 {
205 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU rl(sub.getUrl());
206 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
207
208 if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
209 {
210 moreBlockingPreferenceSubscriptions.add(sub);
211 }
212
213 if (info != null && !info.isComplete() && sub.isEnabled())
214 {
215 moreBlockingPreferenceSubscriptions.add(sub);
216 }
217
218 if ((!(engine.isAcceptableAdsUrl(sub)) || sub.getTitle().startsWith("__"))
219 && resInt != null
220 && (info == null || info.getPrefixes().isEmpty() || sub.getType() != S ubscriptionInfo.Type.ADS))
221 {
222 moreBlockingPreferenceSubscriptions.add(sub);
223 }
224 }
225
226 return moreBlockingPreferenceSubscriptions;
136 } 227 }
137 228
138 @Override 229 @Override
139 public boolean onPreferenceChange(final Preference preference, final Object ne wValue) 230 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
140 { 231 {
141 final String id = preference.getKey(); 232 final String id = preference.getKey();
142 final boolean enabled = (Boolean) newValue; 233 final boolean enabled = (Boolean) newValue;
143 234
144 this.engine.changeSubscriptionState(id, enabled); 235 this.engine.changeSubscriptionState(id, enabled);
145 236
146 return true; 237 return true;
147 } 238 }
239
240 @Override
241 public void subscriptionAdded()
242 {
243 refreshEntries();
244 }
148 } 245 }
OLDNEW

Powered by Google App Engine
This is Rietveld