一.这个类提供了一个用于存储和管理以太网网络配置的API
frameworksopt etethernetjavacomandroidserverethernetEthernetConfigStore.java
package com.android.server.ethernet; import android.net.IpConfiguration; import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.ProxySettings; import android.os.Environment; import android.util.Log; import android.util.SparseArray; import com.android.server.net.IpConfigStore; /** * This class provides an API to store and manage Ethernet network configuration. */ public class EthernetConfigStore extends IpConfigStore { private static final String TAG = "EthernetConfigStore"; private static final String ipConfigFile = Environment.getDataDirectory() + "/misc/ethernet/ipconfig.txt"; public EthernetConfigStore() { } public IpConfiguration readIpAndProxyConfigurations() { SparseArray<IpConfiguration> networks = readIpAndProxyConfigurations(ipConfigFile); if (networks.size() == 0) { Log.w(TAG, "No Ethernet configuration found. Using default."); return new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null); } if (networks.size() > 1) { // Currently we only support a single Ethernet interface. Log.w(TAG, "Multiple Ethernet configurations detected. Only reading first one."); } return networks.valueAt(0); } public void writeIpAndProxyConfigurations(IpConfiguration config) { SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>(); networks.put(0, config); writeIpAndProxyConfigurations(ipConfigFile, networks); } }
二.出厂开机第一次默认
2.1.packagesappsProvisionAndroidManifest.xml
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
2.2.packagesappsProvisionsrccomandroidprovisionDefaultActivity.java
package com.android.provision; import android.app.Activity; import android.content.ComponentName; import android.content.pm.PackageManager; import android.os.Bundle; import android.provider.Settings; import android.content.Context; import android.util.Log; import android.net.EthernetManager; import android.net.StaticIpConfiguration; import android.net.IpConfiguration; import android.net.IpConfiguration.IpAssignment; import android.net.IpConfiguration.ProxySettings; import android.net.NetworkUtils; import android.net.LinkAddress; import java.net.InetAddress; import java.net.Inet4Address; import java.util.regex.Pattern; import android.os.SystemProperties; import java.lang.reflect.Method; /** * Application that sets the provisioned bit, like SetupWizard does. */ public class DefaultActivity extends Activity { EthernetManager mEthManager; StaticIpConfiguration mStaticIpConfiguration; IpConfiguration mIpConfiguration; String ipAddr = "1.1.1.1"; String netMask = "255.255.255.0"; String gateway = "1.1.1.1"; String dns1 = "0.0.0.0"; String dns2 = "0.0.0.0"; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); // Add a persistent setting to allow other apps to know the device has been provisioned. Settings.Global.putInt(getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1); Settings.Secure.putInt(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, 1); mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE); mStaticIpConfiguration = new StaticIpConfiguration(); Settings.System.putString(getContentResolver(), "ethernet_static_ip", ipAddr); Settings.System.putString(getContentResolver(), "ethernet_static_gateway", gateway); Settings.System.putString(getContentResolver(), "ethernet_static_netmask", netMask); Settings.System.putString(getContentResolver(), "ethernet_static_dns1", dns1); Settings.System.putString(getContentResolver(), "ethernet_static_dns2", dns2); /* * get ip address, netmask,dns ,gw etc. */ Inet4Address inetAddr = getIPv4Address(ipAddr); int prefixLength = maskStr2InetMask(netMask); InetAddress gatewayAddr = getIPv4Address(gateway); InetAddress dnsAddr = getIPv4Address(dns1); String dnsStr2 = dns2; mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength); mStaticIpConfiguration.gateway = gatewayAddr; mStaticIpConfiguration.dnsServers.add(dnsAddr); mStaticIpConfiguration.dnsServers.add(getIPv4Address(dnsStr2)); mIpConfiguration = new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE, mStaticIpConfiguration, null); mEthManager.setConfiguration(mIpConfiguration); setProperty("persist.sys.first_xhboot","true"); // remove this activity from the package manager. PackageManager pm = getPackageManager(); ComponentName name = new ComponentName(this, DefaultActivity.class); pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); // terminate the activity. finish(); } private Inet4Address getIPv4Address(String text) { try { return (Inet4Address) NetworkUtils.numericToInetAddress(text); } catch (IllegalArgumentException|ClassCastException e) { return null; } } private int maskStr2InetMask(String maskStr) { StringBuffer sb; String str; int inetmask = 0; int count = 0; /* * check the subMask format */ Pattern pattern = Pattern .compile("(^((\d|[01]?\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[01]?\d\d|2[0-4]\d|25[0-5])$)|^(\d|[1-2]\d|3[0-2])$"); if (pattern.matcher(maskStr).matches() == false) { Log.e("gatsby", "subMask is error"); return 0; } String[] ipSegment = maskStr.split("\."); for (int n = 0; n < ipSegment.length; n++) { sb = new StringBuffer(Integer.toBinaryString(Integer .parseInt(ipSegment[n]))); str = sb.reverse().toString(); count = 0; for (int i = 0; i < str.length(); i++) { i = str.indexOf("1", i); if (i == -1) break; count++; } inetmask += count; } return inetmask; } public void setProperty(String key, String value) { try { Class<?> c = Class.forName("android.os.SystemProperties"); Method set = c.getMethod("set", String.class, String.class); set.invoke(c, key, value); } catch (Exception e) { e.printStackTrace(); } } }