Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
better windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
NonSwag committed Sep 25, 2022
1 parent e72c5da commit 84e2e48
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
11 changes: 7 additions & 4 deletions src/main/java/net/nonswag/tnl/adb/ADB.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static List<Device> getDevices() throws AdbException {
if (s.isEmpty()) return;
List<String> split = Arrays.stream(s.split(" ")).filter(s1 -> !s1.isEmpty()).toList();
if (split.isEmpty()) return;
devices.add(new Device(split.get(0), split.size() >= 2 ? split.get(1) : null));
String status = split.size() >= 2 ? split.get(1) : null;
if ("unauthorized".equalsIgnoreCase(status)) throw new AdbException("device not authorized");
devices.add(new Device(split.get(0), status));
} catch (AdbException e) {
Logger.error.println("Failed to read device information: " + s, e);
}
Expand All @@ -44,18 +46,19 @@ public static List<Device> getDevices() throws AdbException {

@Nonnull
public static List<String> execute(@Nonnull String command) throws AdbException {
List<String> callback = new ArrayList<>();
try {
Process process = Runtime.getRuntime().exec("adb ".concat(command), null, SystemUtil.TYPE.isLinux() ? null : new File("platform-tools"));
List<String> callback = new ArrayList<>();
Process process = SystemUtil.TYPE.isLinux() ? Runtime.getRuntime().exec("adb ".concat(command)) :
Runtime.getRuntime().exec("cmd.exe /C adb.exe ".concat(command), null, new File("platform-tools"));
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String string;
while ((string = br.readLine()) != null) callback.add(string);
process.waitFor();
process.destroy();
br.close();
return callback;
} catch (Exception e) {
throw new AdbException(e.getMessage(), e);
}
return callback;
}
}
21 changes: 17 additions & 4 deletions src/main/java/net/nonswag/tnl/adb/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Getter;
import net.nonswag.tnl.core.api.logger.Logger;
import net.nonswag.tnl.core.api.math.Range;
import net.nonswag.tnl.core.utils.SystemUtil;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -40,15 +41,17 @@ public Device(@Nonnull String serialNumber, @Nullable String status) throws AdbE

@Nonnull
private String retrieveMacAddress() throws AdbException {
List<String> callback = runShellCommand("ip addr show wlan0 | grep 'link/ether '| cut -d' ' -f6");
List<String> callback = runShellCommand("ip addr show wlan0 | grep 'link/ether '| cut -d' ' -f6", "ip addr show wlan0 | findstr \"link/ether \"");
if (callback.isEmpty()) throw new AdbException("WLAN information not found");
if (SystemUtil.TYPE.isWindows()) return callback.get(0).replace(" ", "").split(" ")[1];
return callback.get(0);
}

@Nonnull
private String retrieveIPAddress() throws AdbException {
List<String> callback = runShellCommand("ip addr show wlan0 | grep 'inet '| cut -d' ' -f6");
List<String> callback = runShellCommand("ip addr show wlan0 | grep 'inet '| cut -d' ' -f6", "ip addr show wlan0 | findstr \"inet \"");
if (callback.isEmpty()) throw new AdbException("WLAN information not found");
if (SystemUtil.TYPE.isWindows()) return callback.get(0).replace(" ", "").split(" ")[1].split("/")[0];
return callback.get(0).split("/")[0];
}

Expand Down Expand Up @@ -125,15 +128,15 @@ public void setBatteryLevel(@Range(from = 0, to = 100) int battery) throws AdbEx

@Range(from = 0, to = 100)
public int getBatteryLevel() throws AdbException {
List<String> callback = runShellCommand("dumpsys battery | grep level");
List<String> callback = runShellCommand("dumpsys battery | grep level", "dumpsys battery | findstr \"level\"");
if (callback.isEmpty()) throw new AdbException("Battery information not found");
String[] level = callback.get(0).split(" ");
return Integer.parseInt(level[level.length - 1]);
}

@Nonnull
public Status getBatteryStatus() throws AdbException {
List<String> callback = runShellCommand("dumpsys battery | grep status");
List<String> callback = runShellCommand("dumpsys battery | grep status", "dumpsys battery | findstr \"status\"");
if (callback.isEmpty()) throw new AdbException("Battery information not found");
String[] level = callback.get(0).split(" ");
return Status.valueOf(Integer.parseInt(level[level.length - 1]));
Expand Down Expand Up @@ -218,6 +221,16 @@ public List<String> runShellCommand(@Nonnull String command) throws AdbException
return runCommand("shell ".concat(command));
}

