• 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/DumpsysPanel.java
1
package edu.jiangxin.apktoolbox.android.dumpsys;
2

3
import edu.jiangxin.apktoolbox.android.dumpsys.tojson.Alarm2Json;
4
import edu.jiangxin.apktoolbox.android.dumpsys.tojson.IDumpsys2Json;
5
import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
6
import edu.jiangxin.apktoolbox.swing.extend.filepanel.FilePanel;
7
import edu.jiangxin.apktoolbox.utils.Constants;
8
import org.apache.commons.exec.CommandLine;
9
import org.apache.commons.exec.DefaultExecutor;
10
import org.apache.commons.exec.PumpStreamHandler;
11
import org.apache.commons.io.FileUtils;
12
import org.apache.commons.io.FilenameUtils;
13
import org.apache.commons.lang3.StringUtils;
14
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
15
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
16
import org.fife.ui.rtextarea.RTextScrollPane;
17

18
import javax.swing.*;
19
import java.awt.*;
20
import java.awt.event.ActionEvent;
21
import java.awt.event.ActionListener;
22
import java.io.ByteArrayOutputStream;
23
import java.io.File;
24
import java.io.IOException;
25
import java.text.SimpleDateFormat;
26
import java.util.Date;
27

28
public class DumpsysPanel extends EasyPanel {
29
    private static final long serialVersionUID = 1L;
30

31
    private JTabbedPane tabbedPane;
32

33
    private JPanel dumpsysPanel;
34

35
    private FilePanel dumpsysTargetDirPanel;
36

37
    private JPanel dumpsysOptionPanel;
38
    private JPanel dumpsysTypeChoosePanel;
39

40
    private JPanel dumpsysOperationPanel;
41
    private JButton dumpsysStartButton;
42

43
    private JPanel analysisPanel;
44
    private FilePanel analysisFilePanel;
45
    private RSyntaxTextArea analysisOutputTextArea;
46
    private RTextScrollPane analysisOutputScrollPane;
47

48
    private JPanel analysisOperationPanel;
49

50

51
    private JButton analysisStartButton;
52

53
    public DumpsysPanel() {
54
        super();
×
55
    }
×
56

57
    @Override
58
    public void initUI() {
NEW
59
        setPreferredSize(new Dimension(700, 400));
×
60

NEW
61
        tabbedPane = new JTabbedPane();
×
NEW
62
        add(tabbedPane);
×
63

NEW
64
        createDumpsysPanel();
×
NEW
65
        tabbedPane.addTab("Dumpsys", null, dumpsysPanel, "Dumpsys");
×
66

NEW
67
        createAnalysisPanel();
×
NEW
68
        tabbedPane.addTab("Analysis", null, analysisPanel, "Analysis");
×
NEW
69
    }
×
70

71
    @Override
72
    public void afterPainted() {
NEW
73
        super.afterPainted();
×
NEW
74
        dumpsysTargetDirPanel.setPersistentKey(Constants.KEY_PREFIX + "dumpsysTargetDirPanel");
×
NEW
75
        analysisFilePanel.setPersistentKey(Constants.KEY_PREFIX + "dumpsysAnalysisFilePanel");
×
NEW
76
    }
×
77

78
    private void createDumpsysPanel() {
NEW
79
        dumpsysPanel = new JPanel();
×
NEW
80
        BoxLayout layout = new BoxLayout(dumpsysPanel, BoxLayout.Y_AXIS);
×
NEW
81
        dumpsysPanel.setLayout(layout);
×
NEW
82
        dumpsysPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
83

NEW
84
        createDumpsysTargetDirPanel();
×
NEW
85
        dumpsysPanel.add(dumpsysTargetDirPanel);
×
NEW
86
        dumpsysPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
87

NEW
88
        createDumpsysOptionPanel();
×
NEW
89
        dumpsysPanel.add(dumpsysOptionPanel);
×
NEW
90
        dumpsysPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
91

NEW
92
        createDumpsysOperationPanel();
×
NEW
93
        dumpsysPanel.add(dumpsysOperationPanel);
×
UNCOV
94
    }
×
95

96
    private void createAnalysisPanel() {
NEW
97
        analysisPanel = new JPanel();
×
NEW
98
        BoxLayout layout = new BoxLayout(analysisPanel, BoxLayout.Y_AXIS);
×
NEW
99
        analysisPanel.setLayout(layout);
×
NEW
100
        dumpsysPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
101

NEW
102
        createAnalysisTargetFilePanel();
×
NEW
103
        analysisPanel.add(analysisFilePanel);
×
NEW
104
        analysisPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
105

NEW
106
        createAnalysisOutputPanel();
×
NEW
107
        analysisPanel.add(analysisOutputScrollPane);
×
NEW
108
        analysisPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
109

NEW
110
        createAnalysisOperationPanel();
×
NEW
111
        analysisPanel.add(analysisOperationPanel);
×
NEW
112
    }
×
113

114
    private void createAnalysisTargetFilePanel() {
NEW
115
        analysisFilePanel = new FilePanel("Analysis File");
×
NEW
116
        analysisFilePanel.setFileSelectionMode(JFileChooser.FILES_ONLY);
×
UNCOV
117
    }
×
118

119
    private void createAnalysisOutputPanel() {
NEW
120
        analysisOutputTextArea = new RSyntaxTextArea();
×
NEW
121
        analysisOutputTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
×
NEW
122
        analysisOutputTextArea.setCodeFoldingEnabled(true);
×
NEW
123
        analysisOutputTextArea.setEditable(false);
×
124

NEW
125
        analysisOutputScrollPane = new RTextScrollPane(analysisOutputTextArea);
×
NEW
126
        analysisOutputScrollPane.setPreferredSize(new Dimension(400, 250));
×
NEW
127
    }
×
128

129
    private void createAnalysisOperationPanel() {
NEW
130
        analysisOperationPanel = new JPanel();
×
NEW
131
        BoxLayout boxLayout = new BoxLayout(analysisOperationPanel, BoxLayout.X_AXIS);
×
NEW
132
        analysisOperationPanel.setLayout(boxLayout);
×
133

NEW
134
        analysisStartButton = new JButton("Start");
×
NEW
135
        analysisStartButton.setEnabled(true);
×
NEW
136
        analysisStartButton.addActionListener(new AnalysisStartButtonActionListener());
×
NEW
137
        analysisOperationPanel.add(analysisStartButton);
×
NEW
138
    }
×
139

140
    private void createDumpsysTargetDirPanel() {
NEW
141
        dumpsysTargetDirPanel = new FilePanel("Target Directory");
×
NEW
142
        dumpsysTargetDirPanel.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
×
NEW
143
    }
×
144

145
    private void createDumpsysOptionPanel() {
NEW
146
        dumpsysOptionPanel = new JPanel();
×
NEW
147
        BoxLayout boxLayout = new BoxLayout(dumpsysOptionPanel, BoxLayout.Y_AXIS);
×
NEW
148
        dumpsysOptionPanel.setLayout(boxLayout);
×
149

NEW
150
        JPanel dumpsysCheckboxPanel = new JPanel();
×
NEW
151
        dumpsysCheckboxPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
×
152

NEW
153
        JCheckBox allSelectedCheckBox = new JCheckBox("Select All");
×
NEW
154
        allSelectedCheckBox.setSelected(false);
×
NEW
155
        allSelectedCheckBox.addActionListener(e -> {
×
NEW
156
            boolean selected = allSelectedCheckBox.isSelected();
×
NEW
157
            if (selected) {
×
NEW
158
                Component[] components = dumpsysTypeChoosePanel.getComponents();
×
NEW
159
                for (Component component : components) {
×
NEW
160
                    if (component instanceof JCheckBox checkBox) {
×
NEW
161
                        checkBox.setSelected(true);
×
NEW
162
                        checkBox.setEnabled(false);
×
163
                    }
164
                }
NEW
165
            } else {
×
NEW
166
                Component[] components = dumpsysTypeChoosePanel.getComponents();
×
NEW
167
                for (Component component : components) {
×
NEW
168
                    if (component instanceof JCheckBox checkBox) {
×
NEW
169
                        checkBox.setSelected(false);
×
NEW
170
                        checkBox.setEnabled(true);
×
171
                    }
172
                }
173
            }
174

NEW
175
        });
×
NEW
176
        dumpsysCheckboxPanel.add(allSelectedCheckBox);
×
177

178
        dumpsysTypeChoosePanel = new JPanel();
×
179
        dumpsysTypeChoosePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
×
NEW
180
        dumpsysTypeChoosePanel.setPreferredSize(new Dimension(500, 200));
×
181

182
        JCheckBox inputCheckBox = new JCheckBox("input");
×
183
        inputCheckBox.setSelected(true);
×
184
        JCheckBox windowCheckBox = new JCheckBox("window");
×
185
        dumpsysTypeChoosePanel.add(windowCheckBox);
×
186
        JCheckBox SurfaceFlingerCheckBox = new JCheckBox("SurfaceFlinger");
×
187
        dumpsysTypeChoosePanel.add(SurfaceFlingerCheckBox);
×
188
        JCheckBox activityCheckBox = new JCheckBox("activity");
×
189
        dumpsysTypeChoosePanel.add(activityCheckBox);
×
190
        JCheckBox accessibilityCheckBox = new JCheckBox("accessibility");
×
191
        dumpsysTypeChoosePanel.add(accessibilityCheckBox);
×
192
        JCheckBox packageCheckBox = new JCheckBox("package");
×
193
        dumpsysTypeChoosePanel.add(packageCheckBox);
×
194
        JCheckBox alarmCheckBox = new JCheckBox("alarm");
×
195
        dumpsysTypeChoosePanel.add(alarmCheckBox);
×
196
        JCheckBox inputMethodCheckBox = new JCheckBox("input_method");
×
197
        dumpsysTypeChoosePanel.add(inputMethodCheckBox);
×
198
        JCheckBox sensorServiceCheckBox = new JCheckBox("sensorservice");
×
199
        dumpsysTypeChoosePanel.add(sensorServiceCheckBox);
×
200
        JCheckBox accountCheckBox = new JCheckBox("account");
×
201
        dumpsysTypeChoosePanel.add(accountCheckBox);
×
202
        JCheckBox displayCheckBox = new JCheckBox("display");
×
203
        dumpsysTypeChoosePanel.add(displayCheckBox);
×
204
        JCheckBox powerCheckBox = new JCheckBox("power");
×
205
        dumpsysTypeChoosePanel.add(powerCheckBox);
×
206
        JCheckBox batteryCheckBox = new JCheckBox("battery");
×
207
        dumpsysTypeChoosePanel.add(batteryCheckBox);
×
208

NEW
209
        dumpsysOptionPanel.add(dumpsysCheckboxPanel);
×
NEW
210
        dumpsysOptionPanel.add(new JSeparator(JSeparator.HORIZONTAL));
×
NEW
211
        dumpsysOptionPanel.add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
×
NEW
212
        dumpsysOptionPanel.add(dumpsysTypeChoosePanel);
×
UNCOV
213
    }
×
214

215
    private void createDumpsysOperationPanel() {
NEW
216
        dumpsysOperationPanel = new JPanel();
×
NEW
217
        BoxLayout boxLayout = new BoxLayout(dumpsysOperationPanel, BoxLayout.X_AXIS);
×
NEW
218
        dumpsysOperationPanel.setLayout(boxLayout);
×
219

NEW
220
        dumpsysStartButton = new JButton("Start");
×
NEW
221
        dumpsysStartButton.setEnabled(true);
×
NEW
222
        dumpsysStartButton.addActionListener(new DumpsysStartButtonActionListener());
×
NEW
223
        dumpsysOperationPanel.add(dumpsysStartButton);
×
UNCOV
224
    }
×
225

NEW
226
    class DumpsysStartButtonActionListener implements ActionListener {
×
227
        @Override
228
        public void actionPerformed(ActionEvent e) {
NEW
229
            dumpsysStartButton.setEnabled(false);
×
NEW
230
            File targetDirFile = dumpsysTargetDirPanel.getFile();
×
231
            if (!targetDirFile.exists()) {
×
232
                JOptionPane.showMessageDialog(getFrame(), "Target directory does not exist!", "ERROR", JOptionPane.ERROR_MESSAGE);
×
233
                return;
×
234
            }
235

236
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(System.currentTimeMillis()));
×
237
            File targetDirWithTimestamp = new File(targetDirFile, timeStamp);
×
238

239
            if (!targetDirWithTimestamp.exists()) {
×
240
                boolean success = targetDirWithTimestamp.mkdirs();
×
241
                if (!success) {
×
242
                    JOptionPane.showMessageDialog(getFrame(), "Create target directory failed!", "ERROR", JOptionPane.ERROR_MESSAGE);
×
243
                    return;
×
244
                }
245
            }
246

247
            Component[] components = dumpsysTypeChoosePanel.getComponents();
×
248
            for (Component component : components) {
×
249
                if (component instanceof JCheckBox checkBox) {
×
250
                    String dumpsysType = checkBox.getText();
×
251
                    if (checkBox.isSelected()) {
×
252
                        dumpsysAndSaveToFile(dumpsysType, targetDirWithTimestamp);
×
253
                    }
254
                }
255
            }
NEW
256
            dumpsysStartButton.setEnabled(true);
×
257
        }
×
258

259
        private void dumpsysAndSaveToFile(String dumpsysType, File targetDir) {
260
            String command = "adb shell dumpsys " + dumpsysType;
×
261
            CommandLine cmdLine = CommandLine.parse(command);
×
262
            DefaultExecutor executor = new DefaultExecutor();
×
263
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
×
264
                 ByteArrayOutputStream errorStream = new ByteArrayOutputStream();) {
×
265
                PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
×
266
                executor.setStreamHandler(streamHandler);
×
267
                int exitValue = executor.execute(cmdLine);
×
268
                if (exitValue == 0) {
×
269
                    String result = outputStream.toString().trim();
×
270
                    if (StringUtils.isNotEmpty(result)) {
×
271
                        File outputFile = new File(targetDir, dumpsysType + ".dumpsys");
×
272
                        FileUtils.writeStringToFile(outputFile, result, "UTF-8");
×
273
                    }
274
                } else {
×
275
                    String errorResult = errorStream.toString().trim();
×
276
                    if (StringUtils.isNotEmpty(errorResult)) {
×
277
                        File errorFile = new File(targetDir, dumpsysType + ".error");
×
278
                        FileUtils.writeStringToFile(errorFile, errorResult, "UTF-8");
×
279
                    }
280
                }
281
            } catch (IOException ex) {
×
282
                JOptionPane.showMessageDialog(getFrame(), "Execute command failed:\n" + ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
×
283
            }
×
284
        }
×
285
    }
