• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jiangxincode / ApkToolBoxGUI / #1164

06 Sep 2025 05:03AM UTC coverage: 2.891% (-0.06%) from 2.955%
#1164

push

jiangxincode
[Feature]Optimization of DumpsysPanel

0 of 215 new or added lines in 2 files covered. (0.0%)

4 existing lines in 1 file now uncovered.

248 of 8579 relevant lines covered (2.89%)

0.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/jiangxin/apktoolbox/android/dumpsys/tojson/Alarm2Json.java
1
package edu.jiangxin.apktoolbox.android.dumpsys.tojson;
2

3
import java.io.File;
4
import java.io.IOException;
5
import java.nio.file.Files;
6
import java.util.*;
7
import java.util.regex.Matcher;
8
import java.util.regex.Pattern;
9
import org.json.JSONArray;
10
import org.json.JSONObject;
11

NEW
12
public class Alarm2Json implements IDumpsys2Json {
×
13
    @Override
14
    public String dumpsys2Json(File file) {
15
        String content;
16
        try {
NEW
17
            content = new String(Files.readAllBytes(file.toPath()));
×
NEW
18
        } catch (IOException e) {
×
NEW
19
            throw new RuntimeException("读取文件失败: " + file.getAbsolutePath(), e);
×
NEW
20
        }
×
NEW
21
        JSONObject result = new JSONObject();
×
NEW
22
        parseSections(content, result);
×
NEW
23
        return result.toString(2);
×
24
    }
25

26
    private void parseSections(String content, JSONObject result) {
27
        // 以两个换行分割各大段
NEW
28
        String[] sections = content.split("\n\\s*\n");
×
NEW
29
        for (String section : sections) {
×
NEW
30
            String title = getSectionTitle(section);
×
NEW
31
            if (title != null) {
×
NEW
32
                Object value = parseSectionContent(title, section);
×
NEW
33
                result.put(title, value);
×
34
            }
35
        }
NEW
36
    }
×
37

38
    private String getSectionTitle(String section) {
NEW
39
        Matcher m = Pattern.compile("^\\s*([\\w .-]+):", Pattern.MULTILINE).matcher(section);
×
NEW
40
        if (m.find()) {
×
NEW
41
            return m.group(1).trim();
×
42
        }
NEW
43
        return null;
×
44
    }
45

46
    private Object parseSectionContent(String title, String section) {
NEW
47
        if (title.equals("Settings") || title.equals("Feature Flags")) {
×
NEW
48
            return parseKeyValueSection(section);
×
NEW
49
        } else if (title.equals("Current AppStateTracker State")) {
×
NEW
50
            return parseAppStateSection(section);
×
NEW
51
        } else if (title.contains("pending alarms")) {
×
NEW
52
            return parsePendingAlarms(section);
×
53
        } else {
NEW
54
            return section.trim();
×
55
        }
56
    }
57

58
    private JSONObject parseKeyValueSection(String section) {
NEW
59
        JSONObject obj = new JSONObject();
×
NEW
60
        Matcher m = Pattern.compile("^\\s*([\\w._-]+)=(.+)$", Pattern.MULTILINE).matcher(section);
×
NEW
61
        while (m.find()) {
×
NEW
62
            obj.put(m.group(1).trim(), m.group(2).trim());
×
63
        }
NEW
64
        return obj;
×
65
    }
66

67
    private JSONObject parseAppStateSection(String section) {
NEW
68
        JSONObject obj = new JSONObject();
×
NEW
69
        Matcher m = Pattern.compile("^\\s*([\\w .-]+): (.+)$", Pattern.MULTILINE).matcher(section);
×
NEW
70
        while (m.find()) {
×
NEW
71
            String key = m.group(1).trim();
×
NEW
72
            String value = m.group(2).trim();
×
NEW
73
            if (value.startsWith("[") && value.endsWith("]")) {
×
NEW
74
                value = value.substring(1, value.length() - 1);
×
NEW
75
                JSONArray arr = new JSONArray();
×
NEW
76
                for (String v : value.split(", ?")) {
×
NEW
77
                    if (!v.isEmpty()) arr.put(v.trim());
×
78
                }
NEW
79
                obj.put(key, arr);
×
NEW
80
            } else {
×
NEW
81
                obj.put(key, value);
×
82
            }
NEW
83
        }
×
NEW
84
        Matcher userMatcher = Pattern.compile("User (\\d+)\\n([\\s\\S]+?)(?=\\n\\s*\\w|$)").matcher(section);
×
NEW
85
        while (userMatcher.find()) {
×
NEW
86
            String userKey = "User " + userMatcher.group(1);
×
NEW
87
            String pkgs = userMatcher.group(2);
×
NEW
88
            JSONArray pkgArr = new JSONArray();
×
NEW
89
            for (String line : pkgs.split("\n")) {
×
NEW
90
                line = line.trim();
×
NEW
91
                if (!line.isEmpty()) pkgArr.put(line);
×
92
            }
NEW
93
            obj.put(userKey, pkgArr);
×
NEW
94
        }
×
NEW
95
        return obj;
×
96
    }
97

98
    private JSONArray parsePendingAlarms(String section) {
NEW
99
        JSONArray alarms = new JSONArray();
×
100
        // 修正正则,支持前面有空格,且允许行首不是严格的alarm类型
NEW
101
        Pattern alarmStartPattern = Pattern.compile("^\s*(ELAPSED|ELAPSED_WAKEUP|RTC|RTC_WAKEUP) #(\\d+): (Alarm\\{.*?)(?=(^\\s*(ELAPSED|ELAPSED_WAKEUP|RTC|RTC_WAKEUP) #(\\d+):)|(\\s{2}))", Pattern.MULTILINE | Pattern.DOTALL);
×
NEW
102
        Matcher m = alarmStartPattern.matcher(section);
×
NEW
103
        while (m.find()) {
×
NEW
104
            JSONObject alarm = new JSONObject();
×
NEW
105
            alarm.put("type", m.group(1));
×
NEW
106
            alarm.put("number", m.group(2));
×
NEW
107
            String details = m.group(3).trim();
×
NEW
108
            String[] tmpArray = details.split("\\n", 2);
×
NEW
109
            alarm.put("alarmInfo", tmpArray[0]);
×
NEW
110
            details = details.replace(tmpArray[0], "");
×
111
            // 提取 tag、type、origWhen、window、repeatInterval、count、flags、operation、listener、idle-options 等字段
NEW
112
            Pattern fieldPattern = Pattern.compile(
×
113
                "tag=([^\n]+)|type=([^\\s]+)|origWhen=([^\\s]+)|window=([^\\s]+)|repeatInterval=([^\\s]+)|count=([^\\s]+)|flags=([^\\s]+)|operation=([^\n]+)|listener=([^\n]+)|idle-options=([^\n]+)",
114
                Pattern.MULTILINE);
NEW
115
            Matcher fieldMatcher = fieldPattern.matcher(details);
×
NEW
116
            while (fieldMatcher.find()) {
×
NEW
117
                if (fieldMatcher.group(1) != null) alarm.put("tag", fieldMatcher.group(1).trim());
×
NEW
118
                if (fieldMatcher.group(2) != null) alarm.put("alarmType", fieldMatcher.group(2).trim());
×
NEW
119
                if (fieldMatcher.group(3) != null) alarm.put("origWhen", fieldMatcher.group(3).trim());
×
NEW
120
                if (fieldMatcher.group(4) != null) alarm.put("window", fieldMatcher.group(4).trim());
×
NEW
121
                if (fieldMatcher.group(5) != null) alarm.put("repeatInterval", fieldMatcher.group(5).trim());
×
NEW
122
                if (fieldMatcher.group(6) != null) alarm.put("count", fieldMatcher.group(6).trim());
×
NEW
123
                if (fieldMatcher.group(7) != null) alarm.put("flags", fieldMatcher.group(7).trim());
×
NEW
124
                if (fieldMatcher.group(8) != null) alarm.put("operation", fieldMatcher.group(8).trim());
×
NEW
125
                if (fieldMatcher.group(9) != null) alarm.put("listener", fieldMatcher.group(9).trim());
×
NEW
126
                if (fieldMatcher.group(10) != null) alarm.put("idleOptions", fieldMatcher.group(10).trim());
×
127
            }
128
            // 额外提取 policyWhenElapsed、whenElapsed、maxWhenElapsed、exactAllowReason、procName、PendingIntent、等
NEW
129
            Pattern extraPattern = Pattern.compile(
×
130
                "policyWhenElapsed: ([^\n]+)|whenElapsed=([^\\s]+)|maxWhenElapsed=([^\\s]+)|exactAllowReason=([^\\s]+)|procName ([^\\s]+)|PendingIntent\\{([^:]+): ([^}]+)\\}",
131
                Pattern.MULTILINE);
NEW
132
            Matcher extraMatcher = extraPattern.matcher(details);
×
NEW
133
            while (extraMatcher.find()) {
×
NEW
134
                if (extraMatcher.group(1) != null) alarm.put("policyWhenElapsed", extraMatcher.group(1).trim());
×
NEW
135
                if (extraMatcher.group(2) != null) alarm.put("whenElapsed", extraMatcher.group(2).trim());
×
NEW
136
                if (extraMatcher.group(3) != null) alarm.put("maxWhenElapsed", extraMatcher.group(3).trim());
×
NEW
137
                if (extraMatcher.group(4) != null) alarm.put("exactAllowReason", extraMatcher.group(4).trim());
×
NEW
138
                if (extraMatcher.group(5) != null) alarm.put("procName", extraMatcher.group(5).trim());
×
NEW
139
                if (extraMatcher.group(6) != null && extraMatcher.group(7) != null) {
×
NEW
140
                    alarm.put("pendingIntent", extraMatcher.group(7).trim());
×
141
                }
142
            }
NEW
143
            alarm.put("rawDetails", details); // 保留原始内容
×
NEW
144
            alarms.put(alarm);
×
NEW
145
        }
×
NEW
146
        return alarms;
×
147
    }
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc