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);
|
}
|
}
|
}
|