android5.1 增加ethernet设置(DHCP与Static ip)

android5.0以上的系统自带了ethernet service,默认开机就会启动,默认ip获取方式是动态分配,这里记录下android5.1增加ethernet设置界面设置ip获取方式及开关.

首先是界面方面要修改Settings增加ethernet设置界面

修改文件packages/apps/Settings/res/xml/dashboard_categories.xml在蓝牙后面加上如下代码

  1. <!-- ethernet add by hclydao-->  
  2.         <dashboard-tile  
  3.             android:id="@+id/ethernet_settings"  
  4.             android:icon="@drawable/ic_settings_dock"  
  5.             android:fragment="com.android.settings.ethernet.EthernetSettings"  
  6.             android:title="@string/ethernet_settings" />  

其中的EthernetSettings后面进行说明,然后增加string修改文件packages/apps/Settings/res/values/strings.xml

增加如下内容

  1. <!-- Eth settings title add by hclydao-->  
  2.     <string name="ethernet_settings">Ethernet</string>  
  3.     <!-- Ethernet configuration dialog add by hcldyao-->  
  4.     <string name="eth_config_title">Configure Ethernet device</string>  
  5.     <string name="eth_setting">Ethernet</string>  
  6.     <string name="eth_dev_list">Ethernet Devices:</string>  
  7.     <string name="eth_con_type">Connection Type</string>  
  8.     <string name="eth_con_type_dhcp">DHCP</string>  
  9.     <string name="eth_con_type_manual">Static IP</string>  
  10.     <string name="eth_dns">DNS address</string>  
  11.     <string name="eth_gw">Gateway address</string>  
  12.     <string name="eth_ipaddr">IP address</string>  
  13.     <string name="eth_quick_toggle_title">Ethernet</string>  
  14.     <string name="eth_quick_toggle_summary">Turn on Ethernet</string>  
  15.     <string name="eth_conf_perf_title">Ethernet configuration</string>  
  16.     <string name="eth_conf_summary">Configure Ethernet devices</string>  
  17.     <string name="eth_mask">Netmask</string>  
  18.     <string name="eth_toggle_summary_off">Turn off Ethernet</string>  
  19.     <string name="eth_toggle_summary_on">Turn on Ethernet</string>  
  20.     <string name="eth_settings_error">Failed to set: Please enter the valid characters 0~255</string>  
  21.     <string name="eth_settings_empty">can\'t be empty</string>  
  22.     <!-- Label for the network prefix of the network [CHAR LIMIT=25]-->  
  23.     <string name="eth_network_prefix_length">Network prefix length</string>  
这是我修改完成后所要加的所有的string

接着修改文件packages/apps/Settings/src/com/Android/settings/SettingsActivity.Java增加

  1. import com.android.settings.ethernet.EthernetSettings;//add by hclydao  

然后在R.id.bluetooth_settings,后增加

  1. R.id.ethernet_settings,//add by hclydao  

在BluetoothSettings.class.getName(),后增加

  1. EthernetSettings.class.getName(),//add by hclydao  

接着修改文件packages/apps/Settings/src/com/android/settings/Settings.java

在public static class WirelessSettingsActivity extends SettingsActivity { /* empty */ }后增加

  1. public static class EthernetSettingsActivity extends SettingsActivity { /* empty */ } //add by hclydao  

这里面应该是声明与继承关系

接着修改
packages/apps/Settings/AndroidManifest.xml

  1. <activity android:name="Settings$EthernetSettingsActivity"  
  2.                android:label="@string/ethernet_settings"  
  3.                android:taskAffinity="">  
  4.            <intent-filter>  
  5.                <action android:name="android.intent.action.MAIN" />  
  6.                <action android:name="com.android.settings.ETHERNET_SETTINGS" />  
  7.                <action android:name="android.settings.ETHERNET_SETTINGS" />  
  8.                <category android:name="android.intent.category.DEFAULT" />  
  9.                <category android:name="android.intent.category.VOICE_LAUNCH" />  
  10.                <category android:name="com.android.settings.SHORTCUT" />  
  11.            </intent-filter>  
  12.            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"  
  13.                android:value="com.android.settings.ethernet.EthernetSettings" />  
  14.            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"  
  15.                android:resource="@id/ethernet_settings" />  
  16.            <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"  
  17.                android:value="true" />  
  18.        </activity>  
