Skip to content

Commit 211b683

Browse files
committed
Fix warnings and clean up some of the code
1 parent 8036936 commit 211b683

File tree

4 files changed

+271
-505
lines changed

4 files changed

+271
-505
lines changed

src/main/java/com/airsquared/blobsaver/Background.java

Lines changed: 20 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@
3535
import java.awt.PopupMenu;
3636
import java.awt.SystemTray;
3737
import java.awt.TrayIcon;
38-
import java.io.BufferedReader;
3938
import java.io.File;
4039
import java.io.IOException;
41-
import java.io.InputStreamReader;
4240
import java.net.URL;
43-
import java.net.URLConnection;
4441
import java.util.ArrayList;
45-
import java.util.Arrays;
4642
import java.util.List;
4743
import java.util.Map;
4844
import java.util.concurrent.Executors;
@@ -53,6 +49,7 @@
5349

5450
import static com.airsquared.blobsaver.Main.appPrefs;
5551
import static com.airsquared.blobsaver.Shared.checkForUpdates;
52+
import static com.airsquared.blobsaver.Shared.executeProgram;
5653
import static com.airsquared.blobsaver.Shared.githubIssue;
5754
import static com.airsquared.blobsaver.Shared.redditPM;
5855
import static com.airsquared.blobsaver.Shared.reportError;
@@ -65,6 +62,15 @@ class Background {
6562
private static ScheduledExecutorService executor;
6663
private static TrayIcon trayIcon;
6764

65+
static ArrayList<String> getPresetsToSaveFor() {
66+
ArrayList<String> presetsToSaveFor = new ArrayList<>();
67+
JSONArray presetsToSaveForJson = new JSONArray(appPrefs.get("Presets to save in background", "[]"));
68+
for (int i = 0; i < presetsToSaveForJson.length(); i++) {
69+
presetsToSaveFor.add(presetsToSaveForJson.getString(i));
70+
}
71+
return presetsToSaveFor;
72+
}
73+
6874
static void startBackground(boolean runOnlyOnce) {
6975
ArrayList<Integer> presetsToSave = new ArrayList<>();
7076
JSONArray presetsToSaveJson = new JSONArray(appPrefs.get("Presets to save in background", "[]"));
@@ -184,15 +190,9 @@ private static void saveBackgroundBlobs(int preset) {
184190
identifier = Shared.textToIdentifier(presetPrefs.get("Device Model", ""));
185191
}
186192
log("identifier:" + identifier);
187-
StringBuilder response = new StringBuilder();
193+
String response;
188194
try {
189-
URLConnection urlConnection = new URL("https://api.ipsw.me/v4/device/" + identifier).openConnection();
190-
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
191-
String inputLine;
192-
while ((inputLine = in.readLine()) != null) {
193-
response.append(inputLine);
194-
}
195-
in.close();
195+
response = Shared.makeRequest(new URL("https://api.ipsw.me/v4/device/" + identifier));
196196
} catch (IOException e) {
197197
Notification notification = new Notification("Saving blobs failed", "Check your internet connection.\nIf it is working, click here to report this error.", Notification.ERROR_ICON);
198198
Notification.Notifier.INSTANCE.setPopupLifetime(Duration.minutes(1));
@@ -211,7 +211,7 @@ private static void saveBackgroundBlobs(int preset) {
211211
return;
212212
}
213213
log("made request");
214-
JSONArray firmwareListJson = new JSONObject(response.toString()).getJSONArray("firmwares");
214+
JSONArray firmwareListJson = new JSONObject(response).getJSONArray("firmwares");
215215
@SuppressWarnings("unchecked") List<Map<String, Object>> firmwareList = (List) firmwareListJson.toList();
216216
List<String> signedVersions = firmwareList.stream().filter(map -> Boolean.TRUE.equals(map.get("signed"))).map(map -> map.get("version").toString()).collect(Collectors.toList());
217217
log("signed versions:" + signedVersions);
@@ -255,65 +255,18 @@ private static void saveBackgroundBlobs(int preset) {
255255
Notification.Notifier.INSTANCE.notify(notification);
256256
return;
257257
}
258-
tsschecker.deleteOnExit();
259-
if (!tsschecker.setExecutable(true, false)) {
260-
Notification notification = new Notification("Saving blobs failed", "There was an error setting tsschecker as executable. Click here to report this error.", Notification.ERROR_ICON);
261-
Notification.Notifier.INSTANCE.setPopupLifetime(Duration.minutes(1));
262-
Notification.Notifier.INSTANCE.setOnNotificationPressed((event) -> {
263-
Notification.Notifier.INSTANCE.stop();
264-
showStage();
265-
Alert alert = new Alert(Alert.AlertType.ERROR,
266-
"There was an error setting tsschecker as executable.\n\n. Please create a new issue on Github or PM me on Reddit.",
267-
githubIssue, redditPM, ButtonType.OK);
268-
resizeAlertButtons(alert);
269-
alert.showAndWait();
270-
alert.getDialogPane().toFront();
271-
reportError(alert);
272-
});
273-
Notification.Notifier.INSTANCE.notify(notification);
274-
deleteTempFiles(tsschecker);
275-
return;
276-
}
277258

278259
//noinspection ResultOfMethodCallIgnored
279260
new File(path).mkdirs();
280-
ArrayList<String> args;
281-
args = new ArrayList<>(Arrays.asList(tsschecker.getPath(), "-d", identifier, "-s", "-e", ecid, "--save-path", path, "-i", version));
282-
if (!boardConfig.equals("none")) {
283-
args.add("--boardconfig");
284-
args.add(boardConfig);
285-
}
286-
Process proc;
287-
try {
288-
log("running:" + args.toString());
289-
proc = new ProcessBuilder(args).start();
290-
} catch (IOException e) {
291-
Notification notification = new Notification("Saving blobs failed", "There was an error starting tsschecker. Click here to report this error.", Notification.ERROR_ICON);
292-
Notification.Notifier.INSTANCE.setPopupLifetime(Duration.minutes(1));
293-
Notification.Notifier.INSTANCE.setOnNotificationPressed((event) -> {
294-
Notification.Notifier.INSTANCE.stop();
295-
showStage();
296-
Alert alert = new Alert(Alert.AlertType.ERROR,
297-
"There was an error starting tsschecker.\n\nPlease create a new issue on Github or PM me on Reddit. The log has been copied to your clipboard.",
298-
githubIssue, redditPM, ButtonType.OK);
299-
resizeAlertButtons(alert);
300-
alert.showAndWait();
301-
alert.getDialogPane().toFront();
302-
reportError(alert, e.getMessage());
303-
});
304-
Notification.Notifier.INSTANCE.notify(notification);
305-
deleteTempFiles(tsschecker);
306-
return;
307-
}
308261
String tsscheckerLog;
309-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
310-
StringBuilder logBuilder = new StringBuilder();
311-
String line;
312-
while ((line = reader.readLine()) != null) {
313-
System.out.println(line + "\n");
314-
logBuilder.append(line).append("\n");
262+
try {
263+
if (!"none".equals(boardConfig) && !"".equals(boardConfig)) { // needs board config
264+
tsscheckerLog = executeProgram(tsschecker.getPath(), "-d", identifier, "-s", "-e", ecid,
265+
"--save-path", path, "-i", version, "--boardconfig", boardConfig);
266+
} else {
267+
tsscheckerLog = executeProgram(tsschecker.getPath(), "-d", identifier, "-s", "-e", ecid,
268+
"--save-path", path, "-i", version);
315269
}
316-
tsscheckerLog = logBuilder.toString();
317270
} catch (IOException e) {
318271
Notification notification = new Notification("Saving blobs failed", "There was an error starting tsschecker. Click here to report this error.", Notification.ERROR_ICON);
319272
Notification.Notifier.INSTANCE.setPopupLifetime(Duration.minutes(1));
@@ -329,7 +282,6 @@ private static void saveBackgroundBlobs(int preset) {
329282
reportError(alert, e.getMessage());
330283
});
331284
Notification.Notifier.INSTANCE.notify(notification);
332-
deleteTempFiles(tsschecker);
333285
return;
334286
}
335287
String presetName;
@@ -372,7 +324,6 @@ private static void saveBackgroundBlobs(int preset) {
372324
});
373325
Notification.Notifier.INSTANCE.notify(notification);
374326
} else if (tsscheckerLog.contains("iOS " + version + " for device " + identifier + " IS NOT being signed")) {
375-
deleteTempFiles(tsschecker);
376327
return;
377328
} else {
378329
Notification notification = new Notification("Saving blobs failed", "An unknown error occurred. Click here to report this error.", Notification.ERROR_ICON);
@@ -389,25 +340,6 @@ private static void saveBackgroundBlobs(int preset) {
389340
});
390341
Notification.Notifier.INSTANCE.notify(notification);
391342
}
392-
try {
393-
proc.waitFor();
394-
} catch (InterruptedException e) {
395-
Notification notification = new Notification("Saving blobs failed", "There was an error starting tsschecker. Click here to report this error.", Notification.ERROR_ICON);
396-
Notification.Notifier.INSTANCE.setPopupLifetime(Duration.minutes(1));
397-
Notification.Notifier.INSTANCE.setOnNotificationPressed((event) -> {
398-
Notification.Notifier.INSTANCE.stop();
399-
showStage();
400-
Alert alert = new Alert(Alert.AlertType.ERROR,
401-
"There tsschecker process was interrupted.\n\nPlease create a new issue on Github or PM me on Reddit. The log has been copied to your clipboard.",
402-
githubIssue, redditPM, ButtonType.OK);
403-
resizeAlertButtons(alert);
404-
alert.showAndWait();
405-
alert.getDialogPane().toFront();
406-
reportError(alert, e.getMessage());
407-
});
408-
Notification.Notifier.INSTANCE.notify(notification);
409-
}
410-
deleteTempFiles(tsschecker);
411343
savedVersions.add(version);
412344
presetPrefs.put("Saved Versions", new JSONArray(savedVersions).toString());
413345
log("it worked");
@@ -440,13 +372,6 @@ static void stopBackground(boolean showAlert) {
440372
log("stopped background");
441373
}
442374

443-
@SuppressWarnings("ResultOfMethodCallIgnored")
444-
private static void deleteTempFiles(File tsschecker) {
445-
if (tsschecker.exists()) {
446-
tsschecker.delete();
447-
}
448-
}
449-
450375
private static void log(String msg) {
451376
System.out.println(msg);
452377
}

0 commit comments

Comments
 (0)