rk3288 android7.1 以太网用开关来控制打开与否

1./rk3288-7.1/frameworks/base:
diff --git a/core/java/android/net/EthernetManager.java b/core/java/android/net/EthernetManager.java
index 7b7e7cd..a991306 100755
--- a/core/java/android/net/EthernetManager.java
+++ b/core/java/android/net/EthernetManager.java
@@ -64,6 +64,28 @@ public class EthernetManager {
      * @hide
      */
     public static final int ETHER_STATE_DISCONNECTING = 3;
+       
+       //wmc add for ethernet
+       /**
+     * @hide
+     */
+    public static final String  EXTRA_ETHERNET_IFACE_STATE = "ethernet_iface_state";
+       
+       /**
+     * @hide
+     */
+    public static final String  ETHERNET_IFACE_STATE_CHANGED_ACTION = "android.net.ethernet.ETHERNET_IFACE_STATE_CHANGED";
+       
+       /**
+     * @hide
+     */
+    public static final int ETHER_IFACE_STATE_DOWN = 0;
+       
+       /**
+     * @hide
+     */
+    public static final int ETHER_IFACE_STATE_UP = 1;
+       //wmc add for ethernet end
 
     private final Context mContext;
     private final IEthernetManager mService;
@@ -144,6 +166,14 @@ public class EthernetManager {
             throw e.rethrowFromSystemServer();
         }
     }