286

NEW
287
    class AnalysisStartButtonActionListener implements ActionListener {
×
288

289
        @Override
290
        public void actionPerformed(ActionEvent e) {
NEW
291
            File analysisFile = analysisFilePanel.getFile();
×
NEW
292
            if (!analysisFile.exists()) {
×
NEW
293
                JOptionPane.showMessageDialog(getFrame(), "Analysis file does not exist!", "ERROR", JOptionPane.ERROR_MESSAGE);
×
NEW
294
                return;
×
295
            }
NEW
296
            String fileNameWithoutExtension = FilenameUtils.getBaseName(analysisFile.getName());
×
NEW
297
            String className = StringUtils.capitalize(fileNameWithoutExtension) + "2Json";
×
NEW
298
            IDumpsys2Json dumpsys2Json = getDumpsys2JsonInstance(className);
×
NEW
299
            if (dumpsys2Json != null) {
×
NEW
300
                String jsonResult = dumpsys2Json.dumpsys2Json(analysisFile);
×
NEW
301
                analysisOutputTextArea.setText(jsonResult);
×
NEW
302
                analysisOutputTextArea.setCaretPosition(0);
×
303
            }
NEW
304
        }
×
305

306
        private IDumpsys2Json getDumpsys2JsonInstance(String className) {
307
            try {
NEW
308
                String fullClassName = "edu.jiangxin.apktoolbox.android.dumpsys.tojson." + className;
×
NEW
309
                Class<?> clazz = Class.forName(fullClassName);
×
NEW
310
                if (IDumpsys2Json.class.isAssignableFrom(clazz)) {
×
NEW
311
                    return (IDumpsys2Json) clazz.getDeclaredConstructor().newInstance();
×
312
                } else {
NEW
313
                    JOptionPane.showMessageDialog(getFrame(), "Class " + fullClassName + " does not implement IDumpsys2Json!", "ERROR", JOptionPane.ERROR_MESSAGE);
×
NEW
314
                    return null;
×
315
                }
NEW
316
            } catch (ClassNotFoundException e) {
×
NEW
317
                JOptionPane.showMessageDialog(getFrame(), "Class not found: " + className, "ERROR", JOptionPane.ERROR_MESSAGE);
×
NEW
318
                return null;
×
NEW
319
            } catch (Exception e) {
×
NEW
320
                JOptionPane.showMessageDialog(getFrame(), "Error loading class: " + e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
×
NEW
321
                return null;
×
322
            }
323
        }
324
    }
325
}
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