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

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/MoreBlockingPreferenceCategory.java

Issue 29716681: Issue 6454 - IllegalStateException crash (Closed)
Patch Set: Add final modifiers and renamed removeOnEngineCreatedCallback Created March 8, 2018, 2:50 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.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
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
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 && engine != null)
René Jeschke 2018/03/09 10:11:03 NIT: Unrelated change, but I don't mind. It's up t
130 { 130 {
131 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) 131 engine.removeSubscriptionById(sub.getId());
132 { 132 continue;
133 engine.removeSubscriptionById(sub.getId());
134 continue;
135 }
136 } 133 }
137 134
138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt)); 135 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI nt));
139 cbp.setChecked(sub.isEnabled()); 136 cbp.setChecked(sub.isEnabled());
140 cbp.setPersistent(false); 137 cbp.setPersistent(false);
141 cbp.setKey(sub.getId()); 138 cbp.setKey(sub.getId());
142 cbp.setOnPreferenceChangeListener(this); 139 cbp.setOnPreferenceChangeListener(this);
143 this.addPreference(cbp); 140 this.addPreference(cbp);
144 } 141 }
145 142
146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(this.getContext()); 143 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo gPreference(this.getContext());
147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp e.URL); 144 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp e.URL);
148 urlPreference.setTitle(R.string.add_another_list); 145 urlPreference.setTitle(R.string.add_another_list);
149 urlPreference.setDialogTitle(R.string.add_another_list); 146 urlPreference.setDialogTitle(R.string.add_another_list);
150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint); 147 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint);
151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener() 148 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI nputReadyListener()
152 { 149 {
153 @Override 150 @Override
154 public void onInputReady(String input) 151 public void onInputReady(String input)
155 { 152 {
156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s tartsWith("https://")) 153 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s tartsWith("https://"))
157 { 154 {
158 input = "http://" + input; 155 input = "http://" + input;
159 } 156 }
160 157
161 try 158 try
162 { 159 {
163 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa tegory.this); 160 if (engine != null)
161 {
162 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreference Category.this);
163 }
164 } 164 }
165 catch (IOException e) 165 catch (IOException e)
166 { 166 {
167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e); 167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url ", e);
168 } 168 }
169 } 169 }
170 }); 170 });
171 this.addPreference(urlPreference); 171 this.addPreference(urlPreference);
172 } 172 }
173 173
(...skipping 20 matching lines...) Expand all
194 } 194 }
195 195
196 return 0; 196 return 0;
197 } 197 }
198 }); 198 });
199 } 199 }
200 200
201 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions() 201 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions()
202 { 202 {
203 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<> (5); 203 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<> (5);
204 if (engine == null)
205 {
206 return moreBlockingPreferenceSubscriptions;
207 }
208
204 for (SubscriptionInfo sub : engine.getListedSubscriptions()) 209 for (SubscriptionInfo sub : engine.getListedSubscriptions())
205 { 210 {
206 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU rl(sub.getUrl()); 211 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU rl(sub.getUrl());
207 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); 212 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl());
208 213
209 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) 214 if (sub.getType() == SubscriptionInfo.Type.CUSTOM)
210 { 215 {
211 moreBlockingPreferenceSubscriptions.add(sub); 216 moreBlockingPreferenceSubscriptions.add(sub);
212 continue; 217 continue;
213 } 218 }
(...skipping 12 matching lines...) Expand all
226 continue; 231 continue;
227 } 232 }
228 } 233 }
229 234
230 return moreBlockingPreferenceSubscriptions; 235 return moreBlockingPreferenceSubscriptions;
231 } 236 }
232 237
233 @Override 238 @Override
234 public boolean onPreferenceChange(final Preference preference, final Object ne wValue) 239 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
235 { 240 {
236 final String id = preference.getKey(); 241 if (engine != null)
237 final boolean enabled = (Boolean) newValue; 242 {
238 243 final String id = preference.getKey();
239 this.engine.changeSubscriptionState(id, enabled); 244 final boolean enabled = (Boolean) newValue;
240 245 this.engine.changeSubscriptionState(id, enabled);
246 }
241 return true; 247 return true;
242 } 248 }
243 249
244 @Override 250 @Override
245 public void subscriptionAdded() 251 public void subscriptionAdded()
246 { 252 {
247 refreshEntries(); 253 refreshEntries();
248 } 254 }
249 } 255 }
OLDNEW

Powered by Google App Engine
This is Rietveld