android4.4增加ethernet 文章 的补充内容

原文网址:http://blog.csdn.net/jingxia2008/article/details/26591005#

增加ethernet主要是三部分:1、ethernet service; 2、ethernet Settings  3、Systemui ethernet通知

以下提到的源文件将会在文章最后提供下载地址

拷贝frameworks/base/ethernet到frameworks/base下

修改frameworks/base/Android.mk


    wifi/Java/android/net/wifi/p2p/IWifiP2pManager.aidl \
下加上如下代码

  1. ethernet/java/android/net/ethernet/IEthernetManager.aidl \  
修改build/core/pathmap.mk在
FRAMEWORKS_BASE_SUBDIRS中加上ethernet

拷贝EthernetService.java到frameworks/base/services/java/com/android/server/下

修改frameworks/base/core/java/android/content/Context.java

    public static final String WIFI_P2P_SERVICE = "wifip2p";
下加上如下内容

  1. /** 
  2.  * Use with {@link #getSystemService} to retrieve a {@link 
  3.  * android.net.ethernet.EthernetManager} for handling management of 
  4.  * Ethernet access. 
  5.  * 
  6.  * @see #getSystemService 
  7.  * @see android.net.ethernet.EthernetManager 
  8.  */  
  9. public static final String ETH_SERVICE = "ethernet";//add by hclydao  
修改frameworks/base/core/java/android/app/ContextImpl.java

import android.net.wifi.p2p.IWifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager;
下增加如下内容
  1. import android.net.ethernet.IEthernetManager;  
  2. import android.net.ethernet.EthernetManager;  

在    

registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
                    IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
                    return new WifiP2pManager(service);
                }});

下增加如下内容


  1. registerService(ETH_SERVICE, new ServiceFetcher() {  
  2.         public Object createService(ContextImpl ctx) {  
  3.             IBinder b = ServiceManager.getService(ETH_SERVICE);  
  4.             IEthernetManager service = IEthernetManager.Stub.asInterface(b);  
  5.             return new EthernetManager(service, ctx.mMainThread.getHandler());  
  6.         }}); //add by hclydao  
修改frameworks/base/services/java/com/android/server/ConnectivityService.java
增加
  1. import android.net.ethernet.EthernetManager;//add by hclydao  
  2. import android.net.ethernet.EthernetStateTracker;  
注释掉
//import android.net.EthernetDataTracker;

            try {
                tracker = netFactory.createTracker(targetNetworkType, config);
                mNetTrackers[targetNetworkType] = tracker;
            } catch (IllegalArgumentException e) {
                Slog.e(TAG, "Problem creating " + getNetworkTypeName(targetNetworkType)
                        + " tracker: " + e);
                continue;
            }

下加上如下代码

            if(mNetConfigs[targetNetworkType].radio == ConnectivityManager.TYPE_ETHERNET) { //add by hclydao
                EthernetService ethernet = new EthernetService(context, (EthernetStateTracker)mNetTrackers[targetNetworkType]);
                ServiceManager.addService(Context.ETH_SERVICE, ethernet);
                mNetTrackers[targetNetworkType].startMonitoring(context, mTrackerHandler);
            }

注释掉
                    //return EthernetDataTracker.getInstance();

增加如下代码

  1. return new EthernetStateTracker(targetNetworkType, config.name);//add by hclydao  
拷贝android_net_ethernet.cpp到frameworks/base/core/jni目录下
修改frameworks/base/core/jni下的Android.mk

    android_net_wifi_WifiNative.cpp \
下加上如下代码


  1. android_net_ethernet.cpp \  

修改frameworks/base/core/jni/AndroidRuntime.cpp

extern int register_android_net_wifi_WifiNative(JNIEnv* env);下
加上

extern int register_android_net_ethernet_EthernetManager(JNIEnv* env);//add by hclydao


REG_JNI(register_android_net_wifi_WifiNative),
下加上如下代码

  1. REG_JNI(register_android_net_ethernet_EthernetManager),  
在framework/base/core/java/android/provider/Settings.java中
       public static final String WIFI_ON = "wifi_on";
下加上如下代码
  1. public static final String ETH_ON      = "eth_on";  
  2. public static final String ETH_MODE    = "eth_mode";  
  3. public static final String ETH_IP      = "eth_ip";  
  4. public static final String ETH_MASK    = "eth_mask";  
  5. public static final String ETH_DNS     = "eth_dns";  
  6. public static final String ETH_ROUTE   = "eth_route";  
  7. public static final String ETH_CONF    = "eth_conf";  
  8. public static final String ETH_IFNAME  = "eth_ifname";  

