| 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 13 matching lines...) Expand all Loading... |
| 24 import android.content.Intent; | 24 import android.content.Intent; |
| 25 import android.net.ConnectivityManager; | 25 import android.net.ConnectivityManager; |
| 26 import android.net.NetworkInfo; | 26 import android.net.NetworkInfo; |
| 27 import android.os.Parcelable; | 27 import android.os.Parcelable; |
| 28 import android.util.Log; | 28 import android.util.Log; |
| 29 | 29 |
| 30 public class ProxySettings | 30 public class ProxySettings |
| 31 { | 31 { |
| 32 private static final String TAG = "ProxySettings"; | 32 private static final String TAG = "ProxySettings"; |
| 33 | 33 |
| 34 public static Object getActiveLinkProxy(ConnectivityManager connectivityManage
r) throws Exception |
| 35 { |
| 36 /* |
| 37 * LinkProperties lp = connectivityManager.getActiveLinkProperties() |
| 38 */ |
| 39 Method method = connectivityManager.getClass().getMethod("getActiveLinkPrope
rties"); |
| 40 Object lp = method.invoke(connectivityManager); |
| 41 |
| 42 Object pp = ProxySettings.getLinkProxy(lp); |
| 43 return pp; |
| 44 } |
| 45 |
| 46 /** |
| 47 * Reads proxy settings from link properties on Android 3.1+ using Java |
| 48 * reflection. |
| 49 * |
| 50 * @param linkProperties |
| 51 * android.net.LinkProperties |
| 52 * @return ProxyProperties |
| 53 * @throws Exception |
| 54 */ |
| 55 public static Object getLinkProxy(Object linkProperties) throws Exception |
| 56 { |
| 57 /* |
| 58 * linkProperties.getHttpProxy(); |
| 59 */ |
| 60 Method method = linkProperties.getClass().getMethod("getHttpProxy"); |
| 61 Object pp = method.invoke(linkProperties); |
| 62 return pp; |
| 63 } |
| 64 |
| 34 /** | 65 /** |
| 35 * Reads system proxy settings on Android 3.1+ using Java reflection. | 66 * Reads system proxy settings on Android 3.1+ using Java reflection. |
| 36 * | 67 * |
| 37 * @return string array of host, port and exclusion list | 68 * @return string array of host, port and exclusion list |
| 38 */ | 69 */ |
| 39 public static String[] getUserProxy(Context context) | 70 public static String[] getUserProxy(Context context) |
| 40 { | 71 { |
| 41 Method method = null; | 72 Method method = null; |
| 42 try | 73 try |
| 43 { | 74 { |
| 44 /* | 75 /* |
| 45 * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); | 76 * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); |
| 46 */ | 77 */ |
| (...skipping 23 matching lines...) Expand all Loading... |
| 70 catch (Exception e) | 101 catch (Exception e) |
| 71 { | 102 { |
| 72 // This should not happen | 103 // This should not happen |
| 73 Log.e(TAG, "getProxy failure", e); | 104 Log.e(TAG, "getProxy failure", e); |
| 74 return null; | 105 return null; |
| 75 } | 106 } |
| 76 } | 107 } |
| 77 | 108 |
| 78 /** | 109 /** |
| 79 * Reads system proxy settings on Android 3.1+ using Java reflection. | 110 * Reads system proxy settings on Android 3.1+ using Java reflection. |
| 80 * | 111 * |
| 81 * @param pp | 112 * @param pp |
| 82 * ProxyProperties object | 113 * ProxyProperties object |
| 83 * @return string array of host, port and exclusion list | 114 * @return string array of host, port and exclusion list |
| 84 * @throws Exception | 115 * @throws Exception |
| 85 */ | 116 */ |
| 86 protected static String[] getUserProxy(Object pp) throws Exception | 117 protected static String[] getUserProxy(Object pp) throws Exception |
| 87 { | 118 { |
| 88 String[] userProxy = new String[3]; | 119 String[] userProxy = new String[3]; |
| 89 | 120 |
| 90 String className = "android.net.ProxyProperties"; | 121 String className = "android.net.ProxyProperties"; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 111 | 142 |
| 112 if (userProxy[0] != null) | 143 if (userProxy[0] != null) |
| 113 return userProxy; | 144 return userProxy; |
| 114 else | 145 else |
| 115 return null; | 146 return null; |
| 116 } | 147 } |
| 117 | 148 |
| 118 /** | 149 /** |
| 119 * Tries to set local proxy in system settings via native call on Android 3.1+ | 150 * Tries to set local proxy in system settings via native call on Android 3.1+ |
| 120 * devices using Java reflection. | 151 * devices using Java reflection. |
| 121 * | 152 * |
| 122 * @return true if device supports native proxy setting | 153 * @return true if device supports native proxy setting |
| 123 */ | 154 */ |
| 124 public static boolean setConnectionProxy(Context context, String host, int por
t, String excl) | 155 public static boolean setConnectionProxy(Context context, String host, int por
t, String excl) |
| 125 { | 156 { |
| 126 Method method = null; | 157 Method method = null; |
| 127 try | 158 try |
| 128 { | 159 { |
| 129 /* | 160 /* |
| 130 * android.net.LinkProperties lp = | 161 * android.net.LinkProperties lp = |
| 131 * ConnectivityManager.getActiveLinkProperties(); | 162 * ConnectivityManager.getActiveLinkProperties(); |
| 132 */ | 163 */ |
| 133 method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); | 164 method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); |
| 134 } | 165 } |
| 135 catch (NoSuchMethodException e) | 166 catch (NoSuchMethodException e) |
| 136 { | 167 { |
| 137 // This is normal situation for pre-ICS devices | 168 // This is normal situation for pre-ICS devices |
| 138 return false; | 169 return false; |
| 139 } | 170 } |
| 140 catch (Exception e) | 171 catch (Exception e) |
| 141 { | 172 { |
| 142 // This should not happen | 173 // This should not happen |
| 143 Log.e(TAG, "setHttpProxy failure", e); | 174 Log.e(TAG, "setHttpProxy failure", e); |
| 144 return false; | 175 return false; |
| 145 } | 176 } |
| 146 try | 177 try |
| 147 { | 178 { |
| 148 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
tSystemService(Context.CONNECTIVITY_SERVICE); | 179 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
tSystemService(Context.CONNECTIVITY_SERVICE); |
| 149 Object lp = method.invoke(connectivityManager); | 180 Object lp = method.invoke(connectivityManager); |
| 150 if (lp == null) // There is no active link now, but device has native prox
y support | 181 if (lp == null) // There is no active link now, but device has native |
| 182 // proxy support |
| 151 return true; | 183 return true; |
| 152 | 184 |
| 153 String className = "android.net.ProxyProperties"; | 185 String className = "android.net.ProxyProperties"; |
| 154 Class<?> c = Class.forName(className); | 186 Class<?> c = Class.forName(className); |
| 155 method = lp.getClass().getMethod("setHttpProxy", c); | 187 method = lp.getClass().getMethod("setHttpProxy", c); |
| 156 if (host != null) | 188 if (host != null) |
| 157 { | 189 { |
| 158 /* | 190 /* |
| 159 * ProxyProperties pp = new ProxyProperties(host, port, excl); | 191 * ProxyProperties pp = new ProxyProperties(host, port, excl); |
| 160 */ | 192 */ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 { | 226 { |
| 195 if (lp != null) | 227 if (lp != null) |
| 196 { | 228 { |
| 197 intent.putExtra("linkProperties", (Parcelable) lp); | 229 intent.putExtra("linkProperties", (Parcelable) lp); |
| 198 } | 230 } |
| 199 context.sendBroadcast(intent); | 231 context.sendBroadcast(intent); |
| 200 } | 232 } |
| 201 | 233 |
| 202 return true; | 234 return true; |
| 203 } | 235 } |
| 236 catch (SecurityException e) |
| 237 { |
| 238 // This is ok for 4.1.2+, 4.2.2+ and later |
| 239 return false; |
| 240 } |
| 204 catch (Exception e) | 241 catch (Exception e) |
| 205 { | 242 { |
| 206 // This should not happen | 243 // This should not happen |
| 207 Log.e(TAG, "setHttpProxy failure", e); | 244 Log.e(TAG, "setHttpProxy failure", e); |
| 208 return false; | 245 return false; |
| 209 } | 246 } |
| 210 } | 247 } |
| 211 } | 248 } |
| OLD | NEW |