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
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.List;
import android.app.AlertDialog;
import android.os.PowerManager;
import android.widget.Toast;
 
public class DisplayActivity extends BaseTestActivity {
    private static final String SERVICE_NOT_FOUND_MESSAGE = "未获取到 ZysjSystemManager 系统服务";
 
    private ZysjSystemManager zysjSystemManager;
 
    @Override
    protected String getPageTitle() {
        return "显示控制接口测试";
    }
 
    @Override
    protected List<TestItem> getTestItems() {
        return Arrays.asList(
                new TestItem(
                        "控制系统状态栏",
                        "zYSystemStatusBar",
                        "cmd=0:状态栏可下拉,cmd=1:状态栏无法下拉,cmd=2:获取状态",
                        this::systemStatusBar,
                        TestItem.InputField.number(
                                "请输入 cmd:0=状态栏可下拉,1=状态栏无法下拉,2=获取状态",
                                "2",
                                "cmd")),
                new TestItem(
                        "控制系统导航栏",
                        "zYSystemNavigationBar",
                        "cmd=0:导航栏显示,cmd=1:导航栏隐藏,cmd=2:获取状态",
                        this::systemNavigationBar,
                        TestItem.InputField.number(
                                "请输入 cmd:0=导航栏显示,1=导航栏隐藏,2=获取状态",
                                "2",
                                "cmd")),
                new TestItem(
                        "控制系统显示方向",
                        "zYsetScreenDirection",
                        "rotate=0:0度,rotate=1:90度,rotate=2:180度,rotate=3:270度",
                        this::setScreenDirection,
                        TestItem.InputField.number(
                                "请输入 rotate:0=0度,1=90度,2=180度,3=270度",
                                "0",
                                "rotate")),
                new TestItem(
                        "获取系统显示方向",
                        "zYgetScreenDirection",
                        "读取当前系统显示方向",
                        this::getScreenDirection),
                new TestItem(
                        "控制系统背光",
                        "zYsetBackLight",
                        "参数范围 0~255",
                        this::setBackLight,
                        TestItem.InputField.number(
                                "请输入背光值:0~255",
                                "80",
                                "backageLight")),
                new TestItem(
                        "获取系统背光",
                        "zYgetBackLight",
                        "读取当前系统背光值",
                        this::getBackLight)
        );
    }
 
    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 systemStatusBar(TestItem item, String[] inputs) {
        int cmd = parseInt(inputs, 0, 0, 2, "cmd 只能是 0、1、2");
        int result = zysjSystemManager().zYSystemStatusBar(cmd);
        return "调用成功:zYSystemStatusBar(" + cmd + ") 返回值:" + result;
    }
 
    private String systemNavigationBar(TestItem item, String[] inputs) {
        int cmd = parseInt(inputs, 0, 0, 2, "cmd 只能是 0、1、2");
        int result = zysjSystemManager().zYSystemNavigationBar(cmd);
        return "调用成功:zYSystemNavigationBar(" + cmd + ") 返回值:" + result;
    }
 
    private String setScreenDirection(TestItem item, String[] inputs) {
        int rotate = parseInt(inputs, 0, 0, 3, "rotate 只能是 0、1、2、3");
        int result = zysjSystemManager().zYsetScreenDirection(rotate);
 
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showRebootConfirmDialog();
            }
        });
 
        return "调用成功:zYsetScreenDirection(" + rotate + ") 返回值:" + result;
    }
    private void showRebootConfirmDialog() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
 
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("旋转方向设置完成,重启后生效,是否立即重启?")
                .setPositiveButton("是", (dialog, which) -> {
                    dialog.dismiss();
                    rebootDevice();
                })
                .setNegativeButton("否", (dialog, which) -> {
                    dialog.dismiss();
                })
                .show();
    }
    private void rebootDevice() {
        try {
            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (powerManager != null) {
                powerManager.reboot(null);
            } else {
                Toast.makeText(this, "重启失败:PowerManager 为空", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "重启失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    private String getScreenDirection(TestItem item, String[] inputs) {
        int result = zysjSystemManager().zYgetScreenDirection();
        return "调用成功:zYgetScreenDirection() 返回值:" + result;
    }
 
    private String setBackLight(TestItem item, String[] inputs) {
        int backLight = parseInt(inputs, 0, 0, 255, "背光值只能是 0~255");
        int result = zysjSystemManager().zYsetBackLight(backLight);
        return "调用成功:zYsetBackLight(" + backLight + ") 返回值:" + result;
    }
 
    private String getBackLight(TestItem item, String[] inputs) {
        int result = zysjSystemManager().zYgetBackLight();
        return "调用成功:zYgetBackLight() 返回值:" + result;
    }
 
    private int parseInt(String[] inputs, int index, int minValue, int maxValue, String errorMessage) {
        if (index >= inputs.length) {
            throw new IllegalArgumentException(errorMessage);
        }
        try {
            int value = Integer.parseInt(inputs[index].trim());
            if (value < minValue || value > maxValue) {
                throw new IllegalArgumentException(errorMessage);
            }
            return value;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(errorMessage);
        }
    }
}