private static NetworkState buildMobile4gState() { final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null); info.setDetailedState(DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_IFACE); return new NetworkState(info, prop, null); }
private static NetworkState buildMobile3gState(String subscriberId) { final NetworkInfo info = new NetworkInfo(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null); info.setDetailedState(DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_IFACE); return new NetworkState(info, prop, null, subscriberId); }
private void showIpConfigFields() { WifiConfiguration config = null; mView.findViewById(R.id.ip_fields).setVisibility(View.VISIBLE); if (mAccessPoint != null && mAccessPoint.networkId != INVALID_NETWORK_ID) { config = mAccessPoint.getConfig(); } if (mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) { mView.findViewById(R.id.staticip).setVisibility(View.VISIBLE); if (mIpAddressView == null) { mIpAddressView = (TextView) mView.findViewById(R.id.ipaddress); mIpAddressView.addTextChangedListener(this); mGatewayView = (TextView) mView.findViewById(R.id.gateway); mGatewayView.addTextChangedListener(this); mNetworkPrefixLengthView = (TextView) mView.findViewById(R.id.network_prefix_length); mNetworkPrefixLengthView.addTextChangedListener(this); mDns1View = (TextView) mView.findViewById(R.id.dns1); mDns1View.addTextChangedListener(this); mDns2View = (TextView) mView.findViewById(R.id.dns2); mDns2View.addTextChangedListener(this); } if (config != null) { LinkProperties linkProperties = config.linkProperties; Iterator<LinkAddress> iterator = linkProperties.getLinkAddresses().iterator(); if (iterator.hasNext()) { LinkAddress linkAddress = iterator.next(); mIpAddressView.setText(linkAddress.getAddress().getHostAddress()); mNetworkPrefixLengthView.setText(Integer.toString(linkAddress.getNetworkPrefixLength())); } for (RouteInfo route : linkProperties.getRoutes()) { if (route.isDefaultRoute()) { mGatewayView.setText(route.getGateway().getHostAddress()); break; } } Iterator<InetAddress> dnsIterator = linkProperties.getDnses().iterator(); if (dnsIterator.hasNext()) { mDns1View.setText(dnsIterator.next().getHostAddress()); } if (dnsIterator.hasNext()) { mDns2View.setText(dnsIterator.next().getHostAddress()); } } } else { mView.findViewById(R.id.staticip).setVisibility(View.GONE); } }
private int validateIpConfigFields(LinkProperties linkProperties) { String ipAddr = mIpAddressView.getText().toString(); InetAddress inetAddr = null; try { inetAddr = NetworkUtils.numericToInetAddress(ipAddr); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_ip_address; } int networkPrefixLength = -1; try { networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString()); } catch (NumberFormatException e) { // Use -1 } if (networkPrefixLength < 0 || networkPrefixLength > 32) { return R.string.wifi_ip_settings_invalid_network_prefix_length; } linkProperties.addLinkAddress(new LinkAddress(inetAddr, networkPrefixLength)); String gateway = mGatewayView.getText().toString(); InetAddress gatewayAddr = null; try { gatewayAddr = NetworkUtils.numericToInetAddress(gateway); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_gateway; } linkProperties.addRoute(new RouteInfo(gatewayAddr)); String dns = mDns1View.getText().toString(); InetAddress dnsAddr = null; try { dnsAddr = NetworkUtils.numericToInetAddress(dns); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_dns; } linkProperties.addDns(dnsAddr); if (mDns2View.length() > 0) { dns = mDns2View.getText().toString(); try { dnsAddr = NetworkUtils.numericToInetAddress(dns); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_dns; } linkProperties.addDns(dnsAddr); } return 0; }
private boolean ipAndProxyFieldsAreValid() { mLinkProperties.clear(); mIpAssignment = (mIpSettingsSpinner != null && mIpSettingsSpinner.getSelectedItemPosition() == STATIC_IP) ? IpAssignment.STATIC : IpAssignment.DHCP; if (mIpAssignment == IpAssignment.STATIC) { int result = validateIpConfigFields(mLinkProperties); if (result != 0) { return false; } } mProxySettings = (mProxySettingsSpinner != null && mProxySettingsSpinner.getSelectedItemPosition() == PROXY_STATIC) ? ProxySettings.STATIC : ProxySettings.NONE; if (mProxySettings == ProxySettings.STATIC) { String host = mProxyHostView.getText().toString(); String portStr = mProxyPortView.getText().toString(); String exclusionList = mProxyExclusionListView.getText().toString(); int port = 0; int result = 0; try { port = Integer.parseInt(portStr); result = ProxySelector.validate(host, portStr, exclusionList); } catch (NumberFormatException e) { result = R.string.proxy_error_invalid_port; } if (result == 0) { ProxyProperties proxyProperties = new ProxyProperties(host, port, exclusionList); mLinkProperties.setHttpProxy(proxyProperties); } else { return false; } } return true; }
private boolean readLinkProperty(String iface) { String DhcpPrefix = "dhcp." + iface + "."; String ip = SystemProperties.get(DhcpPrefix + "ipaddress"); String dns1 = SystemProperties.get(DhcpPrefix + "dns1"); String dns2 = SystemProperties.get(DhcpPrefix + "dns2"); String gateway = SystemProperties.get(DhcpPrefix + "gateway"); String mask = SystemProperties.get(DhcpPrefix + "mask"); if (ip.isEmpty() || gateway.isEmpty()) { Log.e(TAG, "readLinkProperty, ip: " + ip + ", gateway: " + gateway + ", can not be empty"); return false; } int PrefixLen = countPrefixLength(NetworkUtils.numericToInetAddress(mask).getAddress()); mLinkProperties.addLinkAddress( new LinkAddress(NetworkUtils.numericToInetAddress(ip), PrefixLen)); RouteInfo ri = new RouteInfo(NetworkUtils.numericToInetAddress(gateway)); mLinkProperties.addRoute(ri); if (!dns1.isEmpty()) mLinkProperties.addDns(NetworkUtils.numericToInetAddress(dns1)); if (!dns2.isEmpty()) mLinkProperties.addDns(NetworkUtils.numericToInetAddress(dns2)); mLinkProperties.setInterfaceName(iface); return true; }
public synchronized void stopReverseTether(String iface) { NetworkUtils.stopDhcp(iface); mLinkProperties.clear(); mNetworkInfo.setIsAvailable(false); mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null); Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo); msg.sendToTarget(); msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo); msg.sendToTarget(); }
public String getIpAddress() { Log.d(TAG, "get ip "); String ipAddress = null; if (getNetworkTypeName().equals("WIFI")) { WifiManager wifiManager = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); ipAddress = Formatter.formatIpAddress(dhcpInfo.ipAddress); Log.i(TAG, "WIFI_IP_Address: " + ipAddress); // } else if (getNetworkTypeName().equals("ETHERNET")) { } else if (getNetworkTypeName().equals("Ethernet")) { Log.d(TAG, "youxian ip "); if (Build.DEVICE.equals("heran") || Build.DEVICE.equals("scifly_m202_1G") || Build.DEVICE.equalsIgnoreCase("Leader") || Build.DEVICE.equalsIgnoreCase("soniq")) { Log.d(TAG, "qita ip"); LinkProperties linkProperties = mConnManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET); if (null == linkProperties) { return "0.0.0.0"; } String ipStr = linkProperties.getAddresses().toString(); ipAddress = ipStr.substring(2, ipStr.length() - 2); Log.i(TAG, "ETHERNET_IP_Address: " + ipAddress); } else { Log.d(TAG, "828ip"); SharedPreferences settings = mContext.getSharedPreferences("test", mContext.MODE_PRIVATE); ipAddress = settings.getString("ip", "0.0.0.0"); Log.i(TAG, ">>>>>828ipAddress = " + ipAddress); // } } } Log.i(TAG, ">>>>>ipAddress = " + ipAddress); Log.i(TAG, ">>>>>ipAddress = " + ipAddress); return ipAddress; }
public String getGateWay() { String gwAddress = null; if (getNetworkTypeName().equals("WIFI")) { WifiManager wm = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE); DhcpInfo dhcpInfo = wm.getDhcpInfo(); gwAddress = Formatter.formatIpAddress(dhcpInfo.gateway); Log.i(TAG, "WIFI_GATEWAY_Address: " + gwAddress); // } else if (getNetworkTypeName().equals("ETHERNET")) { } else if (getNetworkTypeName().equals("Ethernet")) { Log.d(TAG, "youxian gateway "); if (Build.DEVICE.equals("heran") || Build.DEVICE.equals("scifly_m202_1G") || Build.DEVICE.equalsIgnoreCase("Leader") || Build.DEVICE.equalsIgnoreCase("soniq")) { Log.d(TAG, "qita wangguan"); LinkProperties linkProperties = mConnManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET); if (null == linkProperties) { return ""; } String gatewayStr = linkProperties.getRoutes().toString(); gwAddress = gatewayStr.substring(gatewayStr.length() - 14, gatewayStr.length() - 1); if (gwAddress.contains(">")) { gwAddress = gwAddress.substring(1, gwAddress.length()); } } else { Log.d(TAG, "828wangguan"); SharedPreferences settings = mContext.getSharedPreferences("test", mContext.MODE_PRIVATE); gwAddress = settings.getString("gateway", "0.0.0.0"); } Log.i(TAG, "ETHERNET_GATEWAY_Address : " + gwAddress); } return gwAddress; }
void startReverseTether(final LinkProperties linkProperties) { if (linkProperties == null || TextUtils.isEmpty(linkProperties.getInterfaceName())) { Log.e(TAG, "attempted to reverse tether with empty interface"); return; } synchronized (mLinkPropertiesLock) { if (mLinkProperties.getInterfaceName() != null) { Log.e(TAG, "attempted to reverse tether while already in process"); return; } mLinkProperties = linkProperties; } Thread dhcpThread = new Thread( new Runnable() { public void run() { // Currently this thread runs independently. DhcpResults dhcpResults = new DhcpResults(); boolean success = NetworkUtils.runDhcp(linkProperties.getInterfaceName(), dhcpResults); synchronized (mLinkPropertiesLock) { if (linkProperties.getInterfaceName() != mLinkProperties.getInterfaceName()) { Log.e(TAG, "obsolete DHCP run aborted"); return; } if (!success) { Log.e(TAG, "DHCP request error:" + NetworkUtils.getDhcpError()); mBtdtHandler.obtainMessage(EVENT_NETWORK_FAILED).sendToTarget(); return; } mLinkProperties = dhcpResults.linkProperties; synchronized (mNetworkInfoLock) { mNetworkInfo.setIsAvailable(true); mNetworkInfo.setDetailedState(DetailedState.CONNECTED, null, null); if (mCsHandler != null) { Message msg = mCsHandler.obtainMessage( EVENT_STATE_CHANGED, new NetworkInfo(mNetworkInfo)); msg.sendToTarget(); } } return; } } }); dhcpThread.start(); }
public synchronized void stopReverseTether() { // NetworkUtils.stopDhcp(iface); if (mDhcpThread != null && mDhcpThread.isAlive()) { mDhcpThread.interrupt(); try { mDhcpThread.join(); } catch (InterruptedException ie) { return; } } mLinkProperties.clear(); mNetworkInfo.setIsAvailable(false); mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null); Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo); msg.sendToTarget(); msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo); msg.sendToTarget(); }
private int validateIpConfigFields(LinkProperties linkProperties) { if (mIpAddressView == null) return 0; String ipAddr = mIpAddressView.getText().toString(); if (TextUtils.isEmpty(ipAddr)) return R.string.wifi_ip_settings_invalid_ip_address; InetAddress inetAddr = null; try { inetAddr = NetworkUtils.numericToInetAddress(ipAddr); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_ip_address; } int networkPrefixLength = -1; try { networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString()); if (networkPrefixLength < 0 || networkPrefixLength > 32) { return R.string.wifi_ip_settings_invalid_network_prefix_length; } linkProperties.addLinkAddress(new LinkAddress(inetAddr, networkPrefixLength)); } catch (NumberFormatException e) { // Set the hint as default after user types in ip address mNetworkPrefixLengthView.setText( mConfigUi.getContext().getString(R.string.wifi_network_prefix_length_hint)); } String gateway = mGatewayView.getText().toString(); if (TextUtils.isEmpty(gateway)) { try { // Extract a default gateway from IP address InetAddress netPart = NetworkUtils.getNetworkPart(inetAddr, networkPrefixLength); byte[] addr = netPart.getAddress(); addr[addr.length - 1] = 1; mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress()); } catch (RuntimeException ee) { } catch (java.net.UnknownHostException u) { } } else { InetAddress gatewayAddr = null; try { gatewayAddr = NetworkUtils.numericToInetAddress(gateway); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_gateway; } linkProperties.addRoute(new RouteInfo(gatewayAddr)); } String dns = mDns1View.getText().toString(); InetAddress dnsAddr = null; if (TextUtils.isEmpty(dns)) { // If everything else is valid, provide hint as a default option mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint)); } else { try { dnsAddr = NetworkUtils.numericToInetAddress(dns); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_dns; } linkProperties.addDns(dnsAddr); } if (mDns2View.length() > 0) { dns = mDns2View.getText().toString(); try { dnsAddr = NetworkUtils.numericToInetAddress(dns); } catch (IllegalArgumentException e) { return R.string.wifi_ip_settings_invalid_dns; } linkProperties.addDns(dnsAddr); } return 0; }