package com.nodka.zysjtest; import android.app.ZysjSystemManager; import android.content.Context; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class WifiActivity extends BaseTestActivity { private static final String SERVICE_NOT_FOUND_MESSAGE = "未获取到 ZysjSystemManager 系统服务"; private static final int WIFI_MODE_DHCP = 0; private static final int WIFI_MODE_STATIC = 1; private ZysjSystemManager zysjSystemManager; @Override protected String getPageTitle() { return "WiFi 控制接口测试"; } @Override protected List getTestItems() { return Arrays.asList( new TestItem( "获取系统WIFI的IP地址", "zYgetWifiIp", "获取系统 WIFI 的 IP 地址,无参数。", this::getWifiIp), new TestItem( "获取系统WIFI的MAC地址", "zYgetWifiMacAddress", "获取系统 WIFI 的 MAC 地址,无参数。", this::getWifiMacAddress), new TestItem( "获取系统WIFI的子网掩码", "zYgetWifiNetMask", "获取系统 WIFI 的子网掩码,无参数。", this::getWifiNetMask), new TestItem( "获取系统WIFI的网关", "zYgetWifiGatWay", "获取系统 WIFI 的网关,无参数。", this::getWifiGatWay), new TestItem( "获取系统WIFI的DNS1", "zYgetWifiDns1", "获取系统 WIFI 的 DNS1,无参数。", this::getWifiDns1), new TestItem( "获取系统WIFI的DNS2", "zYgetWifiDns2", "获取系统 WIFI 的 DNS2,无参数。", this::getWifiDns2), new TestItem( "设置系统WIFI动、静态模式", "zYsetWifiParams", "mode:0=动态模式,1=静态模式;静态模式需填写 IP、Gateway、Netmask、DNS1、DNS2。", this::setWifiParams, TestItem.InputField.number( "请输入模式:0=动态模式,1=静态模式", "1", "mode"), TestItem.InputField.text( "请输入 IP", "192.168.1.100", "IP"), TestItem.InputField.text( "请输入 Gateway", "192.168.1.1", "Gateway"), TestItem.InputField.text( "请输入 Netmask", "255.255.255.0", "Netmask"), TestItem.InputField.text( "请输入 DNS1", "8.8.8.8", "DNS1"), TestItem.InputField.text( "请输入 DNS2", "114.114.114.114", "DNS2")) ); } private ZysjSystemManager zysjSystemManager() { if (zysjSystemManager != null) { return zysjSystemManager; } for (String serviceName : getServiceNameCandidates()) { Object service = getSystemService(serviceName); if (service instanceof ZysjSystemManager) { zysjSystemManager = (ZysjSystemManager) service; return zysjSystemManager; } } throw new IllegalStateException(SERVICE_NOT_FOUND_MESSAGE); } private List getServiceNameCandidates() { List candidates = new ArrayList<>(); addContextFieldCandidate(candidates, "ZYSJ_SYSTEM_SERVICE"); addContextFieldCandidate(candidates, "ZYSJ_SERVICE"); candidates.add("zysj_system"); candidates.add("zysj"); candidates.add("zysj_system_service"); return candidates; } private void addContextFieldCandidate(List candidates, String fieldName) { try { Field field = Context.class.getField(fieldName); Object value = field.get(null); if (value instanceof String && !candidates.contains(value)) { candidates.add((String) value); } } catch (NoSuchFieldException ignored) { } catch (IllegalAccessException e) { throw new IllegalStateException("读取 Context." + fieldName + " 失败", e); } } private String getWifiIp(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiIp(); return "调用成功:zYgetWifiIp(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String getWifiMacAddress(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiMacAddress(); return "调用成功:zYgetWifiMacAddress(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String getWifiNetMask(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiNetMask(); return "调用成功:zYgetWifiNetMask(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String getWifiGatWay(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiGatWay(); return "调用成功:zYgetWifiGatWay(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String getWifiDns1(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiDns1(); return "调用成功:zYgetWifiDns1(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String getWifiDns2(TestItem item, String[] inputs) { String result = zysjSystemManager().zYgetWifiDns2(); return "调用成功:zYgetWifiDns2(),参数:无,返回值:" + result + "," + formatStringResult(result); } private String setWifiParams(TestItem item, String[] inputs) { int mode = parseMode(inputs); if (mode == WIFI_MODE_DHCP) { zysjSystemManager().zYsetWifiParams(WIFI_MODE_DHCP, null); return "调用完成:zYsetWifiParams(0, null),参数:mode=0 动态模式,返回值:void,设置完成"; } String ip = requireInput(inputs, 1, "IP"); String gateway = requireInput(inputs, 2, "Gateway"); String netmask = requireInput(inputs, 3, "Netmask"); String dns1 = requireInput(inputs, 4, "DNS1"); String dns2 = requireInput(inputs, 5, "DNS2"); Map params = new HashMap<>(); params.put(ZysjSystemManager.STATIC_IP, ip); params.put(ZysjSystemManager.STATIC_GATEWAY, gateway); params.put(ZysjSystemManager.STATIC_NETMASK, netmask); params.put(ZysjSystemManager.STATIC_DNS1, dns1); params.put(ZysjSystemManager.STATIC_DNS2, dns2); zysjSystemManager().zYsetWifiParams(WIFI_MODE_STATIC, params); return "调用完成:zYsetWifiParams(1, params),参数:mode=1 静态模式" + ", IP=" + ip + ", Gateway=" + gateway + ", Netmask=" + netmask + ", DNS1=" + dns1 + ", DNS2=" + dns2 + ",返回值:void,设置完成"; } private int parseMode(String[] inputs) { if (inputs.length == 0 || inputs[0] == null || inputs[0].trim().isEmpty()) { throw new IllegalArgumentException("mode 不能为空"); } try { int mode = Integer.parseInt(inputs[0].trim()); if (mode != WIFI_MODE_DHCP && mode != WIFI_MODE_STATIC) { throw new IllegalArgumentException("mode 只能是 0 或 1"); } return mode; } catch (NumberFormatException e) { throw new IllegalArgumentException("mode 只能是 0 或 1"); } } private String requireInput(String[] inputs, int index, String fieldName) { if (index >= inputs.length || inputs[index] == null || inputs[index].trim().isEmpty()) { throw new IllegalArgumentException(fieldName + " 不能为空"); } return inputs[index].trim(); } private String formatStringResult(String result) { return result == null || result.trim().isEmpty() ? "获取失败或返回为空" : "获取成功"; } }