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

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/MainPreferences.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 org.adblockplus.sbrowser.contentblocker.engine.Engine; 20 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
21 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 21 import org.adblockplus.sbrowser.contentblocker.engine.EngineManager;
22 import org.adblockplus.adblockplussbrowser.R; 22 import org.adblockplus.adblockplussbrowser.R;
23 import org.adblockplus.sbrowser.contentblocker.util.ConnectivityUtils; 23 import org.adblockplus.sbrowser.contentblocker.util.ConnectivityUtils;
24 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; 24 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils;
25 25
26 import android.app.AlertDialog; 26 import android.app.AlertDialog;
27 import android.app.Fragment; 27 import android.app.Fragment;
28 import android.app.ProgressDialog; 28 import android.app.ProgressDialog;
29 import android.content.DialogInterface; 29 import android.content.DialogInterface;
30 import android.content.DialogInterface.OnClickListener; 30 import android.content.DialogInterface.OnClickListener;
31 import android.content.Intent; 31 import android.content.Intent;
32 import android.net.Uri; 32 import android.net.Uri;
33 import android.os.Bundle; 33 import android.os.Bundle;
34 import android.preference.Preference; 34 import android.preference.Preference;
35 import android.preference.PreferenceActivity; 35 import android.preference.PreferenceActivity;
36 import android.preference.PreferenceManager; 36 import android.preference.PreferenceManager;
37 import android.text.Html; 37 import android.text.Html;
38 import android.util.Log; 38 import android.util.Log;
39 import android.view.Gravity; 39 import android.view.Gravity;
40 import android.widget.Button; 40 import android.widget.Button;
41 import android.widget.LinearLayout; 41 import android.widget.LinearLayout;
42 42
43 public class MainPreferences extends PreferenceActivity implements 43 public class MainPreferences extends PreferenceActivity implements
44 EngineService.OnEngineCreatedCallback, Engine.SubscriptionUpdateCallback, 44 EngineManager.OnEngineCreatedCallback, Engine.SubscriptionUpdateCallback,
45 Preference.OnPreferenceClickListener 45 Preference.OnPreferenceClickListener
46 { 46 {
47 private static final String TAG = MainPreferences.class.getSimpleName(); 47 private static final String TAG = MainPreferences.class.getSimpleName();
48 private Engine engine = null; 48 private Engine engine = null;
49 private AlertDialog dialog; 49 private AlertDialog dialog;
50 private int dialogTitleResId; 50 private int dialogTitleResId;
51 51
52 @Override 52 @Override
53 public void onCreate(Bundle savedInstanceState) 53 public void onCreate(Bundle savedInstanceState)
54 { 54 {
55 super.onCreate(savedInstanceState); 55 super.onCreate(savedInstanceState);
56 PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false); 56 PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false);
57 57
58 this.getFragmentManager() 58 this.getFragmentManager()
59 .beginTransaction() 59 .beginTransaction()
60 .replace(android.R.id.content, new Preferences(), Preferences.class.getS impleName()) 60 .replace(android.R.id.content, new Preferences(), Preferences.class.getS impleName())
61 .commit(); 61 .commit();
62 } 62 }
63 63
64 @Override 64 @Override
65 protected void onStart() 65 protected void onStart()
66 { 66 {
67 this.dialogTitleResId = R.string.initialization_title; 67 this.dialogTitleResId = R.string.initialization_title;
68 this.dialog = ProgressDialog.show(this, 68 this.dialog = ProgressDialog.show(this,
69 this.getString(this.dialogTitleResId), 69 this.getString(this.dialogTitleResId),
70 this.getString(R.string.initialization_message)); 70 this.getString(R.string.initialization_message));
71 super.onStart(); 71 super.onStart();
72 SharedPrefsUtils.registerOnSharedPreferenceChangeListener(this, listener); 72 SharedPrefsUtils.registerOnSharedPreferenceChangeListener(this, listener);
73 EngineService.startService(this.getApplicationContext(), this); 73 EngineManager.getInstance().retrieveEngine(this, this);
74 } 74 }
75 75
76 @Override 76 @Override
77 protected void onStop() 77 protected void onStop()
78 { 78 {
79 super.onStop(); 79 super.onStop();
80 SharedPrefsUtils.unregisterOnSharedPreferenceChangeListener(this, listener); 80 SharedPrefsUtils.unregisterOnSharedPreferenceChangeListener(this, listener);
81 this.dismissDialog(); 81 this.dismissDialog();
82 } 82 }
83 83
84 @Override
85 protected void onDestroy()
86 {
87 EngineManager.getInstance().removeOnEngineCreatedCallback(this);
88 super.onDestroy();
89 }
90
84 private void dismissDialog() 91 private void dismissDialog()
85 { 92 {
86 if (this.dialog != null) 93 if (this.dialog != null)
87 { 94 {
88 this.dialogTitleResId = 0; 95 this.dialogTitleResId = 0;
89 this.dialog.dismiss(); 96 this.dialog.dismiss();
90 this.dialog = null; 97 this.dialog = null;
91 } 98 }
92 } 99 }
93 100
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 186
180 final Button btNeutral = this.dialog.getButton(AlertDialog.BUTTON_NEUTRAL) ; 187 final Button btNeutral = this.dialog.getButton(AlertDialog.BUTTON_NEUTRAL) ;
181 final LinearLayout.LayoutParams btNeutralLayoutParams = (LinearLayout.Layo utParams) btNeutral.getLayoutParams(); 188 final LinearLayout.LayoutParams btNeutralLayoutParams = (LinearLayout.Layo utParams) btNeutral.getLayoutParams();
182 btNeutralLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT; 189 btNeutralLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
183 btNeutralLayoutParams.gravity = Gravity.CENTER; 190 btNeutralLayoutParams.gravity = Gravity.CENTER;
184 btNeutral.setLayoutParams(btNeutralLayoutParams); 191 btNeutral.setLayoutParams(btNeutralLayoutParams);
185 } 192 }
186 } 193 }
187 194
188 @Override 195 @Override
189 public void onEngineCreated(Engine engine, boolean success) 196 public void onEngineCreated(Engine engine)
190 { 197 {
191 Log.d(TAG, "onEngineCreated: " + success); 198 Log.d(TAG, "onEngineCreated: " + (engine != null));
192 this.engine = success ? engine : null; 199 this.engine = engine;
193 200
194 if (engine != null) 201 if (engine != null)
195 { 202 {
196 this.engine.setSubscriptionUpdateCallback(this); 203 this.engine.setSubscriptionUpdateCallback(this);
197 } 204 }
198 205
199 if (this.dialogTitleResId == R.string.initialization_title) 206 if (this.dialogTitleResId == R.string.initialization_title)
200 { 207 {
201 this.dismissDialog(); 208 this.dismissDialog();
202 209
203 this.checkForCompatibleSBrowserAndProceed(); 210 this.checkForCompatibleSBrowserAndProceed();
204 } 211 }
205 212
206 final Fragment preferecesFragment = getFragmentManager() 213 final Fragment preferecesFragment = getFragmentManager()
207 .findFragmentByTag(Preferences.class.getSimpleName()); 214 .findFragmentByTag(Preferences.class.getSimpleName());
208 final Preference button = ((Preferences) preferecesFragment) 215 if (preferecesFragment != null)
209 .findPreference(getString(R.string.key_force_update_subscriptions)); 216 {
210 button.setOnPreferenceClickListener(this); 217 final Preference button = ((Preferences) preferecesFragment)
218 .findPreference(getString(R.string.key_force_update_subscriptions));
219 button.setOnPreferenceClickListener(this);
220 }
211 } 221 }
212 222
213 @Override 223 @Override
214 public void subscriptionUpdateRequested(final boolean enabled) 224 public void subscriptionUpdateRequested(final boolean enabled)
215 { 225 {
216 this.dialog = ProgressDialog.show(this, null, enabled 226 this.dialog = ProgressDialog.show(this, null, enabled
217 ? getString(R.string.add_subscription_dialog_message) 227 ? getString(R.string.add_subscription_dialog_message)
218 : getString(R.string.remove_subscription_dialog_message)); 228 : getString(R.string.remove_subscription_dialog_message));
219 } 229 }
220 230
221 @Override 231 @Override
222 public void subscriptionUpdatedApplied() 232 public void subscriptionUpdatedApplied()
223 { 233 {
224 this.dismissDialog(); 234 this.dismissDialog();
225 } 235 }
226 236
227 @Override 237 @Override
228 public boolean onPreferenceClick(Preference preference) 238 public boolean onPreferenceClick(Preference preference)
229 { 239 {
230 if (getString(R.string.key_force_update_subscriptions).equals(preference.get Key())) 240 if (getString(R.string.key_force_update_subscriptions).equals(preference.get Key()))
231 { 241 {
232 if (ConnectivityUtils.hasNonMeteredConnection(this)) 242 if (ConnectivityUtils.hasNonMeteredConnection(this))
233 { 243 {
234 engine.forceUpdateSubscriptions(false); 244 if (engine != null)
245 {
246 engine.forceUpdateSubscriptions(false);
247 }
235 } 248 }
236 else 249 else
237 { 250 {
238 boolean meteredInternetAvailable = ConnectivityUtils.canUseInternet(this , true); 251 boolean meteredInternetAvailable = ConnectivityUtils.canUseInternet(this , true);
239 this.dialog = new AlertDialog.Builder(this) 252 this.dialog = new AlertDialog.Builder(this)
240 .setTitle(R.string.update_subscriptions) 253 .setTitle(R.string.update_subscriptions)
241 .setMessage( 254 .setMessage(
242 meteredInternetAvailable 255 meteredInternetAvailable
243 ? R.string.metered_connection_warning 256 ? R.string.metered_connection_warning
244 : R.string.check_your_connection) 257 : R.string.check_your_connection)
245 .setNegativeButton(android.R.string.cancel, new OnClickListener() 258 .setNegativeButton(android.R.string.cancel, new OnClickListener()
246 { 259 {
247 @Override 260 @Override
248 public void onClick(DialogInterface dialog, int which) 261 public void onClick(DialogInterface dialog, int which)
249 { 262 {
250 dialog.cancel(); 263 dialog.cancel();
251 } 264 }
252 }) 265 })
253 .create(); 266 .create();
254 267
255 if (meteredInternetAvailable) 268 if (meteredInternetAvailable)
256 { 269 {
257 this.dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(andro id.R.string.yes), 270 this.dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(andro id.R.string.yes),
258 new OnClickListener() 271 new OnClickListener()
259 { 272 {
260 @Override 273 @Override
261 public void onClick(DialogInterface dialogInterface, int i) 274 public void onClick(DialogInterface dialogInterface, int i)
262 { 275 {
263 engine.forceUpdateSubscriptions(true); 276 if (engine != null)
277 {
278 engine.forceUpdateSubscriptions(true);
279 }
264 } 280 }
265 }); 281 });
266 } 282 }
267 this.dialog.show(); 283 this.dialog.show();
268 } 284 }
269 return true; 285 return true;
270 } 286 }
271 return false; 287 return false;
272 } 288 }
273 289
274 private final SharedPrefsUtils.OnSharedPreferenceChangeListener listener = 290 private final SharedPrefsUtils.OnSharedPreferenceChangeListener listener =
275 new SharedPrefsUtils.OnSharedPreferenceChangeListener() 291 new SharedPrefsUtils.OnSharedPreferenceChangeListener()
276 { 292 {
277 @Override 293 @Override
278 protected void onSharedPreferenceChanged(String key) 294 protected void onSharedPreferenceChanged(String key)
279 { 295 {
280 if (getString(R.string.key_automatic_updates).equals(key) && engine != nul l) 296 if (getString(R.string.key_automatic_updates).equals(key) && engine != nul l)
281 { 297 {
282 engine.connectivityChanged(); 298 engine.connectivityChanged();
283 } 299 }
284 else if (getString(R.string.key_acceptable_ads).equals(key)) 300 else if (getString(R.string.key_acceptable_ads).equals(key) && engine != n ull)
285 { 301 {
286 final boolean enabled = SharedPrefsUtils.getBoolean 302 final boolean enabled = SharedPrefsUtils.getBoolean
287 (MainPreferences.this, R.string.key_acceptable_ads, true); 303 (MainPreferences.this, R.string.key_acceptable_ads, true);
288 final String id = "url:" + engine.getPrefsDefault(Engine.SUBSCRIPTIONS_E XCEPTIONSURL); 304 final String id = "url:" + engine.getPrefsDefault(Engine.SUBSCRIPTIONS_E XCEPTIONSURL);
289 Log.d(TAG, "Acceptable ads " + (enabled ? "enabled" : "disabled")); 305 Log.d(TAG, "Acceptable ads " + (enabled ? "enabled" : "disabled"));
290 engine.changeSubscriptionState(id, enabled); 306 engine.changeSubscriptionState(id, enabled);
291 } 307 }
292 else if (getString(R.string.key_application_activated).equals(key)) 308 else if (getString(R.string.key_application_activated).equals(key))
293 { 309 {
294 if (dialogTitleResId == R.string.setup_dialog_title) 310 if (dialogTitleResId == R.string.setup_dialog_title)
295 { 311 {
296 dismissDialog(); 312 dismissDialog();
297 } 313 }
298 } 314 }
299 } 315 }
300 }; 316 };
301 } 317 }
OLDNEW

Powered by Google App Engine
This is Rietveld