11package blobsaver ;
22
33import com .sun .javafx .PlatformUtil ;
4+ import javafx .application .Platform ;
45import javafx .beans .value .ObservableValue ;
56import javafx .collections .FXCollections ;
67import javafx .collections .ObservableList ;
8+ import javafx .concurrent .Service ;
9+ import javafx .concurrent .Task ;
710import javafx .event .ActionEvent ;
811import javafx .fxml .FXML ;
912import javafx .scene .control .*;
1215import javafx .scene .control .TextField ;
1316import javafx .scene .effect .DropShadow ;
1417import javafx .scene .paint .Color ;
18+ import javafx .stage .Modality ;
19+ import org .json .JSONException ;
20+ import org .json .JSONObject ;
1521
1622import java .awt .*;
1723import java .awt .datatransfer .StringSelection ;
1824import java .io .*;
1925import java .net .URI ;
2026import java .net .URISyntaxException ;
27+ import java .net .URL ;
28+ import java .net .URLConnection ;
2129import java .util .ArrayList ;
2230import java .util .Arrays ;
31+ import java .util .concurrent .CountDownLatch ;
2332import java .util .prefs .Preferences ;
2433
2534public class Controller {
@@ -53,6 +62,9 @@ public class Controller {
5362 private ButtonType redditPM = new ButtonType ("PM on Reddit" );
5463 private ButtonType githubIssue = new ButtonType ("Create Issue on Github" );
5564
65+ private URI githubIssueURI ;
66+ private URI redditPMURI ;
67+
5668 @ SuppressWarnings ("unchecked" )
5769 @ FXML
5870 public void initialize () {
@@ -62,8 +74,8 @@ public void initialize() {
6274 "iPhone 6+ " , "iPhone 6" , "iPhone 6s" , "iPhone 6s+" , "iPhone SE" , "iPhone 7 (Global)(iPhone9,1)" ,
6375 "iPhone 7+ (Global)(iPhone9,2)" , "iPhone 7 (GSM)(iPhone9,3)" , "iPhone 7+ (GSM)(iPhone9,4)" ,
6476 "iPhone 8 (iPhone10,1)" , "iPhone 8+ (iPhone10,2)" , "iPhone X (iPhone10,3)" , "iPhone 8 (iPhone10,4)" ,
65- "iPhone 8+ (iPhone10,5)" , "iPhone X (iPhone10,6)" , "" );
66- final ObservableList iPods = FXCollections .observableArrayList ("iPod Touch 3" , "iPod Touch 4" , "iPod Touch 5" , "iPod Touch 6" , "" );
77+ "iPhone 8+ (iPhone10,5)" , "iPhone X (iPhone10,6)" );
78+ final ObservableList iPods = FXCollections .observableArrayList ("iPod Touch 3" , "iPod Touch 4" , "iPod Touch 5" , "iPod Touch 6" );
6779 final ObservableList iPads = FXCollections .observableArrayList ("iPad 1" , "iPad 2 (WiFi)" , "iPad 2 (GSM)" ,
6880 "iPad 2 (CDMA)" , "iPad 2 (Mid 2012)" , "iPad Mini (Wifi)" , "iPad Mini (GSM)" , "iPad Mini (Global)" ,
6981 "iPad 3 (WiFi)" , "iPad 3 (CDMA)" , "iPad 3 (GSM)" , "iPad 4 (WiFi)" , "iPad 4 (GSM)" , "iPad 4 (Global)" ,
@@ -73,11 +85,14 @@ public void initialize() {
7385 "iPad Pro 9.7 (Wifi)" , "iPad Pro 9.7 (Cellular)" , "iPad Pro 12.9 (WiFi)" , "iPad Pro 12.9 (Cellular)" ,
7486 "iPad 5 (Wifi)" , "iPad 5 (Cellular)" , "iPad Pro 2 12.9 (WiFi)(iPad7,1)" , "iPad Pro 2 12.9 (Cellular)(iPad7,2)" ,
7587 "iPad Pro 10.5 (WiFi)(iPad7,3)" , "iPad 10.5 (Cellular)(iPad7,4)" , "iPad 6 (WiFi)(iPad 7,5)" , "iPad 6 (Cellular)(iPad7,6)" , "" );
76- final ObservableList AppleTVs = FXCollections .observableArrayList ("Apple TV 2G" , "Apple TV 3" , "Apple TV 3 (2013)" , "Apple TV 4 (2015)" , "Apple TV 4K" , "" );
77- deviceTypeChoiceBox .setItems (FXCollections .observableArrayList ("iPhone" , "iPod" , "iPad" , "AppleTV" , "" ));
78-
88+ final ObservableList AppleTVs = FXCollections .observableArrayList ("Apple TV 2G" , "Apple TV 3" , "Apple TV 3 (2013)" , "Apple TV 4 (2015)" , "Apple TV 4K" );
89+ deviceTypeChoiceBox .setItems (FXCollections .observableArrayList ("iPhone" , "iPod" , "iPad" , "AppleTV" ));
7990 deviceTypeChoiceBox .getSelectionModel ().selectedItemProperty ().addListener ((ObservableValue observable , Object oldValue , Object newValue ) -> {
80- String v = (String ) newValue ;
91+ if (newValue == null ) {
92+ versionLabel .setText ("Version" );
93+ return ;
94+ }
95+ final String v = (String ) newValue ;
8196 switch (v ) {
8297 case "iPhone" :
8398 deviceModelChoiceBox .setItems (iPhones );
@@ -97,8 +112,17 @@ public void initialize() {
97112 break ;
98113 }
99114 });
115+ deviceTypeChoiceBox .setValue ("iPhone" );
116+
100117 deviceModelChoiceBox .getSelectionModel ().selectedItemProperty ().addListener ((ObservableValue observable , Object oldValue , Object newValue ) -> {
101- String v = (String ) newValue ;
118+ if (newValue == null ) {
119+ boardConfigField .setEffect (null );
120+ boardConfig = false ;
121+ boardConfigField .setText ("" );
122+ boardConfigField .setDisable (true );
123+ return ;
124+ }
125+ final String v = (String ) newValue ;
102126 if (v .equals ("iPhone 6s" ) || v .equals ("iPhone 6s+" ) || v .equals ("iPhone SE" )) {
103127 int depth = 20 ;
104128 DropShadow borderGlow = new DropShadow ();
@@ -117,9 +141,10 @@ public void initialize() {
117141 boardConfigField .setDisable (true );
118142 }
119143 });
144+
120145 identifierField .textProperty ().addListener ((observable , oldValue , newValue ) -> {
121146 if (newValue .equals ("iPhone8,1" ) || newValue .equals ("iPhone8,2" ) || newValue .equals ("iPhone8,4" )) {
122- int depth = 20 ;
147+ final int depth = 20 ;
123148 DropShadow borderGlow = new DropShadow ();
124149 borderGlow .setOffsetY (0f );
125150 borderGlow .setOffsetX (0f );
@@ -143,6 +168,78 @@ public void initialize() {
143168 errorBorder .setColor (Color .RED );
144169 errorBorder .setWidth (20 );
145170 errorBorder .setHeight (20 );
171+
172+ try {
173+ githubIssueURI = new URI ("https://github.com/airsquared/blobsaver/issues/new" );
174+ redditPMURI = new URI ("https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" );
175+ } catch (URISyntaxException e ) {
176+ e .printStackTrace ();
177+ }
178+
179+ checkForUpdates ();
180+ }
181+
182+ private void checkForUpdates () {
183+ Service <Void > service = new Service <Void >() {
184+ @ Override
185+ protected Task <Void > createTask () {
186+ return new Task <Void >() {
187+ @ Override
188+ protected Void call () throws Exception {
189+ StringBuilder response = new StringBuilder ();
190+ try {
191+ URLConnection urlConnection = new URL ("https://api.github.com/repos/airsquared/blobsaver/releases/latest" ).openConnection ();
192+ BufferedReader in = new BufferedReader (new InputStreamReader (urlConnection .getInputStream ()));
193+ String inputLine ;
194+ while ((inputLine = in .readLine ()) != null ) {
195+ response .append (inputLine );
196+ }
197+ in .close ();
198+ } catch (FileNotFoundException ignored ) {
199+ return null ;
200+ } catch (IOException e ) {
201+ e .printStackTrace ();
202+ }
203+ String version ;
204+ try {
205+ version = new JSONObject (response .toString ()).getString ("tag_name" );
206+ } catch (JSONException e ) {
207+ version = Main .appVersion ;
208+ }
209+ if (!version .equals (Main .appVersion )) {
210+ final CountDownLatch latch = new CountDownLatch (1 );
211+ String finalVersion = version ;
212+ Platform .runLater (() -> {
213+ try {
214+ ButtonType downloadNow = new ButtonType ("Download now" );
215+
216+ Alert alert = new Alert (
217+ Alert .AlertType .INFORMATION , "There is a new version available: "
218+ + finalVersion + ". You have version " + Main .appVersion , downloadNow , ButtonType .CANCEL );
219+ alert .setHeaderText ("New Update Available" );
220+ Button dlButton = (Button ) alert .getDialogPane ().lookupButton (downloadNow );
221+ dlButton .setDefaultButton (true );
222+ alert .initModality (Modality .NONE );
223+ alert .showAndWait ();
224+ try {
225+ if (alert .getResult ().equals (downloadNow ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
226+ Desktop .getDesktop ().browse (new URI ("https://github.com/airsquared/blobsaver/releases/latest" ));
227+ }
228+ } catch (IOException | URISyntaxException ee ) {
229+ ee .printStackTrace ();
230+ }
231+ } finally {
232+ latch .countDown ();
233+ }
234+ });
235+ latch .await ();
236+ }
237+ return null ;
238+ }
239+ };
240+ }
241+ };
242+ service .start ();
146243 }
147244
148245 private void run (String device ) {
@@ -175,11 +272,11 @@ private void run(String device) {
175272 alert .showAndWait ();
176273 try {
177274 if (alert .getResult ().equals (githubIssue ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
178- Desktop .getDesktop ().browse (new URI ( "https://github.com/airsquared/blobsaver/issues/new" ) );
275+ Desktop .getDesktop ().browse (githubIssueURI );
179276 } else if (alert .getResult ().equals (redditPM ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
180- Desktop .getDesktop ().browse (new URI ( "https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" ) );
277+ Desktop .getDesktop ().browse (redditPMURI );
181278 }
182- } catch (IOException | URISyntaxException ee ) {
279+ } catch (IOException ee ) {
183280 ee .printStackTrace ();
184281 }
185282 return ;
@@ -204,7 +301,6 @@ private void run(String device) {
204301 }
205302 Process proc = null ;
206303 try {
207- System .out .println (args .toString ());
208304 proc = new ProcessBuilder (args ).start ();
209305 } catch (IOException e ) {
210306 Alert alert = new Alert (Alert .AlertType .ERROR , "There was an error starting tsschecker.\n \n Please create a new issue on Github or PM me on Reddit. The crash log has been copied to your clipboard" , githubIssue , redditPM , ButtonType .CANCEL );
@@ -213,12 +309,12 @@ private void run(String device) {
213309 alert .showAndWait ();
214310 try {
215311 if (alert .getResult ().equals (githubIssue ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
216- Desktop .getDesktop ().browse (new URI ( "https://github.com/airsquared/blobsaver/issues/new" ) );
312+ Desktop .getDesktop ().browse (githubIssueURI );
217313
218314 } else if (alert .getResult ().equals (redditPM ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
219- Desktop .getDesktop ().browse (new URI ( "https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" ) );
315+ Desktop .getDesktop ().browse (redditPMURI );
220316 }
221- } catch (IOException | URISyntaxException ee ) {
317+ } catch (IOException ee ) {
222318 ee .printStackTrace ();
223319 }
224320 e .printStackTrace ();
@@ -227,7 +323,6 @@ private void run(String device) {
227323 StringBuilder log = new StringBuilder ();
228324 String line ;
229325 while ((line = reader .readLine ()) != null ) {
230- System .out .print (line + "\n " );
231326 log .append (line ).append ("\n " );
232327 }
233328 Alert alert = new Alert (Alert .AlertType .INFORMATION , log .toString (), ButtonType .OK );
@@ -239,12 +334,12 @@ private void run(String device) {
239334 alert .showAndWait ();
240335 try {
241336 if (alert .getResult ().equals (githubIssue ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
242- Desktop .getDesktop ().browse (new URI ( "https://github.com/airsquared/blobsaver/issues/new" ) );
337+ Desktop .getDesktop ().browse (githubIssueURI );
243338
244339 } else if (alert .getResult ().equals (redditPM ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
245- Desktop .getDesktop ().browse (new URI ( "https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" ) );
340+ Desktop .getDesktop ().browse (redditPMURI );
246341 }
247- } catch (IOException | URISyntaxException ee ) {
342+ } catch (IOException ee ) {
248343 ee .printStackTrace ();
249344 }
250345 e .printStackTrace ();
@@ -259,12 +354,12 @@ private void run(String device) {
259354 alert .showAndWait ();
260355 try {
261356 if (alert .getResult ().equals (githubIssue ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
262- Desktop .getDesktop ().browse (new URI ( "https://github.com/airsquared/blobsaver/issues/new" ) );
357+ Desktop .getDesktop ().browse (githubIssueURI );
263358
264359 } else if (alert .getResult ().equals (redditPM ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
265- Desktop .getDesktop ().browse (new URI ( "https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" ) );
360+ Desktop .getDesktop ().browse (redditPMURI );
266361 }
267- } catch (IOException | URISyntaxException ee ) {
362+ } catch (IOException ee ) {
268363 ee .printStackTrace ();
269364 }
270365 }
@@ -325,10 +420,13 @@ public void identifierCheckBoxHandler() {
325420 borderGlow .setWidth (depth );
326421 borderGlow .setHeight (depth );
327422 identifierField .setEffect (borderGlow );
328- deviceTypeChoiceBox .setValue ("" );
329- deviceModelChoiceBox .setValue ("" );
423+ deviceTypeChoiceBox .getSelectionModel ().clearSelection ();
424+ deviceModelChoiceBox .getSelectionModel ().clearSelection ();
425+ deviceTypeChoiceBox .setValue (null );
330426 deviceTypeChoiceBox .setDisable (true );
331427 deviceModelChoiceBox .setDisable (true );
428+ deviceTypeChoiceBox .setEffect (null );
429+ deviceModelChoiceBox .setEffect (null );
332430 } else {
333431 identifierField .setEffect (null );
334432 identifierField .setText ("" );
@@ -443,6 +541,10 @@ public void go() {
443541 ecidField .setEffect (errorBorder );
444542 doReturn = true ;
445543 }
544+ if (!identifierCheckBox .isSelected () && ((deviceTypeChoiceBox .getValue () == null ) || (deviceTypeChoiceBox .getValue () == "" ))) {
545+ deviceTypeChoiceBox .setEffect (errorBorder );
546+ doReturn = true ;
547+ }
446548 if (!identifierCheckBox .isSelected () && ((deviceModelChoiceBox .getValue () == null ) || (deviceModelChoiceBox .getValue () == "" ))) {
447549 deviceModelChoiceBox .setEffect (errorBorder );
448550 doReturn = true ;
@@ -463,6 +565,9 @@ public void go() {
463565 return ;
464566 }
465567 String deviceModel = (String ) deviceModelChoiceBox .getValue ();
568+ if (deviceModel == null ) {
569+ deviceModel = "" ;
570+ }
466571 switch (deviceModel ) {
467572 case "iPhone 3G[S]" :
468573 run ("iPhone2,1" );
@@ -689,14 +794,7 @@ public void go() {
689794 case "" :
690795 String identifierText = identifierField .getText ();
691796 try {
692- // Throws StringIndexOutOfBoundsException even if identifier is correct if I don't do it like this:
693- if (identifierText .substring (0 , 4 ).equals ("iPad" )) {
694- run (identifierField .getText ());
695- } else if (identifierText .substring (0 , 4 ).equals ("iPod" )) {
696- run (identifierField .getText ());
697- } else if (identifierText .substring (0 , 6 ).equals ("iPhone" )) {
698- run (identifierField .getText ());
699- } else if (identifierText .substring (0 , 7 ).equals ("AppleTV" )) {
797+ if (identifierText .startsWith ("iPad" ) || identifierText .startsWith ("iPod" ) || identifierText .startsWith ("iPhone" ) || identifierText .startsWith ("AppleTV" )) {
700798 run (identifierField .getText ());
701799 } else {
702800 Alert alert = new Alert (Alert .AlertType .ERROR , "\" " + identifierText +
@@ -718,12 +816,12 @@ public void go() {
718816 alert .showAndWait ();
719817 try {
720818 if (alert .getResult ().equals (githubIssue ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
721- Desktop .getDesktop ().browse (new URI ( "https://github.com/airsquared/blobsaver/issues/new" ) );
819+ Desktop .getDesktop ().browse (githubIssueURI );
722820
723821 } else if (alert .getResult ().equals (redditPM ) && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
724- Desktop .getDesktop ().browse (new URI ( "https://www.reddit.com//message/compose?to=01110101_00101111&subject=Blobsaver+Bug+Report" ) );
822+ Desktop .getDesktop ().browse (redditPMURI );
725823 }
726- } catch (IOException | URISyntaxException ee ) {
824+ } catch (IOException ee ) {
727825 ee .printStackTrace ();
728826 }
729827 break ;
0 commit comments