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: Renamed URLInputPreference and adjusted layout design Created Sept. 15, 2017, 12:36 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;
20 import java.util.Collections; 21 import java.util.Collections;
21 import java.util.HashMap; 22 import java.util.Comparator;
22 import java.util.List; 23 import java.util.List;
23 24
24 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
25 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 25 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
26 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 26 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
27 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; 27 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
28 import org.adblockplus.adblockplussbrowser.R; 28 import org.adblockplus.adblockplussbrowser.R;
29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence; 29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref erence;
30 30
31 import android.content.Context; 31 import android.content.Context;
32 import android.preference.Preference; 32 import android.preference.Preference;
33 import android.preference.PreferenceCategory; 33 import android.preference.PreferenceCategory;
34 import android.preference.Preference.OnPreferenceChangeListener; 34 import android.preference.Preference.OnPreferenceChangeListener;
35 import android.text.format.DateUtils; 35 import android.text.format.DateUtils;
36 import android.util.AttributeSet; 36 import android.util.AttributeSet;
37 import android.util.Log;
38
39 import static org.adblockplus.sbrowser.contentblocker.engine.Engine.URL_TO_RES_I D_MAP;
37 40
38 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s 41 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement s
39 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener 42 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su bscriptionAddedCallback
40 { 43 {
41 private Engine engine = null; 44 private Engine engine = null;
42 private static final int[] WHITELISTED_LIST_TITLES = 45 private static final int[] WHITELISTED_LIST_TITLES =
43 { 46 {
44 R.string.subscription_disable_tracking, 47 R.string.subscription_disable_tracking,
45 R.string.subscription_disable_malware, 48 R.string.subscription_disable_malware,
46 R.string.subscription_disable_anti_adblock, 49 R.string.subscription_disable_anti_adblock,
47 R.string.subscription_disable_social_media 50 R.string.subscription_disable_social_media
48 }; 51 };
49 52
50 private static final String[] WHITELISTED_LIST_URLS = 53 private static final String[] WHITELISTED_LIST_URLS =
51 { 54 {
52 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", 55 "https://easylist-downloads.adblockplus.org/easyprivacy.txt",
53 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", 56 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt",
54 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", 57 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt",
55 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 58 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
56 }; 59 };
57 60
58 private static final HashMap<String, Integer> URL_TO_RES_ID_MAP = new HashMap< >();
59
60 static 61 static
61 { 62 {
62 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++) 63 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++)
63 { 64 {
64 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] ); 65 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i] );
65 } 66 }
66 } 67 }
67 68
68 public MoreBlockingPreferenceCategory(final Context context) 69 public MoreBlockingPreferenceCategory(final Context context)
69 { 70 {
70 super(context); 71 super(context);
71 } 72 }
72 73
73 public MoreBlockingPreferenceCategory(final Context context, final AttributeSe t attrs) 74 public MoreBlockingPreferenceCategory(final Context context, final AttributeSe t attrs)
74 { 75 {
75 super(context, attrs); 76 super(context, attrs);
76 } 77 }
77 78
78 @Override 79 @Override
79 protected void onAttachedToActivity() 80 protected void onAttachedToActivity()
80 { 81 {
81 EngineService.startService(this.getContext().getApplicationContext(), this); 82 EngineService.startService(this.getContext().getApplicationContext(), this);
82 super.onAttachedToActivity(); 83 super.onAttachedToActivity();
83 } 84 }
84 85
85 @Override 86 @Override
86 public void onEngineCreated(final Engine engine, final boolean success) 87 public void onEngineCreated(final Engine engine, final boolean success)
87 { 88 {
88 this.engine = engine; 89 this.engine = engine;
89 final String aaLink = engine.getPrefsDefault(Engine.SUBSCRIPTIONS_EXCEPTIONS URL);
90 90
91 if (success) 91 if (success)
92 { 92 {
93 final List<SubscriptionInfo> subs = engine.getListedSubscriptions(); 93 initialize();
94 Collections.sort(subs); 94 }
95 this.removeAll(); 95 }
96 96
97 for (final SubscriptionInfo sub : subs) 97 private void initialize()
98 {
99 final List<SubscriptionInfo> subs = engine.getMoreBlockingPreferenceSubscrip tions();
100 sortSubscriptionsByRelevance(subs);
101 this.removeAll();
102
103 for (final SubscriptionInfo sub : subs)
104 {
105 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
106 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th is.getContext());
107
108 if (sub.isEnabled())
98 { 109 {
99 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoFo rUrl(sub.getUrl()); 110 final StringBuilder sb = new StringBuilder();
100 111 sb.append(this.getContext().getString(R.string.last_update));
101 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); 112 sb.append(' ');
102 if (!(aaLink.equals(sub.getUrl()) || sub.getTitle().startsWith("__")) 113 final long timestamp = sub.getLastUpdateTime();
103 && resInt != null 114 if (timestamp > 0)
104 && (info == null || info.getPrefixes().isEmpty() || sub.getType() != SubscriptionInfo.Type.ADS))
105 { 115 {
106 116 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp,
107 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreferenc e(this.getContext()); 117 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
108 118 }
109 if (sub.isEnabled()) 119 else
110 { 120 {
111 final StringBuilder sb = new StringBuilder(); 121 sb.append(this.getContext().getString(R.string.last_update_never));
112 sb.append(this.getContext().getString(R.string.last_update)); 122 }
113 sb.append(' '); 123 cbp.setSummary(sb.toString());
114 final long timestamp = sub.getLastUpdateTime(); 124 }
115 if (timestamp > 0) 125 else
116 { 126 {
117 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp, 127 if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
118 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)); 128 {
119 } 129 engine.removeSubscriptionById(sub.getId());
120 else 130 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 } 131 }
134 } 132 }
133
134 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt));
135 cbp.setChecked(sub.isEnabled());
136 cbp.setPersistent(false);
137 cbp.setKey(sub.getId());
138 cbp.setOnPreferenceChangeListener(this);
139 this.addPreference(cbp);
135 } 140 }
141
142 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(
143 this.getContext(), InputValidatorDialogPreference.ValidationType.URL);
144 urlPreference.setTitle(R.string.add_other_list);
145 urlPreference.setDialogTitle(R.string.add_other_list);
146 urlPreference.getEditText().setHint(R.string.add_other_list_url_hint);
147 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener()
148 {
149 @Override
150 public void onInputReady(String url)
151 {
152 if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().start sWith("https://"))
153 {
154 url = "http://" + url;
155 }
156
157 try
158 {
159 engine.createAndAddSubscriptionFromUrl(url, MoreBlockingPreferenceCate gory.this);
160 }
161 catch (IOException e)
162 {
163 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e);
164 }
165 }
166 });
167 this.addPreference(urlPreference);
168 }
169
170 private void sortSubscriptionsByRelevance(final List<SubscriptionInfo> moreBlo ckingPreferenceSubscriptions)
171 {
172 Collections.sort(moreBlockingPreferenceSubscriptions, new Comparator<Subscri ptionInfo>()
173 {
174 @Override
175 public int compare(SubscriptionInfo o1, SubscriptionInfo o2)
176 {
177 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.cont ainsKey(o2.getUrl()))
178 {
179 return o1.getTitle().compareTo(o2.getTitle());
180 }
181
182 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && !URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl()))
183 {
184 return -1;
185 }
186
187 if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.con tainsKey(o2.getUrl()))
188 {
189 return 1;
190 }
191
192 return 0;
193 }
194 });
136 } 195 }
137 196
138 @Override 197 @Override
139 public boolean onPreferenceChange(final Preference preference, final Object ne wValue) 198 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
140 { 199 {
141 final String id = preference.getKey(); 200 final String id = preference.getKey();
142 final boolean enabled = (Boolean) newValue; 201 final boolean enabled = (Boolean) newValue;
143 202
144 this.engine.changeSubscriptionState(id, enabled); 203 this.engine.changeSubscriptionState(id, enabled);
145 204
146 return true; 205 return true;
147 } 206 }
207
208 @Override
209 public void subscriptionAdded()
210 {
211 initialize();
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 }
148 } 213 }
OLDNEW

Powered by Google App Engine
This is Rietveld