RK Android7.1 出厂第一次开机 以太网模式默认静态

修改Android5.1源码,配置以太网设置默认为静态ip

一.这个类提供了一个用于存储和管理以太网网络配置的API

frameworks\opt\net\ethernet\java\com\android\server\ethernet\EthernetConfigStore.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.packages\apps\Provision\AndroidManifest.xml

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

 

2.2.packages\apps\Provision\src\com\android\provision\DefaultActivity.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();
       }
   }	
		
}

 三.第二种方法 太low了

diff --git a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java
index 7729d9c..7de3bc7 100755
--- a/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java
+++ b/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java
@@ -43,6 +43,7 @@ import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.concurrent.atomic.AtomicBoolean;
 import android.provider.Settings;
+import java.util.regex.Pattern;
 
 /**
  * EthernetServiceImpl handles remote Ethernet operation requests by implementing
 */  @@ -72,20 +73,26 @@ public class EthernetServiceImpl extends IEthernetManager.Stub { mStaticIpConfiguration = new StaticIpConfiguration(); if (SystemProperties.get("persist.sys.static.enable","1").equals("1")){ String mIpAddress = "192.168.5.66"; - int mNetmask = 24; + String mNetmask = "255.255.255.0"; String mGateway = "192.168.5.1"; - String mDns1 = "202.96.128.86"; - String mDns2 = "202.96.128.166"; + String mDns1 = "192.168.5.1"; + //String mDns2 = "202.96.128.166"; Inet4Address inetAddr = getIPv4Address(mIpAddress); - int prefixLength = mNetmask; + int prefixLength = maskStr2InetMask(mNetmask); InetAddress gatewayAddr =getIPv4Address(mGateway); InetAddress dnsAddr = getIPv4Address(mDns1); + Settings.System.putString(mContext.getContentResolver(), "ethernet_static_ip", mIpAddress); + Settings.System.putString(mContext.getContentResolver(), "ethernet_static_gateway", mGateway); + Settings.System.putString(mContext.getContentResolver(), "ethernet_static_netmask", mNetmask); + Settings.System.putString(mContext.getContentResolver(), "ethernet_static_dns1", mDns1); + //Settings.System.putString(mContext.getContentResolver(), "ethernet_static_dns2", mDns2); + mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength); mStaticIpConfiguration.gateway=gatewayAddr; mStaticIpConfiguration.dnsServers.add(dnsAddr); - mStaticIpConfiguration.dnsServers.add(getIPv4Address(mDns2)); + //mStaticIpConfiguration.dnsServers.add(getIPv4Address(mDns2)); mIpConfiguration = new IpConfiguration(IpAssignment.STATIC, ProxySettings.NONE,mStaticIpConfiguration,null); mEthernetConfigStore.writeIpAndProxyConfigurations(mIpConfiguration); SystemProperties.set("persist.sys.static.enable","0"); @@ -98,6 +105,36 @@ public class EthernetServiceImpl extends IEthernetManager.Stub { mTracker = new EthernetNetworkFactory(mListeners); } + 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(TAG,"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; + } + private Inet4Address getIPv4Address(String text) { try { return (Inet4Address) NetworkUtils.numericToInetAddress(text);

  

posted @ 2021-08-24 18:29  CrushGirl  阅读(1178)  评论(0编辑  收藏  举报