Left: | ||
Right: |
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 25 matching lines...) Expand all Loading... | |
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.content.res.Resources; |
44 import android.net.ConnectivityManager; | 44 import android.net.ConnectivityManager; |
45 import android.net.NetworkInfo; | 45 import android.net.NetworkInfo; |
46 import android.net.Proxy; | |
46 import android.os.Binder; | 47 import android.os.Binder; |
47 import android.os.Build; | 48 import android.os.Build; |
48 import android.os.Handler; | 49 import android.os.Handler; |
49 import android.os.IBinder; | 50 import android.os.IBinder; |
50 import android.preference.PreferenceManager; | 51 import android.preference.PreferenceManager; |
51 import android.util.Log; | 52 import android.util.Log; |
52 import android.widget.Toast; | 53 import android.widget.Toast; |
53 | 54 |
54 import com.stericson.RootTools.RootTools; | 55 import com.stericson.RootTools.RootTools; |
55 import com.stericson.RootTools.RootToolsException; | 56 import com.stericson.RootTools.RootToolsException; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 | 96 |
96 protected ProxyServer proxy = null; | 97 protected ProxyServer proxy = null; |
97 protected int port; | 98 protected int port; |
98 private Properties proxyConfiguration = new Properties(); | 99 private Properties proxyConfiguration = new Properties(); |
99 | 100 |
100 /** | 101 /** |
101 * Indicates that service is working with root privileges. | 102 * Indicates that service is working with root privileges. |
102 */ | 103 */ |
103 private boolean transparent = false; | 104 private boolean transparent = false; |
104 /** | 105 /** |
105 * Indicates that service has autoconfigured Android proxy settings (version | 106 * Indicates that service has autoconfigured Android proxy settings (version 3 .1+). |
106 * 3.1+). | |
107 */ | 107 */ |
108 private boolean nativeProxy = false; | 108 private boolean nativeProxy = false; |
109 /** | |
110 * Indicates that Android proxy settings are correctly configured (version 4.1 .2+ 4.2.2+). | |
111 */ | |
112 private boolean proxyManualyConfigured = false; | |
109 | 113 |
110 private String iptables = null; | 114 private String iptables = null; |
111 | 115 |
112 @Override | 116 @Override |
113 public void onCreate() | 117 public void onCreate() |
114 { | 118 { |
115 super.onCreate(); | 119 super.onCreate(); |
116 | 120 |
117 // Get port for local proxy | 121 // Get port for local proxy |
118 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); | 122 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); |
119 Resources resources = getResources(); | 123 Resources resources = getResources(); |
120 | 124 |
121 String p = prefs.getString(getString(R.string.pref_port), null); | 125 String p = prefs.getString(getString(R.string.pref_port), null); |
122 try | 126 try |
123 { | 127 { |
124 port = p != null ? Integer.valueOf(p) : resources.getInteger(R.integer.def _port); | 128 port = p != null ? Integer.valueOf(p) : resources.getInteger(R.integer.def _port); |
125 } | 129 } |
126 catch (NumberFormatException e) | 130 catch (NumberFormatException e) |
127 { | 131 { |
128 Toast.makeText(this, getString(R.string.msg_badport) + ": " + p, Toast.LEN GTH_LONG).show(); | 132 Toast.makeText(this, getString(R.string.msg_badport) + ": " + p, Toast.LEN GTH_LONG).show(); |
129 port = resources.getInteger(R.integer.def_port); | 133 port = resources.getInteger(R.integer.def_port); |
130 } | 134 } |
131 | 135 |
132 // Try to read user proxy settings | 136 // Try to read user proxy settings |
133 String proxyHost = null; | 137 String proxyHost = null; |
134 String proxyPort = null; | 138 String proxyPort = null; |
135 String proxyExcl = null; | 139 String proxyExcl = null; |
136 String proxyUser = null; | 140 String proxyUser = null; |
137 String proxyPass = null; | 141 String proxyPass = null; |
138 | 142 |
139 if (hasNativeProxy) | 143 if (hasNativeProxy) |
140 { | 144 { |
141 // Read system settings | 145 // Read system settings |
142 proxyHost = System.getProperty("http.proxyHost"); | 146 proxyHost = System.getProperty("http.proxyHost"); |
143 proxyPort = System.getProperty("http.proxyPort"); | 147 proxyPort = System.getProperty("http.proxyPort"); |
144 proxyExcl = System.getProperty("http.nonProxyHosts"); | 148 proxyExcl = System.getProperty("http.nonProxyHosts"); |
145 | 149 |
146 Log.d(TAG, "PRX: " + proxyHost + ":" + proxyPort + "(" + proxyExcl + ")"); | 150 Log.d(TAG, "PRX: " + proxyHost + ":" + proxyPort + "(" + proxyExcl + ")"); |
147 String[] px = ProxySettings.getUserProxy(getApplicationContext()); // not used but left for future reference | 151 // not used but left for future reference |
Felix Dahlke
2013/02/22 09:11:14
Why would we need that in the future? Even if, we
Andrey Novikov
2013/02/22 09:25:05
You asked me this already in previous reviews. :)
| |
152 String[] px = ProxySettings.getUserProxy(getApplicationContext()); | |
148 if (px != null) | 153 if (px != null) |
149 Log.d(TAG, "PRX: " + px[0] + ":" + px[1] + "(" + px[2] + ")"); | 154 Log.d(TAG, "PRX: " + px[0] + ":" + px[1] + "(" + px[2] + ")"); |
150 } | 155 } |
151 else | 156 else |
152 { | 157 { |
153 // Read application settings | 158 // Read application settings |
154 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); | 159 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); |
155 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); | 160 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); |
156 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); | 161 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); |
157 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); | 162 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 { | 199 { |
195 Log.e(TAG, "Failed to initialize iptables", e); | 200 Log.e(TAG, "Failed to initialize iptables", e); |
196 } | 201 } |
197 } | 202 } |
198 | 203 |
199 if (!transparent) | 204 if (!transparent) |
200 { | 205 { |
201 // Try to set native proxy | 206 // Try to set native proxy |
202 nativeProxy = ProxySettings.setConnectionProxy(getApplicationContext(), LO CALHOST, port, ""); | 207 nativeProxy = ProxySettings.setConnectionProxy(getApplicationContext(), LO CALHOST, port, ""); |
203 | 208 |
204 if (nativeProxy) | 209 if (hasNativeProxy) |
205 { | 210 { |
206 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION)); | 211 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION)); |
207 registerReceiver(connectionReceiver, new IntentFilter("android.net.wifi. LINK_CONFIGURATION_CHANGED")); | 212 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE _ACTION)); |
208 } | 213 } |
209 } | 214 } |
210 | 215 |
211 // Start engine | 216 // Start engine |
212 AdblockPlus.getApplication().startEngine(); | 217 AdblockPlus.getApplication().startEngine(); |
213 | 218 |
214 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED)); | 219 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED)); |
215 registerReceiver(matchesReceiver, new IntentFilter(AdblockPlus.BROADCAST_FIL TER_MATCHES)); | 220 registerReceiver(matchesReceiver, new IntentFilter(AdblockPlus.BROADCAST_FIL TER_MATCHES)); |
216 | 221 |
217 // Start proxy | 222 // Start proxy |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass); | 255 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass); |
251 | 256 |
252 proxy = new ProxyServer(); | 257 proxy = new ProxyServer(); |
253 proxy.logLevel = Server.LOG_DIAGNOSTIC; | 258 proxy.logLevel = Server.LOG_DIAGNOSTIC; |
254 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration); | 259 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration); |
255 proxy.start(); | 260 proxy.start(); |
256 } | 261 } |
257 | 262 |
258 prefs.registerOnSharedPreferenceChangeListener(this); | 263 prefs.registerOnSharedPreferenceChangeListener(this); |
259 | 264 |
260 String msg = getString(transparent ? R.string.notif_all : nativeProxy ? R.st ring.notif_wifi : R.string.notif_waiting); | |
261 if (!transparent && !nativeProxy) | |
262 { | |
263 // Initiate no traffic check | |
264 notrafficHandler = new Handler(); | |
265 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); | |
266 } | |
267 // Lock service | 265 // Lock service |
268 boolean hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), resou rces.getBoolean(R.bool.def_hideicon)); | 266 boolean hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), resou rces.getBoolean(R.bool.def_hideicon)); |
267 String msg = getString(transparent ? R.string.notif_all : nativeProxy ? R.st ring.notif_wifi : R.string.notif_waiting); | |
269 ongoingNotification = new Notification(); | 268 ongoingNotification = new Notification(); |
270 ongoingNotification.when = 0; | 269 ongoingNotification.when = 0; |
271 contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Preferen ces.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TA SK), 0); | 270 contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Preferen ces.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TA SK), 0); |
272 ongoingNotification.icon = hideIcon ? R.drawable.transparent : R.drawable.ic _stat_blocking; | 271 ongoingNotification.icon = hideIcon ? R.drawable.transparent : R.drawable.ic _stat_blocking; |
273 ongoingNotification.setLatestEventInfo(getApplicationContext(), getText(R.st ring.app_name), msg, contentIntent); | 272 ongoingNotification.setLatestEventInfo(getApplicationContext(), getText(R.st ring.app_name), msg, contentIntent); |
274 startForeground(ONGOING_NOTIFICATION_ID, ongoingNotification); | 273 startForeground(ONGOING_NOTIFICATION_ID, ongoingNotification); |
275 | 274 |
276 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true). putExtra("port", port).putExtra("manual", isManual())); | 275 // If automatic setting of proxy was blocked, check if user has set it manua lly |
276 boolean manual = isManual(); | |
277 if (manual && hasNativeProxy) | |
278 { | |
279 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemS ervice(Context.CONNECTIVITY_SERVICE); | |
280 updateNoTrafficCheck(connectivityManager); | |
281 } | |
282 | |
283 sendStateChangedBroadcast(); | |
277 Log.i(TAG, "Service started"); | 284 Log.i(TAG, "Service started"); |
278 } | 285 } |
279 | 286 |
280 @Override | 287 @Override |
281 public int onStartCommand(Intent intent, int flags, int startId) | 288 public int onStartCommand(Intent intent, int flags, int startId) |
282 { | 289 { |
283 return START_STICKY; | 290 return START_STICKY; |
284 } | 291 } |
285 | 292 |
286 @Override | 293 @Override |
287 public void onDestroy() | 294 public void onDestroy() |
288 { | 295 { |
289 super.onDestroy(); | 296 super.onDestroy(); |
290 | 297 |
291 stopNoTrafficCheck(false); | 298 stopNoTrafficCheck(false, false); |
292 | 299 |
293 unregisterReceiver(matchesReceiver); | 300 unregisterReceiver(matchesReceiver); |
294 unregisterReceiver(proxyReceiver); | 301 unregisterReceiver(proxyReceiver); |
295 | 302 |
296 // Stop IP redirecting | 303 // Stop IP redirecting |
297 if (transparent) | 304 if (transparent) |
298 { | 305 { |
299 new Thread() { | 306 new Thread() |
307 { | |
300 @Override | 308 @Override |
301 public void run() | 309 public void run() |
302 { | 310 { |
303 try | 311 try |
304 { | 312 { |
305 RootTools.sendShell(iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT) ; | 313 RootTools.sendShell(iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT) ; |
306 } | 314 } |
307 catch (Exception e) | 315 catch (Exception e) |
308 { | 316 { |
309 Log.e(TAG, "Failed to clear iptables", e); | 317 Log.e(TAG, "Failed to clear iptables", e); |
310 } | 318 } |
311 } | 319 } |
312 }.start(); | 320 }.start(); |
313 } | 321 } |
314 | 322 |
323 if (hasNativeProxy) | |
324 unregisterReceiver(connectionReceiver); | |
325 | |
315 // Clear native proxy | 326 // Clear native proxy |
316 if (nativeProxy) | 327 if (nativeProxy) |
317 { | 328 { |
318 unregisterReceiver(connectionReceiver); | |
319 clearConnectionProxy(); | 329 clearConnectionProxy(); |
320 } | 330 } |
321 | 331 |
322 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); | 332 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); |
323 | 333 |
324 // Stop proxy server | 334 // Stop proxy server |
325 if (proxy != null) | 335 if (proxy != null) |
326 proxy.close(); | 336 proxy.close(); |
327 | 337 |
328 // Stop engine if not in interactive mode | 338 // Stop engine if not in interactive mode |
329 AdblockPlus.getApplication().stopEngine(false); | 339 AdblockPlus.getApplication().stopEngine(false); |
330 | 340 |
331 // Release service lock | 341 // Release service lock |
332 stopForeground(true); | 342 stopForeground(true); |
333 | 343 |
334 Log.i(TAG, "Service stopped"); | 344 Log.i(TAG, "Service stopped"); |
335 } | 345 } |
336 | 346 |
337 /** | 347 /** |
338 * Restores system proxy settings via native call on Android 3.1+ devices usin g | 348 * Restores system proxy settings via native call on Android 3.1+ devices |
339 * Java reflection. | 349 * using Java reflection. |
340 */ | 350 */ |
341 private void clearConnectionProxy() | 351 private void clearConnectionProxy() |
342 { | 352 { |
343 String proxyHost = (String) proxyConfiguration.getProperty("adblock.proxyHos t"); | 353 String proxyHost = (String) proxyConfiguration.getProperty("adblock.proxyHos t"); |
344 String proxyPort = (String) proxyConfiguration.getProperty("adblock.proxyPor t"); | 354 String proxyPort = (String) proxyConfiguration.getProperty("adblock.proxyPor t"); |
345 String proxyExcl = (String) proxyConfiguration.getProperty("adblock.proxyExc l"); | 355 String proxyExcl = (String) proxyConfiguration.getProperty("adblock.proxyExc l"); |
346 int port = 0; | 356 int port = 0; |
347 try | 357 try |
348 { | 358 { |
349 if (proxyHost != null) | 359 if (proxyHost != null) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 return nativeProxy; | 481 return nativeProxy; |
472 } | 482 } |
473 | 483 |
474 /** | 484 /** |
475 * Checks if user has to set proxy settings manually | 485 * Checks if user has to set proxy settings manually |
476 */ | 486 */ |
477 public boolean isManual() | 487 public boolean isManual() |
478 { | 488 { |
479 return !transparent && !nativeProxy; | 489 return !transparent && !nativeProxy; |
480 } | 490 } |
491 | |
492 /** | |
493 * Checks whether traffic check is pending | |
494 */ | |
495 public boolean noTraffic() | |
496 { | |
497 return notrafficHandler != null; | |
498 } | |
481 | 499 |
482 /** | 500 /** |
483 * Checks if specified host is local. | 501 * Checks if specified host is local. |
484 */ | 502 */ |
485 private static final boolean isLocalHost(String host) | 503 private static final boolean isLocalHost(String host) |
486 { | 504 { |
487 if (host == null) | 505 if (host == null) |
488 return false; | 506 return false; |
489 | 507 |
490 try | 508 try |
(...skipping 14 matching lines...) Expand all Loading... | |
505 } | 523 } |
506 catch (Exception e) | 524 catch (Exception e) |
507 { | 525 { |
508 Log.w(TAG, null, e); | 526 Log.w(TAG, null, e); |
509 } | 527 } |
510 return false; | 528 return false; |
511 } | 529 } |
512 | 530 |
513 /** | 531 /** |
514 * Initializes iptables executable. | 532 * Initializes iptables executable. |
515 * | 533 * |
516 * @throws FileNotFoundException If iptables initialization failed due to prov ided reasons. | 534 * @throws FileNotFoundException |
535 * If iptables initialization failed due to provided reasons. | |
517 */ | 536 */ |
518 private void initIptables() throws IOException, RootToolsException, TimeoutExc eption, FileNotFoundException | 537 private void initIptables() throws IOException, RootToolsException, TimeoutExc eption, FileNotFoundException |
519 { | 538 { |
520 if (!RootTools.isAccessGiven()) | 539 if (!RootTools.isAccessGiven()) |
521 throw new FileNotFoundException("No root access"); | 540 throw new FileNotFoundException("No root access"); |
522 | 541 |
523 File ipt = getFileStreamPath("iptables"); | 542 File ipt = getFileStreamPath("iptables"); |
524 | 543 |
525 if (!ipt.exists()) | 544 if (!ipt.exists()) |
526 { | 545 { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 return RootTools.sendShell(command, DEFAULT_TIMEOUT); | 585 return RootTools.sendShell(command, DEFAULT_TIMEOUT); |
567 } | 586 } |
568 catch (Exception e) | 587 catch (Exception e) |
569 { | 588 { |
570 Log.e(TAG, "Failed to get iptables configuration", e); | 589 Log.e(TAG, "Failed to get iptables configuration", e); |
571 return null; | 590 return null; |
572 } | 591 } |
573 } | 592 } |
574 | 593 |
575 /** | 594 /** |
595 * Raises or removes no traffic notification based on current link proxy | |
596 * settings | |
597 */ | |
598 private void updateNoTrafficCheck(ConnectivityManager connectivityManager) | |
599 { | |
600 try | |
601 { | |
602 Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); | |
603 String[] userProxy = ProxySettings.getUserProxy(pp); | |
604 if (userProxy != null) | |
605 Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); | |
606 updateNoTrafficCheck(userProxy); | |
607 } | |
608 catch (Exception e) | |
609 { | |
610 // This should not happen | |
611 Log.e(TAG, null, e); | |
612 } | |
613 } | |
614 | |
615 /** | |
616 * Raises or removes no traffic notification based on the user proxy settings | |
617 */ | |
618 private void updateNoTrafficCheck(String[] userProxy) | |
619 { | |
620 boolean ourProxy = userProxy != null && isLocalHost(userProxy[0]) && Integer .valueOf(userProxy[1]) == port; | |
621 if (ourProxy) | |
622 { | |
623 stopNoTrafficCheck(true, true); | |
624 } | |
625 else | |
626 { | |
627 // Initiate no traffic check | |
628 notrafficHandler = new Handler(); | |
629 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); | |
630 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); | |
631 ongoingNotification.setLatestEventInfo(ProxyService.this, getText(R.string .app_name), getText(R.string.notif_waiting), contentIntent); | |
632 notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification); | |
633 } | |
634 if (ourProxy != proxyManualyConfigured) | |
635 { | |
636 proxyManualyConfigured = ourProxy; | |
637 sendStateChangedBroadcast(); | |
638 } | |
639 } | |
640 | |
641 /** | |
576 * Stops no traffic check, optionally resetting notification message. | 642 * Stops no traffic check, optionally resetting notification message. |
577 * | 643 * |
578 * @param changeStatus | 644 * @param changeStatus |
579 * true if notification message should be set to normal operating | 645 * true if notification message should be set to normal operating |
580 * mode | 646 * mode |
647 * @param forceMessageReset | |
648 * if true unconditionally reset main notification message | |
581 */ | 649 */ |
582 private void stopNoTrafficCheck(boolean changeStatus) | 650 private void stopNoTrafficCheck(boolean changeStatus, boolean forceMessageRese t) |
583 { | 651 { |
584 if (notrafficHandler != null) | 652 if (notrafficHandler != null) |
585 { | 653 { |
586 notrafficHandler.removeCallbacks(noTraffic); | 654 notrafficHandler.removeCallbacks(noTraffic); |
587 if (changeStatus) | 655 proxyManualyConfigured = true; |
588 { | 656 sendStateChangedBroadcast(); |
589 NotificationManager notificationManager = (NotificationManager) getSyste mService(NOTIFICATION_SERVICE); | 657 } |
590 ongoingNotification.setLatestEventInfo(ProxyService.this, getText(R.stri ng.app_name), getText(R.string.notif_wifi), contentIntent); | 658 if (changeStatus && (notrafficHandler != null || forceMessageReset)) |
591 notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification) ; | 659 { |
592 } | 660 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); |
661 ongoingNotification.setLatestEventInfo(ProxyService.this, getText(R.string .app_name), getText(R.string.notif_wifi), contentIntent); | |
662 notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification); | |
593 } | 663 } |
594 notrafficHandler = null; | 664 notrafficHandler = null; |
595 } | 665 } |
596 | 666 |
597 public void setEmptyIcon(boolean empty) | 667 public void setEmptyIcon(boolean empty) |
598 { | 668 { |
599 ongoingNotification.icon = empty ? R.drawable.transparent : R.drawable.ic_st at_blocking; | 669 ongoingNotification.icon = empty ? R.drawable.transparent : R.drawable.ic_st at_blocking; |
600 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE); | 670 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE); |
601 notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification); | 671 notificationManager.notify(ONGOING_NOTIFICATION_ID, ongoingNotification); |
602 } | 672 } |
603 | 673 |
674 public void sendStateChangedBroadcast() | |
675 { | |
676 Log.i(TAG, "Broadcasting " + BROADCAST_STATE_CHANGED); | |
677 boolean manual = isManual(); | |
678 Intent stateIntent = new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true).putExtra("port", port).putExtra("manual", manual); | |
679 if (manual) | |
680 stateIntent.putExtra("configured", proxyManualyConfigured); | |
681 sendBroadcast(stateIntent); | |
682 } | |
683 | |
604 private final IBinder binder = new LocalBinder(); | 684 private final IBinder binder = new LocalBinder(); |
605 | 685 |
606 public final class LocalBinder extends Binder | 686 public final class LocalBinder extends Binder |
607 { | 687 { |
608 public ProxyService getService() | 688 public ProxyService getService() |
609 { | 689 { |
610 return ProxyService.this; | 690 return ProxyService.this; |
611 } | 691 } |
612 } | 692 } |
613 | 693 |
614 @Override | 694 @Override |
615 public IBinder onBind(Intent intent) | 695 public IBinder onBind(Intent intent) |
616 { | 696 { |
617 return binder; | 697 return binder; |
618 } | 698 } |
619 | 699 |
620 /** | 700 /** |
621 * Executed if no traffic is detected after a period of time. Notifies user | 701 * Executed if no traffic is detected after a period of time. Notifies user |
622 * about possible configuration problems. | 702 * about possible configuration problems. |
623 */ | 703 */ |
624 private Runnable noTraffic = new Runnable() { | 704 private Runnable noTraffic = new Runnable() |
705 { | |
625 public void run() | 706 public void run() |
626 { | 707 { |
627 // Show warning notification | 708 // Show warning notification |
628 Notification notification = new Notification(); | 709 Notification notification = new Notification(); |
629 notification.icon = R.drawable.ic_stat_warning; | 710 notification.icon = R.drawable.ic_stat_warning; |
630 notification.when = System.currentTimeMillis(); | 711 notification.when = System.currentTimeMillis(); |
631 notification.flags |= Notification.FLAG_AUTO_CANCEL; | 712 notification.flags |= Notification.FLAG_AUTO_CANCEL; |
632 notification.defaults |= Notification.DEFAULT_SOUND; | 713 notification.defaults |= Notification.DEFAULT_SOUND; |
633 Intent intent = new Intent(ProxyService.this, ConfigurationActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 714 Intent intent = new Intent(ProxyService.this, ConfigurationActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
634 intent.putExtra("port", port); | 715 intent.putExtra("port", port); |
635 PendingIntent contentIntent = PendingIntent.getActivity(ProxyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | 716 PendingIntent contentIntent = PendingIntent.getActivity(ProxyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
636 notification.setLatestEventInfo(ProxyService.this, getText(R.string.app_na me), getString(R.string.notif_notraffic), contentIntent); | 717 notification.setLatestEventInfo(ProxyService.this, getText(R.string.app_na me), getString(R.string.notif_notraffic), contentIntent); |
637 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); | 718 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); |
638 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, notification); | 719 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, notification); |
639 } | 720 } |
640 }; | 721 }; |
641 | 722 |
642 /** | 723 /** |
643 * Stops no traffic check if traffic is detected by proxy service. | 724 * Stops no traffic check if traffic is detected by proxy service. |
644 */ | 725 */ |
645 private BroadcastReceiver matchesReceiver = new BroadcastReceiver() { | 726 private BroadcastReceiver matchesReceiver = new BroadcastReceiver() |
727 { | |
646 @Override | 728 @Override |
647 public void onReceive(final Context context, Intent intent) | 729 public void onReceive(final Context context, Intent intent) |
648 { | 730 { |
649 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) | 731 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) |
650 stopNoTrafficCheck(true); | 732 stopNoTrafficCheck(true, false); |
651 } | 733 } |
652 }; | 734 }; |
653 | 735 |
654 /** | 736 /** |
655 * Stops service if proxy fails. | 737 * Stops service if proxy fails. |
656 */ | 738 */ |
657 private BroadcastReceiver proxyReceiver = new BroadcastReceiver() { | 739 private BroadcastReceiver proxyReceiver = new BroadcastReceiver() |
740 { | |
658 @Override | 741 @Override |
659 public void onReceive(final Context context, Intent intent) | 742 public void onReceive(final Context context, Intent intent) |
660 { | 743 { |
661 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 744 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) |
662 { | 745 { |
663 stopSelf(); | 746 stopSelf(); |
664 } | 747 } |
665 } | 748 } |
666 }; | 749 }; |
667 | 750 |
668 /** | 751 /** |
669 * Monitors system network connection settings changes and updates proxy | 752 * Monitors system network connection settings changes and updates proxy |
670 * settings accordingly. | 753 * settings accordingly. |
671 */ | 754 */ |
672 private BroadcastReceiver connectionReceiver = new BroadcastReceiver() { | 755 private BroadcastReceiver connectionReceiver = new BroadcastReceiver() |
756 { | |
673 @Override | 757 @Override |
674 public void onReceive(Context ctx, Intent intent) | 758 public void onReceive(Context ctx, Intent intent) |
675 { | 759 { |
676 String action = intent.getAction(); | 760 String action = intent.getAction(); |
677 Log.i(TAG, "Action: " + action); | 761 Log.i(TAG, "Action: " + action); |
762 // Connectivity change | |
678 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) | 763 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) |
679 { | 764 { |
680 ConnectivityManager connectivityManager = (ConnectivityManager) getSyste mService(Context.CONNECTIVITY_SERVICE); | 765 ConnectivityManager connectivityManager = (ConnectivityManager) getSyste mService(Context.CONNECTIVITY_SERVICE); |
681 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); | 766 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); |
682 if (info == null) | 767 if (info == null) |
683 return; | 768 return; |
684 String typeName = info.getTypeName(); | 769 String typeName = info.getTypeName(); |
685 String subtypeName = info.getSubtypeName(); | 770 String subtypeName = info.getSubtypeName(); |
686 boolean available = info.isAvailable(); | 771 boolean available = info.isAvailable(); |
772 | |
687 Log.i(TAG, "Network Type: " + typeName + ", subtype: " + subtypeName + " , available: " + available); | 773 Log.i(TAG, "Network Type: " + typeName + ", subtype: " + subtypeName + " , available: " + available); |
688 if (info.getType() == ConnectivityManager.TYPE_WIFI) | 774 if (info.getType() == ConnectivityManager.TYPE_WIFI) |
689 ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST, p ort, ""); | 775 { |
776 if (nativeProxy) | |
777 { | |
778 ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST, port, ""); | |
779 } | |
780 else | |
781 { | |
782 updateNoTrafficCheck(connectivityManager); | |
783 } | |
784 } | |
690 } | 785 } |
691 else if ("android.net.wifi.LINK_CONFIGURATION_CHANGED".equals(action)) | 786 // Proxy change |
787 else if (Proxy.PROXY_CHANGE_ACTION.equals(action)) | |
692 { | 788 { |
693 Object lp = intent.getParcelableExtra("linkProperties"); | 789 Object pp = intent.getParcelableExtra("proxy"); |
694 Method method; | |
695 try | 790 try |
696 { | 791 { |
697 /* | |
698 * lp.getHttpProxy(); | |
699 */ | |
700 method = lp.getClass().getMethod("getHttpProxy"); | |
701 Object pp = method.invoke(lp); | |
702 | |
703 String[] userProxy = ProxySettings.getUserProxy(pp); | 792 String[] userProxy = ProxySettings.getUserProxy(pp); |
704 if (userProxy != null && Integer.valueOf(userProxy[1]) != port) | 793 if (nativeProxy) |
705 { | 794 { |
706 Log.i(TAG, "User has set new proxy: " + userProxy[0] + ":" + userPro xy[1] + "(" + userProxy[2] + ")"); | 795 if (userProxy != null && Integer.valueOf(userProxy[1]) != port) |
707 if (proxy != null) | |
708 { | 796 { |
709 configureUserProxy(proxyConfiguration, userProxy[0], userProxy[1], userProxy[2], null, null); | 797 Log.i(TAG, "User has set new proxy: " + userProxy[0] + ":" + userP roxy[1] + "(" + userProxy[2] + ")"); |
710 proxy.restart(proxyConfiguration.getProperty("handler")); | 798 if (proxy != null) |
799 { | |
800 configureUserProxy(proxyConfiguration, userProxy[0], userProxy[1 ], userProxy[2], null, null); | |
801 proxy.restart(proxyConfiguration.getProperty("handler")); | |
802 } | |
711 } | 803 } |
712 } | 804 } |
805 else | |
806 { | |
807 Log.i(TAG, "User has set proxy: " + userProxy[0] + ":" + userProxy[1 ] + "(" + userProxy[2] + ")"); | |
808 updateNoTrafficCheck(userProxy); | |
809 } | |
713 } | 810 } |
714 catch (Exception e) | 811 catch (Exception e) |
715 { | 812 { |
716 // This should not happen | 813 // This should not happen |
717 Log.e(TAG, null, e); | 814 Log.e(TAG, null, e); |
718 } | 815 } |
719 | |
720 } | 816 } |
721 } | 817 } |
722 }; | 818 }; |
723 | 819 |
724 final class ProxyServer extends Server | 820 final class ProxyServer extends Server |
725 { | 821 { |
726 @Override | 822 @Override |
727 public void close() | 823 public void close() |
728 { | 824 { |
729 try | 825 try |
(...skipping 12 matching lines...) Expand all Loading... | |
742 @Override | 838 @Override |
743 public void log(int level, Object obj, String message) | 839 public void log(int level, Object obj, String message) |
744 { | 840 { |
745 if (level <= logLevel) | 841 if (level <= logLevel) |
746 { | 842 { |
747 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 843 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
748 } | 844 } |
749 } | 845 } |
750 } | 846 } |
751 } | 847 } |
OLD | NEW |