Android->Eth:fix can not save static ip
This commit is contained in:
@ -38,6 +38,15 @@ import java.io.PrintWriter;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import android.provider.Settings;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.net.StaticIpConfiguration;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Inet4Address;
|
||||
import android.net.IpConfiguration;
|
||||
import android.net.EthernetManager;
|
||||
import android.net.NetworkUtils;
|
||||
import android.net.LinkAddress;
|
||||
|
||||
/**
|
||||
* EthernetServiceImpl handles remote Ethernet operation requests by implementing
|
||||
* the IEthernetManager interface.
|
||||
@ -63,11 +72,87 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
|
||||
mEthernetConfigStore = new EthernetConfigStore();
|
||||
mIpConfiguration = mEthernetConfigStore.readIpAndProxyConfigurations();
|
||||
|
||||
checkUseStaticIp() ;
|
||||
|
||||
Log.i(TAG, "Read stored IP configuration: " + mIpConfiguration);
|
||||
|
||||
mTracker = new EthernetNetworkFactory(mListeners);
|
||||
}
|
||||
|
||||
private String mIpAddr;
|
||||
private String mGateway;
|
||||
private String mNetmask;
|
||||
private String mDns1;
|
||||
private String mDns2;
|
||||
private void checkUseStaticIp() {
|
||||
final ContentResolver cr = mContext.getContentResolver();
|
||||
try {
|
||||
if (Settings.System.getInt(cr, Settings.System.ETHERNET_USE_STATIC_IP) == 0) {
|
||||
Log.d(TAG, "checkUseStaticIp() : user set to use DHCP, about to Return.");
|
||||
return;
|
||||
}
|
||||
} catch (Settings.SettingNotFoundException e) {
|
||||
return;
|
||||
}
|
||||
|
||||
String addr = Settings.System.getString(cr, Settings.System.ETHERNET_STATIC_IP);
|
||||
if (addr != null) {
|
||||
mIpAddr = addr;
|
||||
} else {
|
||||
Log.d(TAG, "checkUseStaticIp() : No valid IP addr.");
|
||||
return;
|
||||
}
|
||||
addr = Settings.System.getString(cr, Settings.System.ETHERNET_STATIC_GATEWAY);
|
||||
if (addr != null) {
|
||||
mGateway = addr;
|
||||
} else {
|
||||
Log.d(TAG, "checkUseStaticIp() : No valid gateway.");
|
||||
return;
|
||||
}
|
||||
addr = Settings.System.getString(cr, Settings.System.ETHERNET_STATIC_NETMASK);
|
||||
if (addr != null) {
|
||||
mNetmask = addr;
|
||||
} else {
|
||||
Log.d(TAG, "checkUseStaticIp() : No valid netmask.");
|
||||
return;
|
||||
}
|
||||
addr = Settings.System.getString(cr, Settings.System.ETHERNET_STATIC_DNS1);
|
||||
if (addr != null) {
|
||||
mDns1 = addr;
|
||||
} else {
|
||||
Log.d(TAG, "checkUseStaticIp() : No valid dns1.");
|
||||
return;
|
||||
}
|
||||
addr = Settings.System.getString(cr, Settings.System.ETHERNET_STATIC_DNS2);
|
||||
if (addr != null) {
|
||||
mDns2 = addr;
|
||||
} else {
|
||||
Log.d(TAG, "checkUseStaticIp() : No valid dns2.");
|
||||
mDns2 = "0.0.0.0";
|
||||
// return;
|
||||
}
|
||||
try{
|
||||
// long now = SystemClock.uptimeMillis();
|
||||
StaticIpConfiguration sic = new StaticIpConfiguration();
|
||||
InetAddress mask = NetworkUtils.numericToInetAddress(mNetmask);
|
||||
int pref = 24 ;
|
||||
if(mask instanceof Inet4Address){
|
||||
pref = NetworkUtils.netmaskIntToPrefixLength(NetworkUtils.inetAddressToInt((Inet4Address)mask));
|
||||
}
|
||||
sic.ipAddress = new LinkAddress(InetAddress.getByName(mIpAddr),pref);
|
||||
sic.gateway = InetAddress.getByName(mGateway);
|
||||
sic.dnsServers.add(InetAddress.getByName(mDns1));
|
||||
sic.dnsServers.add(InetAddress.getByName(mDns2));
|
||||
|
||||
mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC
|
||||
,IpConfiguration.ProxySettings.UNASSIGNED,sic,null);
|
||||
}catch(Exception ex){
|
||||
return ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void enforceAccessPermission() {
|
||||
mContext.enforceCallingOrSelfPermission(
|
||||
android.Manifest.permission.ACCESS_NETWORK_STATE,
|
||||
|
||||
@ -76,6 +76,8 @@ import android.preference.ListPreference;
|
||||
|
||||
import com.android.settings.ethernet_static_ip_dialog;
|
||||
|
||||
import android.provider.Settings.System;
|
||||
|
||||
public class EthernetSettings extends SettingsPreferenceFragment
|
||||
implements DialogInterface.OnClickListener ,OnPreferenceChangeListener {
|
||||
private static final String TAG = "EthernetSettings";
|
||||
@ -275,6 +277,7 @@ public class EthernetSettings extends SettingsPreferenceFragment
|
||||
if(preference==mkeyEthMode) {
|
||||
String value=(String)newValue;
|
||||
if(value.equals("DHCP")){
|
||||
System.putInt(getContentResolver(), System.ETHERNET_USE_STATIC_IP, 0);
|
||||
mEthManager.setConfiguration(new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE,null,null));
|
||||
log("switch to dhcp");
|
||||
}else if(value.equals("StaticIP")){
|
||||
@ -499,6 +502,7 @@ public class EthernetSettings extends SettingsPreferenceFragment
|
||||
if(button==ethernet_static_ip_dialog.BUTTON_SUBMIT) {
|
||||
mDialog.saveIpSettingInfo(); //从Dialog获取静态数据
|
||||
if(setStaticIpConfiguration()) {
|
||||
System.putInt(getContentResolver(), System.ETHERNET_USE_STATIC_IP, 1);
|
||||
mEthManager.setConfiguration(mIpConfiguration);
|
||||
} else {
|
||||
Log.e(TAG, mIpConfiguration.toString());
|
||||
|
||||
Reference in New Issue
Block a user