这三个文件按照wifi的代码改就行了。
然后增加点击进去后的布局文件,增加
packages/apps/Settings/res/xml/ethernet_settings.xml

内容如下

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- Copyright (C) 2010 The Android Open Source Project  
  3.   
  4.      Licensed under the Apache License, Version 2.0 (the "License");  
  5.      you may not use this file except in compliance with the License.  
  6.      You may obtain a copy of the License at  
  7.   
  8.           http://www.apache.org/licenses/LICENSE-2.0  
  9.   
  10.      Unless required by applicable law or agreed to in writing, software  
  11.      distributed under the License is distributed on an "AS IS" BASIS,  
  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.      See the License for the specific language governing permissions and  
  14.      limitations under the License.  
  15. -->  
  16.   
  17. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  18.         android:title="@string/ethernet_settings"  
  19.         xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">  
  20.         <Preference  
  21.             android:title="@string/eth_conf_perf_title"  
  22.             android:summary="@string/eth_conf_summary"  
  23.             android:key="ETHERNET_CONFIG"  
  24.             android:persistent="false" />  
  25. </PreferenceScreen>  
然后增加了packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java
这是我最终的代码,这里就不贴上来了,下面我会给下载地址
编译后效果应该是这个样子的:


点击进去后会有一个Dialog布局,需要增加文件packages/apps/Settings/res/layout/eth_configure.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content">  
  5.   
  6.     <LinearLayout  
  7.             android:layout_width="fill_parent"  
  8.             android:layout_height="wrap_content"  
  9.             android:padding="8dip"  
  10.             android:orientation="vertical">  
  11.   
  12.             <LinearLayout  
  13.                 android:id="@+id/table"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="fill_parent"  
  16.                 android:orientation="vertical">  
  17.             </LinearLayout>  
  18.   
  19.         <!-- Connection type -->  
  20.     <TextView android:id="@+id/eth_con_type"  
  21.                 style="?android:attr/textAppearanceSmall"  
  22.                 android:layout_width="fill_parent"  
  23.                 android:layout_height="wrap_content"  
  24.                 android:layout_marginTop="8dip"  
  25.                 android:text="@string/eth_con_type" />  
  26.         <RadioGroup android:id="@+id/con_type"  
  27.                 android:layout_width="fill_parent"  
  28.                 android:layout_height="wrap_content"  
  29.                 >  
  30.                 <RadioButton android:id="@+id/dhcp_radio"  
  31.             style="?android:attr/textAppearanceSmall"  
  32.             android:layout_width="fill_parent"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="@string/eth_con_type_dhcp"  
  35.                 ></RadioButton>  
  36.                 <RadioButton android:id="@+id/manual_radio"  
  37.             style="?android:attr/textAppearanceSmall"  
  38.             android:layout_width="fill_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:text="@string/eth_con_type_manual"  
  41.                 ></RadioButton>  
  42.         </RadioGroup>  
  43.   
  44.         <!-- IP address -->  
  45.         <LinearLayout android:id="@+id/enterprise_wrapper"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:padding="0dip"  
  49.             android:orientation="vertical">  
  50.                 <TextView android:id="@+id/ipaddr_text"  
  51.                         style="?android:attr/textAppearanceSmall"  
  52.                         android:layout_width="fill_parent"  
  53.                         android:layout_height="wrap_content"  
  54.                         android:layout_marginTop="8dip"  
  55.                         android:text="@string/eth_ipaddr" />  
  56.                 <EditText android:id="@+id/ipaddr_edit"  
  57.                         android:layout_width="fill_parent"  
  58.                         android:layout_height="wrap_content"  
  59.                         android:layout_marginTop="2dip"  
  60.                         android:singleLine="true" />  
  61. <!--  
  62.                 <TextView android:id="@+id/netmask_text"  
  63.                         style="?android:attr/textAppearanceSmall"  
  64.                         android:layout_width="fill_parent"  
  65.                         android:layout_height="wrap_content"  
  66.                         android:layout_marginTop="8dip"  
  67.                         android:text="@string/eth_mask" />  
  68.                 <EditText android:id="@+id/netmask_edit"  
  69.                         android:layout_width="fill_parent"  
  70.                         android:layout_height="wrap_content"  
  71.                         android:layout_marginTop="2dip"  
  72.                         android:singleLine="true" />  
  73. -->  
  74.                 <TextView android:id="@+id/prefix_text"  
  75.                         style="?android:attr/textAppearanceSmall"  
  76.                         android:layout_width="fill_parent"  
  77.                         android:layout_height="wrap_content"  
  78.                         android:layout_marginTop="8dip"  
  79.                         android:text="@string/eth_network_prefix_length" />  
  80.                 <EditText android:id="@+id/prefix_edit"  
  81.                         android:layout_width="fill_parent"  
  82.                         android:layout_height="wrap_content"  
  83.                         android:layout_marginTop="2dip"  
  84.                         android:singleLine="true" />  
  85.   
  86.                 <TextView android:id="@+id/dns_text"  
  87.                         style="?android:attr/textAppearanceSmall"  
  88.                         android:layout_width="fill_parent"  
  89.                         android:layout_height="wrap_content"  
  90.                         android:layout_marginTop="8dip"  
  91.                         android:text="@string/eth_dns" />  
  92.                 <EditText android:id="@+id/eth_dns_edit"  
  93.                         android:layout_width="fill_parent"  
  94.                         android:layout_height="wrap_content"  
  95.                         android:layout_marginTop="2dip"  
  96.                         android:singleLine="true" />  
  97.                 <TextView android:id="@+id/gw_text"  
  98.                         style="?android:attr/textAppearanceSmall"  
  99.                         android:layout_width="fill_parent"  
  100.                         android:layout_height="wrap_content"  
  101.                         android:layout_marginTop="8dip"  
  102.                         android:text="@string/eth_gw" />  
  103.                 <EditText android:id="@+id/eth_gw_edit"  
  104.                         android:layout_width="fill_parent"  
  105.                         android:layout_height="wrap_content"  
  106.                         android:layout_marginTop="2dip"  
  107.                         android:singleLine="true" />  
  108.         </LinearLayout>  
  109.   
  110.     </LinearLayout>  
  111.   
  112. </ScrollView>  

