Skip to content

Commit

Permalink
minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
airsquared committed Mar 22, 2020
1 parent 577ad44 commit 6d6eeef
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 83 deletions.
21 changes: 8 additions & 13 deletions src/main/java/com/airsquared/blobsaver/Background.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ static void startBackground(boolean runOnlyOnce) {
// add the application tray icon to the system tray.
try {
tray.add(trayIcon);
log("in tray");
System.out.println("in tray");
} catch (AWTException e) {
e.printStackTrace();
}
}
if (runOnlyOnce) {
if (!presetsToSave.isEmpty()) {
log("there are some presets to save");
System.out.println("there are some presets to save");
presetsToSave.forEach(Background::saveBackgroundBlobs);
}
inBackground = false;
Expand All @@ -162,25 +162,24 @@ static void startBackground(boolean runOnlyOnce) {
}
executor.scheduleAtFixedRate(() -> {
if (!presetsToSave.isEmpty()) {
log("there are some presets to save");
System.out.println("there are some presets to save");
presetsToSave.forEach(Background::saveBackgroundBlobs);
}
log("done w execution of executor");
System.out.println("done with executor");
}, 0, timeAmount, timeUnit);
executor.scheduleAtFixedRate(() -> checkForUpdates(false), 4, 4, TimeUnit.DAYS);
}
}

private static void saveBackgroundBlobs(int preset) {
log("attempting to save for preset " + preset);
System.out.println("attempting to save for preset " + preset);
Preferences presetPrefs = Preferences.userRoot().node("airsquared/blobsaver/preset" + preset);
String identifier;
if ("none".equals(presetPrefs.get("Device Model", ""))) {
identifier = presetPrefs.get("Device Identifier", "");
} else {
identifier = textToIdentifier(presetPrefs.get("Device Model", ""));
}
log("identifier:" + identifier);
List<Map<String, Object>> signedFirmwares;
try {
signedFirmwares = getAllSignedFirmwares(identifier);
Expand Down Expand Up @@ -259,7 +258,7 @@ private static void saveBackgroundBlobs(int preset) {
});
Notification.Notifier.INSTANCE.notify(notification);

log("displayed message");
System.out.println("displayed message");

} else if (containsIgnoreCase(tsscheckerLog, "Could not resolve host")) {
Notification notification = new Notification("Saving blobs failed", "Check your internet connection. If it is working, click here to report this error.", Notification.ERROR_ICON);
Expand Down Expand Up @@ -293,7 +292,7 @@ private static void saveBackgroundBlobs(int preset) {
});
Notification.Notifier.INSTANCE.notify(notification);
}
log("it worked");
System.out.println("Success");
}
}

Expand All @@ -313,10 +312,6 @@ static void stopBackground(boolean showAlert) {
alert.showAndWait();
});
}
log("stopped background");
}

