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

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

Issue 29716681: Issue 6454 - IllegalStateException crash (Closed)
Patch Set: Adjusting engine check Created March 9, 2018, 11:42 a.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.util.Collections; 20 import java.util.Collections;
21 import java.util.HashMap; 21 import java.util.HashMap;
22 import java.util.List; 22 import java.util.List;
23 import java.util.Locale; 23 import java.util.Locale;
24 24
25 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo; 25 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
26 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 26 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
27 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 27 import org.adblockplus.sbrowser.contentblocker.engine.EngineManager;
28 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; 28 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilinePreferenceCa tegory; 29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilinePreferenceCa tegory;
30 import org.adblockplus.adblockplussbrowser.R; 30 import org.adblockplus.adblockplussbrowser.R;
31 31
32 import android.annotation.SuppressLint; 32 import android.annotation.SuppressLint;
33 import android.content.Context; 33 import android.content.Context;
34 import android.preference.CheckBoxPreference; 34 import android.preference.CheckBoxPreference;
35 import android.preference.Preference; 35 import android.preference.Preference;
36 import android.preference.Preference.OnPreferenceChangeListener; 36 import android.preference.Preference.OnPreferenceChangeListener;
37 import android.text.format.DateUtils; 37 import android.text.format.DateUtils;
38 import android.util.AttributeSet; 38 import android.util.AttributeSet;
39 39
40 @SuppressLint("DefaultLocale") 40 @SuppressLint("DefaultLocale")
41 public class ListedSubscriptionsPreferenceCategory extends MultilinePreferenceCa tegory implements 41 public class ListedSubscriptionsPreferenceCategory extends MultilinePreferenceCa tegory implements
42 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener 42 EngineManager.OnEngineCreatedCallback, OnPreferenceChangeListener
43 { 43 {
44 private Engine engine = null; 44 private Engine engine = null;
45 private boolean isEnabledView = false; 45 private boolean isEnabledView = false;
46 46
47 private static final String[] LANGUAGE_TRANSLATIONS = 47 private static final String[] LANGUAGE_TRANSLATIONS =
48 { 48 {
49 "id", "Bahasa Indonesia", 49 "id", "Bahasa Indonesia",
50 "he", "עברית" 50 "he", "עברית"
51 }; 51 };
52 52
(...skipping 13 matching lines...) Expand all
66 } 66 }
67 67
68 public ListedSubscriptionsPreferenceCategory(final Context context, final Attr ibuteSet attrs) 68 public ListedSubscriptionsPreferenceCategory(final Context context, final Attr ibuteSet attrs)
69 { 69 {
70 super(context, attrs); 70 super(context, attrs);
71 } 71 }
72 72
73 @Override 73 @Override
74 protected void onAttachedToActivity() 74 protected void onAttachedToActivity()
75 { 75 {
76 EngineService.startService(this.getContext().getApplicationContext(), this); 76 EngineManager.getInstance().retrieveEngine(getContext(), this);
77 super.onAttachedToActivity(); 77 super.onAttachedToActivity();
78 } 78 }
79 79
80 @Override 80 @Override
81 public void onEngineCreated(final Engine engine, final boolean success) 81 public void onEngineCreated(final Engine engine)
82 { 82 {
83 this.engine = engine; 83 this.engine = engine;
84 if (this.engine != null)
85 {
86 initEntries();
87 }
88 }
89
90 @Override
91 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
92 {
93 if (engine != null)
94 {
95 final String id = preference.getKey();
96 final boolean enabled = (Boolean) newValue;
97 this.engine.changeSubscriptionState(id, enabled);
98 }
99 return true;
100 }
101
102 private void initEntries()
103 {
84 this.isEnabledView = this.getTitleRes() == R.string.enabled_subscriptions; 104 this.isEnabledView = this.getTitleRes() == R.string.enabled_subscriptions;
105 this.removeAll();
85 106
86 final HashMap<String, Locale> localeMap = new HashMap<>(); 107 final HashMap<String, Locale> localeMap = new HashMap<>();
87 for (final Locale l : Locale.getAvailableLocales()) 108 for (final Locale l : Locale.getAvailableLocales())
88 { 109 {
89 final String lang = l.getLanguage(); 110 final String lang = l.getLanguage();
90 if (!lang.isEmpty()) 111 if (!lang.isEmpty())
91 { 112 {
92 localeMap.put(lang.toLowerCase(), l); 113 localeMap.put(lang.toLowerCase(), l);
93 } 114 }
94 } 115 }
95 116
96 if (success) 117 final List<SubscriptionInfo> subs = engine.getListedSubscriptions();
118 Collections.sort(subs);
119
120 for (final SubscriptionInfo sub : subs)
97 { 121 {
98 final List<SubscriptionInfo> subs = engine.getListedSubscriptions(); 122 if (sub.isEnabled() == this.isEnabledView)
99 Collections.sort(subs); 123 {
100 this.removeAll(); 124 switch (sub.getType())
125 {
126 case ADS:
127 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionIn foForUrl(
128 sub.getUrl());
129 if (info != null && !info.getPrefixes().isEmpty() && info.isComplete ())
130 {
131 final CheckBoxPreference cbp = new CheckBoxPreference(this.getCont ext());
132 if (this.isEnabledView)
133 {
134 final StringBuilder sb = new StringBuilder();
135 sb.append(this.getContext().getString(R.string.last_update));
136 sb.append(' ');
137 final long timestamp = sub.getLastUpdateTime();
138 if (timestamp > 0)
139 {
140 sb.append(DateUtils.formatDateTime(this.getContext(), timestam p,
141 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
142 }
143 else
144 {
145 sb.append(this.getContext().getString(R.string.last_update_nev er));
146 }
147 cbp.setSummary(sb.toString());
148 }
101 149
102 for (final SubscriptionInfo sub : subs) 150 cbp.setTitle(sub.getTitle());
103 { 151 final String[] prefixes = info.getPrefixes().split(",");
104 if (sub.isEnabled() == this.isEnabledView) 152 final StringBuilder sb = new StringBuilder();
105 { 153 for (String p : prefixes)
106 switch (sub.getType())
107 {
108 case ADS:
109 final DefaultSubscriptionInfo info = engine.getDefaultSubscription InfoForUrl(
110 sub.getUrl());
111 if (info != null && !info.getPrefixes().isEmpty() && info.isComple te())
112 { 154 {
113 final CheckBoxPreference cbp = new CheckBoxPreference(this.getCo ntext()); 155 final Locale loc = localeMap.get(p.trim().toLowerCase());
114 if (this.isEnabledView) 156 if (loc != null)
115 { 157 {
116 final StringBuilder sb = new StringBuilder(); 158 if (sb.length() > 0)
117 sb.append(this.getContext().getString(R.string.last_update));
118 sb.append(' ');
119 final long timestamp = sub.getLastUpdateTime();
120 if (timestamp > 0)
121 { 159 {
122 sb.append(DateUtils.formatDateTime(this.getContext(), timest amp, 160 sb.append(", ");
123 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME) );
124 } 161 }
125 else 162 sb.append(loc.getDisplayLanguage(loc));
163 }
164 else
165 {
166 final String name = LANGUAGE_TRANSLATION_MAP.get(p.trim().toLo werCase());
126 { 167 {
127 sb.append(this.getContext().getString(R.string.last_update_n ever)); 168 if (name != null)
128 }
129 cbp.setSummary(sb.toString());
130 }
131
132 cbp.setTitle(sub.getTitle());
133 final String[] prefixes = info.getPrefixes().split(",");
134 final StringBuilder sb = new StringBuilder();
135 for (String p : prefixes)
136 {
137 final Locale loc = localeMap.get(p.trim().toLowerCase());
138 if (loc != null)
139 {
140 if (sb.length() > 0)
141 { 169 {
142 sb.append(", "); 170 if (sb.length() > 0)
143 }
144 sb.append(loc.getDisplayLanguage(loc));
145 }
146 else
147 {
148 final String name = LANGUAGE_TRANSLATION_MAP.get(p.trim().to LowerCase());
149 {
150 if (name != null)
151 { 171 {
152 if (sb.length() > 0) 172 sb.append(", ");
153 {
154 sb.append(", ");
155 }
156 sb.append(name);
157 } 173 }
174 sb.append(name);
158 } 175 }
159 } 176 }
160 } 177 }
178 }
161 179
162 if (sb.length() > 0) 180 if (sb.length() > 0)
163 { 181 {
164 cbp.setTitle(sb.toString()); 182 cbp.setTitle(sb.toString());
165 } 183 }
166 184
167 cbp.setChecked(sub.isEnabled()); 185 cbp.setChecked(sub.isEnabled());
168 cbp.setPersistent(false); 186 cbp.setPersistent(false);
169 cbp.setKey(sub.getId()); 187 cbp.setKey(sub.getId());
170 cbp.setOnPreferenceChangeListener(this); 188 cbp.setOnPreferenceChangeListener(this);
171 this.addPreference(cbp); 189 this.addPreference(cbp);
172 } 190 }
173 break; 191 break;
174 default: 192 default:
175 break; 193 break;
176 }
177 } 194 }
178 } 195 }
179 } 196 }
180 } 197 }
181
182 @Override
183 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
184 {
185 final String id = preference.getKey();
186 final boolean enabled = (Boolean) newValue;
187
188 this.engine.changeSubscriptionState(id, enabled);
189
190 return true;
191 }
192 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld