• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

jiangxincode / ApkToolBoxGUI / #1047

10 May 2025 05:21AM UTC coverage: 3.081% (-0.03%) from 3.109%
#1047

push

jiangxincode
fix #41: finish feature: add to startup function

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

236 of 7659 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/help/settings/AddToStartupPanel.java
1
package edu.jiangxin.apktoolbox.help.settings;
2

3
import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel;
4
import edu.jiangxin.apktoolbox.utils.Constants;
5
import org.apache.commons.collections4.CollectionUtils;
6
import org.apache.commons.io.FileUtils;
7

8
import javax.swing.*;
9
import java.io.File;
10
import java.io.IOException;
11
import java.net.URL;
12
import java.nio.file.Files;
13
import java.util.Collection;
14

NEW
15
public class AddToStartupPanel extends EasyChildTabbedPanel {
×
16

17
    private static final String STARTUP_FILE;
18

19
    private JPanel optionPanel;
20

21
    static {
NEW
22
        final String SEPARATOR = File.separator;
×
23
        final String STARTUP_FOLDER;
NEW
24
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
×
NEW
25
            STARTUP_FOLDER = System.getenv("APPDATA") + SEPARATOR + "Microsoft" + SEPARATOR + "Windows" + SEPARATOR + "Start Menu" + SEPARATOR + "Programs" + SEPARATOR + "Startup";
×
NEW
26
        } else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
×
NEW
27
            STARTUP_FOLDER = System.getProperty("user.home") + SEPARATOR + "Library" + SEPARATOR + "LaunchAgents";
×
28
        } else {
NEW
29
            STARTUP_FOLDER = System.getProperty("user.home") + SEPARATOR + ".config" + SEPARATOR + "autostart";
×
30
        }
NEW
31
        STARTUP_FILE = STARTUP_FOLDER + SEPARATOR + "ApkToolBoxGUI.lnk";
×
NEW
32
    }
×
33

34
    @Override
35
    public void createUI() {
NEW
36
        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
×
NEW
37
        setLayout(boxLayout);
×
38

NEW
39
        createOptionPanel();
×
NEW
40
        add(optionPanel);
×
41

NEW
42
        add(Box.createVerticalStrut(15 * Constants.DEFAULT_Y_BORDER));
×
NEW
43
    }
×
44

45
    private void createOptionPanel() {
NEW
46
        optionPanel = new JPanel();
×
NEW
47
        optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
×
48

NEW
49
        JLabel typeLabel = new JLabel("Add to startup:");
×
NEW
50
        JCheckBox addToStartupCheckBox = new JCheckBox();
×
NEW
51
        addToStartupCheckBox.setSelected(conf.getBoolean("add.to.startup", false));
×
NEW
52
        addToStartupCheckBox.addActionListener(e -> {
×
NEW
53
            conf.setProperty("add.to.startup", addToStartupCheckBox.isSelected());
×
NEW
54
            if (addToStartupCheckBox.isSelected()) {
×
NEW
55
                addToStartup();
×
56
            } else {
NEW
57
                FileUtils.deleteQuietly(new File(STARTUP_FILE));
×
58
            }
NEW
59
        });
×
60

NEW
61
        optionPanel.add(typeLabel);
×
NEW
62
        optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
×
NEW
63
        optionPanel.add(addToStartupCheckBox);
×
NEW
64
    }
×
65

66
    private void addToStartup() {
NEW
67
        String executableFilePath = getExecutableFilePath();
×
NEW
68
        if (executableFilePath == null) {
×
NEW
69
            return;
×
70
        }
71

NEW
72
        File shortcutFile = new File(STARTUP_FILE);
×
73

NEW
74
        if (!shortcutFile.exists()) {
×
75
            try {
NEW
76
                createShortcut(executableFilePath);
×
NEW
77
                logger.info("create shortcut file successfully: {}", STARTUP_FILE);
×
NEW
78
            } catch (IOException e) {
×
NEW
79
                logger.error("create shortcut file failed: {}", STARTUP_FILE, e);
×
NEW
80
            }
×
81
        }
NEW
82
    }
×
83

84
    private String getExecutableFilePath() {
NEW
85
        URL jarUrl = AddToStartupPanel.class.getProtectionDomain().getCodeSource().getLocation();
×
NEW
86
        if (jarUrl == null) {
×
NEW
87
            logger.error("jarUrl is null");
×
NEW
88
            return null;
×
89
        }
NEW
90
        File jarFile = new File(jarUrl.getPath());
×
NEW
91
        logger.info("jarFile: {}", jarFile.getAbsolutePath());
×
NEW
92
        File grandpaFile = jarFile.getParentFile().getParentFile();
×
NEW
93
        logger.info("parentFile: {}", grandpaFile.getAbsolutePath());
×
NEW
94
        Collection<File> executableFiles = FileUtils.listFiles(grandpaFile, new String[]{"exe"}, false);
×
NEW
95
        if (CollectionUtils.isEmpty(executableFiles)) {
×
NEW
96
            logger.error("Can not find executable file");
×
NEW
97
            return null;
×
98
        }
NEW
99
        if (executableFiles.size() > 1) {
×
NEW
100
            logger.error("There are more than one executable file");
×
NEW
101
            return null;
×
102
        }
NEW
103
        File executableFile = executableFiles.iterator().next();
×
NEW
104
        logger.info("executableFile: {}", executableFile.getAbsolutePath());
×
NEW
105
        return executableFile.getAbsolutePath();
×
106
    }
107

108
    private static void createShortcut(String targetPath) throws IOException {
NEW
109
        String vbsScript = """
×
110
                Set WshShell = CreateObject("WScript.Shell")
111
                Set Shortcut = WshShell.CreateShortcut("%s")
112
                Shortcut.TargetPath = "%s"
113
                Shortcut.Save
NEW
114
                """.formatted(STARTUP_FILE, targetPath);
×
115

NEW
116
        File tempVbs = Files.createTempFile("createShortcut", ".vbs").toFile();
×
NEW
117
        Files.writeString(tempVbs.toPath(), vbsScript);
×
118

NEW
119
        Runtime.getRuntime().exec("wscript " + tempVbs.getAbsolutePath());
×
NEW
120
        tempVbs.deleteOnExit();
×
NEW
121
    }
×
122
}
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