里面包括了dhcp与static ip的选择,以及static ip的设置

然后增加Settings/src/com/android/settings/ethernet/EthernetDialog.java
以及packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java
这些是具体的实现,最后我会给下载地址

最后出来的效果应该是这样的:

配制界面


增加这些修改和文件后,基本上就可以进行动态设置了。但是设置的关于ethernet的开关是没有作用的,所以这里增加开关的控制
修改文件frameworks/base/core/java/android/provider/Settings.java
在Settings数据库中增加一个ethernet的控制

  1. public static final String ETHERNET_ON = "ethernet_on";//add by hclydao  
增加这个以后需要更新api才能编译过,增加这个后在ethernet-service和Settings中ethernet中进行读写操作
修改frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetServiceImpl.java文件

在mHandler = new Handler(handlerThread.getLooper());后增加如下代码

  1. int enable = Settings.Global.getInt(mContext.getContentResolver(),Settings.Global.ETHERNET_ON,0);//add by hclydao  
  2. if(enable != EthernetManager.ETH_STATE_ENABLED) {  
  3.     Log.i(TAG, "Ethernet is not enable");  
  4.     return;  
  5. }  

如果没有打开就直接返回,不启动Service后继的操作,然后增加两个接口

  1. class TstartThread extends Thread {  
  2.     public void run() {  
  3.         Looper.prepare();  
  4.         mTracker.start(mContext, mHandler);  
  5.         mStarted.set(true);  
  6.         Looper.loop();  
  7.     }  
  8. }  
  9.   
  10. public void Trackstart() { //add by hclydao  
  11.     new TstartThread().start();  
  12. }  
  13.   
  14. public void Trackstop() {  
  15.     Log.i(TAG, "Stop Ethernet service");  
  16.     Thread tstopthread = new Thread(new Runnable() {  
  17.         public void run() {  
  18.             Looper.prepare();  
  19.             mTracker.stop();  
  20.             mStarted.set(false);  
  21.             Looper.loop();  
  22.         }  
  23.     });  
  24.     tstopthread.start();  
  25. }  

