2016-12-01 18:24:58 +01:00
/ * *
* updater for Project HomeFlix
* checks for Updates and downloads it in case there is one
* /
package application ;
import java.io.BufferedReader ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.net.URL ;
import java.nio.channels.Channels ;
import java.nio.channels.ReadableByteChannel ;
2017-03-25 16:20:48 +01:00
import javafx.application.Platform ;
2017-03-28 17:36:05 +02:00
//TODO rework the process after the update is downloaded, need to replace the old config.xml
2017-03-05 18:29:24 +01:00
public class updater extends Thread {
2016-12-01 18:24:58 +01:00
2017-03-05 18:29:24 +01:00
private MainWindowController mainWindowController ;
private String buildURL ;
private String downloadLink ;
2017-03-25 16:20:48 +01:00
private String updateBuildNumber ;
2017-03-05 18:29:24 +01:00
private String buildNumber ;
2017-03-25 16:20:48 +01:00
public updater ( MainWindowController m , String buildURL , String downloadLink , String buildNumber ) {
2016-12-01 18:24:58 +01:00
mainWindowController = m ;
2017-03-05 18:29:24 +01:00
this . buildURL = buildURL ;
this . downloadLink = downloadLink ;
this . buildNumber = buildNumber ;
2016-12-01 18:24:58 +01:00
}
2017-03-05 18:29:24 +01:00
public void run ( ) {
2016-12-01 18:24:58 +01:00
System . out . println ( " check for updates ... " ) ;
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " checkingUpdates " ) ) ;
} ) ;
2016-12-01 18:24:58 +01:00
try {
2017-03-25 16:20:48 +01:00
URL url = new URL ( buildURL ) ; //URL of the text file with the current build number
2016-12-01 18:24:58 +01:00
BufferedReader in = new BufferedReader ( new InputStreamReader ( url . openStream ( ) ) ) ;
2017-03-25 16:20:48 +01:00
updateBuildNumber = in . readLine ( ) ; //write InputStream in String
2016-12-01 18:24:58 +01:00
in . close ( ) ;
} catch ( IOException e1 ) {
mainWindowController . showErrorMsg ( mainWindowController . errorUpdateV , e1 ) ;
}
2017-03-25 16:20:48 +01:00
System . out . println ( " Build: " + buildNumber + " , Update: " + updateBuildNumber ) ;
2016-12-01 18:24:58 +01:00
2017-03-25 16:20:48 +01:00
//Compares the program BuildNumber with the current BuildNumber if program BuildNumber < current BuildNumber then perform a update
2016-12-01 18:24:58 +01:00
int iversion = Integer . parseInt ( buildNumber ) ;
2017-03-25 16:20:48 +01:00
int iaktVersion = Integer . parseInt ( updateBuildNumber . replace ( " . " , " " ) ) ;
2016-12-01 18:24:58 +01:00
if ( iversion > = iaktVersion ) {
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " updateBtnNotavail " ) ) ;
} ) ;
2016-12-01 18:24:58 +01:00
System . out . println ( " no update available " ) ;
} else {
2017-03-25 16:20:48 +01:00
Platform . runLater ( ( ) - > {
mainWindowController . updateBtn . setText ( mainWindowController . bundle . getString ( " updateBtnavail " ) ) ;
} ) ;
2016-12-01 18:24:58 +01:00
System . out . println ( " update available " ) ;
2017-03-05 18:29:24 +01:00
try {
URL website ;
URL downloadURL = new URL ( downloadLink ) ;
BufferedReader in = new BufferedReader ( new InputStreamReader ( downloadURL . openStream ( ) ) ) ;
String updateDataURL = in . readLine ( ) ;
website = new URL ( updateDataURL ) ; //Update URL
ReadableByteChannel rbc = Channels . newChannel ( website . openStream ( ) ) ; //open new Stream/Channel
2017-03-25 16:20:48 +01:00
FileOutputStream fos = new FileOutputStream ( " ProjectHomeFlix.jar " ) ; //new FileOutputStream for ProjectHomeFLix.jar
2017-03-05 18:29:24 +01:00
fos . getChannel ( ) . transferFrom ( rbc , 0 , Long . MAX_VALUE ) ; //gets file from 0 to max size
fos . close ( ) ; //close fos (extrem wichtig!)
Runtime . getRuntime ( ) . exec ( " java -jar ProjectHomeFlix.jar " ) ; //start again
System . exit ( 0 ) ; //finishes itself
} catch ( IOException e ) {
//in case there is an error
mainWindowController . showErrorMsg ( mainWindowController . errorUpdateD , e ) ;
}
2016-12-01 18:24:58 +01:00
}
}
}