WifiProxyManager.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package me.yoqi.wifiproxy.utils;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.util.List;
  6. import android.content.Context;
  7. import android.net.wifi.WifiConfiguration;
  8. import android.net.wifi.WifiManager;
  9. /**
  10. * 代理设置
  11. *
  12. * @author liuyuqi
  13. *
  14. */
  15. public class WifiProxyManager {
  16. private static Object proxySettings;
  17. /**
  18. * 设置公有成员值
  19. *
  20. * @param obj
  21. * @param value
  22. * @param name
  23. * @throws SecurityException
  24. * @throws NoSuchFieldException
  25. * @throws IllegalArgumentException
  26. * @throws IllegalAccessException
  27. */
  28. public static void setEnumField(Object obj, String value, String name)
  29. throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
  30. Field f = obj.getClass().getField(name);
  31. f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
  32. }
  33. /**
  34. * 获取public字段
  35. *
  36. * @param obj
  37. * @param name
  38. * @return
  39. * @throws SecurityException
  40. * @throws NoSuchFieldException
  41. * @throws IllegalArgumentException
  42. * @throws IllegalAccessException
  43. */
  44. public static Object getDeclaredFieldObject(Object obj, String name)
  45. throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
  46. Field f = obj.getClass().getField(name);
  47. Object out = f.get(obj);
  48. return out;
  49. }
  50. /**
  51. * @param config
  52. * @param string
  53. * @param mIpConfiguration
  54. */
  55. private static void setDeclardFildObject(WifiConfiguration config, String string, Object mIpConfiguration) {
  56. }
  57. /**
  58. * 获取当前WiFi状态
  59. *
  60. * @param wifiManager
  61. * @return
  62. */
  63. public static WifiConfiguration getCurrentWifiConfiguration(WifiManager wifiManager) {
  64. if (!wifiManager.isWifiEnabled())
  65. return null;// 没有打开wifi
  66. List<WifiConfiguration> configurationList = wifiManager.getConfiguredNetworks();
  67. WifiConfiguration configuration = null;
  68. int cur = wifiManager.getConnectionInfo().getNetworkId();
  69. for (int i = 0; i < configurationList.size(); ++i) {
  70. WifiConfiguration wifiConfiguration = configurationList.get(i);
  71. if (wifiConfiguration.networkId == cur)
  72. configuration = wifiConfiguration;
  73. }
  74. return configuration;
  75. }
  76. /**
  77. * 设置wifi代理 API>17
  78. *
  79. * @param context
  80. * @param host
  81. * 代理ip
  82. * @param port
  83. * 代理端口
  84. * @param exclList
  85. * 添加不用代理的网址
  86. */
  87. public static void setWifiProxySettingsFor17And(Context context, String host, int port, String exclList) {
  88. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  89. WifiConfiguration config = getCurrentWifiConfiguration(wifiManager);
  90. if (config == null)
  91. return;
  92. try {
  93. Object linkProperties = getDeclaredFieldObject(config, "linkProperties");
  94. if (null == linkProperties)
  95. return;
  96. // 获取类 LinkProperties的setHttpProxy方法
  97. Class<?> proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
  98. Class<?>[] setHttpProxyParams = new Class[1];
  99. setHttpProxyParams[0] = proxyPropertiesClass;
  100. Class<?> lpClass = Class.forName("android.net.LinkProperties");
  101. Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
  102. setHttpProxy.setAccessible(true);
  103. // 获取类 ProxyProperties的构造函数
  104. Constructor<?> proxyPropertiesCtor = proxyPropertiesClass.getConstructor(String.class, int.class,
  105. String.class);
  106. // 实例化类ProxyProperties
  107. proxySettings = proxyPropertiesCtor.newInstance(host, port, exclList);
  108. Object[] params = new Object[1];
  109. params[0] = proxySettings;
  110. setHttpProxy.invoke(linkProperties, params);
  111. setEnumField(config, "STATIC", "proxySettings");
  112. // save the settings
  113. wifiManager.updateNetwork(config);
  114. wifiManager.disconnect();
  115. wifiManager.reconnect();
  116. } catch (Exception e) {
  117. }
  118. }
  119. /**
  120. * 取消代理设置
  121. *
  122. * @param context
  123. */
  124. public static void unsetWifiProxySettingsFor17And(Context context) {
  125. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  126. WifiConfiguration config = getCurrentWifiConfiguration(wifiManager);
  127. if (null == config)
  128. return;
  129. try {
  130. Object linkProperties = getDeclaredFieldObject(config, "linkProperties");
  131. if (null == linkProperties)
  132. return;
  133. // get the setHttpProxy method for LinkProperties
  134. Class<?> proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
  135. Class<?>[] setHttpProxyParams = new Class[1];
  136. setHttpProxyParams[0] = proxyPropertiesClass;
  137. Class<?> lpClass = Class.forName("android.net.LinkProperties");
  138. Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
  139. setHttpProxy.setAccessible(true);
  140. // pass null as the proxy
  141. Object[] params = new Object[1];
  142. params[0] = null;
  143. setHttpProxy.invoke(linkProperties, params);
  144. setEnumField(config, "NONE", "proxySettings");
  145. wifiManager.updateNetwork(config);
  146. wifiManager.disconnect();
  147. wifiManager.reconnect();
  148. } catch (Exception e) {
  149. }
  150. }
  151. }