同时要修改frameworks/base/core/java/android/net/EthernetManager.java文件增加

  1. public static final int ETH_STATE_UNKNOWN = 0;  
  2. public static final int ETH_STATE_DISABLED = 1;  
  3. public static final int ETH_STATE_ENABLED = 2;  
  4.   
  5. public void start() {  
  6.     try {  
  7.         mService.Trackstart();  
  8.     } catch (NullPointerException | RemoteException e) {  
  9.     }  
  10. }  
  11.   
  12. public void stop() {  
  13.     try {  
  14.         mService.Trackstop();  
  15.     } catch (NullPointerException | RemoteException e) {  
  16.     }  
  17. }  


同时修改frameworks/base/core/java/android/net/IEthernetManager.aidl

增加

  1. void Trackstart();//add by hclydao  
  2. void Trackstop();  
提供给设置进行状态控制,这里基本功能就实现了。
跟踪测试时发现静态ip设置的时候有时候不成功,修改文件frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java

增加

  1. private Handler mHandler;  
然后在
mContext = context;

后增加

  1. mHandler = target;//add by hclydao  

在if (!setStaticIpAddress(config.getStaticIpConfiguration())) {后增加

  1. //if error then stop and restart add by hclydao  
  2. if((mContext != null) && (mHandler != null)) {  
  3.     Log.d(TAG, "Setting static ip failed now restart");  
  4.     stop();  
  5.     start(mContext,mHandler);  
  6. }  
如果设置失败,stop后重新start

然后在if (mNMService.getInterfaceConfig(iface).hasFlag("running")) {前面增加

  1. if(!iface.equals("eth0"))//add by hclydao make sure the interface is eth0  
  2.     continue;  

这里只设置了一个设备名,为保存是eth0所以加上这句,防止意外.

最后来增加Systemui statusbar中的状态提示,这里只增加了两种状态,一种是连接成功,一种是连接不成功,简单点来
修改frameworks/base/packages/SystemUI/res/layout/signal_cluster_view.xml

     <View
         android:id="@+id/wifi_signal_spacer"
         android:layout_width="4dp"
前增加

  1. </FrameLayout>  
  2.     <!--add by hclyado for ethernet-->  
  3.     <FrameLayout  
  4.         android:id="@+id/ethernet_combo"  
  5.         android:layout_height="wrap_content"  
  6.         android:layout_width="wrap_content"  
  7.         android:layout_marginRight="-6dp"  
  8.         >  
  9.         <ImageView  
  10.             android:id="@+id/ethernet_state"  
  11.             android:layout_height="wrap_content"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_alignParentRight="true"  
  14.             android:layout_centerVertical="true"  
  15.             android:scaleType="center"  
  16.             />  
  17.     </FrameLayout>  

修改文件frameworks/base/packages/SystemUI/res/values/strings.xml
增加

  1. <!-- Content description of the Ethernet connected icon for accessibility (not shown on the screen). [CHAR LIMIT=NONE] add by hclydao-->  
  2. <string name="accessibility_ethernet_connected">Ethernet connected.</string>  
  3. <string name="accessibility_ethernet_disconnected">Ethernet disconnected.</string>  
  4. <string name="accessibility_ethernet_connecting">Ethernet connecting.</string>  
修改文件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java

增加

  1. import android.net.EthernetManager;  

在mWifiSignalController.notifyListeners();前面加上

  1. cluster.setEthernetIndicators(false,R.drawable.ethernet_disconnected,R.string.accessibility_ethernet_disconnected);  
在pushConnectivityToSignals函数中的
         mWifiSignalController.setInetCondition(
                 mValidatedTransports.get(mWifiSignalController.getTransportType()) ? 1 : 0);

后面增加

  1. //add by hclydao  
  2.         int length = mSignalClusters.size();  
  3.         int ethicon = R.drawable.ethernet_connecting;  
  4.         int ethacc = R.string.accessibility_ethernet_connecting;  
  5.         if(mValidatedTransports.get(TRANSPORT_ETHERNET)) {  
  6.             ethicon = R.drawable.ethernet_connected;  
  7.             ethacc = R.string.accessibility_ethernet_connected;  
  8.         }  
  9.         for (int i = 0; i < length; i++) {  
  10.             mSignalClusters.get(i).setEthernetIndicators(mEthernetConnected, ethicon,ethacc);  
  11.         }  
  12. //end add  

void setIsAirplaneMode(boolean is, int airplaneIcon, int contentDescription);

后增加

  1. public void setEthernetIndicators(boolean visible, int stateIcon, int contentDescription);  
修改文件frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java

增加

  1.    private boolean mEthernetVisible = false;//add by hclydao  
  2.    private int mEthernetStateId = 0;  
  3. private int mEthernetDescription;  
  4.    ViewGroup mWifiGroup,mEthernetGroup;//modify by hclydao  
  5.    ImageView mVpn, mWifi, mAirplane, mNoSims,mEthernet;//modify by hclydao  

         mWifiAirplaneSpacer =         findViewById(R.id.wifi_airplane_spacer);
         mWifiSignalSpacer =           findViewById(R.id.wifi_signal_spacer);
         mMobileSignalGroup = (LinearLayout) findViewById(R.id.mobile_signal_group);

后增加

  1. mEthernetGroup  = (ViewGroup) findViewById(R.id.ethernet_combo);//add by hclydao  
  2. mEthernet       = (ImageView) findViewById(R.id.ethernet_state);  

         mWifi           = null;
         mAirplane       = null;

后增加

  1. mEthernetGroup  = null;//add by hclydao  
  2. mEthernet       = null;  

增加函数

  1. //add by hclydao  
  2. @Override  
  3.    public void setEthernetIndicators(boolean visible, int stateIcon, int contentDescription) {  
  4.        mEthernetVisible = visible;  
  5.        mEthernetStateId = stateIcon;  
  6.        mEthernetDescription = contentDescription;  
  7.   
  8.        apply();  
  9.    }  
在apply函数中
             mWifiSignalSpacer.setVisibility(View.GONE);
         }

后增加

  1. if (mEthernetVisible && !mWifiVisible) {//add by hclydao  
  2.     mEthernetGroup.setVisibility(View.VISIBLE);  
  3.     mEthernet.setImageResource(mEthernetStateId);  
  4.     mEthernetGroup.setContentDescription(mContext.getString(mEthernetDescription));  
  5. else {  
  6.     mEthernetGroup.setVisibility(View.GONE);  
  7. }  

在boolean anythingVisible = mNoSimsVisible || mWifiVisible || mIsAirplaneMode上增加

  1. || mEthernetVisible  

最后效果图如下:




============================================
作者:hclydao
http://blog.csdn.net/hclydao
版权没有,但是转载请保留此段声明

============================================

参考文章:

http://blog.csdn.net/moyu123456789/article/details/50002099

http://my.oschina.net/hiliusl/blog/174973?fromerr=IPmtDOdk


相关源码下载地址:http://download.csdn.net/detail/hclydao/9472077

下载文件为zip文件,上传后自动在最后加了_ 下载后将_删除



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值