Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/ProxySettings.java

Issue 4705284891082752: Proxy configurators (Closed)
Patch Set: Last batch of review issues Created Aug. 24, 2014, 11:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.android;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.ConnectivityManager;
26 import android.net.NetworkInfo;
27 import android.os.Parcelable;
28 import android.util.Log;
29
30 public class ProxySettings
31 {
32 private static final String TAG = Utils.getTag(ProxySettings.class);
33
34 public static Object getActiveLinkProxy(final ConnectivityManager connectivity Manager) throws Exception
35 {
36 /*
37 * LinkProperties lp = connectivityManager.getActiveLinkProperties()
38 */
39 final Method method = connectivityManager.getClass().getMethod("getActiveLin kProperties");
40 final Object lp = method.invoke(connectivityManager);
41
42 final 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(final Object linkProperties) throws Exceptio n
56 {
57 /*
58 * linkProperties.getHttpProxy();
59 */
60 final Method method = linkProperties.getClass().getMethod("getHttpProxy");
61 final Object pp = method.invoke(linkProperties);
62 return pp;
63 }
64
65 /**
66 * Reads system proxy settings on Android 3.1+ using Java reflection.
67 *
68 * @return string array of host, port and exclusion list
69 */
70 public static String[] getUserProxy(final Context context)
71 {
72 Method method = null;
73 try
74 {
75 /*
76 * ProxyProperties proxyProperties = ConnectivityManager.getProxy();
77 */
78 method = ConnectivityManager.class.getMethod("getProxy");
79 }
80 catch (final NoSuchMethodException e)
81 {
82 // This is normal situation for pre-ICS devices
83 return null;
84 }
85 catch (final Exception e)
86 {
87 // This should not happen
88 Log.e(TAG, "getProxy failure", e);
89 return null;
90 }
91
92 try
93 {
94 final ConnectivityManager connectivityManager = (ConnectivityManager) cont ext.getSystemService(Context.CONNECTIVITY_SERVICE);
95 final Object pp = method.invoke(connectivityManager);
96 if (pp == null)
97 return null;
98
99 return getUserProxy(pp);
100 }
101 catch (final Exception e)
102 {
103 // This should not happen
104 Log.e(TAG, "getProxy failure", e);
105 return null;
106 }
107 }
108
109 /**
110 * Reads system proxy settings on Android 3.1+ using Java reflection.
111 *
112 * @param pp
113 * ProxyProperties object
114 * @return string array of host, port and exclusion list
115 * @throws Exception
116 */
117 protected static String[] getUserProxy(final Object pp) throws Exception
118 {
119 final String[] userProxy = new String[3];
120
121 final String className = "android.net.ProxyProperties";
122 final Class<?> c = Class.forName(className);
123 Method method;
124
125 /*
126 * String proxyHost = pp.getHost()
127 */
128 method = c.getMethod("getHost");
129 userProxy[0] = (String) method.invoke(pp);
130
131 /*
132 * int proxyPort = pp.getPort();
133 */
134 method = c.getMethod("getPort");
135 userProxy[1] = String.valueOf(method.invoke(pp));
136
137 /*
138 * String proxyEL = pp.getExclusionList()
139 */
140 method = c.getMethod("getExclusionList");
141 userProxy[2] = (String) method.invoke(pp);
142
143 if (userProxy[0] != null)
144 return userProxy;
145 else
146 return null;
147 }
148
149 /**
150 * Tries to set local proxy in system settings via native call on Android 3.1+
151 * devices using Java reflection.
152 *
153 * @return true if device supports native proxy setting
154 */
155 public static boolean setConnectionProxy(final Context context, final String h ost, final int port, final String excl)
156 {
157 Method method = null;
158 try
159 {
160 /*
161 * android.net.LinkProperties lp =
162 * ConnectivityManager.getActiveLinkProperties();
163 */
164 method = ConnectivityManager.class.getMethod("getActiveLinkProperties");
165 }
166 catch (final NoSuchMethodException e)
167 {
168 // This is normal situation for pre-ICS devices
169 return false;
170 }
171 catch (final Exception e)
172 {
173 // This should not happen
174 Log.e(TAG, "setHttpProxy failure", e);
175 return false;
176 }
177 try
178 {
179 final ConnectivityManager connectivityManager = (ConnectivityManager) cont ext.getSystemService(Context.CONNECTIVITY_SERVICE);
180 final Object lp = method.invoke(connectivityManager);
181 // There is no active link now, but device has native proxy support
182 if (lp == null)
183 return true;
184
185 final String className = "android.net.ProxyProperties";
186 final Class<?> c = Class.forName(className);
187 method = lp.getClass().getMethod("setHttpProxy", c);
188 if (host != null)
189 {
190 /*
191 * ProxyProperties pp = new ProxyProperties(host, port, excl);
192 */
193 final Class<?>[] parameter = new Class[] { String.class, int.class, Stri ng.class };
194 final Object args[] = new Object[3];
195 args[0] = host;
196 args[1] = Integer.valueOf(port);
197 args[2] = excl;
198 final Constructor<?> cons = c.getConstructor(parameter);
199 final Object pp = cons.newInstance(args);
200 /*
201 * lp.setHttpProxy(pp);
202 */
203 method.invoke(lp, pp);
204 }
205 else
206 {
207 /*
208 * lp.setHttpProxy(null);
209 */
210 method.invoke(lp, new Object[] { null });
211 }
212
213 Intent intent = null;
214 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
215 switch (ni.getType())
216 {
217 case ConnectivityManager.TYPE_WIFI:
218 intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED");
219 break;
220 case ConnectivityManager.TYPE_MOBILE:
221 // TODO We leave it here for future, it does not work now
222 // intent = new Intent("android.intent.action.ANY_DATA_STATE");
223 break;
224 }
225 if (intent != null)
226 {
227 if (lp != null)
228 {
229 intent.putExtra("linkProperties", (Parcelable) lp);
230 }
231 context.sendBroadcast(intent);
232 }
233
234 return true;
235 }
236 catch (final SecurityException e)
237 {
238 // This is ok for 4.1.2+, 4.2.2+ and later
239 return false;
240 }
241 catch (final Exception e)
242 {
243 // This should not happen
244 Log.e(TAG, "setHttpProxy failure", e);
245 return false;
246 }
247 }
248 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/android/ProxyService.java ('k') | src/org/adblockplus/android/ServiceBinder.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld