From 552bdb764ece01f57e1f174432a69f0af3b2c141 Mon Sep 17 00:00:00 2001
From: huangcm <1263938474@qq.com>
Date: Sat, 26 Apr 2025 01:27:44 +0000
Subject: [PATCH] fix(DeviceTest): fix Ethernet Test pro

---
 android/vendor/aw/public/package/apk/DeviceTest_V1/src/com/DeviceTest/EthernetTestActivity.java |  105 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 104 insertions(+), 1 deletions(-)

diff --git a/android/vendor/aw/public/package/apk/DeviceTest_V1/src/com/DeviceTest/EthernetTestActivity.java b/android/vendor/aw/public/package/apk/DeviceTest_V1/src/com/DeviceTest/EthernetTestActivity.java
index f95745e..5de99e4 100755
--- a/android/vendor/aw/public/package/apk/DeviceTest_V1/src/com/DeviceTest/EthernetTestActivity.java
+++ b/android/vendor/aw/public/package/apk/DeviceTest_V1/src/com/DeviceTest/EthernetTestActivity.java
@@ -40,6 +40,8 @@
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;
+import java.net.Inet4Address;
+import android.net.RouteInfo;
 
 import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
 import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
@@ -71,6 +73,7 @@
     private boolean mReadyToGetEthInfo;
     private boolean mStop;
 
+    private ConnectivityManager mConnectivityManager;
     private EthernetManager mEthManager;
     private BroadcastReceiver mReceiver;
     private String mIfaceName;
@@ -151,6 +154,8 @@
         ControlButtonUtil.initControlButtonView(this);
         findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
         findViewById(R.id.btn_Fail).setVisibility(View.INVISIBLE);
+
+        mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 
         mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);
         if (null == mEthManager) {
@@ -242,12 +247,110 @@
         }
         IpAssignment mode = ipConfiguration.getIpAssignment();
         if (mode == IpAssignment.DHCP || mode == IpAssignment.UNASSIGNED) {
-            getEthInfoFromDhcp();
+            getEthInfoFromDhcp_V1();
         } else if (mode == IpAssignment.STATIC) {
             getEthInfoFromStaticIp();
         }
     }
 
+    public static String intToNetmask(int prefixLength) {
+        if (prefixLength < 0 || prefixLength > 32) {
+            throw new IllegalArgumentException("Prefix length must be between 0 and 32");
+        }
+        StringBuilder netmask = new StringBuilder();
+        int[] parts = new int[4];
+        // 计算每一部分的值
+        for (int i = 0; i < 4; i++) {
+            if (prefixLength >= 8) {
+                parts[i] = 255;
+                prefixLength -= 8;
+            } else if (prefixLength > 0) {
+                parts[i] = 256 - (1 << (8 - prefixLength));
+                prefixLength = 0;
+            } else {
+                parts[i] = 0;
+            }
+        }
+        // 构建点分十进制字符串
+        for (int i = 0; i < 4; i++) {
+            netmask.append(parts[i]);
+            if (i < 3) {
+                netmask.append(".");
+            }
+        }
+        return netmask.toString();
+    }
+
+    private void getEthInfoFromDhcp_V1() {
+        int network = ConnectivityManager.TYPE_ETHERNET;
+        String mMacPreference = mConnectivityManager.getNetworkInfo(network).getExtraInfo();
+
+        final LinkProperties linkProperties = mConnectivityManager.getLinkProperties(network);
+        for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) { //set ipaddress.
+            if (linkAddress.getAddress() instanceof Inet4Address) {
+                String mgetHostAddress = linkAddress.getAddress().getHostAddress();
+                String netMask = intToNetmask(linkAddress.getPrefixLength());
+                Log.v(TAG, "mgetHostAddress " + mgetHostAddress);
+                Log.v(TAG, "netMask " + netMask);
+                mEthIpAddress = mgetHostAddress;
+                mEthNetmask = netMask;
+                break;
+            }
+        }
+
+        String dns1 = null;
+        String dns2 = null;
+        int dnsCount = 0;
+        for (InetAddress inetAddress:linkProperties.getDnsServers()) {
+            if (0 == dnsCount) {
+                dns1 = inetAddress.getHostAddress().toString();
+                if (inetAddress instanceof Inet4Address) {
+                    dns1 = inetAddress.getHostAddress().toString();
+                } else {
+                    dns1 = "0.0.0.0";
+                }
+            }
+            if (1 == dnsCount) {
+                dns2 = inetAddress.getHostAddress().toString();
+                if (inetAddress instanceof Inet4Address) {
+                    dns2 = inetAddress.getHostAddress().toString();
+                } else {
+                    dns2 = "0.0.0.0";
+                }
+            }
+            dnsCount++;
+        }
+
+        mEthdns1 = dns1;
+        if (dnsCount > 1) {
+            mEthdns2 = dns2;
+        } else {
+            dns2 = "0.0.0.0";
+            mEthdns2 = dns2;
+        }
+
+        String Route = null;
+        for (RouteInfo route:linkProperties.getRoutes()) {
+            if (route.isIPv4Default()) {
+                Route = route.getGateway().toString();
+                break;
+            }
+        }
+        if (Route != null) {
+            String[] RouteStr = Route.split("/");
+            mEthGateway = RouteStr[1];
+        } else {
+            mEthGateway = "0.0.0.0";
+        }
+        Log.v(TAG, "mIfaceName " + mIfaceName);
+        Log.v(TAG, "mEthIpAddress " + mEthIpAddress);
+        Log.v(TAG, "mEthNetmask " + mEthNetmask);
+        Log.v(TAG, "mEthGateway " + mEthGateway);
+        Log.v(TAG, "mEthdns1 " + mEthdns1);
+        Log.v(TAG, "mEthdns2 " + mEthdns2);
+
+    }
+
     public void getEthInfoFromDhcp() throws Exception {
         boolean isSdkAfterO = Build.VERSION.SDK_INT >= ConfigUtil.ANDROID_SDK_VERSION_P;
         String tempIpInfo;

--
Gitblit v1.6.2