@Nonnull
private List<String> runCommand(@Nonnull String command, @Nonnull String alternative) throws AdbException {
return SystemUtil.TYPE.isWindows() ? runCommand(alternative) : runCommand(command);
}

@Nonnull
private List<String> runShellCommand(@Nonnull String command, @Nonnull String alternative) throws AdbException {
return SystemUtil.TYPE.isWindows() ? runShellCommand(alternative) : runShellCommand(command);
}

@Getter
public enum Status {
UNKNOWN("unknown", 1),
Expand Down
40 changes: 18 additions & 22 deletions src/main/java/net/nonswag/tnl/passprotect/Installer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.nonswag.tnl.passprotect;

import net.nonswag.tnl.core.api.errors.file.FileException;
import net.nonswag.tnl.core.api.file.formats.ShellFile;
import net.nonswag.tnl.core.api.file.formats.TextFile;
import net.nonswag.tnl.core.api.file.helper.FileDownloader;
import net.nonswag.tnl.core.api.file.helper.FileHelper;
Expand Down Expand Up @@ -36,13 +35,13 @@ public static void init(boolean update) throws FileNotFoundException {
} catch (FileException e) {
PassProtect.showErrorDialog("Failed to copy resource file", e);
}
if (SystemUtil.TYPE.isWindows()) installADB(destination);
if (SystemUtil.TYPE.isWindows()) installADB(destination, update);
if (!update) {
if (JOptionPane.showConfirmDialog(PassProtect.getInstance().getWindow(), "Create desktop entry?", null, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
createDesktopEntry("%s/Desktop".formatted(home), destination.getAbsolutePath());
createDesktopEntry(new File(home, "Desktop"), destination.getAbsoluteFile());
}
if (JOptionPane.showConfirmDialog(PassProtect.getInstance().getWindow(), "Create activities entry?", null, JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
createDesktopEntry("%s/.local/share/applications".formatted(home), destination.getAbsolutePath());
if (SystemUtil.TYPE.isLinux() && JOptionPane.showConfirmDialog(PassProtect.getInstance().getWindow(), "Create activities entry?", null, JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
createDesktopEntry(new File(home, "%s/.local/share/applications"), destination.getAbsoluteFile());
}
JOptionPane.showMessageDialog(PassProtect.getInstance().getWindow(), "Successfully installed PassProtect", "Installer", JOptionPane.INFORMATION_MESSAGE);
} else {
Expand All @@ -56,7 +55,7 @@ public static void init(boolean update) throws FileNotFoundException {
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void installADB(@Nonnull File destination) throws IOException {
private static void installADB(@Nonnull File destination, boolean update) throws IOException {
Thread thread = new Thread(() -> JOptionPane.showMessageDialog(PassProtect.getInstance().getWindow(), "Installing ADB-Drivers", "Installing…", JOptionPane.INFORMATION_MESSAGE));
thread.start();
File file = new File(destination, "platform-tools.zip");
Expand All @@ -74,20 +73,13 @@ private static void installADB(@Nonnull File destination) throws IOException {
}
} else entryDestination.mkdirs();
}
JOptionPane.showMessageDialog(PassProtect.getInstance().getWindow(), "Successfully installed ADB-Drivers", "Success", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(PassProtect.getInstance().getWindow(), "Successfully %s ADB-Drivers".formatted(update ? "updated" : "installed"), "Success", JOptionPane.INFORMATION_MESSAGE);
} finally {
FileHelper.delete(file);
}
}

public static void createCommand() throws FileException {
ShellFile file = new ShellFile("/usr/bin/", "pass-protect");
file.setContent(new String[]{"cd /home/david/.pass-protect/", "java -jar PassProtect.jar ${@}"});
if (!file.getFile().setExecutable(true, false)) throw new FileException("Failed to mark file as executable");
file.save();
}

private static void createDesktopEntry(@Nonnull String location, @Nonnull String destination) {
private static void createDesktopEntry(@Nonnull File location, @Nonnull File destination) {
try {
if (SystemUtil.TYPE.isLinux()) createLinuxDesktopEntry(location, destination);
else if (SystemUtil.TYPE.isWindows()) createWindowsDesktopEntry(location, destination);
Expand All @@ -98,25 +90,29 @@ private static void createDesktopEntry(@Nonnull String location, @Nonnull String
}
}

private static void createLinuxDesktopEntry(@Nonnull String location, @Nonnull String destination) {
new TextFile(location, "pass-protect.desktop").setContent("""
private static void createLinuxDesktopEntry(@Nonnull File location, @Nonnull File destination) {
new TextFile(new File(location, "pass-protect.desktop")).setContent("""
[Desktop Entry]
Type=Application
Version=1.0
Name=PassProtect
Exec=java -jar PassProtect.jar start installed
Path=%s
Icon=%s/icon.png
Icon=%s
Terminal=false
Categories=Security;
""".formatted(destination, destination)).save();
""".formatted(destination.getAbsolutePath(), new File(destination, "icon.png").getAbsolutePath())).save();
}

private static void createWindowsDesktopEntry(@Nonnull String location, @Nonnull String destination) {
throw new UnsupportedOperationException("This feature is currently not working on windows");
private static void createWindowsDesktopEntry(@Nonnull File location, @Nonnull File destination) {
new TextFile(new File(location, "PassProtect.vbs")).setContent("""
Set WshShell = CreateObject("WScript.Shell")
WshShell.CurrentDirectory = "%s"
WshShell.Run "java -jar PassProtect.jar start installed", 0, false
""".formatted(destination)).save();
}

private static void createMacDesktopEntry(@Nonnull String location, @Nonnull String destination) {
private static void createMacDesktopEntry(@Nonnull File location, @Nonnull File destination) {
throw new UnsupportedOperationException("This feature is currently not working on mac");
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/nonswag/tnl/passprotect/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import net.nonswag.tnl.core.Core;
import net.nonswag.tnl.core.api.logger.Logger;
import net.nonswag.tnl.core.utils.SystemUtil;
import net.nonswag.tnl.passprotect.api.files.Config;
import net.nonswag.tnl.passprotect.api.files.Storage;

Expand Down Expand Up @@ -76,7 +77,7 @@ private static void updateLAF(@Nullable Window window) {
}

public static void main(String[] args) throws Exception {
Core.main(args);
if (!SystemUtil.TYPE.isWindows()) Core.main(args);
List<String> arguments = Arrays.asList(args);
boolean shortcut = !arguments.isEmpty() && arguments.get(arguments.size() - 1).equalsIgnoreCase("sc");
if (args.length >= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ private void setupWindow(@Nonnull Config config, @Nonnull Storage storage) {
this.trustedDevices.removeAll();
try {
TrustedDevices trustedDevices = config.getTrustedDevices();
List<Device> devices = new ArrayList<>();
ADB.getDevices().forEach(device -> {
if (!trustedDevices.isTrusted(device)) devices.add(device);
});
trustedDevices.getDevices().forEach(device -> {
JButton button = new JButton(device.getName());
button.addActionListener(actionEvent -> new DeviceInformation(this, device, config, storage));
this.trustedDevices.add(button);
});
List<Device> devices = new ArrayList<>();
ADB.getDevices().forEach(device -> {
if (!trustedDevices.isTrusted(device)) devices.add(device);
});
if (trustedDevices.hasTrustedDevices() && !devices.isEmpty()) this.trustedDevices.add(new JSeparator());
devices.forEach(device -> {
JButton button = new JButton("Trust ".concat(device.getModel()));
Expand All @@ -124,10 +124,10 @@ private void setupWindow(@Nonnull Config config, @Nonnull Storage storage) {
}
} catch (Exception e) {
if (e.getMessage() != null && e.getMessage().startsWith("Cannot run program")) {
trustedDevices.add(new JLabel("To use this feature you have to install ADB"));
trustedDevices.add(new JLabel("Failed to access ADB-Drivers"));
} else if (e.getMessage() != null) trustedDevices.add(new JLabel(e.getMessage()));
else trustedDevices.add(new JLabel("Something went wrong"));
PassProtect.showErrorDialog("Failed to start ADB-Drivers", e);
PassProtect.showErrorDialog("Failed to access ADB-Drivers", e);
}
appearance.removeAll();
appearanceScrollBar.getVerticalScrollBar().setUnitIncrement(15);
Expand Down

0 comments on commit 84e2e48

Please sign in to comment.