Settings部分修改

拷贝eth_configure.xml到Settings/res/layout/
拷贝ic_setttings_ethernet.png到Settings/res/drawable-hdpi与drawable-mdpi
拷贝ethernet_settings.xml到Settings/res/xml下
修改xml下settings_headers.xml在wifi下增加如下内容

  1. <!-- Ethernet -->  
  2. lt;header  
  3.     android:id="@+id/ethernet_settings"  
  4.     android:title="@string/eth_setting"  
  5.     android:icon="@drawable/ic_settings_ethernet"  
  6.     android:fragment="com.android.settings.ethernet.EthernetSettings"/>  
修改values/strings.xml增加如下内容
  1. <!-- Ethernet configuration dialog -->  
  2. <string name="eth_config_title">Configure Ethernet device</string>  
  3. <string name="eth_setting">Ethernet</string>  
  4. <string name="eth_dev_list">Ethernet Devices:</string>  
  5. <string name="eth_con_type">Connection Type</string>  
  6. <string name="eth_con_type_dhcp">DHCP</string>  
  7. <string name="eth_con_type_manual">Static IP</string>  
  8. <string name="eth_dns">DNS address</string>  
  9. <string name="eth_gw">Gateway address</string>  
  10. <string name="eth_ipaddr">IP address</string>  
  11. <string name="eth_quick_toggle_title">Ethernet</string>  
  12. <string name="eth_quick_toggle_summary">Turn on Ethernet</string>  
  13. <string name="eth_conf_perf_title">Ethernet configuration</string>  
  14. <string name="eth_conf_summary">Configure Ethernet devices</string>  
  15. <string name="eth_mask">Netmask</string>  
  16. <string name="eth_toggle_summary_off">Turn off Ethernet</string>  
  17. <string name="eth_toggle_summary_on">Turn on Ethernet</string>  
  18. <string name="eth_settings_error">Failed to set: Please enter the valid characters 0~255</string>  
拷贝Settings/src/ethernet到Settings/src文件夹下

修改Settings/AndroidManifest.xml

        <!-- Wireless Controls -->

        <activity android:name="Settings$WirelessSettingsActivity"
                android:taskAffinity="com.android.settings"
                android:label="@string/wireless_networks_settings_title"
                android:parentActivityName="Settings">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.settings.WIRELESS_SETTINGS" />
                <action android:name="android.settings.AIRPLANE_MODE_SETTINGS" />
                <action android:name="android.settings.NFC_SETTINGS" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.VOICE_LAUNCH" />
            </intent-filter>
            <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                android:value="com.android.settings.WirelessSettings" />
            <meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
                android:resource="@id/wireless_settings" />
        </activity>

下增加如下代码


  1.  <!-- Ethernet controls add by hclydao-->  
  2.   
  3. <activity android:name="Settings$EthernetSettingsActivity"  
  4.         android:label="@string/eth_setting">  
  5.     <intent-filter>  
  6.         <action android:name="android.intent.action.MAIN" />  
  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. </activity>  
修改Settings/src/Utils.java

    public static String getWifiIpAddresses(Context context) {
        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI);
        return formatIpAddresses(prop);
    }
下增加如下代码

  1. public static String getEtherProperties(Context context) {  
  2.     ConnectivityManager cm = (ConnectivityManager)  
  3.             context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  4.     LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_ETHERNET);  
  5.     return prop.toString();  
  6. }  
修改Settings/src/Settings.java
加上
  1. import com.android.settings.ethernet.EthernetSettings;  

            R.id.wifi_settings,
下加上
  1. R.id.ethernet_settings,  

        WifiSettings.class.getName(),
下加上
  1. EthernetSettings.class.getName(),  

SystemUI修改

拷贝systemui下所有.png文件到frameworks/base/packages/SystemUI/res/drawable下
修改frameworks/base/packages/SystemUI/res/values/strings.xml

    <string name="accessibility_no_sim">No SIM.</string>
下增加如下

  1. <!-- Content description of the Ethernet connected icon for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->  
  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/res/layout/signal_cluster_view.xml

    <View
        android:layout_height="6dp"
        android:layout_width="6dp"
        android:visibility="gone"
        android:id="@+id/spacer"
        />
    <!--<FrameLayout
        android:id="@+id/wimax_combo"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginEnd="-6dp"
        >