private static void log(String msg) {
System.out.println(msg);
System.out.println("Stopped background");
}
}
76 changes: 13 additions & 63 deletions src/main/java/com/airsquared/blobsaver/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public class Controller {
private boolean editingPresets = false;
private boolean choosingRunInBackground = false;

static DropShadow errorBorder = new DropShadow();
private static DropShadow borderGlow = new DropShadow();
static DropShadow errorBorder = new DropShadow(9.5, 0f, 0f, Color.RED);
private static DropShadow borderGlow = new DropShadow(9.5, 0f, 0f, Color.DARKCYAN);

static Controller INSTANCE;

Expand All @@ -117,19 +117,6 @@ static void afterStageShowing() {
@FXML
public void initialize() {
INSTANCE = this;
// create effects
borderGlow.setOffsetY(0f);
borderGlow.setOffsetX(0f);
borderGlow.setColor(Color.DARKCYAN);
borderGlow.setWidth(20);
borderGlow.setHeight(20);
errorBorder.setOffsetY(0f);
errorBorder.setOffsetX(0f);
errorBorder.setColor(Color.RED);
errorBorder.setWidth(20);
errorBorder.setHeight(20);

deviceTypeChoiceBox.setItems(FXCollections.observableArrayList(Devices.getDeviceTypes()));

deviceTypeChoiceBox.getSelectionModel().selectedItemProperty().addListener((x, y, newValue) -> {
deviceTypeChoiceBox.setEffect(null);
Expand Down Expand Up @@ -170,20 +157,8 @@ public void initialize() {
presetButtons = new ArrayList<>(Arrays.asList(preset1Button, preset2Button, preset3Button, preset4Button, preset5Button, preset6Button, preset7Button, preset8Button, preset9Button, preset10Button));
presetButtons.forEach(btn -> btn.setOnAction(this::presetButtonHandler));

// the following is to set the path to save blobs to the correct location
String path = Main.jarDirectory.getAbsolutePath();
if (path.endsWith("blobsaver.app/Contents/Java")) {
path = path.replaceAll("blobsaver\\.app/Contents/Java", "");
}
if (path.contains("\\Program Files") || path.contains("/Applications")) {
path = System.getProperty("user.home");
}
if (path.endsWith(System.getProperty("file.separator"))) {
path = path + "Blobs";
} else {
path = path + System.getProperty("file.separator") + "Blobs";
}
pathField.setText(path);

pathField.setText(new File(System.getProperty("user.home"), "Blobs").getAbsolutePath());


if (PlatformUtil.isMac()) {
Expand Down Expand Up @@ -250,7 +225,6 @@ public void versionCheckBoxHandler() {
versionField.setText("");
} else {
versionField.setEffect(borderGlow);

versionField.setDisable(false);
}
}
Expand Down Expand Up @@ -367,12 +341,12 @@ private void presetButtonHandler(ActionEvent evt) {
if (presetsToSaveFor.isEmpty()) {
appPrefs.putBoolean("Background setup", false);
}
log("removed " + preset + " from list");
System.out.println("removed " + preset + " from list");
backgroundSettingsButton.fire();
} else {
presetsToSaveFor.add(Integer.toString(preset));
appPrefs.putBoolean("Background setup", true);
log("added preset" + preset + " to list");
System.out.println("added preset" + preset + " to list");
Alert alert = new Alert(Alert.AlertType.INFORMATION, "If it doesn't work, please remove it, fix the error, and add it back");
alert.setTitle("Testing preset " + preset);
alert.setHeaderText("Testing preset");
Expand Down Expand Up @@ -657,7 +631,7 @@ private void useMacOSMenuBar() {

// needs to be run with Platform.runLater(), otherwise the application menu doesn't show up
Platform.runLater(() -> tk.setGlobalMenuBar(macOSMenuBar));
log("using macOS menu bar");
System.out.println("using macOS menu bar");
}

public void backgroundSettingsHandler() {
Expand Down Expand Up @@ -733,11 +707,9 @@ public void chooseTimeToRunHandler() {
alert.getDialogPane().setContent(hBox);
alert.showAndWait();
if ((alert.getResult() != null) && !ButtonType.CANCEL.equals(alert.getResult()) && !"".equals(textField.getText()) && (choiceBox.getValue() != null)) {
log("info given");
appPrefs.putInt("Time to run", Integer.parseInt(textField.getText()));
appPrefs.put("Time unit for background", choiceBox.getValue());
} else {
log("alert menu canceled");
backgroundSettingsButton.fire(); //goes back to main menu
return;
}
Expand Down Expand Up @@ -797,8 +769,6 @@ public void resetAppHandler() {
prefs.clear();
prefs.removeNode();
prefs.flush();
File blobsaver_bin = new File(System.getProperty("user.home"), ".blobsaver_bin");
deleteFolder(blobsaver_bin);
Alert applicationCloseAlert = new Alert(Alert.AlertType.INFORMATION, "The application data and files have been removed. If you are running Windows, you still will need to run the uninstall .exe. Otherwise, you can just delete the .app or .jar file.\nThe application will now exit.", ButtonType.OK);
applicationCloseAlert.showAndWait();
Platform.exit();
Expand All @@ -807,22 +777,6 @@ public void resetAppHandler() {
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void deleteFolder(File folder) {
if (folder.exists()) {
File[] files = folder.listFiles();
if (files != null) {
Arrays.asList(files).forEach(file -> {
if (file.isDirectory()) {
deleteFolder(file);
} else {
file.delete();
}
});
}
}
}

public void debugLogHandler() {
if (DebugWindow.isShowing()) {
DebugWindow.hide();
Expand Down Expand Up @@ -907,8 +861,6 @@ public void readInfo() {

public void donate() { openURL("https://www.paypal.me/airsqrd"); }

private static void log(String msg) { System.out.println(msg); }

@SuppressWarnings("Duplicates")
public void goButtonHandler() {
boolean doReturn = false;
Expand All @@ -928,22 +880,20 @@ public void goButtonHandler() {
doReturn = doReturn || isTextFieldInvalid(!versionCheckBox.isSelected(), versionField);
doReturn = doReturn || isTextFieldInvalid(betaCheckBox, buildIDField);
doReturn = doReturn || isTextFieldInvalid(betaCheckBox, ipswField);
if (doReturn) {
return;
}
if (doReturn) return;

String deviceModel = (String) deviceModelChoiceBox.getValue();
if ("".equals(deviceModel)) {
String identifierText = identifierField.getText();
try {
if (identifierText.startsWith("iPad") || identifierText.startsWith("iPod") || identifierText.startsWith("iPhone") || identifierText.startsWith("AppleTV")) {
TSSChecker.run(identifierText);
} else {
identifierField.setEffect(errorBorder);
newUnreportableError("\"" + identifierText + "\" is not a valid identifier");
return;
}
} catch (StringIndexOutOfBoundsException e) {
newUnreportableError("\"" + identifierText + "\" is not a valid identifier");
} catch (StringIndexOutOfBoundsException ignored) {
}
identifierField.setEffect(errorBorder);
newUnreportableError("\"" + identifierText + "\" is not a valid identifier");
} else {
TSSChecker.run(textToIdentifier(deviceModel));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/airsquared/blobsaver/Devices.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Map;
import java.util.Properties;

class Devices {
public class Devices {

private static final ObservableList<String> iPhones = FXCollections.observableArrayList("iPhone 3G[S]", "iPhone 4 (GSM)",
"iPhone 4 (GSM 2012)", "iPhone 4 (CDMA)", "iPhone 4[S]", "iPhone 5 (GSM)", "iPhone 5 (Global)",
Expand Down Expand Up @@ -91,7 +91,7 @@ static ObservableList<String> getAppleTVs() {
return AppleTVs;
}

static ObservableList<String> getDeviceTypes() {
public static ObservableList<String> getDeviceTypes() {
return deviceTypes;
}

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/airsquared/blobsaver/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ static void showStage() {
if (PlatformUtil.isMac()) {
DockVisibility.show();
}
System.out.println(1);
try {
primaryStage.show();
} catch (Throwable t) {
t.printStackTrace();
}
System.out.println(2);
primaryStage.centerOnScreen();
System.out.println(3);
primaryStage.requestFocus();
System.out.println(4);
}

static void hideStage() {
Expand Down Expand Up @@ -153,7 +149,13 @@ public void init() {
@SuppressWarnings("unchecked")
public void start(Stage primaryStage) throws IOException, ReflectiveOperationException {
Main.primaryStage = primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("blobsaver.fxml"));
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("blobsaver.fxml"));
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
primaryStage.setTitle("blobsaver " + Main.appVersion);
primaryStage.setScene(new Scene(root));
primaryStage.getScene().getStylesheets().add(getClass().getResource("app.css").toExternalForm());
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/com/airsquared/blobsaver/blobsaver.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
~ along with blobsaver. If not, see <https://www.gnu.org/licenses/>.
-->

<?import com.airsquared.blobsaver.Devices?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
Expand Down Expand Up @@ -119,6 +120,9 @@
<HBox.margin>
<Insets left="10.0"/>
</HBox.margin>
<items>
<Devices fx:factory="getDeviceTypes"/>
</items>
</ChoiceBox>
<ChoiceBox fx:id="deviceModelChoiceBox" prefWidth="150.0">
<HBox.margin>
Expand Down

0 comments on commit 6d6eeef

Please sign in to comment.