package com.nodka.zysjtest;
|
|
import android.app.ZysjSystemManager;
|
import android.content.Context;
|
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
public class NetworkActivity extends BaseTestActivity {
|
private static final String SERVICE_NOT_FOUND_MESSAGE = "未获取到 ZysjSystemManager 系统服务";
|
private static final String DEFAULT_ETH_IFACE = "eth0";
|
private static final int ETHERNET_MODE_STATIC = 1;
|
|
private ZysjSystemManager zysjSystemManager;
|
|
@Override
|
protected String getPageTitle() {
|
return "以太网控制接口测试";
|
}
|
|
@Override
|
protected List<TestItem> getTestItems() {
|
return Arrays.asList(
|
new TestItem(
|
"控制系统以太网开关",
|
"zYsetEthernetEnabled",
|
"enable:true 表示打开,false 表示关闭;默认网口 eth0。",
|
true,
|
"enable",
|
true,
|
this::setEthernetEnabled),
|
new TestItem(
|
"获取系统以太网IP地址",
|
"zYgetEthIp",
|
"获取系统以太网的 IP 地址,默认网口 eth0。",
|
this::getEthIp),
|
new TestItem(
|
"获取系统以太网MAC地址",
|
"zYgetEthMacAddress",
|
"获取系统以太网的 MAC 地址,默认网口 eth0。",
|
this::getEthMacAddress),
|
new TestItem(
|
"获取系统以太网网关地址",
|
"zYgetEthGateway",
|
"获取系统以太网的网关地址,默认网口 eth0。",
|
this::getEthGateway),
|
new TestItem(
|
"获取系统以太网子网掩码",
|
"zYgetEthNetMask",
|
"获取系统以太网的子网掩码,默认网口 eth0。",
|
this::getEthNetMask),
|
new TestItem(
|
"获取系统以太网DNS1",
|
"zYgetEthDns1",
|
"获取系统以太网的 DNS1,默认网口 eth0。",
|
this::getEthDns1),
|
new TestItem(
|
"获取系统以太网DNS2",
|
"zYgetEthDns2",
|
"获取系统以太网的 DNS2,默认网口 eth0。",
|
this::getEthDns2),
|
new TestItem(
|
"获取系统以太网开关状态",
|
"zYgetEthernetIfaceState",
|
"获取系统以太网开关状态,默认网口 eth0。",
|
this::getEthernetIfaceState),
|
new TestItem(
|
"设置系统以太网,静态模式",
|
"zYsetEthernetParams",
|
"设置系统以太网为静态模式,默认网口 eth0。",
|
this::setEthernetStaticParams,
|
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<String> getServiceNameCandidates() {
|
List<String> 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<String> 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 setEthernetEnabled(TestItem item, String[] inputs) throws Exception {
|
boolean result = invokeSetEthernetEnabled(item.switchChecked);
|
return "调用成功:zYsetEthernetEnabled(" + item.switchChecked + ") 返回值:" + result
|
+ "," + (result ? "设置成功" : "设置失败");
|
}
|
|
private boolean invokeSetEthernetEnabled(boolean enable) throws Exception {
|
Method method = zysjSystemManager().getClass().getMethod("zYsetEthernetEnabled", boolean.class);
|
Object result = method.invoke(zysjSystemManager(), enable);
|
if (result instanceof Boolean) {
|
return (Boolean) result;
|
}
|
throw new IllegalStateException("zYsetEthernetEnabled(boolean) 返回值不是 boolean");
|
}
|
|
private String getEthIp(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthIp(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthIp(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthMacAddress(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthMacAddress(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthMacAddress(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthGateway(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthGateway(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthGateway(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthNetMask(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthNetMask(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthNetMask(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthDns1(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthDns1(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthDns1(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthDns2(TestItem item, String[] inputs) {
|
String result = zysjSystemManager().zYgetEthDns2(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthDns2(" + DEFAULT_ETH_IFACE + ") 返回值:" + result;
|
}
|
|
private String getEthernetIfaceState(TestItem item, String[] inputs) {
|
int result = zysjSystemManager().zYgetEthernetIfaceState(DEFAULT_ETH_IFACE);
|
return "调用成功:zYgetEthernetIfaceState(" + DEFAULT_ETH_IFACE + ") 返回值:" + result + "," + formatEthernetState(result);
|
}
|
|
private String setEthernetStaticParams(TestItem item, String[] inputs) {
|
String ip = requireInput(inputs, 0, "IP");
|
String gateway = requireInput(inputs, 1, "Gateway");
|
String netmask = requireInput(inputs, 2, "Netmask");
|
String dns1 = requireInput(inputs, 3, "DNS1");
|
String dns2 = requireInput(inputs, 4, "DNS2");
|
|
Map<String, String> 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().zYsetEthernetParams(DEFAULT_ETH_IFACE, ETHERNET_MODE_STATIC, params);
|
return "调用完成:zYsetEthernetParams(" + DEFAULT_ETH_IFACE + ", " + ETHERNET_MODE_STATIC
|
+ ", IP=" + ip
|
+ ", Gateway=" + gateway
|
+ ", Netmask=" + netmask
|
+ ", DNS1=" + dns1
|
+ ", DNS2=" + dns2 + ")";
|
}
|
|
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 formatEthernetState(int state) {
|
if (state == 1) {
|
return "开启";
|
} else if (state == 0) {
|
return "关闭";
|
} else if (state == -1) {
|
return "失败";
|
}
|
return "未知状态";
|
}
|
}
|