[RK3576][Android14] Android->添加以太网MAC地址选项
测试平台
Platform: RK3576
OS: Android14
需求:
客户反馈:设置->网络和互联网->以太网 中没有MAC地址功能选项
解决方法:
代码路径:packages/apps/Settings
diff --git a/src/com/android/settings/ethernet/EthernetSettings.java b/src/com/android/settings/ethernet/EthernetSettings.java
index f2dfc1f1db..91a0f76443 100644
--- a/src/com/android/settings/ethernet/EthernetSettings.java
+++ b/src/com/android/settings/ethernet/EthernetSettings.java
@@ -66,6 +66,9 @@ import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.ethernet.EthernetStaticIpDialog;
import com.android.settings.utils.ReflectUtils;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
public class EthernetSettings extends SettingsPreferenceFragment implements
Preference.OnPreferenceChangeListener, EthernetStaticIpDialog.OnStaticIpDialogClickListener {
@@ -276,6 +279,12 @@ public class EthernetSettings extends SettingsPreferenceFragment implements
ethernetIpAddrPref.setSummary(R.string.device_info_default);
ethernetIpAddrPref.setTitle(R.string.ethernet_ip_addr);
+ // Create Ethernet MAC Address Preference
+ Preference ethernetMacAddrPref = new Preference(mContext);
+ ethernetMacAddrPref.setKey(prefix + KEY_ETH_HW_ADDRESS);
+ ethernetMacAddrPref.setSummary(R.string.device_info_default);
+ ethernetMacAddrPref.setTitle(R.string.ethernet_hw_addr);
+
// Create Ethernet Netmask Preference
Preference ethernetNetmaskPref = new Preference(mContext);
ethernetNetmaskPref.setKey(prefix + KEY_ETH_NET_MASK);
@@ -311,6 +320,7 @@ public class EthernetSettings extends SettingsPreferenceFragment implements
preferenceScreen.addPreference(ethernetNetInterface);
ethernetNetInterface.addPreference(ethernetIpAddrPref);
+ ethernetNetInterface.addPreference(ethernetMacAddrPref);
ethernetNetInterface.addPreference(ethernetNetmaskPref);
ethernetNetInterface.addPreference(ethernetGatewayPref);
ethernetNetInterface.addPreference(ethernetDns1Pref);
@@ -379,6 +389,7 @@ public class EthernetSettings extends SettingsPreferenceFragment implements
if (info != null) {
//setStringSummary(prefix + KEY_NET_INTERFACE, key);
setStringSummary(prefix + KEY_ETH_IP_ADDRESS, info.getIpAddress());
+ setStringSummary(prefix + KEY_ETH_HW_ADDRESS, getEthMac());
setStringSummary(prefix + KEY_ETH_NET_MASK, info.getNetmask());
setStringSummary(prefix + KEY_ETH_GATEWAY, info.getGateway());
log("hjf info.getGateway() = " + info.getGateway());
@@ -389,6 +400,7 @@ public class EthernetSettings extends SettingsPreferenceFragment implements
updateCheckbox(key, (ListPreference) findPreference(prefix + KEY_ETH_MODE));
} else {
setStringSummary(prefix + KEY_ETH_IP_ADDRESS, nullIpInfo);
+ setStringSummary(prefix + KEY_ETH_HW_ADDRESS, nullIpInfo);
setStringSummary(prefix + KEY_ETH_NET_MASK, nullIpInfo);
setStringSummary(prefix + KEY_ETH_GATEWAY, nullIpInfo);
setStringSummary(prefix + KEY_ETH_DNS1, nullIpInfo);
@@ -397,6 +409,26 @@ public class EthernetSettings extends SettingsPreferenceFragment implements
}
}
+ // 获取 以太网MAC地址信息
+ public String getEthMac(){
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader("/sys/class/net/eth0/address"));
+ return reader.readLine();
+ }catch (Exception e) {
+ Log.e(TAG, "open sys/class/net/eth0/address failed : " + e);
+ return "";
+ }finally {
+ try {
+ if (reader != null)
+ reader.close();
+ } catch (IOException e) {
+ Log.e(TAG, "close sys/class/net/eth0/address failed : " + e);
+ }
+ }
+ }
+
+
private void updateCheckbox(String ifaceName, ListPreference preference) {
if (mEthManager == null) {
preference.setSummary("null");