Skip to content

Commit 02f7f37

Browse files
committed
Minor refactoring
1 parent c2db154 commit 02f7f37

File tree

8 files changed

+105
-90
lines changed

8 files changed

+105
-90
lines changed

.idea/modules/blobsaver.test.iml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/airsquared/blobsaver/app/Controller.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class Controller {
5555
@FXML private TextField ecidField, boardConfigField, apnonceField, versionField, identifierField,
5656
pathField, ipswField;
5757

58-
@FXML private CheckBox apnonceCheckBox, versionCheckBox, identifierCheckBox, betaCheckBox;
58+
@FXML private CheckBox apnonceCheckBox, allSignedVersionsCheckBox, identifierCheckBox, betaCheckBox;
5959

6060
@FXML private Label versionLabel;
6161

@@ -192,7 +192,7 @@ public void apnonceCheckBoxHandler() {
192192
}
193193

194194
public void versionCheckBoxHandler() {
195-
if (versionCheckBox.isSelected()) {
195+
if (allSignedVersionsCheckBox.isSelected()) {
196196
versionField.setDisable(true);
197197
versionField.setEffect(null);
198198
versionField.setText("");
@@ -218,16 +218,16 @@ public void identifierCheckBoxHandler() {
218218
public void betaCheckBoxHandler() {
219219
if (betaCheckBox.isSelected()) {
220220
ipswField.setEffect(Utils.borderGlow);
221-
if (!versionCheckBox.isSelected()) {
222-
versionCheckBox.fire(); // disable version field
221+
if (!allSignedVersionsCheckBox.isSelected()) {
222+
allSignedVersionsCheckBox.fire(); // disable version field
223223
}
224-
versionCheckBox.setSelected(false); // turn latest version off without enabling version field
225-
versionCheckBox.setDisable(true);
224+
allSignedVersionsCheckBox.setSelected(false); // turn latest version off without enabling version field
225+
allSignedVersionsCheckBox.setDisable(true);
226226
} else {
227227
ipswField.setEffect(null);
228228
ipswField.setText("");
229-
versionCheckBox.setDisable(false);
230-
versionCheckBox.setSelected(true);
229+
allSignedVersionsCheckBox.setDisable(false);
230+
allSignedVersionsCheckBox.setSelected(true);
231231
}
232232
}
233233

@@ -296,7 +296,7 @@ public void presetButtonHandler(Event evt) {
296296
} else {
297297
loadPreset(preset);
298298
if (betaCheckBox.isSelected()) betaCheckBox.fire();
299-
if (!versionCheckBox.isSelected()) versionCheckBox.fire();
299+
if (!allSignedVersionsCheckBox.isSelected()) allSignedVersionsCheckBox.fire();
300300
TSS tss = createTSS();
301301
EventHandler<WorkerStateEvent> oldEventHandler = tss.getOnSucceeded();
302302
tss.setOnSucceeded(event -> {
@@ -720,7 +720,7 @@ private boolean checkInputs() {
720720
incorrect |= Utils.isFieldEmpty(getBoardConfig.get(), boardConfigField);
721721
incorrect |= Utils.isFieldEmpty(apnonceCheckBox, apnonceField);
722722
incorrect |= Utils.isFieldEmpty(true, pathField);
723-
incorrect |= Utils.isFieldEmpty(!versionCheckBox.isSelected(), versionField);
723+
incorrect |= Utils.isFieldEmpty(!allSignedVersionsCheckBox.isSelected() && !betaCheckBox.isSelected(), versionField);
724724
incorrect |= Utils.isFieldEmpty(betaCheckBox, ipswField);
725725
return incorrect;
726726
}
@@ -734,7 +734,7 @@ private TSS createTSS() {
734734
if (getBoardConfig.get()) {
735735
builder.setBoardConfig(boardConfigField.getText());
736736
}
737-
if (!versionCheckBox.isSelected() && !betaCheckBox.isSelected()) {
737+
if (!allSignedVersionsCheckBox.isSelected() && !betaCheckBox.isSelected()) {
738738
builder.setManualVersion(versionField.getText());
739739
} else if (betaCheckBox.isSelected()) {
740740
builder.setManualIpswURL(ipswField.getText());
@@ -747,23 +747,14 @@ private TSS createTSS() {
747747

748748
tss.setOnSucceeded(event -> {
749749
runningAlert.close();
750-
int versionsSavedAmt = tss.getValue().size();
751-
String versionsSavedString = tss.getValue().toString();
752-
versionsSavedString = versionsSavedString.substring(1, versionsSavedString.length() - 1);
753-
if (versionsSavedAmt > 1) {
754-
versionsSavedString = "\n\nFor versions " + versionsSavedString;
755-
} else if (versionsSavedAmt == 1) {
756-
versionsSavedString = "\n\nFor version " + versionsSavedString;
757-
} else {
758-
versionsSavedString = "";
759-
}
760-
Alert alert = new Alert(Alert.AlertType.INFORMATION,
761-
"Successfully saved blobs in\n" + pathField.getText() + versionsSavedString);
750+
runningAlert = null;
751+
Alert alert = new Alert(Alert.AlertType.INFORMATION, tss.getValue());
762752
alert.setHeaderText("Success!");
763753
alert.showAndWait();
764754
});
765755
tss.setOnFailed(event -> {
766756
runningAlert.close();
757+
runningAlert = null;
767758
tss.getException().printStackTrace();
768759
parseException(tss.getException());
769760
});
@@ -773,7 +764,7 @@ private TSS createTSS() {
773764
private void showRunningAlert(String title) {
774765
runningAlert = new Alert(Alert.AlertType.INFORMATION);
775766
runningAlert.setTitle(title);
776-
runningAlert.setHeaderText("Saving blobs... ");
767+
runningAlert.setHeaderText("Saving blobs... ");
777768
runningAlert.getDialogPane().setContent(new ProgressBar());
778769
Utils.forEachButton(runningAlert, button -> button.setDisable(true));
779770
runningAlert.getDialogPane().getScene().getWindow().setOnCloseRequest(Event::consume);
@@ -798,7 +789,7 @@ private void parseException(Throwable t) {
798789
} else if (message.contains("not being signed")) {
799790
if (betaCheckBox.isSelected()) {
800791
ipswField.setEffect(Utils.errorBorder);
801-
} else if (!versionCheckBox.isSelected()) {
792+
} else if (!allSignedVersionsCheckBox.isSelected()) {
802793
versionField.setEffect(Utils.errorBorder);
803794
}
804795
}

src/main/java/airsquared/blobsaver/app/Main.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,13 @@ public static void main(String[] args) {
6868
Class.forName("javafx.application.Application");
6969
if (!PlatformUtil.isMac() && !PlatformUtil.isWindows()) {
7070
try {
71-
JUnique.acquireLock("airsquared.blobsaver.app.blobsaver");
71+
JUnique.acquireLock("airsquared.blobsaver.app");
7272
} catch (AlreadyLockedException e) {
7373
javax.swing.JOptionPane.showMessageDialog(null, "blobsaver already running, exiting");
7474
System.exit(-1);
7575
}
7676
}
7777
setJNALibraryPath();
78-
if (!PlatformUtil.isMac() && !PlatformUtil.isWindows() && !PlatformUtil.isLinux()) {
79-
int result = javax.swing.JOptionPane.showOptionDialog(null, "Cannot detect the OS. Assuming it is Linux. Continue?",
80-
"Warning", javax.swing.JOptionPane.OK_CANCEL_OPTION, javax.swing.JOptionPane.WARNING_MESSAGE, null, null, null);
81-
if (result == javax.swing.JOptionPane.CANCEL_OPTION) {
82-
System.exit(0);
83-
}
84-
}
8578
JavaFxApplication.launch(JavaFxApplication.class, args);
8679
} catch (ClassNotFoundException e) {
8780
javax.swing.JOptionPane.showMessageDialog(null, "JavaFX is not installed. " +

src/main/java/airsquared/blobsaver/app/TSS.java

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@
2626
import java.net.MalformedURLException;
2727
import java.net.URL;
2828
import java.util.ArrayList;
29-
import java.util.Arrays;
3029
import java.util.Collections;
3130
import java.util.List;
3231
import java.util.regex.Matcher;
3332
import java.util.regex.Pattern;
34-
import java.util.stream.Collectors;
3533

3634
import static airsquared.blobsaver.app.Utils.containsIgnoreCase;
3735
import static airsquared.blobsaver.app.Utils.executeProgram;
3836
import static airsquared.blobsaver.app.Utils.extractBuildManifest;
3937
import static airsquared.blobsaver.app.Utils.getFirmwareList;
4038
import static airsquared.blobsaver.app.Utils.getSignedFirmwares;
4139

42-
public class TSS extends Task<List<String>> {
40+
public class TSS extends Task<String> {
4341

4442
// note: Matcher is NOT thread safe
4543
private static final Matcher ipswURLMatcher = Pattern.compile("https?://.*apple.*\\.ipsw").matcher("");
@@ -70,39 +68,44 @@ private TSS(String deviceIdentifier, String ecid, String savePath, String boardC
7068
}
7169

7270
/**
73-
* @return a list of versions that blobs have been saved for
74-
* (an empty list if a build manifest was manually supplied)
71+
* @return a printable multiline string describing the blobs saved
7572
* @throws TSSException if blobs were not saved successfully
7673
*/
7774
@Override
78-
protected List<String> call() throws TSSException {
79-
checkValues();
75+
protected String call() throws TSSException {
76+
checkInputs();
8077

81-
ArrayList<String> ipswURLs = new ArrayList<>();
82-
ArrayList<String> versionStrings = new ArrayList<>();
83-
getIpswURLS(ipswURLs, versionStrings);
78+
List<Utils.IOSVersion> iosVersions = getIOSVersions();
79+
System.out.println("iosVersions = " + iosVersions);
80+
ArrayList<String> args = constructArgs();
8481

85-
String[] args = constructArgs();
82+
StringBuilder sb = new StringBuilder("Successfully saved blobs in\n").append(savePath);
83+
if (manualIpswURL == null) {
84+
sb.append(iosVersions.size() == 1 ? "\n\nFor version " : "\n\nFor versions ");
85+
}
8686

8787
// can't use forEach() because exception won't be caught
88-
for (String ipswURL : ipswURLs) {
88+
for (Utils.IOSVersion iosVersion : iosVersions) {
8989
try {
90-
args[args.length - 1] = extractBuildManifest(ipswURL).getAbsolutePath();
90+
args.set(args.size() - 1, extractBuildManifest(iosVersion.ipswURL).getAbsolutePath());
9191
} catch (IOException e) {
9292
throw new TSSException("Unable to extract BuildManifest.", true, e);
9393
}
9494
try {
95-
System.out.println("Running: " + Arrays.toString(args));
95+
System.out.println("Running: " + args);
9696
String tssLog = executeProgram(args);
9797
parseTSSLog(tssLog);
9898
} catch (IOException e) {
9999
throw new TSSException("There was an error starting tsschecker.", true, e);
100100
}
101+
102+
if (iosVersion.versionString != null) sb.append(iosVersion.versionString).append(", ");
101103
}
102-
return versionStrings;
104+
105+
return sb.substring(0, sb.length() - 2);
103106
}
104107

105-
private void checkValues() throws TSSException {
108+
private void checkInputs() throws TSSException {
106109
boolean hasCorrectIdentifierPrefix = deviceIdentifier.startsWith("iPad") || deviceIdentifier.startsWith("iPod")
107110
|| deviceIdentifier.startsWith("iPhone") || deviceIdentifier.startsWith("AppleTV");
108111
if (!deviceIdentifier.contains(",") || !hasCorrectIdentifierPrefix) {
@@ -126,24 +129,19 @@ private void checkValues() throws TSSException {
126129
* Adds to the arraylists, depending on the fields {@link #manualIpswURL} and {@link #manualVersion}.
127130
* <p>
128131
* If both of those fields are null, it adds all signed versions.
129-
* If {@link #manualIpswURL} is specified, the parameter {@code versionStrings} will not be modified.
130132
*
131-
* @param ipswURLS an empty list that will be filled with URLs to IPSW(s)
132-
* @param versionStrings an empty list that will be filled with strings of iOS versions to be displayed to the user
133+
* @return a list of {@link Utils.IOSVersion} to save blobs for
133134
*/
134-
private void getIpswURLS(ArrayList<String> ipswURLS, ArrayList<String> versionStrings) throws TSSException {
135+
private List<Utils.IOSVersion> getIOSVersions() throws TSSException {
135136
try {
136137
if (manualVersion != null) {
137-
ipswURLS.add(getFirmwareList(deviceIdentifier).stream().filter(stringObjectMap ->
138-
manualVersion.equals(stringObjectMap.get("version")))
139-
.collect(Collectors.toList()).get(0).get("url").toString());
138+
return Collections.singletonList(getFirmwareList(deviceIdentifier).stream().filter(iosVersion ->
139+
manualVersion.equals(iosVersion.versionString)).findFirst()
140+
.orElseThrow(() -> new TSSException("No versions found.", false)));
140141
} else if (manualIpswURL != null) {
141-
ipswURLS.add(manualIpswURL);
142+
return Collections.singletonList(new Utils.IOSVersion(null, manualIpswURL, null));
142143
} else { // all signed firmwares
143-
getSignedFirmwares(deviceIdentifier).forEach(firmware -> {
144-
ipswURLS.add(firmware.get("url").toString());
145-
versionStrings.add(firmware.get("version").toString());
146-
});
144+
return getSignedFirmwares(deviceIdentifier);
147145
}
148146
} catch (FileNotFoundException e) {
149147
throw new TSSException("The device \"" + deviceIdentifier + "\" could not be found.", false, e);
@@ -152,23 +150,23 @@ private void getIpswURLS(ArrayList<String> ipswURLS, ArrayList<String> versionSt
152150
}
153151
}
154152

155-
private String[] constructArgs() {
156-
ArrayList<String> argsList = new ArrayList<>(13);
153+
private ArrayList<String> constructArgs() {
154+
ArrayList<String> args = new ArrayList<>(15);
157155
String tsscheckerPath = Utils.getTsschecker().getAbsolutePath();
158156
//noinspection ResultOfMethodCallIgnored
159157
new File(savePath).mkdirs();
160-
Collections.addAll(argsList, tsscheckerPath, "--nocache", "--save", "--device", deviceIdentifier, "--ecid", ecid, "--save-path", savePath);
158+
Collections.addAll(args, tsscheckerPath, "--nocache", "--save", "--device", deviceIdentifier, "--ecid", ecid, "--save-path", savePath);
161159
if (boardConfig != null) {
162-
Collections.addAll(argsList, "--boardconfig", boardConfig);
160+
Collections.addAll(args, "--boardconfig", boardConfig);
163161
}
164162
if (apnonce != null) {
165-
Collections.addAll(argsList, "--apnonce", apnonce);
163+
Collections.addAll(args, "--apnonce", apnonce);
166164
} else {
167-
Collections.addAll(argsList, "--generator", "0x1111111111111111");
165+
Collections.addAll(args, "--generator", "0x1111111111111111");
168166
}
169-
Collections.addAll(argsList, "--build-manifest", "");
167+
Collections.addAll(args, "--build-manifest", "");
170168

171-
return argsList.toArray(new String[0]);
169+
return args;
172170
}
173171

174172
private void parseTSSLog(String tsscheckerLog) throws TSSException {

0 commit comments

Comments
 (0)