上增加
  1. <FrameLayout  
  2.     android:id="@+id/ethernet_combo"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_marginRight="-6dp"  
  6.     >  
  7.     <ImageView  
  8.         android:id="@+id/ethernet_state"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_centerVertical="true"  
  13.         android:scaleType="center"  
  14.         />  
  15. </FrameLayout>  
修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java

    private int mWifiStrengthId = 0;
下增加
  1. private boolean mEthernetVisible = false;  
  2. private int mEthernetStateId = 0;  
在private String mWifiDescription, mMobileDescription, mMobileTypeDescription,mEthernetDescription;
后增加,mEthernetDescription如上

然后增加mEthernetGroup,mEthernet如下

    ViewGroup mWifiGroup, mMobileGroup,mEthernetGroup;
    ImageView mWifi, mMobile, mWifiActivity, mMobileActivity, mMobileType, mAirplane,mEthernet;


    @Override
    public void setIsAirplaneMode(boolean is, int airplaneIconId) {
        mIsAirplaneMode = is;
        mAirplaneIconId = airplaneIconId;

        apply();
    }
下增加

  1. @Override  
  2. public void setEthernetIndicators(boolean visible, int stateIcon, int activityIcon,  
  3.         String contentDescription) {  
  4.     mEthernetVisible = visible;  
  5.     mEthernetStateId = stateIcon;  
  6.     //mEthernetActivityId = activityIcon;  
  7.     mEthernetDescription = contentDescription;  
  8.   
  9.     apply();  
  10. }  

mAirplane       = (ImageView) findViewById(R.id.airplane);
下增加
  1. mEthernetGroup  = (ViewGroup) findViewById(R.id.ethernet_combo);  
  2. mEthernet       = (ImageView) findViewById(R.id.ethernet_state);  
在mAirplane       = null;
下增加

  1. mEthernetGroup  = null;  
  2. mEthernet       = null;  

        if (mIsAirplaneMode) {
            mAirplane.setImageResource(mAirplaneIconId);
            mAirplane.setVisibility(View.VISIBLE);
        } else {
            mAirplane.setVisibility(View.GONE);
        }
