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 22 matching lines...) Expand all Loading... |
33 import android.app.NotificationManager; | 33 import android.app.NotificationManager; |
34 import android.app.PendingIntent; | 34 import android.app.PendingIntent; |
35 import android.app.Service; | 35 import android.app.Service; |
36 import android.content.BroadcastReceiver; | 36 import android.content.BroadcastReceiver; |
37 import android.content.Context; | 37 import android.content.Context; |
38 import android.content.Intent; | 38 import android.content.Intent; |
39 import android.content.IntentFilter; | 39 import android.content.IntentFilter; |
40 import android.content.SharedPreferences; | 40 import android.content.SharedPreferences; |
41 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; | 41 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
42 import android.content.pm.PackageManager.NameNotFoundException; | 42 import android.content.pm.PackageManager.NameNotFoundException; |
| 43 import android.content.res.Resources; |
43 import android.net.ConnectivityManager; | 44 import android.net.ConnectivityManager; |
44 import android.net.NetworkInfo; | 45 import android.net.NetworkInfo; |
45 import android.os.Binder; | 46 import android.os.Binder; |
46 import android.os.Build; | 47 import android.os.Build; |
47 import android.os.Handler; | 48 import android.os.Handler; |
48 import android.os.IBinder; | 49 import android.os.IBinder; |
49 import android.preference.PreferenceManager; | 50 import android.preference.PreferenceManager; |
50 import android.util.Log; | 51 import android.util.Log; |
51 import android.widget.Toast; | 52 import android.widget.Toast; |
52 | 53 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 private String iptables = null; | 109 private String iptables = null; |
109 | 110 |
110 @Override | 111 @Override |
111 public void onCreate() | 112 public void onCreate() |
112 { | 113 { |
113 super.onCreate(); | 114 super.onCreate(); |
114 | 115 |
115 // Get port for local proxy | 116 // Get port for local proxy |
116 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); | 117 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); |
| 118 Resources resources = getResources(); |
117 String p = prefs.getString(getString(R.string.pref_port), null); | 119 String p = prefs.getString(getString(R.string.pref_port), null); |
118 try | 120 try |
119 { | 121 { |
120 port = p != null ? Integer.valueOf(p) : getResources().getInteger(R.intege
r.def_port); | 122 port = p != null ? Integer.valueOf(p) : resources.getInteger(R.integer.def
_port); |
121 } | 123 } |
122 catch (NumberFormatException e) | 124 catch (NumberFormatException e) |
123 { | 125 { |
124 Toast.makeText(this, getString(R.string.msg_badport) + ": " + p, Toast.LEN
GTH_LONG).show(); | 126 Toast.makeText(this, getString(R.string.msg_badport) + ": " + p, Toast.LEN
GTH_LONG).show(); |
125 port = getResources().getInteger(R.integer.def_port); | 127 port = getResources().getInteger(R.integer.def_port); |
126 } | 128 } |
127 | 129 |
128 // Try to read user proxy settings | 130 // Try to read user proxy settings |
129 String proxyHost = null; | 131 String proxyHost = null; |
130 String proxyPort = null; | 132 String proxyPort = null; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 256 |
255 prefs.registerOnSharedPreferenceChangeListener(this); | 257 prefs.registerOnSharedPreferenceChangeListener(this); |
256 | 258 |
257 String msg = getString(transparent ? R.string.notif_all : nativeProxy ? R.st
ring.notif_wifi : R.string.notif_waiting); | 259 String msg = getString(transparent ? R.string.notif_all : nativeProxy ? R.st
ring.notif_wifi : R.string.notif_waiting); |
258 if (!transparent && !nativeProxy) | 260 if (!transparent && !nativeProxy) |
259 { | 261 { |
260 // Initiate no traffic check | 262 // Initiate no traffic check |
261 notrafficHandler = new Handler(); | 263 notrafficHandler = new Handler(); |
262 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); | 264 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); |
263 } | 265 } |
264 // Lock service | 266 // Prepare notification |
265 ongoingNotification = new Notification(); | 267 ongoingNotification = new Notification(); |
266 ongoingNotification.when = 0; | 268 ongoingNotification.when = 0; |
267 contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Preferen
ces.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TA
SK), 0); | 269 contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Preferen
ces.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TA
SK), 0); |
268 ongoingNotification.icon = R.drawable.ic_stat_blocking; | 270 ongoingNotification.icon = R.drawable.ic_stat_blocking; |
269 ongoingNotification.setLatestEventInfo(getApplicationContext(), getText(R.st
ring.app_name), msg, contentIntent); | 271 ongoingNotification.setLatestEventInfo(getApplicationContext(), getText(R.st
ring.app_name), msg, contentIntent); |
270 | 272 |
| 273 // Lock service |
| 274 if (prefs.getBoolean(getString(R.string.pref_priority), resources.getBoolean
(R.bool.def_priority))) |
| 275 { |
| 276 startForeground(ONGOING_NOTIFICATION_ID, ongoingNotification); |
| 277 } |
| 278 |
271 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true).
putExtra("port", port).putExtra("manual", isManual())); | 279 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true).
putExtra("port", port).putExtra("manual", isManual())); |
272 Log.i(TAG, "Service started"); | 280 Log.i(TAG, "Service started"); |
273 } | 281 } |
274 | 282 |
275 @Override | 283 @Override |
| 284 public int onStartCommand(Intent intent, int flags, int startId) |
| 285 { |
| 286 return START_STICKY; |
| 287 } |
| 288 |
| 289 @Override |
276 public void onDestroy() | 290 public void onDestroy() |
277 { | 291 { |
278 super.onDestroy(); | 292 super.onDestroy(); |
279 | 293 |
280 stopNoTrafficCheck(false); | 294 stopNoTrafficCheck(false); |
281 | 295 |
282 unregisterReceiver(matchesReceiver); | 296 unregisterReceiver(matchesReceiver); |
283 unregisterReceiver(proxyReceiver); | 297 unregisterReceiver(proxyReceiver); |
284 | 298 |
285 // Stop IP redirecting | 299 // Stop IP redirecting |
(...skipping 24 matching lines...) Expand all Loading... |
310 | 324 |
311 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); | 325 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); |
312 | 326 |
313 // Stop proxy server | 327 // Stop proxy server |
314 if (proxy != null) | 328 if (proxy != null) |
315 proxy.close(); | 329 proxy.close(); |
316 | 330 |
317 // Stop engine if not in interactive mode | 331 // Stop engine if not in interactive mode |
318 AdblockPlus.getApplication().stopEngine(false); | 332 AdblockPlus.getApplication().stopEngine(false); |
319 | 333 |
| 334 // Release service lock |
| 335 stopForeground(true); |
| 336 |
320 Log.i(TAG, "Service stopped"); | 337 Log.i(TAG, "Service stopped"); |
321 } | 338 } |
322 | 339 |
323 /** | 340 /** |
324 * Restores system proxy settings via native call on Android 3.1+ devices usin
g | 341 * Restores system proxy settings via native call on Android 3.1+ devices usin
g |
325 * Java reflection. | 342 * Java reflection. |
326 */ | 343 */ |
327 private void clearConnectionProxy() | 344 private void clearConnectionProxy() |
328 { | 345 { |
329 String proxyHost = (String) proxy.props.getProperty("adblock.proxyHost"); | 346 String proxyHost = (String) proxy.props.getProperty("adblock.proxyHost"); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 436 } |
420 catch (ClassCastException e) | 437 catch (ClassCastException e) |
421 { | 438 { |
422 // ignore - default handler in use | 439 // ignore - default handler in use |
423 } | 440 } |
424 } | 441 } |
425 | 442 |
426 @Override | 443 @Override |
427 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str
ing key) | 444 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str
ing key) |
428 { | 445 { |
| 446 if (getString(R.string.pref_priority).equals(key)) |
| 447 { |
| 448 if (sharedPreferences.getBoolean(key, false)) |
| 449 { |
| 450 startForeground(ONGOING_NOTIFICATION_ID, ongoingNotification); |
| 451 } |
| 452 else |
| 453 { |
| 454 stopForeground(true); |
| 455 } |
| 456 return; |
| 457 } |
429 if (hasNativeProxy) | 458 if (hasNativeProxy) |
430 { | 459 { |
431 String ketHost = getString(R.string.pref_proxyhost); | 460 String ketHost = getString(R.string.pref_proxyhost); |
432 String keyPort = getString(R.string.pref_proxyport); | 461 String keyPort = getString(R.string.pref_proxyport); |
433 String keyUser = getString(R.string.pref_proxyuser); | 462 String keyUser = getString(R.string.pref_proxyuser); |
434 String keyPass = getString(R.string.pref_proxypass); | 463 String keyPass = getString(R.string.pref_proxypass); |
435 if (key.equals(ketHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) | 464 if (key.equals(ketHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) |
436 { | 465 { |
437 String proxyHost = sharedPreferences.getString(ketHost, null); | 466 String proxyHost = sharedPreferences.getString(ketHost, null); |
438 String proxyPort = sharedPreferences.getString(keyPort, null); | 467 String proxyPort = sharedPreferences.getString(keyPort, null); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 @Override | 747 @Override |
719 public void log(int level, Object obj, String message) | 748 public void log(int level, Object obj, String message) |
720 { | 749 { |
721 if (level <= logLevel) | 750 if (level <= logLevel) |
722 { | 751 { |
723 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 752 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
724 } | 753 } |
725 } | 754 } |
726 } | 755 } |
727 } | 756 } |
OLD | NEW |