lzh
3 days ago 9e3ee0fb3332f604000c20863cece6a09ad23f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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 "未知状态";
    }
}