PLATFORM:RK3399
KERNEL:Linux4.4
OS:Android7.1
问题描述:
客户希望能够支持应用层打开\关闭有线网,且重启设备、插拔网线均能记住以上设置状态。
我们需要在不拔出网线的同时关闭上网的功能,这个时候解决方安就是将网口关闭,等到允许上网时再打开网口,需要我们建立一个以太网网口的开关。
问题解决:
add Ethernet switch in Settings for user to enable/disable it
modified dirs are as below:
frameworks/base/
frameworks/opt/net/ethernet/
packages/apps/Settings
$ pwd
/home/tommy/nougat_rk3399_android7.1/frameworks/base
diff --git a/core/java/android/net/EthernetManager.java b/core/java/android/net/EthernetManager.java
old mode 100755
new mode 100644
index 7b7e7cd..11a7b5e
--- a/core/java/android/net/EthernetManager.java
+++ b/core/java/android/net/EthernetManager.java
@@ -64,7 +64,22 @@ public class EthernetManager {
* @hide
*/
public static final int ETHER_STATE_DISCONNECTING = 3;
-
+ /**
+ * @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;
private final Context mContext;
private final IEthernetManager mService;
private final Handler mHandler = new Handler() {
@@ -144,7 +159,13 @@ 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 {
return mService.getEthernetCarrierState(ifname);
@@ -169,6 +190,14 @@ public class EthernetManager {
}
}
+ public int getEthernetIfaceState() {
+ try {
+ return mService.getEthernetIfaceState();
+ } catch (RemoteException e) {
+ return EthernetManager.ETHER_IFACE_STATE_DOWN;
+ }
+ }
+
public String getIpAddress() {
try {
return mService.getIpAddress();
diff --git a/core/java/android/net/IEthernetManager.aidl b/core/java/android/net/IEthernetManager.aidl
index 39d6b13..08a18da 100644
--- 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
old mode 100755
new mode 100644
index 052eb8f..1c1920b
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -2544,7 +2544,10 @@ public final class Settings {
*/
@Deprecated
public static final String MODE_RINGER = Global.MODE_RINGER;
-
+ /**
+ * @hide
+ */
+ public static final String ETHERNET_ON = Secure.ETHERNET_ON;
/**
* @hide
*/
@@ -5105,7 +5108,10 @@ public final class Settings {
* @hide
*/
public static final String PPPOE_PHY_IFACE = "pppoe_phy_iface";
-
+ /**
+ * @hide
+ */
+ public static final String ETHERNET_ON = "ethernet_on";
/**
* Whether to draw text with high contrast while in accessibility mode.
$ pwd
/home/tommy/nougat_rk3399_android7.1/frameworks/opt/net/ethernet
diff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.java
old mode 100755
new mode 100644
index 15fe2e2..8d6561a
--- 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;
@@ -156,6 +157,15 @@ class EthernetNetworkFactory {
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()) {
try {
@@ -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,8 @@ class EthernetNetworkFactory {
mNMService.setInterfaceDown(iface);
}
}
+ if (ethernet_on == 0)
+ mNMService.setInterfaceDown(iface);
} catch (RemoteException e) {
Log.e(TAG, "Error upping interface " + mIface + ": " + e);
}
@@ -817,6 +830,57 @@ class EthernetNetworkFactory {
}
}
+ 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()) {
pw.println("Tracking interface: " + mIface);
diff --git a/java/com/android/server/ethernet/EthernetServiceImpl.java b/java/com/android/server/ethernet/EthernetServiceImpl.java
old mode 100755
new mode 100644
index dcac91b..55ba9de
--- a/java/com/android/server/ethernet/EthernetServiceImpl.java
+++ b/java/com/android/server/ethernet/EthernetServiceImpl.java
@@ -229,6 +229,22 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
}
@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) {
final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
$ pwd
/home/tommy/nougat_rk3399_android7.1/packages/apps/Settings
diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml
old mode 100755
new mode 100644
index e9e8f0a..a78eb9e
--- a/res/values-zh-rCN/strings.xml
+++ b/res/values-zh-rCN/strings.xml
@@ -3088,6 +3088,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
index b05cdec..300606d 100644
--- 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
index 38dedfd..d03562a 100644
--- 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");
@@ -287,9 +287,15 @@ public class EthernetSettings extends SettingsPreferenceFragment
mkeyEthMode.setSummary(R.string.usestatic);
}else{
mkeyEthMode.setValue("PPPoE");
- mkeyEthMode.setSummary(R.string.usepppoe);
+ mkeyEthMode.setSummary(R.string.usepppoe);
+ }
+
+ int isEnable = mEthManager.getEthernetIfaceState();
+ if (isEnable == EthernetManager.ETHER_IFACE_STATE_UP) {
+ mEthCheckBox.setChecked(true);
+ } else {
+ mEthCheckBox.setChecked(false);
}
-
}
}
@@ -312,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) {
String netMask = null;