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

Powered by Google App Engine
This is Rietveld