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

Powered by Google App Engine
This is Rietveld