+       
+    public boolean setEthernetEnabled(boolean enabled) {
+        try {
+            return mService.setEthernetEnabled(enabled);
+        } catch (RemoteException e) {
+            return false;
+        }
+    }
 
     public int getEthernetCarrierState(String ifname) {
         try {
@@ -168,6 +198,14 @@ public class EthernetManager {
             throw e.rethrowFromSystemServer();
         }
     }
+       
+       public int getEthernetIfaceState() {
+       try {
+            return mService.getEthernetIfaceState();
+       } catch (RemoteException e) {
+            return EthernetManager.ETHER_IFACE_STATE_DOWN;
+       }
+    }
 
     public String getIpAddress() {
         try {
diff --git a/core/java/android/net/IEthernetManager.aidl b/core/java/android/net/IEthernetManager.aidl
old mode 100644
new mode 100755
index 39d6b13..4a04fcb
--- a/core/java/android/net/IEthernetManager.aidl
+++ b/core/java/android/net/IEthernetManager.aidl
@@ -41,4 +41,6 @@ interface IEthernetManager
     void disconnect(String iface);
     void addListener(in IEthernetServiceListener listener);
     void removeListener(in IEthernetServiceListener listener);
+       boolean setEthernetEnabled(in boolean enable);
+    int getEthernetIfaceState();
 }
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index b08eeb9..9da039f 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -2549,6 +2549,11 @@ public final class Settings {
         @Deprecated
         public static final String MODE_RINGER = Global.MODE_RINGER;
 
+               /**
+         * @hide wmc add for ethernet
+         */
+        public static final String ETHERNET_ON = Secure.ETHERNET_ON;
+               
         /**
          * @hide
          */
@@ -5131,6 +5136,11 @@ public final class Settings {
          * @hide
          */
         public static final String PPPOE_PHY_IFACE = "pppoe_phy_iface";
+               
+        /**
+         * @hide wmc add for ethernet
+         */
+        public static final String ETHERNET_ON = "ethernet_on";
 
         /**
          * Whether to draw text with high contrast while in accessibility mode.
          
2.rk3288-7.1/frameworks/opt/net/ethernet:
diff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.java
index 15fe2e2..45cc4ae 100755
--- a/java/com/android/server/ethernet/EthernetNetworkFactory.java
+++ b/java/com/android/server/ethernet/EthernetNetworkFactory.java
@@ -130,6 +130,7 @@ class EthernetNetworkFactory {
     private Thread mIpProvisioningThread;
 
     public int mEthernetCurrentState = EthernetManager.ETHER_STATE_DISCONNECTED;
+       public int ethCurrentIfaceState = EthernetManager.ETHER_IFACE_STATE_DOWN;
     private boolean mReconnecting;
     private IpAssignment mConnectMode;
 
@@ -155,6 +156,15 @@ class EthernetNetworkFactory {
         intent.putExtra(EthernetManager.EXTRA_ETHERNET_STATE, curState);
         mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
     }
+       
+    private void sendEthIfaceStateChangedBroadcast(int curState) {
+        final Intent intent = new Intent(EthernetManager.ETHERNET_IFACE_STATE_CHANGED_ACTION);
+        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
+        intent.putExtra(EthernetManager.EXTRA_ETHERNET_IFACE_STATE, curState);
+        ethCurrentIfaceState = curState;
+        Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ETHERNET_ON, curState);
+        mContext.sendStickyBroadcast(intent);
+    }
 
     private String ReadFromFile(File file) {
         if((file != null) && file.exists()) {
@@ -308,6 +318,7 @@ class EthernetNetworkFactory {
 
     private void setInterfaceUp(String iface) {
         // Bring up the interface so we get link status indications.
+               int ethernet_on = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.ETHERNET_ON, 1);
         Log.d(TAG, "setInterfaceUp: " + iface);
         try {
             mNMService.setInterfaceUp(iface);
@@ -329,6 +340,9 @@ class EthernetNetworkFactory {
                     mNMService.setInterfaceDown(iface);
                 }
             }
+
+                        if (ethernet_on == 0)
+                mNMService.setInterfaceDown(iface);
         } catch (RemoteException e) {
             Log.e(TAG, "Error upping interface " + mIface + ": " + e);
         }
@@ -816,6 +830,57 @@ class EthernetNetworkFactory {
             mListeners.finishBroadcast();
         }
     }
+       
+       public int getEthernetIfaceState() {
+        // enforceAccessPermission();
+        //Log.d(TAG, "getEthernetIfaceState()");
+        File file = new File("/sys/class/net/" + mIface + "/flags");
+        String flags = ReadFromFile(file);
+        //Log.d(TAG, "flags=" + flags);
+        if (flags == null) {
+            return EthernetManager.ETHER_IFACE_STATE_DOWN;
+        }
+
+        String flags_no_0x = flags.substring(2);
+        int flags_int = Integer.parseInt(flags_no_0x, 16);
+        if ((flags_int & 0x1) > 0) {
+            //Log.d(TAG, "state=up");
+            return EthernetManager.ETHER_IFACE_STATE_UP;
+        } else {
+            //Log.d(TAG, "state=down");
+            return EthernetManager.ETHER_IFACE_STATE_DOWN;
+        }
+    }
+
+    public boolean setInterfaceDown() {
+        try {
+            if (!TextUtils.isEmpty(mIface)) {
+                mNMService.setInterfaceDown(mIface);
+                sendEthIfaceStateChangedBroadcast(EthernetManager.ETHER_IFACE_STATE_DOWN);
+                return true;
+            } else {
+                Log.e(TAG, "mIface is null");
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "Error downing interface " + mIface + ": " + e);
+        }
+        return false;
+    }
+
+    public boolean setInterfaceUp() {
+        try {
+            if (!TextUtils.isEmpty(mIface)) {
+                mNMService.setInterfaceUp(mIface);
+                sendEthIfaceStateChangedBroadcast(EthernetManager.ETHER_IFACE_STATE_UP);
+                return true;
+            } else {
+                Log.e(TAG, "mIface is null");
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "Error downing interface " + mIface + ": " + e);
+        }
+        return false;
+    }
 
     synchronized void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) {
         if (isTrackingInterface()) {
diff --git a/java/com/android/server/ethernet/EthernetServiceImpl.java b/java/com/android/server/ethernet/EthernetServiceImpl.java
index dcac91b..2918bc4 100755
--- a/java/com/android/server/ethernet/EthernetServiceImpl.java
+++ b/java/com/android/server/ethernet/EthernetServiceImpl.java
@@ -227,6 +227,22 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
         enforceAccessPermission();
         mTracker.disconnect(iface);
     }
+       
+       @Override
+    public int getEthernetIfaceState() {
+        return mTracker.getEthernetIfaceState();
+    }
+
+    @Override
+    public boolean setEthernetEnabled(boolean enable) {
+        //enforceChangePermission();
+        Log.i(TAG, "setEthernetEnabled() : enable=" + enable);
+        if (enable) {
+            return mTracker.setInterfaceUp();
+        } else {
+            return mTracker.setInterfaceDown();
+        }
+    }
 
     @Override
     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
    
3.rk3288-7.1/packages/apps/Settings:
diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml
index a1b2173..6f45f55 100755
--- a/res/values-zh-rCN/strings.xml
+++ b/res/values-zh-rCN/strings.xml
@@ -3089,6 +3089,7 @@
     <string name="ethernet_data_usage" msgid="5108764537574354616">"以太网流量消耗"</string>
     <string name="wifi" msgid="1081550856200013637">"WLAN"</string>
     <string name="ethernet" msgid="6600095783781389720">"以太网"</string>
+       <string name="ethernet_quick_toggle_title">以太网</string>
     <string name="cell_data_template" msgid="3308709914705592281">"<xliff:g id="ID_1">%1$s</xliff:g> 移动数据网络流量"</string>
     <string name="wifi_data_template" msgid="501654649753761778">"<xliff:g id="ID_1">%1$s</xliff:g> WLAN 网络流量"</string>
     <string name="ethernet_data_template" msgid="5775729859383597507">"<xliff:g id="ID_1">%1$s</xliff:g> 以太网流量"</string>
diff --git a/res/xml/ethernet_settings.xml b/res/xml/ethernet_settings.xml
old mode 100644
new mode 100755
index b05cdec..300606d
--- a/res/xml/ethernet_settings.xml
+++ b/res/xml/ethernet_settings.xml
@@ -4,7 +4,7 @@
     android:title="@string/ethernet_settings" >
 
     <!-- $_rbox_$_modify_$_lijiehong: change to support bluetooth checkbox -->
-<!--
+
     <SwitchPreference
         android:key="ethernet"
         android:title="@string/ethernet_quick_toggle_title"
@@ -12,7 +12,7 @@
         android:summaryOn="@string/ethernet_quick_toggle_summary_on"
         android:persistent="false"
         android:disableDependentsState="true" />
-  -->
+
   <!--    
     <Preference
         style="?android:preferenceInformationStyle"
diff --git a/src/com/android/settings/ethernet/EthernetSettings.java b/src/com/android/settings/ethernet/EthernetSettings.java
old mode 100644
new mode 100755
index 38dedfd..382a31d
--- a/src/com/android/settings/ethernet/EthernetSettings.java
+++ b/src/com/android/settings/ethernet/EthernetSettings.java
@@ -116,7 +116,7 @@ public class EthernetSettings extends SettingsPreferenceFragment
     private final static String nullIpInfo = "0.0.0.0";
 
     private ListPreference mkeyEthMode;
-//    private SwitchPreference mEthCheckBox;
+    private SwitchPreference mEthCheckBox;
     private CheckBoxPreference staticEthernet;
        
     private final IntentFilter mIntentFilter;
@@ -215,12 +215,12 @@ public class EthernetSettings extends SettingsPreferenceFragment
             mkeyEthMode=(ListPreference)findPreference(KEY_ETH_MODE);
             mkeyEthMode.setOnPreferenceChangeListener(this);
         }
-    /*    
+        
         if (mEthCheckBox== null) {
             mEthCheckBox = (SwitchPreference) findPreference("ethernet");
             mEthCheckBox.setOnPreferenceChangeListener(this);
         }
-    */    
+        
         handleEtherStateChange(mEthManager.getEthernetConnectState());
         refreshUI();
         log("resume");
@@ -289,6 +289,13 @@ public class EthernetSettings extends SettingsPreferenceFragment
                 mkeyEthMode.setValue("PPPoE");
                        mkeyEthMode.setSummary(R.string.usepppoe);
             }
+                       
+                       int isEnable = mEthManager.getEthernetIfaceState();
+            if (isEnable == EthernetManager.ETHER_IFACE_STATE_UP) {
+                mEthCheckBox.setChecked(true);
+            } else {
+                mEthCheckBox.setChecked(false);
+             }
            
         }    
     }
@@ -311,7 +318,23 @@ public class EthernetSettings extends SettingsPreferenceFragment
                
        }
        return true;
-    }    
+    }  
+
+    @Override
+    public boolean onPreferenceTreeClick(Preference preference) {
+        if (preference == mEthCheckBox) {
+            boolean newState = mEthCheckBox.isChecked();
+            if (newState) {
+                log("turn on Ethernet");
+                mEthManager.setEthernetEnabled(true);
+            } else {
+                log("turn off Ethernet");
+                mEthManager.setEthernetEnabled(false);
+            }
+            //log("IpAssignment: "+mEthManager.getConfiguration().toString());
+        }
+        return super.onPreferenceTreeClick(preference);
+    }  
     
   //将子网掩码转换成ip子网掩码形式,比如输入32输出为255.255.255.255  
     public  String interMask2String(int prefixLength) {
    
    
参考:https://www.codeleading.com/article/5500991651/

 

posted @ 2020-06-03 11:44  M-kobe  阅读(1984)  评论(0编辑  收藏  举报