OLD | NEW |
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.ArrayList; |
22 import java.util.Collections; | 22 import java.util.Collections; |
23 import java.util.Comparator; | 23 import java.util.Comparator; |
24 import java.util.HashMap; | 24 import java.util.HashMap; |
25 import java.util.List; | 25 import java.util.List; |
26 import java.util.Map; | 26 import java.util.Map; |
27 | 27 |
28 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo; | 28 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo; |
29 import org.adblockplus.sbrowser.contentblocker.engine.Engine; | 29 import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
30 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; | 30 import org.adblockplus.sbrowser.contentblocker.engine.EngineManager; |
31 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; | 31 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; |
32 import org.adblockplus.adblockplussbrowser.R; | 32 import org.adblockplus.adblockplussbrowser.R; |
33 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref
erence; | 33 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref
erence; |
34 | 34 |
35 import android.content.Context; | 35 import android.content.Context; |
36 import android.preference.Preference; | 36 import android.preference.Preference; |
37 import android.preference.PreferenceCategory; | 37 import android.preference.PreferenceCategory; |
38 import android.preference.Preference.OnPreferenceChangeListener; | 38 import android.preference.Preference.OnPreferenceChangeListener; |
39 import android.text.format.DateUtils; | 39 import android.text.format.DateUtils; |
40 import android.util.AttributeSet; | 40 import android.util.AttributeSet; |
41 import android.util.Log; | 41 import android.util.Log; |
42 | 42 |
43 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement
s | 43 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement
s |
44 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su
bscriptionAddedCallback | 44 EngineManager.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su
bscriptionAddedCallback |
45 { | 45 { |
46 private Engine engine = null; | 46 private Engine engine = null; |
47 private static final int[] WHITELISTED_LIST_TITLES = | 47 private static final int[] WHITELISTED_LIST_TITLES = |
48 { | 48 { |
49 R.string.subscription_disable_tracking, | 49 R.string.subscription_disable_tracking, |
50 R.string.subscription_disable_malware, | 50 R.string.subscription_disable_malware, |
51 R.string.subscription_disable_anti_adblock, | 51 R.string.subscription_disable_anti_adblock, |
52 R.string.subscription_disable_social_media | 52 R.string.subscription_disable_social_media |
53 }; | 53 }; |
54 | 54 |
(...skipping 21 matching lines...) Expand all Loading... |
76 } | 76 } |
77 | 77 |
78 public MoreBlockingPreferenceCategory(final Context context, final AttributeSe
t attrs) | 78 public MoreBlockingPreferenceCategory(final Context context, final AttributeSe
t attrs) |
79 { | 79 { |
80 super(context, attrs); | 80 super(context, attrs); |
81 } | 81 } |
82 | 82 |
83 @Override | 83 @Override |
84 protected void onAttachedToActivity() | 84 protected void onAttachedToActivity() |
85 { | 85 { |
86 EngineService.startService(this.getContext().getApplicationContext(), this); | 86 EngineManager.getInstance().retrieveEngine(getContext(), this); |
87 super.onAttachedToActivity(); | 87 super.onAttachedToActivity(); |
88 } | 88 } |
89 | 89 |
90 @Override | 90 @Override |
91 public void onEngineCreated(final Engine engine, final boolean success) | 91 public void onEngineCreated(final Engine engine) |
92 { | 92 { |
93 this.engine = engine; | 93 this.engine = engine; |
94 | 94 |
95 if (success) | 95 if (engine != null) |
96 { | 96 { |
97 refreshEntries(); | 97 refreshEntries(); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 private void refreshEntries() | 101 private void refreshEntries() |
102 { | 102 { |
103 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions()
; | 103 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions()
; |
104 sortSubscriptionsByRelevance(subs); | 104 sortSubscriptionsByRelevance(subs); |
105 this.removeAll(); | 105 this.removeAll(); |
(...skipping 13 matching lines...) Expand all Loading... |
119 { | 119 { |
120 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp, | 120 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp, |
121 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)); | 121 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)); |
122 } | 122 } |
123 else | 123 else |
124 { | 124 { |
125 sb.append(this.getContext().getString(R.string.last_update_never)); | 125 sb.append(this.getContext().getString(R.string.last_update_never)); |
126 } | 126 } |
127 cbp.setSummary(sb.toString()); | 127 cbp.setSummary(sb.toString()); |
128 } | 128 } |
129 else | 129 else if (sub.getType() == SubscriptionInfo.Type.CUSTOM) |
130 { | 130 { |
131 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) | 131 if (engine != null) |
132 { | 132 { |
133 engine.removeSubscriptionById(sub.getId()); | 133 engine.removeSubscriptionById(sub.getId()); |
134 continue; | |
135 } | 134 } |
| 135 continue; |
136 } | 136 } |
137 | 137 |
138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI
nt)); | 138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI
nt)); |
139 cbp.setChecked(sub.isEnabled()); | 139 cbp.setChecked(sub.isEnabled()); |
140 cbp.setPersistent(false); | 140 cbp.setPersistent(false); |
141 cbp.setKey(sub.getId()); | 141 cbp.setKey(sub.getId()); |
142 cbp.setOnPreferenceChangeListener(this); | 142 cbp.setOnPreferenceChangeListener(this); |
143 this.addPreference(cbp); | 143 this.addPreference(cbp); |
144 } | 144 } |
145 | 145 |
146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo
gPreference(this.getContext()); | 146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo
gPreference(this.getContext()); |
147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp
e.URL); | 147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp
e.URL); |
148 urlPreference.setTitle(R.string.add_another_list); | 148 urlPreference.setTitle(R.string.add_another_list); |
149 urlPreference.setDialogTitle(R.string.add_another_list); | 149 urlPreference.setDialogTitle(R.string.add_another_list); |
150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint); | 150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint); |
151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI
nputReadyListener() | 151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI
nputReadyListener() |
152 { | 152 { |
153 @Override | 153 @Override |
154 public void onInputReady(String input) | 154 public void onInputReady(String input) |
155 { | 155 { |
156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s
tartsWith("https://")) | 156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s
tartsWith("https://")) |
157 { | 157 { |
158 input = "http://" + input; | 158 input = "http://" + input; |
159 } | 159 } |
160 | 160 |
161 try | 161 try |
162 { | 162 { |
163 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa
tegory.this); | 163 if (engine != null) |
| 164 { |
| 165 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreference
Category.this); |
| 166 } |
164 } | 167 } |
165 catch (IOException e) | 168 catch (IOException e) |
166 { | 169 { |
167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url
", e); | 170 Log.e(getClass().getSimpleName(), "Unable to add subscription from url
", e); |
168 } | 171 } |
169 } | 172 } |
170 }); | 173 }); |
171 this.addPreference(urlPreference); | 174 this.addPreference(urlPreference); |
172 } | 175 } |
173 | 176 |
(...skipping 20 matching lines...) Expand all Loading... |
194 } | 197 } |
195 | 198 |
196 return 0; | 199 return 0; |
197 } | 200 } |
198 }); | 201 }); |
199 } | 202 } |
200 | 203 |
201 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions() | 204 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions() |
202 { | 205 { |
203 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<>
(5); | 206 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<>
(5); |
| 207 if (engine == null) |
| 208 { |
| 209 return moreBlockingPreferenceSubscriptions; |
| 210 } |
| 211 |
204 for (SubscriptionInfo sub : engine.getListedSubscriptions()) | 212 for (SubscriptionInfo sub : engine.getListedSubscriptions()) |
205 { | 213 { |
206 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU
rl(sub.getUrl()); | 214 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU
rl(sub.getUrl()); |
207 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); | 215 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); |
208 | 216 |
209 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) | 217 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) |
210 { | 218 { |
211 moreBlockingPreferenceSubscriptions.add(sub); | 219 moreBlockingPreferenceSubscriptions.add(sub); |
212 continue; | 220 continue; |
213 } | 221 } |
(...skipping 12 matching lines...) Expand all Loading... |
226 continue; | 234 continue; |
227 } | 235 } |
228 } | 236 } |
229 | 237 |
230 return moreBlockingPreferenceSubscriptions; | 238 return moreBlockingPreferenceSubscriptions; |
231 } | 239 } |
232 | 240 |
233 @Override | 241 @Override |
234 public boolean onPreferenceChange(final Preference preference, final Object ne
wValue) | 242 public boolean onPreferenceChange(final Preference preference, final Object ne
wValue) |
235 { | 243 { |
236 final String id = preference.getKey(); | 244 if (engine != null) |
237 final boolean enabled = (Boolean) newValue; | 245 { |
238 | 246 final String id = preference.getKey(); |
239 this.engine.changeSubscriptionState(id, enabled); | 247 final boolean enabled = (Boolean) newValue; |
240 | 248 this.engine.changeSubscriptionState(id, enabled); |
| 249 } |
241 return true; | 250 return true; |
242 } | 251 } |
243 | 252 |
244 @Override | 253 @Override |
245 public void subscriptionAdded() | 254 public void subscriptionAdded() |
246 { | 255 { |
247 refreshEntries(); | 256 refreshEntries(); |
248 } | 257 } |
249 } | 258 } |
OLD | NEW |