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

转自:http://blog.csdn.net/hclydao/article/details/50972932

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

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

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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.   
  2.         
  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

增加如下内容


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.   
  2.     "ethernet_settings">Ethernet  
  3.       
  4.     "eth_config_title">Configure Ethernet device  
  5.     "eth_setting">Ethernet  
  6.     "eth_dev_list">Ethernet Devices:  
  7.     "eth_con_type">Connection Type  
  8.     "eth_con_type_dhcp">DHCP  
  9.     "eth_con_type_manual">Static IP  
  10.     "eth_dns">DNS address  
  11.     "eth_gw">Gateway address  
  12.     "eth_ipaddr">IP address  
  13.     "eth_quick_toggle_title">Ethernet  
  14.     "eth_quick_toggle_summary">Turn on Ethernet  
  15.     "eth_conf_perf_title">Ethernet configuration  
  16.     "eth_conf_summary">Configure Ethernet devices  
  17.     "eth_mask">Netmask  
  18.     "eth_toggle_summary_off">Turn off Ethernet  
  19.     "eth_toggle_summary_on">Turn on Ethernet  
  20.     "eth_settings_error">Failed to set: Please enter the valid characters 0~255  
  21.     "eth_settings_empty">can\'t be empty  
  22.       
  23.     "eth_network_prefix_length">Network prefix length  
这是我修改完成后所要加的所有的string


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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import com.android.settings.ethernet.EthernetSettings;//add by hclydao  


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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. R.id.ethernet_settings,//add by hclydao  


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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. EthernetSettings.class.getName(),//add by hclydao  


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

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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static class EthernetSettingsActivity extends SettingsActivity { /* empty */ } //add by hclydao  


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

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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. "Settings$EthernetSettingsActivity"  
  2.                android:label="@string/ethernet_settings"  
  3.                android:taskAffinity="">  
  4.              
  5.                "android.intent.action.MAIN" />  
  6.                "com.android.settings.ETHERNET_SETTINGS" />  
  7.                "android.settings.ETHERNET_SETTINGS" />  
  8.                "android.intent.category.DEFAULT" />  
  9.                "android.intent.category.VOICE_LAUNCH" />  
  10.                "com.android.settings.SHORTCUT" />  
  11.              
  12.            "com.android.settings.FRAGMENT_CLASS"  
  13.                android:value="com.android.settings.ethernet.EthernetSettings" />  
  14.            "com.android.settings.TOP_LEVEL_HEADER_ID"  
  15.                android:resource="@id/ethernet_settings" />  
  16.            "com.android.settings.PRIMARY_PROFILE_CONTROLLED"  
  17.                android:value="true" />  
  18.          


这三个文件按照wifi的代码改就行了。
然后增加点击进去后的布局文件,增加
packages/apps/Settings/res/xml/ethernet_settings.xml

内容如下


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. "1.0" encoding="utf-8"?>  
  2.   
  3.   
  4. "http://schemas.android.com/apk/res/android"  
  5.         android:title="@string/ethernet_settings"  
  6.         xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">  
  7.         
  8.             android:title="@string/eth_conf_perf_title"  
  9.             android:summary="@string/eth_conf_summary"  
  10.             android:key="ETHERNET_CONFIG"  
  11.             android:persistent="false" />  
  12.   
然后增加了packages/apps/Settings/src/com/android/settings/ethernet/EthernetSettings.java


这是我最终的代码,这里就不贴上来了,下面我会给下载地址
编译后效果应该是这个样子的:


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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. "1.0" encoding="utf-8"?>  
  2. "http://schemas.android.com/apk/res/android"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content">  
  5.   
  6.     
  7.             android:layout_width="fill_parent"  
  8.             android:layout_height="wrap_content"  
  9.             android:padding="8dip"  
  10.             android:orientation="vertical">  
  11.   
  12.             
  13.                 android:id="@+id/table"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="fill_parent"  
  16.                 android:orientation="vertical">  
  17.               
  18.   
  19.           
  20.     "@+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.         "@+id/con_type"  
  27.                 android:layout_width="fill_parent"  
  28.                 android:layout_height="wrap_content"  
  29.                 >  
  30.                 "@+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.                 >  
  36.                 "@+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.                 >  
  42.           
  43.   
  44.           
  45.         "@+id/enterprise_wrapper"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:padding="0dip"  
  49.             android:orientation="vertical">  
  50.                 "@+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.                 "@+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.                 "@+id/prefix_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_network_prefix_length" />  
  68.                 "@+id/prefix_edit"  
  69.                         android:layout_width="fill_parent"  
  70.                         android:layout_height="wrap_content"  
  71.                         android:layout_marginTop="2dip"  
  72.                         android:singleLine="true" />  
  73.   
  74.                 "@+id/dns_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_dns" />  
  80.                 "@+id/eth_dns_edit"  
  81.                         android:layout_width="fill_parent"  
  82.                         android:layout_height="wrap_content"  
  83.                         android:layout_marginTop="2dip"  
  84.                         android:singleLine="true" />  
  85.                 "@+id/gw_text"  
  86.                         style="?android:attr/textAppearanceSmall"  
  87.                         android:layout_width="fill_parent"  
  88.                         android:layout_height="wrap_content"  
  89.                         android:layout_marginTop="8dip"  
  90.                         android:text="@string/eth_gw" />  
  91.                 "@+id/eth_gw_edit"  
  92.                         android:layout_width="fill_parent"  
  93.                         android:layout_height="wrap_content"  
  94.                         android:layout_marginTop="2dip"  
  95.                         android:singleLine="true" />  
  96.           
  97.   
  98.       
  99.   
  100.   


里面包括了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的控制


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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());后增加如下代码


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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后继的操作,然后增加两个接口


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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文件增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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

增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void Trackstart();//add by hclydao  
  2. void Trackstop();  



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

增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. private Handler mHandler;  
然后在


mContext = context;

后增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mHandler = target;//add by hclydao  


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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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")) {前面增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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

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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.   
  2.       
  3.     
  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.         
  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.       


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


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.   
  2. "accessibility_ethernet_connected">Ethernet connected.  
  3. "accessibility_ethernet_disconnected">Ethernet disconnected.  
  4. "accessibility_ethernet_connecting">Ethernet connecting.  


修改文件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java

增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.net.EthernetManager;  


在mWifiSignalController.notifyListeners();前面加上


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. cluster.setEthernetIndicators(false,R.drawable.ethernet_disconnected,R.string.accessibility_ethernet_disconnected);  


在pushConnectivityToSignals函数中的
         mWifiSignalController.setInetCondition(
                 mValidatedTransports.get(mWifiSignalController.getTransportType()) ? 1 : 0);

后面增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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);

后增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void setEthernetIndicators(boolean visible, int stateIcon, int contentDescription);  


修改文件frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java

增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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);

后增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mEthernetGroup  = (ViewGroup) findViewById(R.id.ethernet_combo);//add by hclydao  
  2. mEthernet       = (ImageView) findViewById(R.id.ethernet_state);  


         mWifi           = null;
         mAirplane       = null;

后增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. mEthernetGroup  = null;//add by hclydao  
  2. mEthernet       = null;  


增加函数


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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);
         }

后增加


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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上增加

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值