Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Added installer
Browse files Browse the repository at this point in the history
  • Loading branch information
micrusa committed Jun 22, 2020
1 parent d6fc4d9 commit 87aeae9
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<finalName>AutoScreenSettings</finalName>
<plugins>
Expand Down Expand Up @@ -68,6 +73,11 @@
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
</dependencies>


Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/micrusa/autoscreensettings/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class Constants {

public static final String APPDATA_PATH = System.getenv("APPDATA") + "/AutoScreenSettings/";
public static final String SETTINGS_PATH = APPDATA_PATH + "settings.properties";
public static final String APP_PATH = APPDATA_PATH + "app.jar";
public static final String START_SCRIPT = "java -jar " + APP_PATH;

public static final String PROPS_CURRENT_VER = "2";
public static final String PROP_VER = "ver";
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/me/micrusa/autoscreensettings/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package me.micrusa.autoscreensettings;

import me.micrusa.autoscreensettings.TrayIcon.DisplayTray;
import me.micrusa.autoscreensettings.utils.AppInstaller;
import me.micrusa.autoscreensettings.utils.utils;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Properties;

public class Main {

private static File appFile;
private static boolean DEBUG = false;


public static void main(String[] args) {
try {
appFile = new File(Main.class.getProtectionDomain().
getCodeSource().
getLocation().toURI().toString());
} catch (URISyntaxException e) {
utils.showException(e);
}
if(!appFile.getPath().contains(System.getenv("APPDATA")) && !DEBUG)
AppInstaller.openDialog();
Properties props = utils.getProps();
if(!props.getProperty(Constants.PROP_VER, "a").equals(Constants.PROPS_CURRENT_VER)){
props.setProperty(Constants.PROP_VER, Constants.PROPS_CURRENT_VER);
Expand All @@ -24,6 +39,8 @@ public static void main(String[] args) {
utils.saveProps(props);
}
MainService.setupMainService().startService();
if(args.length > 0 && args[0].toLowerCase().contains("update"))
MainService.getMainService().getDisplayTray().showMsg("AutoScreenSettings", "App installed/updated successfully");
}

}
4 changes: 4 additions & 0 deletions src/main/java/me/micrusa/autoscreensettings/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public void stopService(){
mainTaskTimer.cancel();
}

public DisplayTray getDisplayTray(){
return displayTray;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ protected static Image createIcon(String path, String desc){
return new ImageIcon(ImageURL, desc).getImage();
}

public void showMsg(String caption, String message){
trayIcon.displayMessage(caption, message, TrayIcon.MessageType.INFO);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.micrusa.autoscreensettings.utils;

import com.google.common.io.Files;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.WinReg;
import me.micrusa.autoscreensettings.Constants;
import me.micrusa.autoscreensettings.Main;
import me.micrusa.autoscreensettings.MainService;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class AppInstaller {

public static void openDialog(){
int install;
if(!new File(Constants.APP_PATH).exists()){
install = JOptionPane.showConfirmDialog((Component) null, "Do you want to install AutoScreenSettings?",
"AutoScreenSettings installation", JOptionPane.OK_CANCEL_OPTION);
} else {
install = JOptionPane.showConfirmDialog((Component) null, "Do you want to update AutoScreenSettings?",
"AutoScreenSettings update", JOptionPane.OK_CANCEL_OPTION);
}
if(install != 0){
System.exit(0);
}
File appFile;
try {
appFile = new File(Main.class.getProtectionDomain().
getCodeSource().
getLocation()
.toURI());
Files.copy(appFile, new File(Constants.APP_PATH));
Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "AutoScreenSettings", Constants.START_SCRIPT);
Runtime.getRuntime().exec(Constants.START_SCRIPT + " update");
System.exit(1);
} catch (URISyntaxException | IOException e) {
utils.showException("Error in installation", e);
}
}

}
6 changes: 5 additions & 1 deletion src/main/java/me/micrusa/autoscreensettings/utils/utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public static Calendar[] getSunriseSunset(double lat, double lon){
}

public static void showException(Exception e){
JOptionPane.showMessageDialog(new JFrame(), "There was an exception in the code: " + e.toString(), "AutoScreenSettings Exception",
showException("AutoScreenSettings Exception", e);
}

public static void showException(String title, Exception e){
JOptionPane.showMessageDialog(new JFrame(), "There was an exception in the code: " + e.toString(), title,
JOptionPane.ERROR_MESSAGE);
}
}

0 comments on commit 87aeae9

Please sign in to comment.