Skip to content

Commit f154ff9

Browse files
committed
Rewrite Devices.java
1 parent d2947b9 commit f154ff9

File tree

7 files changed

+211
-249
lines changed

7 files changed

+211
-249
lines changed

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

Lines changed: 51 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,67 +23,19 @@
2323

2424
import java.io.IOException;
2525
import java.io.UncheckedIOException;
26+
import java.util.ArrayList;
2627
import java.util.HashMap;
28+
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Properties;
31+
import java.util.function.Consumer;
2932

3033
@SuppressWarnings({"unchecked", "rawtypes"})
3134
public final class Devices {
3235

33-
private static final String[] iPhones = {"iPhone 3G[S]", "iPhone 4 (GSM)",
34-
"iPhone 4 (GSM 2012)", "iPhone 4 (CDMA)", "iPhone 4[S]", "iPhone 5 (GSM)", "iPhone 5 (Global)",
35-
"iPhone 5c (GSM)", "iPhone 5c (Global)", "iPhone 5s (GSM)", "iPhone 5s (Global)",
36-
"iPhone 6+", "iPhone 6", "iPhone 6s", "iPhone 6s+", "iPhone SE", "iPhone 7 (Global)(iPhone9,1)",
37-
"iPhone 7+ (Global)(iPhone9,2)", "iPhone 7 (GSM)(iPhone9,3)", "iPhone 7+ (GSM)(iPhone9,4)",
38-
"iPhone 8 (iPhone10,1)", "iPhone 8+ (iPhone10,2)", "iPhone X (iPhone10,3)", "iPhone 8 (iPhone10,4)",
39-
"iPhone 8+ (iPhone10,5)", "iPhone X (iPhone10,6)", "iPhone XS (Global) (iPhone11,2)",
40-
"iPhone XS Max (China) (iPhone11,4)", "iPhone XS Max (iPhone11,6)", "iPhone XR (iPhone11,8)",
41-
"iPhone 11 (iPhone12,1)", "iPhone 11 Pro (iPhone12,3)", "iPhone 11 Pro Max (iPhone12,5)",
42-
"iPhone SE 2 (iPhone12,8)", "iPhone 12 mini (iPhone13,1)", "iPhone 12 (iPhone13,2)",
43-
"iPhone 12 Pro (iPhone13,3)", "iPhone 12 Pro Max (iPhone13,4)",
44-
"iPhone 13 Pro (iPhone14,2)", "iPhone 13 Pro Max (iPhone14,3)",
45-
"iPhone 13 mini (iPhone14,4)", "iPhone 13 (iPhone14,5)", "iPhone SE 3 (iPhone14,6)", "iPhone 14 (iPhone14,7)",
46-
"iPhone 14+ (iPhone14,8)", "iPhone 14 Pro (iPhone15,2)", "iPhone 14 Pro Max (iPhone15,3)"};
47-
private static final String[] iPads = {"iPad 1", "iPad 2 (WiFi)", "iPad 2 (GSM)",
48-
"iPad 2 (CDMA)", "iPad 2 (Mid 2012)", "iPad Mini (Wifi)", "iPad Mini (GSM)", "iPad Mini (Global)",
49-
"iPad 3 (WiFi)", "iPad 3 (CDMA)", "iPad 3 (GSM)", "iPad 4 (WiFi)", "iPad 4 (GSM)", "iPad 4 (Global)",
50-
"iPad Air (Wifi)", "iPad Air (Cellular)", "iPad Air (China)", "iPad Mini 2 (WiFi)", "iPad Mini 2 (Cellular)",
51-
"iPad Mini 2 (China)", "iPad Mini 3 (WiFi)", "iPad Mini 3 (Cellular)", "iPad Mini 3 (China)",
52-
"iPad Mini 4 (Wifi)", "iPad Mini 4 (Cellular)", "iPad Air 2 (WiFi)", "iPad Air 2 (Cellular)",
53-
"iPad Pro 9.7 (Wifi)", "iPad Pro 9.7 (Cellular)", "iPad Pro 12.9 (WiFi)", "iPad Pro 12.9 (Cellular)",
54-
"iPad 5 (Wifi)", "iPad 5 (Cellular)", "iPad Pro 2 12.9 (WiFi)(iPad7,1)", "iPad Pro 2 12.9 (Cellular)(iPad7,2)",
55-
"iPad Pro 10.5 (WiFi)(iPad7,3)", "iPad Pro 10.5 (Cellular)(iPad7,4)", "iPad 6 (WiFi)(iPad 7,5)",
56-
"iPad 6 (Cellular)(iPad7,6)", "iPad 7 (WiFi)(iPad7,11)", "iPad 7 (Cellular)(iPad7,12)",
57-
"iPad Pro 3 11' (WiFi)(iPad8,1)", "iPad Pro 3 11' (WiFi)(iPad8,2)", "iPad Pro 3 11' (Cellular)(iPad8,3)",
58-
"iPad Pro 3 11' (Cellular)(iPad8,4)", "iPad Pro 3 12.9'(WiFi)(iPad8,5)", "iPad Pro 3 12.9 (WiFi)(iPad8,6)",
59-
"iPad Pro 3 12.9 (Cellular)(iPad8,7)", "iPad Pro 3 12.9 (Cellular)(iPad8,8)", "iPad Pro 4 11' (WiFi)(iPad8,9)",
60-
"iPad Pro 4 11' (Cellular)(iPad8,10)", "iPad Pro 4 12.9' (WiFi)(iPad8,11)",
61-
"iPad Pro 4 12.9' (Cellular)(iPad8,12)", "iPad Mini 5 (WiFi)(iPad11,1)", "iPad Mini 5 (Cellular)(iPad11,2)",
62-
"iPad Air 3 (WiFi)(iPad11,3)", "iPad Air 3 (Cellular)(iPad11,4)", "iPad 8 (WiFi) (iPad11,6)",
63-
"iPad 8 (Cellular) (iPad11,7)", "iPad 9 (WiFi) (iPad12,1)", "iPad 9 (Cellular) (iPad12,2)",
64-
"iPad Air 4 (WiFi) (iPad13,1)", "iPad Air 4 (Cellular) (iPad13,2)",
65-
"iPad Pro 11' (3rd gen) (WiFi) (iPad13,4)", "iPad Pro 11' (3rd gen) (WiFi) (iPad13,5)",
66-
"iPad Pro 11' (3rd gen) (Cellular) (iPad13,6)", "iPad Pro 11' (3rd gen) (Cellular) (iPad13,7)",
67-
"iPad Pro 12.9' (5th gen) (WiFi) (iPad13,8)", "iPad Pro 12.9' (5th gen) (WiFi) (iPad13,9)",
68-
"iPad Pro 12.9' (5th gen) (Cellular) (iPad13,10)", "iPad Pro 12.9' (5th gen) (Cellular) (iPad13,11)",
69-
"iPad Mini 6 (WiFi) (iPad14,1)", "iPad Mini 6 (Cellular) (iPad14,2)", "iPad Air 5 (WiFi) (iPad13,16)",
70-
"iPad Air 5 (Cellular) (iPad13,17)", "iPad 10 (WiFi) (iPad13,18)", "iPad 10 (Cellular) (iPad13,19)",
71-
"iPad Pro 11' (4th gen) (WiFi) (iPad14,3)", "iPad Pro 11' (4th gen) (Cellular) (iPad14,4)",
72-
"iPad Pro 12.9' (6th gen) (WiFi) (iPad14,5)", "iPad Pro 12.9' (6th gen) (Cellular) (iPad14,6)"};
73-
74-
private static final String[] iBridges = {"iMac Pro (2017) (iMacPro1,1)", "MacBook Pro 15' (2018-2019) (MacBookPro15,1)",
75-
"MacBook Pro 13' 2018-2019) Four Thunderbolt 3 ports, (MacBookPro15,2)", "Mac mini (2018) (Macmini8,1)",
76-
"Mac Pro (2019) (MacPro7,1)", "MacBook Pro 15' (2019, AMD Radeon Pro Vega graphics) (MacBookPro15,3)",
77-
"MacBook Air 13' (Retina, 2018) (MacBookAir8,1)", "MacBook Pro 13' (2019, Two Thunderbolt 3 ports) (MacBookPro15,4)",
78-
"MacBook Air 13' (Retina, 2019) (MacBookAir8,2)", "MacBook Pro 16' (2019) (MacBookPro16,1)",
79-
"MacBook Air 13' (Retina, 2020) (MacBookAir9,1)", "MacBook Pro 13' (2020, Four Thunderbolt 3 ports) (MacBookPro16,2)",
80-
"iMac 27' (Retina 5K, 2020) (iMac20,1)", "iMac 27' (Retina 5K, 2020) (iMac20,2)",
81-
"MacBook Pro 13' (2020, Two Thunderbolt 3 ports) (MacBookPro16,3)",
82-
"MacBook Pro 16' (2019, AMD Radeon Pro 5600M graphics) (MacBookPro16,4)"};
83-
84-
private static final ObservableList<String> iPhoneList = FXCollections.observableArrayList(iPhones);
85-
private static final ObservableList<String> iPadList = FXCollections.observableArrayList(iPads);
86-
private static final ObservableList<String> iBridgeList = FXCollections.observableArrayList(iBridges);
36+
private static final List<String> iPhones = new ArrayList<>(), iPads = new ArrayList<>();
37+
38+
private static final ObservableList<String> iPhoneList, iPadList, iBridgeList = FXCollections.observableArrayList();
8739

8840
private static final ObservableList<String> iPodList = unmodifiableArrayList("iPod Touch 3", "iPod Touch 4",
8941
"iPod Touch 5", "iPod Touch 6", "iPod Touch 7 (iPod9,1)");
@@ -93,18 +45,20 @@ public final class Devices {
9345

9446
private static final ObservableList<String> deviceTypes = unmodifiableArrayList("iPhone", "iPod", "iPad", "AppleTV", "T2 Mac");
9547

96-
private static final Map<String, String> boardConfigs;
97-
98-
private static final Map<String, String> deviceModelIdentifiers;
48+
private static final Map<String, String> boardConfigs = (Map) new Properties();
9949

100-
private static final Map<String, String> identifierDeviceModels = new HashMap<>();
50+
private static final Map<String, String> deviceModelIdentifiers = new HashMap<>(), identifierDeviceModels = new HashMap<>();
10151

10252
static {
10353
try {
104-
deviceModelIdentifiers = loadProperties("devicemodels.properties");
105-
deviceModelIdentifiers.forEach((k, v) -> identifierDeviceModels.put(v, k));
54+
try (var stream = Devices.class.getResourceAsStream("boardconfigs.properties")) {
55+
((Properties) (Map) boardConfigs).load(stream);
56+
}
57+
58+
loadProperties();
10659

107-
boardConfigs = loadProperties("boardconfigs.properties");
60+
iPadList = FXCollections.observableArrayList(iPads);
61+
iPhoneList = FXCollections.observableArrayList(iPhones);
10862
} catch (IOException e) {
10963
throw new UncheckedIOException(e);
11064
}
@@ -119,8 +73,8 @@ public static void updateLists() {
11973
if (Prefs.getShowOldDevices()) {
12074
iPhoneList.setAll(iPhones);
12175
iPadList.setAll(iPads);
122-
} else if (!iPhoneList.get(0).equals("iPhone 6+")) {
123-
iPhoneList.remove(0, iPhoneList.indexOf("iPhone 6+"));
76+
} else if (!iPhoneList.get(0).equals("iPhone 6s")) {
77+
iPhoneList.remove(0, iPhoneList.indexOf("iPhone 6s"));
12478
iPadList.remove(0, iPadList.indexOf("iPad Mini 4 (Wifi)"));
12579
}
12680
}
@@ -129,8 +83,6 @@ public static ObservableList<String> getiPhoneList() {
12983
return iPhoneList;
13084
}
13185

132-
// other accessors not used
133-
13486
public static ObservableList<String> getDeviceTypes() {
13587
return deviceTypes;
13688
}
@@ -205,21 +157,46 @@ public static boolean doesRequireBoardConfig(String deviceIdentifier) {
205157
}
206158

207159
public static boolean doesRequireApnonce(String deviceIdentifier) {
208-
return deviceIdentifier.startsWith("iPhone11,") || deviceIdentifier.startsWith("iPhone12,") ||
209-
deviceIdentifier.startsWith("iPhone13,") || deviceIdentifier.startsWith("iPhone14,") ||
210-
deviceIdentifier.startsWith("iPad8,") || deviceIdentifier.startsWith("iPad11,") ||
211-
deviceIdentifier.startsWith("iPad12,") || deviceIdentifier.startsWith("iPad13,") ||
212-
deviceIdentifier.startsWith("iPad14,");
160+
String model = identifierToModel(deviceIdentifier);
161+
if (deviceIdentifier.startsWith("iPhone")) {
162+
return iPhoneList.indexOf(model) >= iPhoneList.indexOf(identifierToModel("iPhone11,2"));
163+
} else if (deviceIdentifier.startsWith("iPad")) {
164+
return iPadList.indexOf(model) >= iPadList.indexOf((identifierToModel("iPad8,1")));
165+
} else {
166+
return false;
167+
}
213168
}
214169

215170
@SafeVarargs
216171
private static <T> ObservableList<T> unmodifiableArrayList(T... items) {
217172
return FXCollections.unmodifiableObservableList(FXCollections.observableArrayList(items));
218173
}
219174

220-
private static Map<String, String> loadProperties(String resourceName) throws IOException {
221-
Properties properties = new Properties();
222-
properties.load(Devices.class.getResourceAsStream(resourceName));
223-
return (Map) properties;
175+
private static void loadProperties() throws IOException {
176+
177+
var loader = new Properties() {
178+
private Consumer<String> keyProcessor;
179+
180+
void load(String resourceName, Consumer<String> keyProcessor) throws IOException {
181+
this.keyProcessor = keyProcessor;
182+
try (var stream = Devices.class.getResourceAsStream(resourceName)) {
183+
load(stream);
184+
}
185+
}
186+
187+
@Override
188+
public Object put(Object key, Object value) {
189+
deviceModelIdentifiers.put((String) key, (String) value);
190+
identifierDeviceModels.put((String) value, (String) key);
191+
keyProcessor.accept((String) key);
192+
return null;
193+
}
194+
};
195+
196+
loader.load("devicemodels/iPhones.properties", iPhones::add);
197+
loader.load("devicemodels/iPads.properties", iPads::add);
198+
loader.load("devicemodels/iBridges.properties", iBridgeList::add);
199+
loader.load("devicemodels/others.properties", o -> {
200+
});
224201
}
225202
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ private void deleteIfPossible(Path file) {
162162
}
163163

164164
private void checkInputs() throws TSSException {
165-
boolean hasCorrectIdentifierPrefix = deviceIdentifier.startsWith("iPad") || deviceIdentifier.startsWith("iPod")
166-
|| deviceIdentifier.startsWith("iPhone") || deviceIdentifier.startsWith("AppleTV")
167-
|| deviceIdentifier.startsWith("iBridge");
168-
if (!deviceIdentifier.contains(",") || !hasCorrectIdentifierPrefix) {
165+
try {
166+
Devices.getDeviceType(deviceIdentifier);
167+
} catch (IllegalArgumentException e) {
169168
throw new TSSException("\"" + deviceIdentifier + "\" is not a valid identifier", false);
170169
}
171170
if (boardConfig == null && Devices.doesRequireBoardConfig(deviceIdentifier)) {

src/main/resources/airsquared/blobsaver/app/devicemodels.properties

Lines changed: 0 additions & 171 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# suppress inspection "UnusedProperty" for whole file
2+
iMac\ Pro\ (2017)\ (iMacPro1,1)=iBridge2,1
3+
MacBook\ Pro\ 15'\ (2018-2019)\ (MacBookPro15,1)=iBridge2,3
4+
MacBook\ Pro\ 13'\ 2018-2019)\ Four\ Thunderbolt\ 3\ ports,\ (MacBookPro15,2)=iBridge2,4
5+
Mac\ mini\ (2018)\ (Macmini8,1)=iBridge2,5
6+
Mac\ Pro\ (2019)\ (MacPro7,1)=iBridge2,6
7+
MacBook\ Pro\ 15'\ (2019,\ AMD\ Radeon\ Pro\ Vega\ graphics)\ (MacBookPro15,3)=iBridge2,7
8+
MacBook\ Air\ 13'\ (Retina,\ 2018)\ (MacBookAir8,1)=iBridge2,8
9+
MacBook\ Pro\ 13'\ (2019,\ Two\ Thunderbolt\ 3\ ports)\ (MacBookPro15,4)=iBridge2,10
10+
MacBook\ Air\ 13'\ (Retina,\ 2019)\ (MacBookAir8,2)=iBridge2,12
11+
MacBook\ Pro\ 16'\ (2019)\ (MacBookPro16,1)=iBridge2,14
12+
MacBook\ Air\ 13'\ (Retina,\ 2020)\ (MacBookAir9,1)=iBridge2,15
13+
MacBook\ Pro\ 13'\ (2020,\ Four\ Thunderbolt\ 3\ ports)\ (MacBookPro16,2)=iBridge2,16
14+
iMac\ 27'\ (Retina\ 5K,\ 2020)\ (iMac20,1)=iBridge2,19
15+
iMac\ 27'\ (Retina\ 5K,\ 2020)\ (iMac20,2)=iBridge2,20
16+
MacBook\ Pro\ 13'\ (2020,\ Two\ Thunderbolt\ 3\ ports)\ (MacBookPro16,3)=iBridge2,21
17+
MacBook\ Pro\ 16'\ (2019,\ AMD\ Radeon\ Pro\ 5600M\ graphics)\ (MacBookPro16,4)=iBridge2,22

0 commit comments

Comments
 (0)