OLD | NEW |
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 11 matching lines...) Expand all Loading... |
22 | 22 |
23 import android.content.Context; | 23 import android.content.Context; |
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 = Utils.getTag(ProxySettings.class); |
33 | 33 |
34 public static Object getActiveLinkProxy(ConnectivityManager connectivityManage
r) throws Exception | 34 public static Object getActiveLinkProxy(final ConnectivityManager connectivity
Manager) throws Exception |
35 { | 35 { |
36 /* | 36 /* |
37 * LinkProperties lp = connectivityManager.getActiveLinkProperties() | 37 * LinkProperties lp = connectivityManager.getActiveLinkProperties() |
38 */ | 38 */ |
39 Method method = connectivityManager.getClass().getMethod("getActiveLinkPrope
rties"); | 39 final Method method = connectivityManager.getClass().getMethod("getActiveLin
kProperties"); |
40 Object lp = method.invoke(connectivityManager); | 40 final Object lp = method.invoke(connectivityManager); |
41 | 41 |
42 Object pp = ProxySettings.getLinkProxy(lp); | 42 final Object pp = ProxySettings.getLinkProxy(lp); |
43 return pp; | 43 return pp; |
44 } | 44 } |
45 | 45 |
46 /** | 46 /** |
47 * Reads proxy settings from link properties on Android 3.1+ using Java | 47 * Reads proxy settings from link properties on Android 3.1+ using Java |
48 * reflection. | 48 * reflection. |
49 * | 49 * |
50 * @param linkProperties | 50 * @param linkProperties |
51 * android.net.LinkProperties | 51 * android.net.LinkProperties |
52 * @return ProxyProperties | 52 * @return ProxyProperties |
53 * @throws Exception | 53 * @throws Exception |
54 */ | 54 */ |
55 public static Object getLinkProxy(Object linkProperties) throws Exception | 55 public static Object getLinkProxy(final Object linkProperties) throws Exceptio
n |
56 { | 56 { |
57 /* | 57 /* |
58 * linkProperties.getHttpProxy(); | 58 * linkProperties.getHttpProxy(); |
59 */ | 59 */ |
60 Method method = linkProperties.getClass().getMethod("getHttpProxy"); | 60 final Method method = linkProperties.getClass().getMethod("getHttpProxy"); |
61 Object pp = method.invoke(linkProperties); | 61 final Object pp = method.invoke(linkProperties); |
62 return pp; | 62 return pp; |
63 } | 63 } |
64 | 64 |
65 /** | 65 /** |
66 * Reads system proxy settings on Android 3.1+ using Java reflection. | 66 * Reads system proxy settings on Android 3.1+ using Java reflection. |
67 * | 67 * |
68 * @return string array of host, port and exclusion list | 68 * @return string array of host, port and exclusion list |
69 */ | 69 */ |
70 public static String[] getUserProxy(Context context) | 70 public static String[] getUserProxy(final Context context) |
71 { | 71 { |
72 Method method = null; | 72 Method method = null; |
73 try | 73 try |
74 { | 74 { |
75 /* | 75 /* |
76 * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); | 76 * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); |
77 */ | 77 */ |
78 method = ConnectivityManager.class.getMethod("getProxy"); | 78 method = ConnectivityManager.class.getMethod("getProxy"); |
79 } | 79 } |
80 catch (NoSuchMethodException e) | 80 catch (final NoSuchMethodException e) |
81 { | 81 { |
82 // This is normal situation for pre-ICS devices | 82 // This is normal situation for pre-ICS devices |
83 return null; | 83 return null; |
84 } | 84 } |
85 catch (Exception e) | 85 catch (final Exception e) |
86 { | 86 { |
87 // This should not happen | 87 // This should not happen |
88 Log.e(TAG, "getProxy failure", e); | 88 Log.e(TAG, "getProxy failure", e); |
89 return null; | 89 return null; |
90 } | 90 } |
91 | 91 |
92 try | 92 try |
93 { | 93 { |
94 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
tSystemService(Context.CONNECTIVITY_SERVICE); | 94 final ConnectivityManager connectivityManager = (ConnectivityManager) cont
ext.getSystemService(Context.CONNECTIVITY_SERVICE); |
95 Object pp = method.invoke(connectivityManager); | 95 final Object pp = method.invoke(connectivityManager); |
96 if (pp == null) | 96 if (pp == null) |
97 return null; | 97 return null; |
98 | 98 |
99 return getUserProxy(pp); | 99 return getUserProxy(pp); |
100 } | 100 } |
101 catch (Exception e) | 101 catch (final Exception e) |
102 { | 102 { |
103 // This should not happen | 103 // This should not happen |
104 Log.e(TAG, "getProxy failure", e); | 104 Log.e(TAG, "getProxy failure", e); |
105 return null; | 105 return null; |
106 } | 106 } |
107 } | 107 } |
108 | 108 |
109 /** | 109 /** |
110 * Reads system proxy settings on Android 3.1+ using Java reflection. | 110 * Reads system proxy settings on Android 3.1+ using Java reflection. |
111 * | 111 * |
112 * @param pp | 112 * @param pp |
113 * ProxyProperties object | 113 * ProxyProperties object |
114 * @return string array of host, port and exclusion list | 114 * @return string array of host, port and exclusion list |
115 * @throws Exception | 115 * @throws Exception |
116 */ | 116 */ |
117 protected static String[] getUserProxy(Object pp) throws Exception | 117 protected static String[] getUserProxy(final Object pp) throws Exception |
118 { | 118 { |
119 String[] userProxy = new String[3]; | 119 final String[] userProxy = new String[3]; |
120 | 120 |
121 String className = "android.net.ProxyProperties"; | 121 final String className = "android.net.ProxyProperties"; |
122 Class<?> c = Class.forName(className); | 122 final Class<?> c = Class.forName(className); |
123 Method method; | 123 Method method; |
124 | 124 |
125 /* | 125 /* |
126 * String proxyHost = pp.getHost() | 126 * String proxyHost = pp.getHost() |
127 */ | 127 */ |
128 method = c.getMethod("getHost"); | 128 method = c.getMethod("getHost"); |
129 userProxy[0] = (String) method.invoke(pp); | 129 userProxy[0] = (String) method.invoke(pp); |
130 | 130 |
131 /* | 131 /* |
132 * int proxyPort = pp.getPort(); | 132 * int proxyPort = pp.getPort(); |
(...skipping 12 matching lines...) Expand all Loading... |
145 else | 145 else |
146 return null; | 146 return null; |
147 } | 147 } |
148 | 148 |
149 /** | 149 /** |
150 * 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+ |
151 * devices using Java reflection. | 151 * devices using Java reflection. |
152 * | 152 * |
153 * @return true if device supports native proxy setting | 153 * @return true if device supports native proxy setting |
154 */ | 154 */ |
155 public static boolean setConnectionProxy(Context context, String host, int por
t, String excl) | 155 public static boolean setConnectionProxy(final Context context, final String h
ost, final int port, final String excl) |
156 { | 156 { |
157 Method method = null; | 157 Method method = null; |
158 try | 158 try |
159 { | 159 { |
160 /* | 160 /* |
161 * android.net.LinkProperties lp = | 161 * android.net.LinkProperties lp = |
162 * ConnectivityManager.getActiveLinkProperties(); | 162 * ConnectivityManager.getActiveLinkProperties(); |
163 */ | 163 */ |
164 method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); | 164 method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); |
165 } | 165 } |
166 catch (NoSuchMethodException e) | 166 catch (final NoSuchMethodException e) |
167 { | 167 { |
168 // This is normal situation for pre-ICS devices | 168 // This is normal situation for pre-ICS devices |
169 return false; | 169 return false; |
170 } | 170 } |
171 catch (Exception e) | 171 catch (final Exception e) |
172 { | 172 { |
173 // This should not happen | 173 // This should not happen |
174 Log.e(TAG, "setHttpProxy failure", e); | 174 Log.e(TAG, "setHttpProxy failure", e); |
175 return false; | 175 return false; |
176 } | 176 } |
177 try | 177 try |
178 { | 178 { |
179 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
tSystemService(Context.CONNECTIVITY_SERVICE); | 179 final ConnectivityManager connectivityManager = (ConnectivityManager) cont
ext.getSystemService(Context.CONNECTIVITY_SERVICE); |
180 Object lp = method.invoke(connectivityManager); | 180 final Object lp = method.invoke(connectivityManager); |
181 // There is no active link now, but device has native proxy support | 181 // There is no active link now, but device has native proxy support |
182 if (lp == null) | 182 if (lp == null) |
183 return true; | 183 return true; |
184 | 184 |
185 String className = "android.net.ProxyProperties"; | 185 final String className = "android.net.ProxyProperties"; |
186 Class<?> c = Class.forName(className); | 186 final Class<?> c = Class.forName(className); |
187 method = lp.getClass().getMethod("setHttpProxy", c); | 187 method = lp.getClass().getMethod("setHttpProxy", c); |
188 if (host != null) | 188 if (host != null) |
189 { | 189 { |
190 /* | 190 /* |
191 * ProxyProperties pp = new ProxyProperties(host, port, excl); | 191 * ProxyProperties pp = new ProxyProperties(host, port, excl); |
192 */ | 192 */ |
193 Class<?>[] parameter = new Class[] {String.class, int.class, String.clas
s}; | 193 final Class<?>[] parameter = new Class[] { String.class, int.class, Stri
ng.class }; |
194 Object args[] = new Object[3]; | 194 final Object args[] = new Object[3]; |
195 args[0] = host; | 195 args[0] = host; |
196 args[1] = Integer.valueOf(port); | 196 args[1] = Integer.valueOf(port); |
197 args[2] = excl; | 197 args[2] = excl; |
198 Constructor<?> cons = c.getConstructor(parameter); | 198 final Constructor<?> cons = c.getConstructor(parameter); |
199 Object pp = cons.newInstance(args); | 199 final Object pp = cons.newInstance(args); |
200 /* | 200 /* |
201 * lp.setHttpProxy(pp); | 201 * lp.setHttpProxy(pp); |
202 */ | 202 */ |
203 method.invoke(lp, pp); | 203 method.invoke(lp, pp); |
204 } | 204 } |
205 else | 205 else |
206 { | 206 { |
207 /* | 207 /* |
208 * lp.setHttpProxy(null); | 208 * lp.setHttpProxy(null); |
209 */ | 209 */ |
210 method.invoke(lp, new Object[] {null}); | 210 method.invoke(lp, new Object[] { null }); |
211 } | 211 } |
212 | 212 |
213 Intent intent = null; | 213 Intent intent = null; |
214 NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); | 214 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); |
215 switch (ni.getType()) | 215 switch (ni.getType()) |
216 { | 216 { |
217 case ConnectivityManager.TYPE_WIFI: | 217 case ConnectivityManager.TYPE_WIFI: |
218 intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED"); | 218 intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED"); |
219 break; | 219 break; |
220 case ConnectivityManager.TYPE_MOBILE: | 220 case ConnectivityManager.TYPE_MOBILE: |
221 // TODO We leave it here for future, it does not work now | 221 // TODO We leave it here for future, it does not work now |
222 // intent = new Intent("android.intent.action.ANY_DATA_STATE"); | 222 // intent = new Intent("android.intent.action.ANY_DATA_STATE"); |
223 break; | 223 break; |
224 } | 224 } |
225 if (intent != null) | 225 if (intent != null) |
226 { | 226 { |
227 if (lp != null) | 227 if (lp != null) |
228 { | 228 { |
229 intent.putExtra("linkProperties", (Parcelable) lp); | 229 intent.putExtra("linkProperties", (Parcelable) lp); |
230 } | 230 } |
231 context.sendBroadcast(intent); | 231 context.sendBroadcast(intent); |
232 } | 232 } |
233 | 233 |
234 return true; | 234 return true; |
235 } | 235 } |
236 catch (SecurityException e) | 236 catch (final SecurityException e) |
237 { | 237 { |
238 // This is ok for 4.1.2+, 4.2.2+ and later | 238 // This is ok for 4.1.2+, 4.2.2+ and later |
239 return false; | 239 return false; |
240 } | 240 } |
241 catch (Exception e) | 241 catch (final Exception e) |
242 { | 242 { |
243 // This should not happen | 243 // This should not happen |
244 Log.e(TAG, "setHttpProxy failure", e); | 244 Log.e(TAG, "setHttpProxy failure", e); |
245 return false; | 245 return false; |
246 } | 246 } |
247 } | 247 } |
248 } | 248 } |
OLD | NEW |