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

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

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.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
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
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 final static 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 // LinkProperties lp = connectivityManager.getActiveLinkProperties()
37 * LinkProperties lp = connectivityManager.getActiveLinkProperties() 37 final Method method = connectivityManager.getClass().getMethod("getActiveLin kProperties");
38 */ 38 final Object lp = method.invoke(connectivityManager);
39 Method method = connectivityManager.getClass().getMethod("getActiveLinkPrope rties");
40 Object lp = method.invoke(connectivityManager);
41 39
42 Object pp = ProxySettings.getLinkProxy(lp); 40 final Object pp = ProxySettings.getLinkProxy(lp);
43 return pp; 41 return pp;
44 } 42 }
45 43
46 /** 44 /**
47 * Reads proxy settings from link properties on Android 3.1+ using Java 45 * Reads proxy settings from link properties on Android 3.1+ using Java
48 * reflection. 46 * reflection.
49 * 47 *
50 * @param linkProperties 48 * @param linkProperties
51 * android.net.LinkProperties 49 * android.net.LinkProperties
52 * @return ProxyProperties 50 * @return ProxyProperties
53 * @throws Exception 51 * @throws Exception
54 */ 52 */
55 public static Object getLinkProxy(Object linkProperties) throws Exception 53 public static Object getLinkProxy(final Object linkProperties) throws Exceptio n
56 { 54 {
57 /* 55 /*
58 * linkProperties.getHttpProxy(); 56 * linkProperties.getHttpProxy();
59 */ 57 */
60 Method method = linkProperties.getClass().getMethod("getHttpProxy"); 58 final Method method = linkProperties.getClass().getMethod("getHttpProxy");
61 Object pp = method.invoke(linkProperties); 59 final Object pp = method.invoke(linkProperties);
62 return pp; 60 return pp;
63 } 61 }
64 62
65 /** 63 /**
66 * Reads system proxy settings on Android 3.1+ using Java reflection. 64 * Reads system proxy settings on Android 3.1+ using Java reflection.
67 * 65 *
68 * @return string array of host, port and exclusion list 66 * @return string array of host, port and exclusion list
69 */ 67 */
70 public static String[] getUserProxy(Context context) 68 public static String[] getUserProxy(final Context context)
71 { 69 {
72 Method method = null; 70 Method method = null;
73 try 71 try
74 { 72 {
75 /* 73 /*
76 * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); 74 * ProxyProperties proxyProperties = ConnectivityManager.getProxy();
77 */ 75 */
78 method = ConnectivityManager.class.getMethod("getProxy"); 76 method = ConnectivityManager.class.getMethod("getProxy");
79 } 77 }
80 catch (NoSuchMethodException e) 78 catch (final NoSuchMethodException e)
81 { 79 {
82 // This is normal situation for pre-ICS devices 80 // This is normal situation for pre-ICS devices
83 return null; 81 return null;
84 } 82 }
85 catch (Exception e) 83 catch (final Exception e)
86 { 84 {
87 // This should not happen 85 // This should not happen
88 Log.e(TAG, "getProxy failure", e); 86 Log.e(TAG, "getProxy failure", e);
89 return null; 87 return null;
90 } 88 }
91 89
92 try 90 try
93 { 91 {
94 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge tSystemService(Context.CONNECTIVITY_SERVICE); 92 final ConnectivityManager connectivityManager = (ConnectivityManager)conte xt.getSystemService(Context.CONNECTIVITY_SERVICE);
95 Object pp = method.invoke(connectivityManager); 93 final Object pp = method.invoke(connectivityManager);
96 if (pp == null) 94 if (pp == null)
95 {
97 return null; 96 return null;
97 }
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();
133 */ 133 */
134 method = c.getMethod("getPort"); 134 method = c.getMethod("getPort");
135 userProxy[1] = String.valueOf(method.invoke(pp)); 135 userProxy[1] = String.valueOf(method.invoke(pp));
136 136
137 /* 137 /*
138 * String proxyEL = pp.getExclusionList() 138 * String proxyEL = pp.getExclusionList()
139 */ 139 */
140 method = c.getMethod("getExclusionList"); 140 method = c.getMethod("getExclusionList");
141 userProxy[2] = (String) method.invoke(pp); 141 userProxy[2] = (String)method.invoke(pp);
142 142
143 if (userProxy[0] != null) 143 if (userProxy[0] != null)
144 {
144 return userProxy; 145 return userProxy;
146 }
145 else 147 else
148 {
146 return null; 149 return null;
150 }
147 } 151 }
148 152
149 /** 153 /**
150 * Tries to set local proxy in system settings via native call on Android 3.1+ 154 * Tries to set local proxy in system settings via native call on Android 3.1+
151 * devices using Java reflection. 155 * devices using Java reflection.
152 * 156 *
153 * @return true if device supports native proxy setting 157 * @return true if device supports native proxy setting
154 */ 158 */
155 public static boolean setConnectionProxy(Context context, String host, int por t, String excl) 159 public static boolean setConnectionProxy(final Context context, final String h ost, final int port, final String excl)
156 { 160 {
157 Method method = null; 161 Method method = null;
158 try 162 try
159 { 163 {
160 /* 164 /*
161 * android.net.LinkProperties lp = 165 * android.net.LinkProperties lp =
162 * ConnectivityManager.getActiveLinkProperties(); 166 * ConnectivityManager.getActiveLinkProperties();
163 */ 167 */
164 method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); 168 method = ConnectivityManager.class.getMethod("getActiveLinkProperties");
165 } 169 }
166 catch (NoSuchMethodException e) 170 catch (final NoSuchMethodException e)
167 { 171 {
168 // This is normal situation for pre-ICS devices 172 // This is normal situation for pre-ICS devices
169 return false; 173 return false;
170 } 174 }
171 catch (Exception e) 175 catch (final Exception e)
172 { 176 {
173 // This should not happen 177 // This should not happen
174 Log.e(TAG, "setHttpProxy failure", e); 178 Log.e(TAG, "setHttpProxy failure", e);
175 return false; 179 return false;
176 } 180 }
177 try 181 try
178 { 182 {
179 ConnectivityManager connectivityManager = (ConnectivityManager) context.ge tSystemService(Context.CONNECTIVITY_SERVICE); 183 final ConnectivityManager connectivityManager = (ConnectivityManager)conte xt.getSystemService(Context.CONNECTIVITY_SERVICE);
180 Object lp = method.invoke(connectivityManager); 184 final Object lp = method.invoke(connectivityManager);
181 // There is no active link now, but device has native proxy support 185 // There is no active link now, but device has native proxy support
182 if (lp == null) 186 if (lp == null)
187 {
183 return true; 188 return true;
189 }
184 190
185 String className = "android.net.ProxyProperties"; 191 final String className = "android.net.ProxyProperties";
186 Class<?> c = Class.forName(className); 192 final Class<?> c = Class.forName(className);
187 method = lp.getClass().getMethod("setHttpProxy", c); 193 method = lp.getClass().getMethod("setHttpProxy", c);
188 if (host != null) 194 if (host != null)
189 { 195 {
190 /* 196 /*
191 * ProxyProperties pp = new ProxyProperties(host, port, excl); 197 * ProxyProperties pp = new ProxyProperties(host, port, excl);
192 */ 198 */
193 Class<?>[] parameter = new Class[] {String.class, int.class, String.clas s}; 199 final Class<?>[] parameter = new Class[] { String.class, int.class, Stri ng.class };
194 Object args[] = new Object[3]; 200 final Object args[] = new Object[3];
195 args[0] = host; 201 args[0] = host;
196 args[1] = Integer.valueOf(port); 202 args[1] = Integer.valueOf(port);
197 args[2] = excl; 203 args[2] = excl;
198 Constructor<?> cons = c.getConstructor(parameter); 204 final Constructor<?> cons = c.getConstructor(parameter);
199 Object pp = cons.newInstance(args); 205 final Object pp = cons.newInstance(args);
200 /* 206 /*
201 * lp.setHttpProxy(pp); 207 * lp.setHttpProxy(pp);
202 */ 208 */
203 method.invoke(lp, pp); 209 method.invoke(lp, pp);
204 } 210 }
205 else 211 else
206 { 212 {
207 /* 213 /*
208 * lp.setHttpProxy(null); 214 * lp.setHttpProxy(null);
209 */ 215 */
210 method.invoke(lp, new Object[] {null}); 216 method.invoke(lp, new Object[] { null });
211 } 217 }
212 218
213 Intent intent = null; 219 Intent intent = null;
214 NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); 220 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
215 switch (ni.getType()) 221 switch (ni.getType())
216 { 222 {
217 case ConnectivityManager.TYPE_WIFI: 223 case ConnectivityManager.TYPE_WIFI:
218 intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED"); 224 intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED");
219 break; 225 break;
220 case ConnectivityManager.TYPE_MOBILE: 226 case ConnectivityManager.TYPE_MOBILE:
221 // TODO We leave it here for future, it does not work now 227 // TODO We leave it here for future, it does not work now
222 // intent = new Intent("android.intent.action.ANY_DATA_STATE"); 228 // intent = new Intent("android.intent.action.ANY_DATA_STATE");
223 break; 229 break;
224 } 230 }
225 if (intent != null) 231 if (intent != null)
226 { 232 {
227 if (lp != null) 233 if (lp != null)
228 { 234 {
229 intent.putExtra("linkProperties", (Parcelable) lp); 235 intent.putExtra("linkProperties", (Parcelable)lp);
230 } 236 }
231 context.sendBroadcast(intent); 237 context.sendBroadcast(intent);
232 } 238 }
233 239
234 return true; 240 return true;
235 } 241 }
236 catch (SecurityException e) 242 catch (final SecurityException e)
237 { 243 {
238 // This is ok for 4.1.2+, 4.2.2+ and later 244 // This is ok for 4.1.2+, 4.2.2+ and later
239 return false; 245 return false;
240 } 246 }
241 catch (Exception e) 247 catch (final Exception e)
242 { 248 {
243 // This should not happen 249 // This should not happen
244 Log.e(TAG, "setHttpProxy failure", e); 250 Log.e(TAG, "setHttpProxy failure", e);
245 return false; 251 return false;
246 } 252 }
247 } 253 }
248 } 254 }
OLDNEW

Powered by Google App Engine
This is Rietveld