From: rnayabed Date: Thu, 25 Feb 2021 18:27:17 +0530 Subject: Added CheckForUpdates --- Added CheckForUpdates --- --- 'a/pom.xml' +++ b/pom.xml @@ -39,6 +39,13 @@ ikonli-fontawesome5-pack ${ikonli.version} + + + + com.google.code.gson + gson + 2.8.6 + --- /dev/null +++ b/src/main/java/com/stream_pi/util/checkforupdates/CheckForUpdates.java @@ -0,0 +1,121 @@ +package com.stream_pi.util.checkforupdates; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.stream_pi.util.alert.StreamPiAlert; +import com.stream_pi.util.alert.StreamPiAlertType; +import com.stream_pi.util.platform.PlatformType; +import com.stream_pi.util.version.Version; +import javafx.application.HostServices; +import javafx.application.Platform; +import javafx.concurrent.Task; +import javafx.scene.control.Button; +import javafx.scene.control.Hyperlink; +import javafx.scene.control.Label; +import javafx.scene.layout.VBox; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; + +public class CheckForUpdates extends Task +{ + + private final Button checkForUpdatesButton; + private final HostServices hostServices; + private final PlatformType platformType; + private final Version currentVersion; + + public CheckForUpdates(Button checkForUpdatesButton, HostServices hostServices, + PlatformType platformType, Version currentVersion) + { + this.checkForUpdatesButton = checkForUpdatesButton; + this.hostServices = hostServices; + this.platformType = platformType; + this.currentVersion = currentVersion; + + new Thread(this).start(); + } + + @Override + protected Void call() + { + try + { + Platform.runLater(()->checkForUpdatesButton.setDisable(true)); + + String url_pre = "https://stream-pi.com/API/get_latest.php?TYPE="; + + if(platformType == PlatformType.SERVER) + { + url_pre+="SERVER"; + } + else + { + url_pre+="CLIENT"; + } + + String content = readUrl(url_pre); + + + JsonObject jsonObject = JsonParser.parseString(content).getAsJsonObject(); + + String latestVersionRaw = jsonObject.get("Version").getAsString(); + String releasePage = jsonObject.get("Release Page").getAsString(); + + Version latestVersion = new Version(latestVersionRaw); + + if(latestVersion.isBiggerThan(currentVersion)) + { + VBox vBox = new VBox(); + + Hyperlink urlLabel = new Hyperlink(releasePage); + urlLabel.setOnAction(event->hostServices.showDocument(releasePage)); + + Label label = new Label( + "New Version "+latestVersionRaw+" Available.\n" + + "Current Version "+currentVersion.getText()+".\n"+ + "Changelog and install instructions are included in the release page.\n" + + "It is recommended to update to ensure maximum stability and least bugs."); + label.setWrapText(true); + + vBox.setSpacing(5); + vBox.getChildren().addAll( + urlLabel, + label + ); + + new StreamPiAlert("New Update Available!", StreamPiAlertType.INFORMATION, vBox).show(); + } + else + { + new StreamPiAlert("Up to Date", "System is up to date. ("+currentVersion.getText()+")", StreamPiAlertType.INFORMATION).show();; + } + } + catch (Exception e) + { + e.printStackTrace(); + new StreamPiAlert("Uh Oh", "Update Check Failed. \n\nMessage : "+e.getMessage(), StreamPiAlertType.ERROR).show();; + } + finally + { + Platform.runLater(()->checkForUpdatesButton.setDisable(false)); + } + return null; + } + + + private String readUrl(String urlString) throws Exception + { + URL url = new URL(urlString); + BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); + StringBuilder buffer = new StringBuilder(); + int read; + char[] chars = new char[1024]; + while ((read = reader.read(chars)) != -1) + buffer.append(chars, 0, read); + + reader.close(); + return buffer.toString(); + } +} --- 'a/src/main/java/com/stream_pi/util/platform/Platform.java' +++ b/src/main/java/com/stream_pi/util/platform/Platform.java @@ -21,8 +21,6 @@ public enum Platform { LINUX("Linux"), MAC("MacOS"), ANDROID("Android"), - IOS("iOS"), - LINUX_RPI("Raspberry Pi"), UNKNOWN("Unknown"); final private String UIName; --- /dev/null +++ b/src/main/java/com/stream_pi/util/platform/PlatformType.java @@ -0,0 +1,5 @@ +package com.stream_pi.util.platform; + +public enum PlatformType { + SERVER, CLIENT +} \ No newline at end of file --- 'a/src/main/java/com/stream_pi/util/startatboot/SoftwareType.java' +++ /dev/null @@ -1,21 +0,0 @@ -/* -Stream-Pi - Free & Open-Source Modular Cross-Platform Programmable Macro Pad -Copyright (C) 2019-2021 Debayan Sutradhar (rnayabed), Samuel QuiƱones (SamuelQuinones) - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -Originally Written by : Debayan Sutradhar (rnayabed) -*/ - -package com.stream_pi.util.startatboot; - -public enum SoftwareType { - SERVER, CLIENT -} --- 'a/src/main/java/com/stream_pi/util/startatboot/StartAtBoot.java' +++ b/src/main/java/com/stream_pi/util/startatboot/StartAtBoot.java @@ -18,6 +18,7 @@ package com.stream_pi.util.startatboot; import com.stream_pi.util.platform.Platform; import com.stream_pi.util.exception.MinorException; +import com.stream_pi.util.platform.PlatformType; import java.io.BufferedWriter; import java.io.File; @@ -25,10 +26,10 @@ import java.io.FileWriter; public class StartAtBoot { - SoftwareType softwareType; + PlatformType softwareType; Platform platform; - public StartAtBoot(SoftwareType softwareType, Platform platform) + public StartAtBoot(PlatformType softwareType, Platform platform) { this.softwareType = softwareType; this.platform = platform; @@ -38,7 +39,7 @@ public class StartAtBoot { { if(platform == Platform.WINDOWS) createStarterForWindows(runnerFile); - else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI) + else if(platform == Platform.LINUX) createStarterForLinux(runnerFile, true); else if(platform == Platform.MAC) createStarterForMac(runnerFile); @@ -50,7 +51,7 @@ public class StartAtBoot { { if(platform == Platform.WINDOWS) createStarterForWindows(runnerFile); - else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI) + else if(platform == Platform.LINUX) createStarterForLinux(runnerFile, isXMode); else if(platform == Platform.MAC) createStarterForMac(runnerFile); @@ -61,7 +62,7 @@ public class StartAtBoot { public boolean delete() throws MinorException { if(platform == Platform.WINDOWS) return deleteStarterForWindows(); - else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI) + else if (platform == Platform.LINUX) return deleteStarterForLinux(); else if(platform == Platform.MAC) deleteStarterForMac(); --- 'a/src/main/java/module-info.java' +++ b/src/main/java/module-info.java @@ -22,6 +22,7 @@ module com.stream_pi.util requires transitive java.logging; requires transitive javafx.controls; requires transitive java.xml; + requires com.google.gson; exports com.stream_pi.util.version; exports com.stream_pi.util.exception; @@ -29,6 +30,7 @@ module com.stream_pi.util exports com.stream_pi.util.uihelper; exports com.stream_pi.util.startatboot; exports com.stream_pi.util.alert; + exports com.stream_pi.util.checkforupdates; exports com.stream_pi.util.combobox; exports com.stream_pi.util.xmlconfighelper; exports com.stream_pi.util.loggerhelper;