| Left: | ||
| Right: | 
| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 import android.os.StrictMode; | 52 import android.os.StrictMode; | 
| 53 import android.preference.PreferenceManager; | 53 import android.preference.PreferenceManager; | 
| 54 import android.support.v4.app.NotificationCompat; | 54 import android.support.v4.app.NotificationCompat; | 
| 55 import android.util.Log; | 55 import android.util.Log; | 
| 56 | 56 | 
| 57 import com.stericson.RootTools.RootTools; | 57 import com.stericson.RootTools.RootTools; | 
| 58 import com.stericson.RootTools.RootToolsException; | 58 import com.stericson.RootTools.RootToolsException; | 
| 59 | 59 | 
| 60 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener | 60 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener | 
| 61 { | 61 { | 
| 62 private static final String LOCALHOST = "127.0.0.1"; | |
| 63 /** | |
| 64 * Indicates that system supports native proxy configuration. | |
| 65 */ | |
| 66 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb 3.1 | |
| 67 | |
| 62 static | 68 static | 
| 63 { | 69 { | 
| 64 RootTools.debugMode = false; | 70 RootTools.debugMode = false; | 
| 65 } | 71 } | 
| 66 | 72 | 
| 67 private static final String TAG = Utils.getTag(ProxyService.class); | 73 private static final String TAG = Utils.getTag(ProxyService.class); | 
| 
 
Felix Dahlke
2014/04/28 07:29:01
What's the logic behind reordering these?
 
René Jeschke
2014/04/28 08:34:32
Reverted.
 
 | |
| 68 | |
| 69 private static final String LOCALHOST = "127.0.0.1"; | |
| 70 /** | |
| 71 * Indicates that system supports native proxy configuration. | |
| 72 */ | |
| 73 private static final boolean logRequests = false; | 74 private static final boolean logRequests = false; | 
| 74 | 75 | 
| 75 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb | 76 // Do not use 8080 because it is a "dirty" port, Android uses it if something goes wrong | 
| 76 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; | 77 // first element is reserved for previously used port | 
| 77 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; | 78 private static final int[] portVariants = new int[] {-1, 2020, 3030, 4040, 505 0, 6060, 7070, 9090, 1234, 12345, 4321, 0}; | 
| 78 | |
| 79 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; | |
| 80 | 79 | 
| 81 private static final int DEFAULT_TIMEOUT = 3000; | 80 private static final int DEFAULT_TIMEOUT = 3000; | 
| 82 private static final int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes | 81 private static final int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes | 
| 83 | 82 | 
| 84 // Do not use 8080 because it is a "dirty" port, Android uses it if something | 83 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; | 
| 85 // goes wrong | 84 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; | 
| 86 // first element is reserved for previously used port | 85 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; | 
| 87 private static final int[] portVariants = new int[] { -1, 2020, 3030, 4040, 50 50, 6060, 7070, 9090, 1234, 12345, 4321, 0 }; | |
| 88 | 86 | 
| 89 /** | 87 /** | 
| 90 * Broadcasted when service starts or stops. | 88 * Broadcasted when service starts or stops. | 
| 91 */ | 89 */ | 
| 92 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. service.state"; | 90 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. service.state"; | 
| 93 /** | 91 /** | 
| 94 * Broadcasted if proxy fails to start. | 92 * Broadcasted if proxy fails to start. | 
| 95 */ | 93 */ | 
| 96 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.p roxy.failure"; | 94 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.p roxy.failure"; | 
| 97 | 95 | 
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 if (proxy != null) | 382 if (proxy != null) | 
| 385 proxy.close(); | 383 proxy.close(); | 
| 386 | 384 | 
| 387 // Release service lock | 385 // Release service lock | 
| 388 stopForeground(true); | 386 stopForeground(true); | 
| 389 | 387 | 
| 390 Log.i(TAG, "Service stopped"); | 388 Log.i(TAG, "Service stopped"); | 
| 391 } | 389 } | 
| 392 | 390 | 
| 393 /** | 391 /** | 
| 394 * Restores system proxy settings via native call on Android 3.1+ devices usin g Java reflection. | 392 * Restores system proxy settings via native call on Android 3.1+ devices | 
| 393 * using Java reflection. | |
| 395 */ | 394 */ | 
| 396 private void clearConnectionProxy() | 395 private void clearConnectionProxy() | 
| 397 { | 396 { | 
| 398 final String proxyHost = proxyConfiguration.getProperty("adblock.proxyHost") ; | 397 final String proxyHost = proxyConfiguration.getProperty("adblock.proxyHost") ; | 
| 399 final String proxyPort = proxyConfiguration.getProperty("adblock.proxyPort") ; | 398 final String proxyPort = proxyConfiguration.getProperty("adblock.proxyPort") ; | 
| 400 final String proxyExcl = proxyConfiguration.getProperty("adblock.proxyExcl") ; | 399 final String proxyExcl = proxyConfiguration.getProperty("adblock.proxyExcl") ; | 
| 401 int port = 0; | 400 int port = 0; | 
| 402 try | 401 try | 
| 403 { | 402 { | 
| 404 if (proxyHost != null) | 403 if (proxyHost != null) | 
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 630 return RootTools.sendShell(command, DEFAULT_TIMEOUT); | 629 return RootTools.sendShell(command, DEFAULT_TIMEOUT); | 
| 631 } | 630 } | 
| 632 catch (final Exception e) | 631 catch (final Exception e) | 
| 633 { | 632 { | 
| 634 Log.e(TAG, "Failed to get iptables configuration", e); | 633 Log.e(TAG, "Failed to get iptables configuration", e); | 
| 635 return null; | 634 return null; | 
| 636 } | 635 } | 
| 637 } | 636 } | 
| 638 | 637 | 
| 639 /** | 638 /** | 
| 640 * Raises or removes no traffic notification based on current link proxy setti ngs | 639 * Raises or removes no traffic notification based on current link proxy | 
| 640 * settings | |
| 641 */ | 641 */ | 
| 642 private void updateNoTrafficCheck(final ConnectivityManager connectivityManage r) | 642 private void updateNoTrafficCheck(final ConnectivityManager connectivityManage r) | 
| 643 { | 643 { | 
| 644 try | 644 try | 
| 645 { | 645 { | 
| 646 final Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); | 646 final Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); | 
| 647 final String[] userProxy = ProxySettings.getUserProxy(pp); | 647 final String[] userProxy = ProxySettings.getUserProxy(pp); | 
| 648 if (userProxy != null) | 648 if (userProxy != null) | 
| 649 Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); | 649 Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); | 
| 650 updateNoTrafficCheck(userProxy); | 650 updateNoTrafficCheck(userProxy); | 
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 } | 757 } | 
| 758 } | 758 } | 
| 759 | 759 | 
| 760 @Override | 760 @Override | 
| 761 public IBinder onBind(final Intent intent) | 761 public IBinder onBind(final Intent intent) | 
| 762 { | 762 { | 
| 763 return binder; | 763 return binder; | 
| 764 } | 764 } | 
| 765 | 765 | 
| 766 /** | 766 /** | 
| 767 * Executed if no traffic is detected after a period of time. Notifies user ab out possible | 767 * Executed if no traffic is detected after a period of time. Notifies user | 
| 768 * configuration problems. | 768 * about possible configuration problems. | 
| 769 */ | 769 */ | 
| 770 private final Runnable noTraffic = new Runnable() | 770 private final Runnable noTraffic = new Runnable() | 
| 771 { | 771 { | 
| 772 @Override | 772 @Override | 
| 773 public void run() | 773 public void run() | 
| 774 { | 774 { | 
| 775 // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not rem ove this callback | 775 // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not rem ove this callback | 
| 776 if (notrafficHandler == null) | 776 if (notrafficHandler == null) | 
| 777 return; | 777 return; | 
| 778 // Show warning notification | 778 // Show warning notification | 
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 828 public void onReceive(final Context context, final Intent intent) | 828 public void onReceive(final Context context, final Intent intent) | 
| 829 { | 829 { | 
| 830 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 830 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 
| 831 { | 831 { | 
| 832 stopSelf(); | 832 stopSelf(); | 
| 833 } | 833 } | 
| 834 } | 834 } | 
| 835 }; | 835 }; | 
| 836 | 836 | 
| 837 /** | 837 /** | 
| 838 * Monitors system network connection settings changes and updates proxy setti ngs accordingly. | 838 * Monitors system network connection settings changes and updates proxy | 
| 839 * settings accordingly. | |
| 839 */ | 840 */ | 
| 840 private final BroadcastReceiver connectionReceiver = new BroadcastReceiver() | 841 private final BroadcastReceiver connectionReceiver = new BroadcastReceiver() | 
| 841 { | 842 { | 
| 842 @Override | 843 @Override | 
| 843 public void onReceive(final Context ctx, final Intent intent) | 844 public void onReceive(final Context ctx, final Intent intent) | 
| 844 { | 845 { | 
| 845 final String action = intent.getAction(); | 846 final String action = intent.getAction(); | 
| 846 Log.i(TAG, "Action: " + action); | 847 Log.i(TAG, "Action: " + action); | 
| 847 // Connectivity change | 848 // Connectivity change | 
| 848 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) | 849 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 924 @Override | 925 @Override | 
| 925 public void log(final int level, final Object obj, final String message) | 926 public void log(final int level, final Object obj, final String message) | 
| 926 { | 927 { | 
| 927 if (level <= logLevel) | 928 if (level <= logLevel) | 
| 928 { | 929 { | 
| 929 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 930 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 
| 930 } | 931 } | 
| 931 } | 932 } | 
| 932 } | 933 } | 
| 933 } | 934 } | 
| LEFT | RIGHT |