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;
|
|
public class AppControlActivity 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(
|
"应用静默安装",
|
"installAPK",
|
"filePath:APK 文件路径,例如 /sdcard/test.apk。",
|
this::installApk,
|
TestItem.InputField.text(
|
"请输入 APK 路径",
|
"/sdcard/test.apk",
|
"filePath"))
|
);
|
}
|
|
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 installApk(TestItem item, String[] inputs) {
|
String filePath = requireInput(inputs, 0, "filePath");
|
int result = zysjSystemManager().installAPK(filePath);
|
return "调用成功:installAPK(" + filePath + ") 返回值:" + result + "," + formatInstallResult(result);
|
}
|
|
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 formatInstallResult(int result) {
|
return result == 0 ? "安装成功" : "安装失败或返回异常";
|
}
|
}
|