util
Clone or download
Modified Files
package com.stream_pi.util.checkforupdates;
package com.stream_pi.util.checkforupdates;
import com.google.gson.JsonObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonParser;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.platform.PlatformType;
import com.stream_pi.util.platform.PlatformType;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBox;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URL;
public class CheckForUpdates extends Task<Void>
public class CheckForUpdates extends Task<Void>
{
{
private final Button checkForUpdatesButton;
private final Button checkForUpdatesButton;
private final HostServices hostServices;
private final PlatformType platformType;
private final PlatformType platformType;
private final Version currentVersion;
private final Version currentVersion;
private final UpdateHyperlinkOnClick updateHyperlinkOnClick;
public CheckForUpdates(Button checkForUpdatesButton, HostServices hostServices,
public CheckForUpdates(Button checkForUpdatesButton, PlatformType platformType, Version currentVersion,
PlatformType platformType, Version currentVersion)
UpdateHyperlinkOnClick updateHyperlinkOnClick)
{
{
this.checkForUpdatesButton = checkForUpdatesButton;
this.checkForUpdatesButton = checkForUpdatesButton;
this.hostServices = hostServices;
this.platformType = platformType;
this.platformType = platformType;
this.currentVersion = currentVersion;
this.currentVersion = currentVersion;
this.updateHyperlinkOnClick = updateHyperlinkOnClick;
new Thread(this).start();
new Thread(this).start();
}
}
@Override
@Override
protected Void call()
protected Void call()
{
{
try
try
{
{
Platform.runLater(()->checkForUpdatesButton.setDisable(true));
Platform.runLater(()->checkForUpdatesButton.setDisable(true));
String url_pre = "https://stream-pi.com/API/get_latest.php?TYPE=";
String url_pre = "https://stream-pi.com/API/get_latest.php?TYPE=";
if(platformType == PlatformType.SERVER)
if(platformType == PlatformType.SERVER)
{
{
url_pre+="SERVER";
url_pre+="SERVER";
}
}
else
else
{
{
url_pre+="CLIENT";
url_pre+="CLIENT";
}
}
String content = readUrl(url_pre);
String content = readUrl(url_pre);
JsonObject jsonObject = JsonParser.parseString(content).getAsJsonObject();
JsonObject jsonObject = JsonParser.parseString(content).getAsJsonObject();
String latestVersionRaw = jsonObject.get("Version").getAsString();
String latestVersionRaw = jsonObject.get("Version").getAsString();
String releasePage = jsonObject.get("Release Page").getAsString();
String releasePage = jsonObject.get("Release Page").getAsString();
Version latestVersion = new Version(latestVersionRaw);
Version latestVersion = new Version(latestVersionRaw);
if(latestVersion.isBiggerThan(currentVersion))
if(latestVersion.isBiggerThan(currentVersion))
{
{
VBox vBox = new VBox();
VBox vBox = new VBox();
Hyperlink urlLabel = new Hyperlink(releasePage);
Hyperlink updateHyperlink = new Hyperlink(releasePage);
urlLabel.setOnAction(event->hostServices.showDocument(releasePage));
updateHyperlinkOnClick.setURL(releasePage);
updateHyperlink.setOnAction(event->updateHyperlinkOnClick.handle(event));
Label label = new Label(
Label label = new Label(
"New Version "+latestVersionRaw+" Available.\n" +
"New Version "+latestVersionRaw+" Available.\n" +
"Current Version "+currentVersion.getText()+".\n"+
"Current Version "+currentVersion.getText()+".\n"+
"Changelog and install instructions are included in the release page.\n" +
"Changelog and install instructions are included in the release page.\n" +
"It is recommended to update to ensure maximum stability and least bugs.");
"It is recommended to update to ensure maximum stability and least bugs.");
label.setWrapText(true);
label.setWrapText(true);
vBox.setSpacing(5);
vBox.setSpacing(5);
vBox.getChildren().addAll(
vBox.getChildren().addAll(
urlLabel,
updateHyperlink,
label
label
);
);
new StreamPiAlert("New Update Available!", StreamPiAlertType.INFORMATION, vBox).show();
new StreamPiAlert("New Update Available!", StreamPiAlertType.INFORMATION, vBox).show();
}
}
else
else
{
{
new StreamPiAlert("Up to Date", "System is up to date. ("+currentVersion.getText()+")", StreamPiAlertType.INFORMATION).show();;
new StreamPiAlert("Up to Date", "System is up to date. ("+currentVersion.getText()+")", StreamPiAlertType.INFORMATION).show();;
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
new StreamPiAlert("Uh Oh", "Update Check Failed. \n\nMessage : "+e.getMessage(), StreamPiAlertType.ERROR).show();;
new StreamPiAlert("Uh Oh", "Update Check Failed. \n\nMessage : "+e.getMessage(), StreamPiAlertType.ERROR).show();;
}
}
finally
finally
{
{
Platform.runLater(()->checkForUpdatesButton.setDisable(false));
Platform.runLater(()->checkForUpdatesButton.setDisable(false));
}
}
return null;
return null;
}
}
private String readUrl(String urlString) throws Exception
private String readUrl(String urlString) throws Exception
{
{
URL url = new URL(urlString);
URL url = new URL(urlString);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer = new StringBuilder();
StringBuilder buffer = new StringBuilder();
int read;
int read;
char[] chars = new char[1024];
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
buffer.append(chars, 0, read);
reader.close();
reader.close();
return buffer.toString();
return buffer.toString();
}
}
}
}
package com.stream_pi.util.checkforupdates;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public abstract class UpdateHyperlinkOnClick implements EventHandler<ActionEvent>
{
private String URL;
public void setURL(String URL)
{
this.URL = URL;
}
public String getURL()
{
return URL;
}
}