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

jiangxincode / ApkToolBoxGUI / #1114

26 Jul 2025 12:05PM UTC coverage: 3.079% (+0.002%) from 3.077%
#1114

push

jiangxincode
fix #543: add error tips

0 of 23 new or added lines in 8 files covered. (0.0%)

1 existing line in 1 file now uncovered.

236 of 7664 relevant lines covered (3.08%)

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/utils/Utils.java
1
package edu.jiangxin.apktoolbox.utils;
2

3
import org.apache.commons.configuration2.Configuration;
4
import org.apache.commons.configuration2.FileBasedConfiguration;
5
import org.apache.commons.configuration2.PropertiesConfiguration;
6
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
7
import org.apache.commons.configuration2.builder.fluent.Parameters;
8
import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
9
import org.apache.commons.configuration2.ex.ConfigurationException;
10
import org.apache.commons.exec.*;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.logging.log4j.Level;
13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
15

16
import javax.swing.*;
17
import java.awt.*;
18
import java.io.*;
19

20
/**
21
 * @author jiangxin
22
 * @author 2018-08-19
23
 *
24
 */
25
public class Utils {
×
26
    private static final Logger logger = LogManager.getLogger(Utils.class.getSimpleName());
×
27

28
    private static String userConfigPath;
29

30
    private static String userPluginDirPath;
31

32
    private static FileBasedConfigurationBuilder<FileBasedConfiguration> builder;
33

34
    public static String getPluginDirPath() {
35
        return userPluginDirPath;
×
36
    }
37

38
    public static boolean checkAndInitEnvironment() {
39
        boolean ret = initUserDir();
×
40
        if (!ret) {
×
41
            return false;
×
42
        }
43
        SystemInfoUtils.logSystemInfo();
×
44
        return true;
×
45
    }
46

47
    private static boolean initUserDir() {
48
        String userHomePath = System.getProperty("user.home");
×
49
        String userDirPath;
50
        if (StringUtils.isEmpty(userHomePath)) {
×
51
            logger.error("user.home is empty");
×
52
            userDirPath = ".apktoolboxgui";
×
53
        } else {
54
            userDirPath = userHomePath + File.separator + ".apktoolboxgui";
×
55
        }
56
        File userDirFile = new File(userDirPath);
×
57
        if (!userDirFile.exists()) {
×
58
            logger.info("userDirFile does not exist");
×
59
            boolean ret = userDirFile.mkdir();
×
60
            if (!ret) {
×
61
                logger.error("mkdir failed: {}", userDirPath);
×
62
                return false;
×
63
            }
64
        }
65
        userConfigPath = userDirPath + File.separator + "apktoolboxgui.properties";
×
66
        File userConfigFile = new File(userConfigPath);
×
67
        if (!userConfigFile.exists()) {
×
68
            try {
69
                logger.info("userConfigFile does not exist");
×
70
                boolean ret = userConfigFile.createNewFile();
×
71
                if (!ret) {
×
72
                    logger.error("createNewFile fail: {}", userConfigPath);
×
73
                    return false;
×
74
                }
75
            } catch (IOException e) {
×
76
                logger.error("createNewFile fail", e);
×
77
            }
×
78
        }
79
        userPluginDirPath = userDirPath + File.separator + "plugins";
×
80
        File userPluginDirFile = new File(userPluginDirPath);
×
81
        if (!userPluginDirFile.exists()) {
×
82
            logger.info("userPluginDirFile does not exist");
×
83
            boolean ret = userPluginDirFile.mkdir();
×
84
            if (!ret) {
×
85
                logger.error("mkdir failed: {}", userPluginDirPath);
×
86
                return false;
×
87
            }
88
        }
89
        return true;
×
90
    }
91

92
    public static Configuration getConfiguration() {
93
        if (builder == null) {
×
94
            logger.info("builder is null, create it");
×
95
            File configFile = new File(userConfigPath);
×
96
            Parameters params = new Parameters();
×
97
            PropertiesBuilderParameters parameters = params.properties().setFile(configFile);
×
98
            builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
×
99
                    .configure(parameters);
×
100
        }
101
        Configuration conf = null;
×
102
        try {
103
            conf = builder.getConfiguration();
×
104
        } catch (ConfigurationException e) {
×
105
            logger.error("getConfiguration error", e);
×
106
        }
×
107
        return conf;
×
108
    }
109

110
    public static void saveConfiguration() {
111
        try {
112
            if (builder == null) {
×
113
                logger.info("builder is null");
×
114
                return;
×
115
            }
116
            builder.save();
×
117
            logger.info("saveConfiguration success");
×
118
        } catch (ConfigurationException e) {
×
119
            logger.error("saveConfiguration error", e);
×
120
        }
×
121
    }
×
122

123
    public static String getToolsPath() {
124
        String tmp = Utils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
×
125
        if (StringUtils.isEmpty(tmp)) {
×
126
            logger.error("tmp is empty");
×
127
            return null;
×
128
        }
129
        File file = new File(tmp);
×
130
        if (!file.exists()) {
×
131
            logger.error("file does not exist");
×
132
            return null;
×
133
        }
134
        while (file.getParentFile().exists()) {
×
135
            File parent = file.getParentFile();
×
136
            File tools = new File(parent, "tools");
×
137
            if (tools.exists()) {
×
138
                try {
139
                    return tools.getCanonicalPath();
×
140
                } catch (IOException e) {
×
141
                    logger.error("getCanonicalPath fail");
×
142
                    return null;
×
143
                }
144
            }
145
            file = file.getParentFile();
×
146
        }
×
147

148
        return null;
×
149

150
    }
151
    
152
    public static String getFrameTitle(JComponent component) {
153
        Container container = component.getParent();
×
154
        while(container != null) {
×
155
            if (container instanceof JFrame) {
×
156
                return ((JFrame) container).getTitle();
×
157
            }
158
            container = container.getParent();
×
159
        }
160
        return "";
×
161
    }
162
    
163
    public static void setFrameTitle(JComponent component, String title) {
164
        Container container = component.getParent();
×
165
        while(container != null) {
×
166
            if (container instanceof JFrame) {
×
167
                ((JFrame) container).setTitle(title);
×
168
                return;
×
169
            }
170
            container = container.getParent();
×
171
        }
172
    }
×
173

174
    public static void executor(String cmd, boolean isBlocked) {
175
        logger.info(cmd);
×
176
        try (ProcessLogOutputStream outStream = new ProcessLogOutputStream(logger, Level.INFO);
×
177
             ProcessLogOutputStream errStream = new ProcessLogOutputStream(logger, Level.ERROR)
×
178
        ) {
179
            CommandLine commandLine = CommandLine.parse(cmd);
×
180
            DefaultExecutor exec = new DefaultExecutor();
×
181
            PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
×
182
            exec.setStreamHandler(streamHandler);
×
NEW
183
            if (isBlocked) {
×
NEW
184
                int exitValue = exec.execute(commandLine);
×
NEW
185
                logger.info("exitValue: [" + exitValue + "]");
×
NEW
186
            } else {
×
NEW
187
                DefaultExecuteResultHandler erh = new DefaultExecuteResultHandler() {
×
188
                    @Override
189
                    public void onProcessComplete(int exitValue) {
NEW
190
                        logger.info("complete: [" + cmd + "], exitValue: [" + exitValue + "]");
×
NEW
191
                    }
×
192

193
                    @Override
194
                    public void onProcessFailed(ExecuteException ee) {
NEW
195
                        logger.info("failed: [" + cmd + "], execute exception: [" + ee.getMessage() + "]");
×
NEW
196
                    }
×
197
                };
NEW
198
                exec.execute(commandLine, erh);
×
199
            }
200

UNCOV
201
        } catch (IOException ioe) {
×
NEW
202
            logger.error("exec fail: {}", ioe.getMessage());
×
NEW
203
            JOptionPane.showMessageDialog(null, ioe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
×
204
        }
×
205
    }
×
206

207
    public static int getFileLineCount(File file) {
208
        try (LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file))) {
×
209
            while (lineNumberReader.skip(Long.MAX_VALUE) > 0) {
×
210
                logger.info("getFileLineCount skip " + Long.MAX_VALUE + " characters");
×
211
            }
212
            return lineNumberReader.getLineNumber();
×
213
        } catch (IOException e) {
×
214
            logger.error("getFileLineCount IOException");
×
215
            return 0;
×
216
        }
217
    }
218
}
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