1. Add 'check for new versions' feature.
2. Fixed broken (was it broken?) error pop-up window.
This commit is contained in:
Dmitry Isaenko 2019-03-23 20:05:48 +03:00
parent 0c1cef57ac
commit ce8c434931
15 changed files with 229 additions and 21 deletions

View file

@ -0,0 +1,63 @@
package nsusbloader.ModelControllers;
import javafx.concurrent.Task;
import nsusbloader.NSLMain;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class UpdatesChecker extends Task<List<String>> {
@Override
protected List<String> call() {
String respondedJson;
try {
URL gitHubUrl = new URL("https://api.github.com/repos/developersu/ns-usbloader/releases/latest");
HttpsURLConnection connection = (HttpsURLConnection) gitHubUrl.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/vnd.github.v3+json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
if (status != 200) {
return null;
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
respondedJson = br.readLine();
br.close();
connection.disconnect();
if (respondedJson == null)
return null;
}
catch (IOException mue){
return null;
}
String newVersion = respondedJson.replaceAll("(^.*\"tag_name\":\")(.?|.+?)(\".+$)", "$2");
String changeLog = respondedJson.replaceAll("(^.*\"body\":\")(.?|.+?)(\".+$)", "$2")
.replaceAll("\\\\r\\\\n","\n")
.replaceAll("#+?\\s", ""); // replace #### dsds | # dsds
if (newVersion.matches("^v(([0-9])+?\\.)+[0-9]+(-.+)$")) // if new version have postfix like v0.1-Experimental
newVersion = newVersion.replaceAll("(-.*$)", ""); // cut postfix
if ( ! newVersion.matches("^v(([0-9])+?\\.)+[0-9]+$")) { // check if new version structure valid
return null;
}
List<String> returningValue = new ArrayList<>();
if (!newVersion.equals(NSLMain.appVersion)) { // if latest noted version in GitHub is different to this version
returningValue.add(newVersion);
returningValue.add(changeLog);
return returningValue;
}
returningValue.add("");
returningValue.add("");
return returningValue;
}
}