packages\apps\Provision\AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.provision" android:sharedUserId="android.uid.system"> <original-package android:name="com.android.provision" /> <!-- For miscellaneous settings --> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <application> <activity android:name="DefaultActivity" android:excludeFromRecents="true"> <intent-filter android:priority="1"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.SETUP_WIZARD" /> </intent-filter> </activity> </application> </manifest>
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(); } } }