下增加
  1. if (mEthernetVisible) {  
  2.     mEthernetGroup.setVisibility(View.VISIBLE);  
  3.     mEthernet.setImageResource(mEthernetStateId);  
  4.     //mEthernetActivity.setImageResource(mEthernetActivityId);  
  5.     mEthernetGroup.setContentDescription(mEthernetDescription);  
  6. else {  
  7.     mEthernetGroup.setVisibility(View.GONE);  
  8. }  
然后增加mEthernetVisible如下

if (mMobileVisible && mWifiVisible && mIsAirplaneMode && mEthernetVisible) {

修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java
增加

  1. import android.net.ethernet.EthernetManager;  
  2. import android.net.ethernet.EthernetStateTracker;  
  3. import android.util.Slog;  
  4. import com.android.systemui.R;  

    String mContentDescriptionWimax;
下增加
  1. String mContentDescriptionEthernet;//add by hclydao  

    //wimax
    private boolean mWimaxSupported = false;
    private boolean mIsWimaxEnabled = false;
    private boolean mWimaxConnected = false;
    private boolean mWimaxIdle = false;
    private int mWimaxIconId = 0;
    private int mWimaxSignal = 0;
    private int mWimaxState = 0;
    private int mWimaxExtraState = 0;
下增加
  1. // Ethernet  
  2. boolean mShowEthIcon, mEthernetWaitingDHCP;  
  3. boolean mEthernetPhyConnect=false ;  
  4. int mEthernetIconId = 0;  

    String mLastCombinedLabel = "";
下增加
  1. int mLastEthernetIconId = -1;  

        void setIsAirplaneMode(boolean is, int airplaneIcon);
下增加
  1. void setEthernetIndicators(boolean visible, int stateIcon, int activityIcon,  
  2.         String contentDescription);  

        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
下增加
  1. filter.addAction(EthernetManager.ETH_STATE_CHANGED_ACTION);  

cluster.setIsAirplaneMode(mAirplaneMode, mAirplaneIconId);
上增加如
  1. cluster.setEthernetIndicators(  
  2.         mShowEthIcon,  
  3.         mEthernetIconId,  
  4.         -1,  
  5.         mContentDescriptionEthernet);    

    else if (action.equals(WimaxManagerConstants.NET_4G_STATE_CHANGED_ACTION) ||
                    action.equals(WimaxManagerConstants.SIGNAL_LEVEL_CHANGED_ACTION) ||
                    action.equals(WimaxManagerConstants.WIMAX_NETWORK_STATE_CHANGED_ACTION)) {
                updateWimaxState(intent);
                refreshViews();
            }
下增加
  1. else if (action.equals(EthernetManager.ETH_STATE_CHANGED_ACTION)) {  
  2.            updateEth(intent);  
  3.            refreshViews();  
  4.        }  

    updateWimaxIcons函数下增加函数

  1. // ===== Ethernet ===================================================================  
  2.   private final void updateEth(Intent intent) {  
  3.       final int event = intent.getIntExtra(EthernetManager.EXTRA_ETH_STATE, EthernetStateTracker.EVENT_HW_DISCONNECTED);  
  4.       Slog.d(TAG, "updateEth event=" + event);  
  5.       switch (event) {  
  6.           case EthernetStateTracker.EVENT_HW_CONNECTED:  
  7.               if (mEthernetWaitingDHCP)  
  8.                   return;  
  9.               // else fallthrough  
  10.           case EthernetStateTracker.EVENT_INTERFACE_CONFIGURATION_SUCCEEDED: {  
  11.                   mEthernetWaitingDHCP = false;  
  12.               EthernetManager ethManager = (EthernetManager) mContext.getSystemService(mContext.ETH_SERVICE);  
  13.               if (ethManager.isEthDeviceAdded()) {  
  14.                   mShowEthIcon = true;  
  15.                   mEthernetIconId =R.drawable.ethernet_connected ; //  sEthImages[0];   
  16.                   mContentDescriptionEthernet = mContext.getString(R.string.accessibility_ethernet_connected);  
  17.               }  
  18.               return;  
  19.           }  
  20.           case EthernetStateTracker.EVENT_INTERFACE_CONFIGURATION_FAILED:  
  21.               mEthernetWaitingDHCP = false;  
  22.         //if(!mEthernetPhyConnect)  
  23.                // return ;  
  24.               mShowEthIcon = true;  
  25.               mEthernetIconId = R.drawable.ethernet_disconnected; // sEthImages[1];   
  26.               mContentDescriptionEthernet = mContext.getString(R.string.accessibility_ethernet_disconnected);  
  27.               return;  
  28.           case EthernetStateTracker.EVENT_DHCP_START:  
  29.               mEthernetWaitingDHCP = true ;  
  30.               return;  
  31.           case EthernetStateTracker.EVENT_HW_PHYCONNECTED:  
  32.               mEthernetPhyConnect = true ;  
  33.               mShowEthIcon = true;  
  34.               mEthernetIconId =R.drawable.ethernet_connecting ; // sEthImages[2]; // 2  
  35.               mContentDescriptionEthernet = mContext.getString(R.string.accessibility_ethernet_connecting);  
  36.               return;  
  37.           case EthernetStateTracker.EVENT_HW_PHYDISCONNECTED:  
  38.               mEthernetPhyConnect = false ;  
  39.               mEthernetWaitingDHCP = false;  
  40.               mShowEthIcon = false;  
  41.               mEthernetIconId = -1;  
  42.               mContentDescriptionEthernet = null;  
  43.               return;  
  44.           case EthernetStateTracker.EVENT_HW_DISCONNECTED:  
  45.               mEthernetPhyConnect = false ;  
  46.               mEthernetWaitingDHCP = false;  
  47.               mShowEthIcon = false;  
  48.               mEthernetIconId = -1;  
  49.               mContentDescriptionEthernet = null;  
  50.               return ;  
  51.           case EthernetStateTracker.EVENT_HW_CHANGED:  
  52.               return;              
  53.             
  54.           default:  
  55.               if (mEthernetWaitingDHCP)  
  56.                   return;  
  57.               mShowEthIcon = false;  
  58.               mEthernetIconId = -1;  
  59.               mContentDescriptionEthernet = null;  
  60.               return;  
  61.       }  
  62.   }  

        if (mBluetoothTethered) {
            combinedLabel = mContext.getString(R.string.bluetooth_tethered);
            combinedSignalIconId = mBluetoothTetherIconId;
            mContentDescriptionCombinedSignal = mContext.getString(
                    R.string.accessibility_bluetooth_tether);
        }
下增加
  1. if (mShowEthIcon) {  
  2.     wifiLabel = mContentDescriptionEthernet;  
  3.     combinedSignalIconId = mEthernetIconId;  
  4.     mContentDescriptionCombinedSignal = mContentDescriptionEthernet;  
  5. }  
在这个下面增加
  1. final boolean ethernetConnected = (mConnectedNetworkType == ConnectivityManager.TYPE_ETHERNET);  
  2. if (ethernetConnected) {  
  3.     // TODO: icons and strings for Ethernet connectivity  
  4.     combinedLabel = mConnectedNetworkTypeName;  
  5. }  
注释掉之前的 
/*
        final boolean ethernetConnected = (mConnectedNetworkType == ConnectivityManager.TYPE_ETHERNET);
        if (ethernetConnected) {
            combinedLabel = context.getString(R.string.ethernet_label);
        }
*///modify by hclydao
在&& !ethernetConnected后增加 && !mShowEthIcon
在         || mLastWifiIconId                 != mWifiIconId
下增加
         || mLastEthernetIconId             != mEthernetIconId


        // the wimax icon on phones
        if (mLastWimaxIconId != mWimaxIconId) {
            mLastWimaxIconId = mWimaxIconId;
        }
下增加

  1. // ethernet icon on phones  
  2. if (mLastEthernetIconId != mEthernetIconId) {  
  3.     mLastEthernetIconId = mEthernetIconId;  
  4.     // Phone UI not supported yet.  
  5. }  
基本上就修改这些地方

先执行下make update-api在编译

碰到的问题

设置静态IP无法启动问题

解决方法

修改frameworks/base/services/java/com/android/server/ConnectivityService.java部分代码为:

  1. if(mNetConfigs[targetNetworkType].radio == ConnectivityManager.TYPE_ETHERNET) { //add by hclydao  
  2.        EthernetService ethernet = new EthernetService(context, (EthernetStateTracker)mNetTrackers[targetNetworkType]);  
  3.        ServiceManager.addService(Context.ETH_SERVICE, ethernet);  
  4.       // mNetTrackers[targetNetworkType].startMonitoring(context, mTrackerHandler);  
  5. }  
    以及
  1. if(mClat != null//add by hclydao  
  2.     if (mClat.requiresClat(netType, tracker)) {  
  3.   
  4.         // If the connection was previously using clat, but is not using it now, stop the clat  
  5.         // daemon. Normally, this happens automatically when the connection disconnects, but if  
  6.         // the disconnect is not reported, or if the connection's LinkProperties changed for  
  7.         // some other reason (e.g., handoff changes the IP addresses on the link), it would  
  8.         // still be running. If it's not running, then stopping it is a no-op.  
  9.         if (Nat464Xlat.isRunningClat(curLp) && !Nat464Xlat.isRunningClat(newLp)) {  
  10.             mClat.stopClat();  
  11.         }  
  12.         // If the link requires clat to be running, then start the daemon now.  
  13.         if (mNetTrackers[netType].getNetworkInfo().isConnected()) {  
  14.             mClat.startClat(tracker);  
  15.         } else {  
  16.             mClat.stopClat();  
  17.         }  
  18.     }  

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

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

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

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

===================以下为补充内容==================================

        1.将文件DhcpInfoInternal.java放到 \frameworks\base\core\java\android\net\下

        2.上文中Settings/res/layout全路径为:packages/apps/Settings/res/layout/

        3.上文中values/strings.xml全路径为:packages/apps/Settings/res/values/strings.xml

        4.上文中Settings/src全路径为:packages/apps/Settings/src

        5.上文中Settings/src/Utils.java全路径为:packages/apps/Settings/src/com/android/Settings/Utils.java

        6.上文中Settings/src/Settings.java全路径为:packages/apps/Settings/src/com/android/Settings/Settings.java
        7.还需要修改frameworks/base/core/java/android/net/NetworkUtils.java文件,将public static int inetAddressToInt(Inet4Address inetAddr) 改为public static int inetAddressToInt(InetAddress inetAddr),否则会报inetAddressToInt(java.net.Inet4Address) in android.net.NetworkUtils cannot be applied to (java.net.InetAddress)这个错误 .




没有更多推荐了,返回首页