| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android.
SERVICE_STATE_CHANGED"; | 69 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android.
SERVICE_STATE_CHANGED"; |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Broadcasted if proxy fails to start. | 72 * Broadcasted if proxy fails to start. |
| 73 */ | 73 */ |
| 74 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P
ROXY_FAILURE"; | 74 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P
ROXY_FAILURE"; |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Common command bridge | 77 * Common command bridge |
| 78 */ | 78 */ |
| 79 public static final String COMMAND_BRIDGE_ACTION = "org.adblockplus.android.CO
MMAND_BRIDGE"; | 79 public static final String PROXY_STATE_CHANGED_ACTION = "org.adblockplus.andro
id.PROXY_STATE_CHANGED"; |
| 80 | 80 |
| 81 boolean hideIcon; | 81 boolean hideIcon; |
| 82 | 82 |
| 83 protected ProxyServer proxy = null; | 83 protected ProxyServer proxy = null; |
| 84 | 84 |
| 85 protected int port; | 85 protected int port; |
| 86 | 86 |
| 87 private final Properties proxyConfiguration = new Properties(); | 87 private final Properties proxyConfiguration = new Properties(); |
| 88 | 88 |
| 89 private ProxyConfigurator proxyConfigurator = null; | 89 private ProxyConfigurator proxyConfigurator = null; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 111 if (proxyHost == null || proxyPort == null) | 111 if (proxyHost == null || proxyPort == null) |
| 112 { | 112 { |
| 113 // Read application settings | 113 // Read application settings |
| 114 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); | 114 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); |
| 115 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); | 115 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); |
| 116 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); | 116 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); |
| 117 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); | 117 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); |
| 118 } | 118 } |
| 119 | 119 |
| 120 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST
_PROXY_FAILED)); | 120 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST
_PROXY_FAILED)); |
| 121 registerReceiver(this.commandBridge, new IntentFilter(ProxyService.COMMAND_B
RIDGE_ACTION)); | 121 registerReceiver(this.proxyStateChangedReceiver, new IntentFilter(ProxyServi
ce.PROXY_STATE_CHANGED_ACTION)); |
| 122 | 122 |
| 123 final InetAddress inetAddress; | 123 final InetAddress inetAddress; |
| 124 try | 124 try |
| 125 { | 125 { |
| 126 inetAddress = InetAddress.getByName(LOCALHOST); | 126 inetAddress = InetAddress.getByName(LOCALHOST); |
| 127 } | 127 } |
| 128 catch (final UnknownHostException e) | 128 catch (final UnknownHostException e) |
| 129 { | 129 { |
| 130 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) | 130 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) |
| 131 .putExtra("msg", "Could not resolve 'localhost'")); | 131 .putExtra("msg", "Could not resolve 'localhost'")); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 { | 226 { |
| 227 return START_STICKY; | 227 return START_STICKY; |
| 228 } | 228 } |
| 229 | 229 |
| 230 @Override | 230 @Override |
| 231 public void onDestroy() | 231 public void onDestroy() |
| 232 { | 232 { |
| 233 super.onDestroy(); | 233 super.onDestroy(); |
| 234 | 234 |
| 235 unregisterReceiver(this.proxyReceiver); | 235 unregisterReceiver(this.proxyReceiver); |
| 236 unregisterReceiver(this.commandBridge); | 236 unregisterReceiver(this.proxyStateChangedReceiver); |
| 237 | 237 |
| 238 if (this.proxyConfigurator != null) | 238 if (this.proxyConfigurator != null) |
| 239 { | 239 { |
| 240 this.proxyConfigurator.unregisterProxy(); | 240 this.proxyConfigurator.unregisterProxy(); |
| 241 this.proxyConfigurator.shutdown(); | 241 this.proxyConfigurator.shutdown(); |
| 242 } | 242 } |
| 243 | 243 |
| 244 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); | 244 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); |
| 245 | 245 |
| 246 // Stop proxy server | 246 // Stop proxy server |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + "
:" + proxyPass)); | 331 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + "
:" + proxyPass)); |
| 332 config.put("adblock.auth", proxyAuth); | 332 config.put("adblock.auth", proxyAuth); |
| 333 if (regType.getProxyType() == ProxyServerType.HTTPS) | 333 if (regType.getProxyType() == ProxyServerType.HTTPS) |
| 334 { | 334 { |
| 335 config.put("https.auth", proxyAuth); | 335 config.put("https.auth", proxyAuth); |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 /** | 340 /** |
| 341 * Lean method to send a {@link BridgeCommand} without additional extras. | |
| 342 * | |
| 343 * @param context | |
| 344 * the context to use for sending the broadcast | |
| 345 * @param command | |
| 346 * the command | |
| 347 */ | |
| 348 public static void sendBridgeCommand(final Context context, final BridgeComman
d command) | |
| 349 { | |
| 350 context.sendBroadcast( | |
| 351 new Intent(COMMAND_BRIDGE_ACTION) | |
| 352 .putExtra("command", command.toString())); | |
| 353 } | |
| 354 | |
| 355 /** | |
| 356 * @return {@code true} if the given host string resolves to {@code localhost} | 341 * @return {@code true} if the given host string resolves to {@code localhost} |
| 357 */ | 342 */ |
| 358 public static boolean isLocalhost(final String host) | 343 public static boolean isLocalhost(final String host) |
| 359 { | 344 { |
| 360 try | 345 try |
| 361 { | 346 { |
| 362 if (StringUtils.isEmpty(host)) | 347 if (StringUtils.isEmpty(host)) |
| 363 { | 348 { |
| 364 return false; | 349 return false; |
| 365 } | 350 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 386 } | 371 } |
| 387 catch (final ClassCastException e) | 372 catch (final ClassCastException e) |
| 388 { | 373 { |
| 389 // ignore - default handler in use | 374 // ignore - default handler in use |
| 390 } | 375 } |
| 391 } | 376 } |
| 392 | 377 |
| 393 @Override | 378 @Override |
| 394 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference
s, final String key) | 379 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference
s, final String key) |
| 395 { | 380 { |
| 396 // TODO verify this | |
| 397 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE) | 381 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE) |
| 398 { | 382 { |
| 399 final String keyHost = getString(R.string.pref_proxyhost); | 383 final String keyHost = getString(R.string.pref_proxyhost); |
| 400 final String keyPort = getString(R.string.pref_proxyport); | 384 final String keyPort = getString(R.string.pref_proxyport); |
| 401 final String keyUser = getString(R.string.pref_proxyuser); | 385 final String keyUser = getString(R.string.pref_proxyuser); |
| 402 final String keyPass = getString(R.string.pref_proxypass); | 386 final String keyPass = getString(R.string.pref_proxypass); |
| 403 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) | 387 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) |
| 404 { | 388 { |
| 405 final String proxyHost = sharedPreferences.getString(keyHost, null); | 389 final String proxyHost = sharedPreferences.getString(keyHost, null); |
| 406 final String proxyPort = sharedPreferences.getString(keyPort, null); | 390 final String proxyPort = sharedPreferences.getString(keyPort, null); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 { | 540 { |
| 557 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 541 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) |
| 558 { | 542 { |
| 559 stopSelf(); | 543 stopSelf(); |
| 560 } | 544 } |
| 561 } | 545 } |
| 562 }; | 546 }; |
| 563 | 547 |
| 564 /** | 548 /** |
| 565 * <p> | 549 * <p> |
| 566 * A BR for simple message passing from various ProxyConfigurators. | 550 * Proxy state change receiver. |
| 567 * </p> | 551 * </p> |
| 568 * <p> | 552 */ |
| 569 * The purpose of this BroadcastReceiver is to allow other components to send
notifications to the service and to be prepared for upcoming service | 553 private final BroadcastReceiver proxyStateChangedReceiver = new BroadcastRecei
ver() |
| 570 * refactorings. | |
| 571 * </p> | |
| 572 */ | |
| 573 private final BroadcastReceiver commandBridge = new BroadcastReceiver() | |
| 574 { | 554 { |
| 575 @Override | 555 @Override |
| 576 public void onReceive(final Context context, final Intent intent) | 556 public void onReceive(final Context context, final Intent intent) |
| 577 { | 557 { |
| 578 if (intent != null) | 558 if (intent != null && PROXY_STATE_CHANGED_ACTION.equals(intent.getAction()
)) |
| 579 { | 559 { |
| 580 switch (BridgeCommand.fromString(intent.getStringExtra("command"))) | 560 final NotificationManager notificationManager = (NotificationManager) ge
tSystemService(NOTIFICATION_SERVICE); |
| 581 { | 561 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
| 582 case STATE_CHANGED: | 562 ProxyService.this.sendStateChangedBroadcast(); |
| 583 final NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); | |
| 584 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification())
; | |
| 585 ProxyService.this.sendStateChangedBroadcast(); | |
| 586 break; | |
| 587 default: | |
| 588 // ignore | |
| 589 break; | |
| 590 } | |
| 591 } | 563 } |
| 592 } | 564 } |
| 593 }; | 565 }; |
| 594 | 566 |
| 595 final class ProxyServer extends Server | 567 final class ProxyServer extends Server |
| 596 { | 568 { |
| 597 @Override | 569 @Override |
| 598 public void close() | 570 public void close() |
| 599 { | 571 { |
| 600 try | 572 try |
| (...skipping 12 matching lines...) Expand all Loading... |
| 613 @Override | 585 @Override |
| 614 public void log(final int level, final Object obj, final String message) | 586 public void log(final int level, final Object obj, final String message) |
| 615 { | 587 { |
| 616 if (level <= logLevel) | 588 if (level <= logLevel) |
| 617 { | 589 { |
| 618 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 590 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
| 619 } | 591 } |
| 620 } | 592 } |
| 621 } | 593 } |
| 622 } | 594 } |
| LEFT | RIGHT |