@Override
public boolean setGlobalProxy(String proxy) throws RemoteException {
boolean ret = false;
synchronized (mLock) {
final long callingId = Binder.clearCallingIdentity();
try {
//ret = Settings.Global.putString(mContext.getContentResolver(), Settings.Global.HTTP_PROXY, proxy);
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
ProxyInfo mHttpProxy = new ProxyInfo(proxy.split(":")[0], Integer.parseInt(proxy.split(":")[1]), null);
WifiConfiguration config = getCurrentConfig();
IpConfiguration wifiIpConfiguration = new IpConfiguration(config.getIpConfiguration().ipAssignment, IpConfiguration.ProxySettings.STATIC, config.getIpConfiguration().staticIpConfiguration, mHttpProxy);
config.setIpConfiguration(wifiIpConfiguration);
wifiManager.setWifiApConfiguration(config);
wifiManager.updateNetwork(config);
wifiManager.save(config, null /* listener */);
wifiManager.connect(config, null /* listener */);
} finally {
Binder.restoreCallingIdentity(callingId);
}
}
return ret;
}
public WifiConfiguration getCurrentConfig() {
WifiManager mWifiManager = ((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE));
WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); //得到连接的wifi网络
WifiConfiguration wifiConfig = null;
List<WifiConfiguration> configuredNetworks = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks) {
if (conf.networkId == connectionInfo.getNetworkId()) {
wifiConfig = conf;
break;
}
}
return wifiConfig;
}
或者
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
ProxyInfo mHttpProxy = TextUtils.isEmpty(proxy) ? null : new ProxyInfo(proxy.split(":")[0], Integer.parseInt(proxy.split(":")[1]), null);
connectivityManager.setGlobalProxy(mHttpProxy);
代码追踪:
/**
* Set a network-independent global http proxy. This is not normally what you want
* for typical HTTP proxies - they are general network dependent. However if you're
* doing something unusual like general internal filtering this may be useful. On
* a private network where the proxy is not accessible, you may break HTTP using this.
*
* @param p A {@link ProxyInfo} object defining the new global
* HTTP proxy. A {@code null} value will clear the global HTTP proxy.
* @hide
*/
@RequiresPermission(android.Manifest.permission.NETWORK_STACK)
public void setGlobalProxy(ProxyInfo p) {
try {
mService.setGlobalProxy(p);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
frameworks\base\services\core\java\com\android\server\ConnectivityService.java
@Override
public void setGlobalProxy(final ProxyInfo proxyProperties) {
NetworkStack.checkNetworkStackPermission(mContext);
mProxyTracker.setGlobalProxy(proxyProperties);
}
frameworks\base\services\core\java\com\android\server\connectivity\ProxyTracker.java
public void setGlobalProxy(@Nullable ProxyInfo proxyInfo) {
synchronized (mProxyLock) {
// ProxyInfo#equals is not commutative :( and is public API, so it can't be fixed.
if (proxyInfo == mGlobalProxy) return;
if (proxyInfo != null && proxyInfo.equals(mGlobalProxy)) return;
if (mGlobalProxy != null && mGlobalProxy.equals(proxyInfo)) return;
final String host;
final int port;
final String exclList;
final String pacFileUrl;
if (proxyInfo != null && (!TextUtils.isEmpty(proxyInfo.getHost()) ||
!Uri.EMPTY.equals(proxyInfo.getPacFileUrl()))) {
if (!proxyInfo.isValid()) {
if (DBG) Slog.d(TAG, "Invalid proxy properties, ignoring: " + proxyInfo);
return;
}
mGlobalProxy = new ProxyInfo(proxyInfo);
host = mGlobalProxy.getHost();
port = mGlobalProxy.getPort();
exclList = mGlobalProxy.getExclusionListAsString();
pacFileUrl = Uri.EMPTY.equals(proxyInfo.getPacFileUrl())
? "" : proxyInfo.getPacFileUrl().toString();
} else {
host = "";
port = 0;
exclList = "";
pacFileUrl = "";
mGlobalProxy = null;
}
final ContentResolver res = mContext.getContentResolver();
final long token = Binder.clearCallingIdentity();
try {
Settings.Global.putString(res, GLOBAL_HTTP_PROXY_HOST, host);
Settings.Global.putInt(res, GLOBAL_HTTP_PROXY_PORT, port);
Settings.Global.putString(res, GLOBAL_HTTP_PROXY_EXCLUSION_LIST, exclList);
Settings.Global.putString(res, GLOBAL_HTTP_PROXY_PAC, pacFileUrl);
} finally {
Binder.restoreCallingIdentity(token);
}
sendProxyBroadcast();
}
}
frameworks\base\core\java\android\provider\Settings.java
/**
* Host name for global http proxy. Set via ConnectivityManager.
*
* @hide
*/
public static final String GLOBAL_HTTP_PROXY_HOST = "global_http_proxy_host";
/**
* Integer host port for global http proxy. Set via ConnectivityManager.
*
* @hide
*/
public static final String GLOBAL_HTTP_PROXY_PORT = "global_http_proxy_port";