OLD | NEW |
1 /* | 1 /* |
2 * This file is part of the Adblock Plus, | 2 * This file is part of the Adblock Plus, |
3 * Copyright (C) 2006-2012 Eyeo GmbH | 3 * Copyright (C) 2006-2012 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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 import android.preference.ListPreference; | 45 import android.preference.ListPreference; |
46 import android.preference.PreferenceManager; | 46 import android.preference.PreferenceManager; |
47 import android.text.format.DateFormat; | 47 import android.text.format.DateFormat; |
48 import android.util.Log; | 48 import android.util.Log; |
49 import android.view.View; | 49 import android.view.View; |
50 import android.widget.TextView; | 50 import android.widget.TextView; |
51 | 51 |
52 import com.actionbarsherlock.view.Menu; | 52 import com.actionbarsherlock.view.Menu; |
53 import com.actionbarsherlock.view.MenuInflater; | 53 import com.actionbarsherlock.view.MenuInflater; |
54 import com.actionbarsherlock.view.MenuItem; | 54 import com.actionbarsherlock.view.MenuItem; |
55 import com.actionbarsherlock.view.Window; | |
56 | 55 |
57 /** | 56 /** |
58 * Main settings UI. | 57 * Main settings UI. |
59 */ | 58 */ |
60 public class Preferences extends SummarizedPreferences | 59 public class Preferences extends SummarizedPreferences |
61 { | 60 { |
62 private static final String TAG = "Preferences"; | 61 private static final String TAG = "Preferences"; |
63 | 62 |
64 private static final int ABOUT_DIALOG = 1; | 63 private static final int ABOUT_DIALOG = 1; |
65 private static final int HIDEICONWARNING_DIALOG = 2; | 64 private static final int HIDEICONWARNING_DIALOG = 2; |
66 | 65 |
67 private static ProxyService proxyService = null; | 66 private static ProxyService proxyService = null; |
68 | 67 |
69 private RefreshableListPreference subscriptionList; | 68 private RefreshableListPreference subscriptionList; |
70 | 69 |
71 private boolean trafficDetected = false; | |
72 private String subscriptionSummary; | 70 private String subscriptionSummary; |
73 | 71 |
74 @Override | 72 @Override |
75 public void onCreate(Bundle savedInstanceState) | 73 public void onCreate(Bundle savedInstanceState) |
76 { | 74 { |
77 super.onCreate(savedInstanceState); | 75 super.onCreate(savedInstanceState); |
78 | 76 |
79 PreferenceManager.setDefaultValues(this, R.xml.preferences, false); | 77 PreferenceManager.setDefaultValues(this, R.xml.preferences, false); |
80 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, false); | 78 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, false); |
81 setContentView(R.layout.preferences); | 79 setContentView(R.layout.preferences); |
(...skipping 27 matching lines...) Expand all Loading... |
109 String[] entries = new String[subscriptions.size()]; | 107 String[] entries = new String[subscriptions.size()]; |
110 String[] entryValues = new String[subscriptions.size()]; | 108 String[] entryValues = new String[subscriptions.size()]; |
111 int i = 0; | 109 int i = 0; |
112 for (Subscription subscription : subscriptions) | 110 for (Subscription subscription : subscriptions) |
113 { | 111 { |
114 entries[i] = subscription.title; | 112 entries[i] = subscription.title; |
115 entryValues[i] = subscription.url; | 113 entryValues[i] = subscription.url; |
116 i++; | 114 i++; |
117 } | 115 } |
118 subscriptionList.setEntries(entries); | 116 subscriptionList.setEntries(entries); |
119 subscriptionList.setEntryValues(entryValues); | 117 subscriptionList.setEntryValues(entryValues); |
120 } | 118 } |
121 | 119 |
122 @Override | 120 @Override |
123 protected void onStart() | 121 protected void onStart() |
124 { | 122 { |
125 super.onStart(); | 123 super.onStart(); |
126 AdblockPlus application = AdblockPlus.getApplication(); | 124 AdblockPlus application = AdblockPlus.getApplication(); |
127 application.startEngine(); | 125 application.startEngine(); |
128 application.startInteractive(); | 126 application.startInteractive(); |
129 } | 127 } |
130 | 128 |
131 @Override | 129 @Override |
132 public void onResume() | 130 public void onResume() |
133 { | 131 { |
134 super.onResume(); | 132 super.onResume(); |
135 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); | 133 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); |
136 | 134 |
137 final AdblockPlus application = AdblockPlus.getApplication(); | 135 final AdblockPlus application = AdblockPlus.getApplication(); |
138 | 136 |
139 boolean firstRun = false; | 137 boolean firstRun = false; |
140 | 138 |
141 // Get current subscription | 139 // Get current subscription |
142 String current = prefs.getString(getString(R.string.pref_subscription), (Str
ing) null); | 140 String current = prefs.getString(getString(R.string.pref_subscription), (Str
ing) null); |
143 | 141 |
144 // If there is no current subscription autoselect one | 142 // If there is no current subscription autoselect one |
145 if (current == null) | 143 if (current == null) |
146 { | 144 { |
147 firstRun = true; | 145 firstRun = true; |
148 Subscription offer = application.offerSubscription(); | 146 Subscription offer = application.offerSubscription(); |
149 current = offer.url; | 147 current = offer.url; |
150 if (offer != null) | 148 if (offer != null) |
151 { | 149 { |
152 subscriptionList.setValue(offer.url); | 150 subscriptionList.setValue(offer.url); |
153 application.setSubscription(offer); | 151 application.setSubscription(offer); |
(...skipping 13 matching lines...) Expand all Loading... |
167 }); | 165 }); |
168 | 166 |
169 // Set subscription status message | 167 // Set subscription status message |
170 if (subscriptionSummary != null) | 168 if (subscriptionSummary != null) |
171 subscriptionList.setSummary(subscriptionSummary); | 169 subscriptionList.setSummary(subscriptionSummary); |
172 else | 170 else |
173 setPrefSummary(subscriptionList); | 171 setPrefSummary(subscriptionList); |
174 | 172 |
175 // Time to start listening for events | 173 // Time to start listening for events |
176 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI
ON_STATUS)); | 174 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI
ON_STATUS)); |
177 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_FILTER_MAT
CHES)); | |
178 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA
NGED)); | 175 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA
NGED)); |
179 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI
LED)); | 176 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI
LED)); |
180 | 177 |
181 final String url = current; | 178 final String url = current; |
182 | 179 |
183 // Initialize subscription verification | 180 // Initialize subscription verification |
184 (new Thread() | 181 (new Thread() |
185 { | 182 { |
186 @Override | 183 @Override |
187 public void run() | 184 public void run() |
(...skipping 25 matching lines...) Expand all Loading... |
213 @Override | 210 @Override |
214 public void onPause() | 211 public void onPause() |
215 { | 212 { |
216 super.onPause(); | 213 super.onPause(); |
217 try | 214 try |
218 { | 215 { |
219 unregisterReceiver(receiver); | 216 unregisterReceiver(receiver); |
220 } | 217 } |
221 catch (IllegalArgumentException e) | 218 catch (IllegalArgumentException e) |
222 { | 219 { |
223 // ignore - it is thrown if receiver is not registered but it can not be t
rue in normal conditions | 220 // ignore - it is thrown if receiver is not registered but it can not be |
| 221 // true in normal conditions |
224 } | 222 } |
225 unbindService(proxyServiceConnection); | 223 unbindService(proxyServiceConnection); |
226 proxyService = null; | 224 proxyService = null; |
| 225 |
| 226 hideConfigurationMsg(); |
227 } | 227 } |
228 | 228 |
229 @Override | 229 @Override |
230 protected void onStop() | 230 protected void onStop() |
231 { | 231 { |
232 super.onStop(); | 232 super.onStop(); |
233 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); | 233 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); |
234 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); | 234 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); |
235 AdblockPlus application = AdblockPlus.getApplication(); | 235 AdblockPlus application = AdblockPlus.getApplication(); |
236 application.stopInteractive(); | 236 application.stopInteractive(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 private void setEnabled(boolean enabled) | 270 private void setEnabled(boolean enabled) |
271 { | 271 { |
272 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen
ces(this).edit(); | 272 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen
ces(this).edit(); |
273 editor.putBoolean(getString(R.string.pref_enabled), enabled); | 273 editor.putBoolean(getString(R.string.pref_enabled), enabled); |
274 editor.commit(); | 274 editor.commit(); |
275 ((CheckBoxPreference) findPreference(getString(R.string.pref_enabled))).setC
hecked(enabled); | 275 ((CheckBoxPreference) findPreference(getString(R.string.pref_enabled))).setC
hecked(enabled); |
276 } | 276 } |
277 | 277 |
278 /** | 278 /** |
279 * Checks if ProxyService is running. | 279 * Checks if ProxyService is running. |
280 * | 280 * |
281 * @return true if service is running | 281 * @return true if service is running |
282 */ | 282 */ |
283 private boolean isServiceRunning() | 283 private boolean isServiceRunning() |
284 { | 284 { |
285 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVIC
E); | 285 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVIC
E); |
286 // Actually it returns not only running services, so extra check is required | 286 // Actually it returns not only running services, so extra check is required |
287 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL
UE)) | 287 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL
UE)) |
288 { | 288 { |
289 if ("org.adblockplus.android.ProxyService".equals(service.service.getClass
Name()) && service.pid > 0) | 289 if ("org.adblockplus.android.ProxyService".equals(service.service.getClass
Name()) && service.pid > 0) |
290 return true; | 290 return true; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 { | 403 { |
404 @Override | 404 @Override |
405 public void onReceive(Context context, Intent intent) | 405 public void onReceive(Context context, Intent intent) |
406 { | 406 { |
407 String action = intent.getAction(); | 407 String action = intent.getAction(); |
408 Bundle extra = intent.getExtras(); | 408 Bundle extra = intent.getExtras(); |
409 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED)) | 409 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED)) |
410 { | 410 { |
411 if (extra.getBoolean("enabled")) | 411 if (extra.getBoolean("enabled")) |
412 { | 412 { |
413 // If service is enabled in manual mode, show configuration message | 413 // Service is enabled in manual mode |
414 if (extra.getBoolean("manual")) | 414 if (extra.getBoolean("manual")) |
415 { | 415 { |
416 showConfigurationMsg(getString(R.string.msg_configuration, extra.get
Int("port"))); | 416 // Proxy is properly configured |
| 417 if (extra.getBoolean("configured")) |
| 418 hideConfigurationMsg(); |
| 419 else |
| 420 showConfigurationMsg(getString(R.string.msg_configuration, extra.g
etInt("port"))); |
417 } | 421 } |
418 } | 422 } |
419 else | 423 else |
420 { | 424 { |
421 setEnabled(false); | 425 setEnabled(false); |
422 hideConfigurationMsg(); | 426 hideConfigurationMsg(); |
423 } | 427 } |
424 } | 428 } |
425 if (action.equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) | |
426 { | |
427 // Hide configuration message if traffic is detected for the first time | |
428 if (!trafficDetected) | |
429 hideConfigurationMsg(); | |
430 trafficDetected = true; | |
431 } | |
432 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) | 429 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) |
433 { | 430 { |
434 String msg = extra.getString("msg"); | 431 String msg = extra.getString("msg"); |
435 new AlertDialog.Builder(Preferences.this).setTitle(R.string.error).setMe
ssage(msg).setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(R.strin
g.ok, null).create().show(); | 432 new AlertDialog.Builder(Preferences.this).setTitle(R.string.error).setMe
ssage(msg).setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(R.strin
g.ok, null).create().show(); |
436 setEnabled(false); | 433 setEnabled(false); |
437 } | 434 } |
438 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) | 435 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) |
439 { | 436 { |
440 final String text = extra.getString("text"); | 437 final String text = extra.getString("text"); |
441 final long time = extra.getLong("time"); | 438 final long time = extra.getLong("time"); |
442 runOnUiThread(new Runnable() | 439 runOnUiThread(new Runnable() |
443 { | 440 { |
444 public void run() | 441 public void run() |
445 { | 442 { |
446 setSubscriptionStatus(text, time); | 443 setSubscriptionStatus(text, time); |
447 } | 444 } |
448 }); | 445 }); |
449 } | 446 } |
450 } | 447 } |
451 }; | 448 }; |
452 | 449 |
453 /** | 450 /** |
454 * Constructs and updates subscription status text. | 451 * Constructs and updates subscription status text. |
455 * | 452 * |
456 * @param text | 453 * @param text |
457 * status message | 454 * status message |
458 * @param time | 455 * @param time |
459 * time of last change | 456 * time of last change |
460 */ | 457 */ |
461 private void setSubscriptionStatus(String text, long time) | 458 private void setSubscriptionStatus(String text, long time) |
462 { | 459 { |
463 ListPreference subscriptionList = (ListPreference) findPreference(getString(
R.string.pref_subscription)); | 460 ListPreference subscriptionList = (ListPreference) findPreference(getString(
R.string.pref_subscription)); |
464 CharSequence summary = subscriptionList.getEntry(); | 461 CharSequence summary = subscriptionList.getEntry(); |
465 StringBuilder builder = new StringBuilder(); | 462 StringBuilder builder = new StringBuilder(); |
(...skipping 22 matching lines...) Expand all Loading... |
488 } | 485 } |
489 subscriptionSummary = builder.toString(); | 486 subscriptionSummary = builder.toString(); |
490 subscriptionList.setSummary(subscriptionSummary); | 487 subscriptionList.setSummary(subscriptionSummary); |
491 } | 488 } |
492 } | 489 } |
493 | 490 |
494 @Override | 491 @Override |
495 protected void onRestoreInstanceState(Bundle state) | 492 protected void onRestoreInstanceState(Bundle state) |
496 { | 493 { |
497 super.onRestoreInstanceState(state); | 494 super.onRestoreInstanceState(state); |
498 trafficDetected = state.getBoolean("trafficDetected"); | |
499 subscriptionSummary = state.getString("subscriptionSummary"); | 495 subscriptionSummary = state.getString("subscriptionSummary"); |
500 } | 496 } |
501 | 497 |
502 @Override | 498 @Override |
503 protected void onSaveInstanceState(Bundle outState) | 499 protected void onSaveInstanceState(Bundle outState) |
504 { | 500 { |
505 outState.putString("subscriptionSummary", subscriptionSummary); | 501 outState.putString("subscriptionSummary", subscriptionSummary); |
506 outState.putBoolean("trafficDetected", trafficDetected); | |
507 super.onSaveInstanceState(outState); | 502 super.onSaveInstanceState(outState); |
508 } | 503 } |
509 | 504 |
510 private ServiceConnection proxyServiceConnection = new ServiceConnection() | 505 private ServiceConnection proxyServiceConnection = new ServiceConnection() |
511 { | 506 { |
512 public void onServiceConnected(ComponentName className, IBinder service) | 507 public void onServiceConnected(ComponentName className, IBinder service) |
513 { | 508 { |
514 proxyService = ((ProxyService.LocalBinder) service).getService(); | 509 proxyService = ((ProxyService.LocalBinder) service).getService(); |
515 Log.d(TAG, "Proxy service connected"); | 510 Log.d(TAG, "Proxy service connected"); |
516 | 511 |
517 if (!trafficDetected && proxyService.isManual()) | 512 if (proxyService.isManual() && proxyService.noTraffic()) |
518 showConfigurationMsg(getString(R.string.msg_configuration, proxyServic
e.port)); | 513 showConfigurationMsg(getString(R.string.msg_configuration, proxyService.
port)); |
519 } | 514 } |
520 | 515 |
521 public void onServiceDisconnected(ComponentName className) | 516 public void onServiceDisconnected(ComponentName className) |
522 { | 517 { |
523 proxyService = null; | 518 proxyService = null; |
524 Log.d(TAG, "Proxy service disconnected"); | 519 Log.d(TAG, "Proxy service disconnected"); |
525 } | 520 } |
526 }; | 521 }; |
527 } | 522 } |
OLD | NEW |