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

Powered by Google App Engine
This is Rietveld