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

jiangxincode / ApkToolBoxGUI / #1046

10 May 2025 05:11AM UTC coverage: 3.079% (-0.03%) from 3.109%
#1046

push

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

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

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/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
                File shortcutFile = new File(STARTUP_FILE);
×
NEW
58
                if (shortcutFile.exists()) {
×
NEW
59
                    boolean ret = shortcutFile.delete();
×
NEW
60
                    if (ret) {
×
NEW
61
                        logger.info("delete shortcut file successfully: {}", STARTUP_FILE);
×
62
                    } else {
NEW
63
                        logger.error("delete shortcut file failed: {}", STARTUP_FILE);
×
64
                    }
65
                }
66
            }
NEW
67
        });
×
68

NEW
69
        optionPanel.add(typeLabel);
×
NEW
70
        optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
×
NEW
71
        optionPanel.add(addToStartupCheckBox);
×
NEW
72
    }
×
73

74
    private void addToStartup() {
NEW
75
        String executableFilePath = getExecutableFilePath();
×
NEW
76
        if (executableFilePath == null) {
×
NEW
77
            return;
×
78
        }
79

NEW
80
        File shortcutFile = new File(STARTUP_FILE);
×
81

NEW
82
        if (!shortcutFile.exists()) {
×
83
            try {
NEW
84
                createShortcut(executableFilePath);
×
NEW
85
                logger.info("create shortcut file successfully: {}", STARTUP_FILE);
×
NEW
86
            } catch (IOException e) {
×
NEW
87
                logger.error("create shortcut file failed: {}", STARTUP_FILE, e);
×
NEW
88
            }
×
89
        }
NEW
90
    }
×
91

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

116
    private static void createShortcut(String targetPath) throws IOException {
NEW
117
        String vbsScript = """
×
118
                Set WshShell = CreateObject("WScript.Shell")
119
                Set Shortcut = WshShell.CreateShortcut("%s")
120
                Shortcut.TargetPath = "%s"
121
                Shortcut.Save
NEW
122
                """.formatted(STARTUP_FILE, targetPath);
×
123

NEW
124
        File tempVbs = Files.createTempFile("createShortcut", ".vbs").toFile();
×
NEW
125
        Files.writeString(tempVbs.toPath(), vbsScript);
×
126

NEW
127
        Runtime.getRuntime().exec("wscript " + tempVbs.getAbsolutePath());
×
NEW
128
        tempVbs.deleteOnExit();
×
NEW
129
    }
×
130
}
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