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
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 ? "安装成功" : "安装失败或返回异常";
    }
}