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
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<TestItem> 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<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 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<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().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() ? "获取失败或返回为空" : "获取成功";
    }
}