Skip to content

Commit 4011e53

Browse files
committed
Extract native methods to the package natives
1 parent 02f7f37 commit 4011e53

File tree

9 files changed

+296
-179
lines changed

9 files changed

+296
-179
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package airsquared.blobsaver.app;
2020

21+
import airsquared.blobsaver.app.natives.Libirecovery;
2122
import com.sun.javafx.PlatformUtil;
2223
import com.sun.javafx.scene.control.skin.LabeledText;
2324
import com.sun.jna.ptr.PointerByReference;
@@ -560,8 +561,7 @@ public void chooseTimeToRunHandler() {
560561
});
561562
ChoiceBox<String> choiceBox = new ChoiceBox<>(FXCollections.observableArrayList("Minutes", "Hours", "Days", "Weeks"));
562563
choiceBox.setValue(Main.appPrefs.get("Time unit for background", "Days"));
563-
HBox hBox = new HBox();
564-
hBox.getChildren().addAll(textField, choiceBox);
564+
HBox hBox = new HBox(textField, choiceBox);
565565
alert.getDialogPane().setContent(hBox);
566566
alert.showAndWait();
567567
if ((alert.getResult() != null) && !ButtonType.CANCEL.equals(alert.getResult()) && !Utils.isEmptyOrNull(textField.getText()) && !Utils.isEmptyOrNull(choiceBox.getValue())) {
@@ -640,9 +640,9 @@ public void showWiki() {
640640
public void readInfo() {
641641
try {
642642
// read ECID
643-
ecidField.setText(Long.toHexString(Libimobiledevice.getECID(true)).toUpperCase());
643+
ecidField.setText(Long.toHexString(LibimobiledeviceUtil.getECID(true)).toUpperCase());
644644
// read device model
645-
String deviceIdentifier = Libimobiledevice.getDeviceModelIdentifier(true);
645+
String deviceIdentifier = LibimobiledeviceUtil.getDeviceModelIdentifier(true);
646646
try {
647647
deviceTypeChoiceBox.setValue(Devices.getDeviceType(deviceIdentifier));
648648
deviceModelChoiceBox.setValue(Devices.getDeviceModelIdentifiersMap().get(deviceIdentifier));
@@ -652,9 +652,9 @@ public void readInfo() {
652652
}
653653
// read board config
654654
if (!boardConfigField.isDisabled()) {
655-
boardConfigField.setText(Libimobiledevice.getBoardConfig(true));
655+
boardConfigField.setText(LibimobiledeviceUtil.getBoardConfig(true));
656656
}
657-
} catch (Libimobiledevice.LibimobiledeviceException e) {
657+
} catch (LibimobiledeviceUtil.LibimobiledeviceException e) {
658658
e.printStackTrace(); // error alert should have already been shown to user
659659
} catch (Throwable e) {
660660
e.printStackTrace();
@@ -677,7 +677,7 @@ public void readApnonce() {
677677
alert2.setHeaderText("Reading apnonce from connected device...");
678678
Utils.forEachButton(alert2, button -> button.setDisable(true));
679679

680-
Task<String> getApnonceTask = Libimobiledevice.createGetApnonceTask();
680+
Task<String> getApnonceTask = LibimobiledeviceUtil.createGetApnonceTask();
681681
getApnonceTask.setOnSucceeded(event -> Utils.forEachButton(alert2, button -> button.setDisable(false)));
682682
getApnonceTask.setOnFailed(event -> {
683683
getApnonceTask.getException().printStackTrace();
@@ -692,12 +692,16 @@ public void readApnonce() {
692692

693693
public void exitRecoveryHandler() {
694694
PointerByReference irecvClient = new PointerByReference();
695-
int errorCode = Libimobiledevice.Libirecovery.irecv_open_with_ecid(irecvClient, 0);
695+
int errorCode = Libirecovery.open(irecvClient);
696696
if (errorCode != 0) {
697697
Utils.showReportableError("irecovery error: code=" + errorCode + "\n\nUnable to find a device, try using another tool to exit recovery mode.");
698698
return;
699699
}
700-
Libimobiledevice.exitRecovery(irecvClient.getValue(), true);
700+
try {
701+
LibimobiledeviceUtil.exitRecovery(irecvClient.getValue(), true);
702+
} catch (LibimobiledeviceUtil.LibimobiledeviceException e) {
703+
e.printStackTrace();
704+
}
701705
}
702706

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

src/main/java/airsquared/blobsaver/app/Libimobiledevice.java renamed to src/main/java/airsquared/blobsaver/app/LibimobiledeviceUtil.java

Lines changed: 31 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,39 @@
1818

1919
package airsquared.blobsaver.app;
2020

21+
import airsquared.blobsaver.app.natives.Libirecovery;
22+
import airsquared.blobsaver.app.natives.Libplist;
2123
import com.sun.javafx.PlatformUtil;
22-
import com.sun.jna.Native;
2324
import com.sun.jna.Pointer;
24-
import com.sun.jna.Structure;
2525
import com.sun.jna.ptr.PointerByReference;
2626
import javafx.concurrent.Task;
2727
import javafx.scene.control.Alert;
2828
import javafx.scene.control.ButtonType;
2929

30-
/**
31-
* This class provides access to functions in the native library libimobiledevice.
32-
* <p>
33-
* See the libimobiledevice docs for
34-
* <a href="https://www.libimobiledevice.org/docs/html/lockdown_8h.html">lockdown.h</a>
35-
* and
36-
* <a href="https://www.libimobiledevice.org/docs/html/libimobiledevice_8h.html">libimobiledevice.h</a>
37-
* for information on the native functions. For information on the libplist functions,
38-
* look at the source code(files plist.c and xplist.c).
39-
*
40-
* @author airsquared
41-
*/
42-
@SuppressWarnings("WeakerAccess")
43-
public class Libimobiledevice {
30+
import static airsquared.blobsaver.app.natives.Libimobiledevice.idevice_free;
31+
import static airsquared.blobsaver.app.natives.Libimobiledevice.idevice_new;
32+
import static airsquared.blobsaver.app.natives.Libimobiledevice.lockdownd_client_free;
33+
import static airsquared.blobsaver.app.natives.Libimobiledevice.lockdownd_client_new;
34+
import static airsquared.blobsaver.app.natives.Libimobiledevice.lockdownd_enter_recovery;
35+
import static airsquared.blobsaver.app.natives.Libimobiledevice.lockdownd_get_value;
36+
import static airsquared.blobsaver.app.natives.Libimobiledevice.lockdownd_pair;
37+
4438

45-
public static long getECID(boolean showErrorAlert) {
39+
public class LibimobiledeviceUtil {
40+
41+
public static long getECID(boolean showErrorAlert) throws LibimobiledeviceException {
4642
return Long.parseLong(getKeyFromConnectedDevice("UniqueChipID", PlistType.INTEGER, showErrorAlert));
4743
}
4844

49-
public static String getDeviceModelIdentifier(boolean showErrorAlert) {
45+
public static String getDeviceModelIdentifier(boolean showErrorAlert) throws LibimobiledeviceException {
5046
return getKeyFromConnectedDevice("ProductType", PlistType.STRING, showErrorAlert);
5147
}
5248

53-
public static String getBoardConfig(boolean showErrorAlert) {
49+
public static String getBoardConfig(boolean showErrorAlert) throws LibimobiledeviceException {
5450
return getKeyFromConnectedDevice("HardwareModel", PlistType.STRING, showErrorAlert);
5551
}
5652

57-
public static String getKeyFromConnectedDevice(String key, PlistType plistType, boolean showErrorAlert) {
53+
public static String getKeyFromConnectedDevice(String key, PlistType plistType, boolean showErrorAlert) throws LibimobiledeviceException {
5854
if (plistType == null) {
5955
plistType = PlistType.STRING;
6056
}
@@ -89,7 +85,7 @@ public static String getKeyFromConnectedDevice(String key, PlistType plistType,
8985
}
9086
}
9187

92-
public static Pointer lockdowndClientFromConnectedDevice(boolean showErrorAlert) {
88+
public static Pointer lockdowndClientFromConnectedDevice(boolean showErrorAlert) throws LibimobiledeviceException {
9389
PointerByReference device = new PointerByReference();
9490
throwIfNeeded(idevice_new(device, Pointer.NULL), showErrorAlert, ErrorCodeType.idevice_error_t);
9591
PointerByReference client = new PointerByReference();
@@ -102,45 +98,45 @@ public static Pointer lockdowndClientFromConnectedDevice(boolean showErrorAlert)
10298
return client.getValue();
10399
}
104100

105-
public static void enterRecovery(boolean showErrorAlert) {
101+
public static void enterRecovery(boolean showErrorAlert) throws LibimobiledeviceException {
106102
Pointer client = lockdowndClientFromConnectedDevice(true);
107103
throwIfNeeded(lockdownd_enter_recovery(client), showErrorAlert, ErrorCodeType.lockdownd_error_t);
108104
lockdownd_client_free(client);
109105
}
110106

111-
public static void exitRecovery(Pointer irecvClient, boolean showErrorAlert) {
112-
throwIfNeeded(Libirecovery.irecv_setenv(irecvClient, "auto-boot", "true"), showErrorAlert, ErrorCodeType.irecv_error_t);
113-
throwIfNeeded(Libirecovery.irecv_saveenv(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
114-
throwIfNeeded(Libirecovery.irecv_reboot(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
115-
throwIfNeeded(Libirecovery.irecv_close(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
107+
public static void exitRecovery(Pointer irecvClient, boolean showErrorAlert) throws LibimobiledeviceException {
108+
throwIfNeeded(Libirecovery.setEnv(irecvClient, "auto-boot", "true"), showErrorAlert, ErrorCodeType.irecv_error_t);
109+
throwIfNeeded(Libirecovery.saveEnv(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
110+
throwIfNeeded(Libirecovery.reboot(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
111+
throwIfNeeded(Libirecovery.close(irecvClient), showErrorAlert, ErrorCodeType.irecv_error_t);
116112
}
117113

118114
public static Task<String> createGetApnonceTask() {
119115
return new Task<String>() {
120116
@Override
121-
protected String call() {
117+
protected String call() throws LibimobiledeviceException {
122118
updateMessage("Entering recovery mode...\n\nThis can take up to 60 seconds");
123119
System.out.println("Entering recovery mode");
124-
Libimobiledevice.enterRecovery(true);
120+
LibimobiledeviceUtil.enterRecovery(true);
125121
PointerByReference irecvClient = new PointerByReference();
126122
long endTime = System.currentTimeMillis() + 60_000; // timeout is 60 seconds
127123
int errorCode = -3;
128124
while (errorCode == -3 && System.currentTimeMillis() < endTime) {
129125
if (!sleep(1000)) {
130126
return null;
131127
}
132-
errorCode = Libimobiledevice.Libirecovery.irecv_open_with_ecid(irecvClient, 0);
128+
errorCode = Libirecovery.open(irecvClient);
133129
}
134-
throwIfNeeded(errorCode, true, Libimobiledevice.ErrorCodeType.irecv_error_t);
135-
Libirecovery.irecv_device_info deviceInfo = Libimobiledevice.Libirecovery.irecv_get_device_info(irecvClient.getValue());
130+
throwIfNeeded(errorCode, true, ErrorCodeType.irecv_error_t);
131+
Libirecovery.irecv_device_info deviceInfo = Libirecovery.getDeviceInfo(irecvClient.getValue());
136132
final StringBuilder apnonce = new StringBuilder();
137133
for (byte b : deviceInfo.ap_nonce.getByteArray(0, deviceInfo.ap_nonce_size)) {
138134
apnonce.append(String.format("%02x", b));
139135
}
140136
System.out.println("Got apnonce");
141137
updateMessage("Successfully got apnonce, exiting recovery mode...");
142138
System.out.println("Exiting recovery mode");
143-
Libimobiledevice.exitRecovery(irecvClient.getValue(), true);
139+
LibimobiledeviceUtil.exitRecovery(irecvClient.getValue(), true);
144140
sleep(3000);
145141
return apnonce.toString();
146142
}
@@ -167,78 +163,7 @@ enum ErrorCodeType {
167163
idevice_error_t, lockdownd_error_t, irecv_error_t
168164
}
169165

170-
public static native int lockdownd_get_value(Pointer client, Pointer domain, String key, PointerByReference value);
171-
172-
public static native int lockdownd_enter_recovery(Pointer client);
173-
174-
public static native int idevice_new(PointerByReference device, Pointer udid);
175-
176-
public static native int lockdownd_client_new(Pointer device, PointerByReference client, String label);
177-
178-
public static native int lockdownd_pair(Pointer lockdownd_client, Pointer lockdownd_pair_record);
179-
180-
public static native void lockdownd_client_free(Pointer client);
181-
182-
public static native void idevice_free(Pointer idevice);
183-
184-
public static class Libplist {
185-
186-
public static void getStringVal(Pointer plist, PointerByReference value) {
187-
plist_get_string_val(plist, value);
188-
}
189-
190-
public static void free(Pointer plist) {
191-
plist_free(plist);
192-
}
193-
194-
public static void toXml(Pointer plist, PointerByReference plist_xml, PointerByReference length) {
195-
plist_to_xml(plist, plist_xml, length);
196-
}
197-
198-
private static native void plist_get_string_val(Pointer plist, PointerByReference value);
199-
200-
private static native void plist_free(Pointer plist);
201-
202-
private static native void plist_to_xml(Pointer plist, PointerByReference plist_xml, PointerByReference length);
203-
204-
static {
205-
Native.register(Libplist.class, "plist");
206-
}
207-
}
208-
209-
public static class Libirecovery {
210-
211-
public static native int irecv_open_with_ecid(PointerByReference irecv_client, long ecid);
212-
213-
public static native int irecv_close(Pointer irecv_client);
214-
215-
public static native int irecv_setenv(Pointer irecv_client, String variable, String value);
216-
217-
public static native int irecv_saveenv(Pointer irecv_client);
218-
219-
public static native int irecv_reboot(Pointer irecv_client);
220-
221-
public static native irecv_device_info irecv_get_device_info(Pointer irecv_client);
222-
223-
@SuppressWarnings({"unused", "SpellCheckingInspection"})
224-
@Structure.FieldOrder({"cpid", "cprv", "cpfm", "scep", "bdid", "ecid", "ibfl", "srnm", "imei", "srtg", "serial_string", "ap_nonce", "ap_nonce_size", "sep_nonce", "sep_nonce_size"})
225-
public static class irecv_device_info extends Structure {
226-
public int cpid, cprv, cpfm, scep, bdid;
227-
public long ecid;
228-
public int ibfl;
229-
public String srnm, imei, srtg, serial_string;
230-
public Pointer ap_nonce;
231-
public int ap_nonce_size;
232-
public Pointer sep_nonce;
233-
public int sep_nonce_size;
234-
}
235-
236-
static {
237-
Native.register(Libirecovery.class, "irecovery");
238-
}
239-
}
240-
241-
public static void throwIfNeeded(int errorCode, boolean showAlert, ErrorCodeType errorType) {
166+
public static void throwIfNeeded(int errorCode, boolean showAlert, ErrorCodeType errorType) throws LibimobiledeviceException {
242167
if (errorCode == 0) {
243168
return;
244169
}
@@ -320,17 +245,8 @@ public static void throwIfNeeded(int errorCode, boolean showAlert, ErrorCodeType
320245
throw new LibimobiledeviceException(exceptionMessage);
321246
}
322247

323-
static {
324-
try {
325-
Native.register(Libimobiledevice.class, "imobiledevice");
326-
} catch (Throwable e) { // need to catch UnsatisfiedLinkError
327-
Utils.showReportableError("Error: unable to register native methods", Utils.exceptionToString(e));
328-
throw new LibimobiledeviceException("Unable to register native methods", e);
329-
}
330-
}
331-
332248
@SuppressWarnings("unused")
333-
public static class LibimobiledeviceException extends RuntimeException {
249+
public static class LibimobiledeviceException extends Exception {
334250
public LibimobiledeviceException() {
335251
super();
336252
}

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
package airsquared.blobsaver.app;
2020

21+
import airsquared.blobsaver.app.natives.Libfragmentzip;
2122
import com.sun.javafx.PlatformUtil;
22-
import com.sun.jna.Native;
2323
import com.sun.jna.Pointer;
2424
import javafx.application.Platform;
2525
import javafx.scene.Node;
@@ -372,10 +372,6 @@ public String toString() {
372372
}
373373
}
374374

375-
// static List<String> getAllSignedVersions(String deviceIdentifier) throws IOException {
376-
// return getAllSignedFirmwares(deviceIdentifier).map(map -> map.get("version").toString()).collect(Collectors.toList());
377-
// }
378-
379375
static File extractBuildManifest(String url) throws IOException {
380376
File buildManifest = File.createTempFile("BuildManifest", ".plist");
381377
System.out.println("Extracting build manifest from " + url);
@@ -392,40 +388,6 @@ static File extractBuildManifest(String url) throws IOException {
392388
}
393389
}
394390

395-
public static class Libfragmentzip {
396-
397-
public static Pointer open(String url) {
398-
return fragmentzip_open(url);
399-
}
400-
401-
public static int downloadFile(Pointer fragmentzip_t, String remotePath, String savePath) {
402-
return fragmentzip_download_file(fragmentzip_t, remotePath, savePath, null);
403-
}
404-
405-
public static void close(Pointer fragmentzip_t) {
406-
fragmentzip_close(fragmentzip_t);
407-
}
408-
409-
/**
410-
* Native declaration:
411-
* <code>fragmentzip_t *fragmentzip_open(const char *url);</code>
412-
*/
413-
private static native Pointer fragmentzip_open(String url);
414-
415-
/**
416-
* Native declaration:
417-
* <code>int fragmentzip_download_file(fragmentzip_t *info, const char *remotepath, const char *savepath, fragmentzip_process_callback_t callback);</code>
418-
*/
419-
private static native int fragmentzip_download_file(Pointer fragmentzip_t, String remotePath, String savePath,
420-
Pointer fragmentzip_process_callback_t);
421-
422-
private static native void fragmentzip_close(Pointer fragmentzip_t);
423-
424-
static {
425-
Native.register(Libfragmentzip.class, "fragmentzip");
426-
}
427-
}
428-
429391
static String exceptionToString(Throwable t) {
430392
StringWriter writer = new StringWriter();
431393
t.printStackTrace(new PrintWriter(writer));

0 commit comments

Comments
 (0)