Skip to content

Commit 45b3085

Browse files
committed
Fix AndroidAPI.current
1 parent 6c8fa92 commit 45b3085

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Sources/AndroidOS/AndroidAPI.swift

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Android
1010
import AndroidNDK
1111
#endif
12+
import JavaKit
1213

1314
/// Android API Level
1415
public struct AndroidAPI: RawRepresentable, Equatable, Hashable, Codable, Sendable {
@@ -25,34 +26,45 @@ public extension AndroidAPI {
2526

2627
/// Available since API level 24. Returns the API level of the device we're actually running on.
2728
static var current: AndroidAPI {
28-
get throws(Throwable) {
29-
let value: CInt
30-
#if os(Android) && canImport(AndroidNDK)
31-
value = try ndkValue().get()
32-
#else
33-
value = try jniValue()
34-
#endif
29+
get throws(Exception) {
30+
AndroidAPI(rawValue: try deviceAPILevel())
3531
}
3632
}
3733
}
3834

3935
internal extension AndroidAPI {
4036

37+
static func deviceAPILevel() throws(Exception) -> Int32 {
38+
#if os(Android) && canImport(AndroidNDK)
39+
try ndkValue().get()
40+
#else
41+
try jniValue()
42+
#endif
43+
}
44+
4145
/// `Build.VERSION.SDK_INT`
4246
static func jniValue() throws(Throwable) -> Int32 {
43-
let javaClass = try JavaClass<Build.VERSION>.init()
44-
return javaClass.sdk_version
47+
do {
48+
let javaClass = try JavaClass<Build.VERSION>.init()
49+
return javaClass.SDK_INT
50+
}
51+
catch let error as Throwable {
52+
throw error
53+
}
54+
catch {
55+
fatalError("Invalid error \(error)")
56+
}
4557
}
4658

47-
59+
/// Available since API level 24. Returns the API level of the device we're actually running on.
4860
static func ndkValue() -> Result<Int32, Exception> {
4961
#if os(Android) && canImport(AndroidNDK)
5062
let value = android_get_device_api_level()
5163
#else
5264
let value: Int32 = -1
5365
#endif
5466
guard value != -1 else {
55-
throw Exception()
67+
return .failure(Exception())
5668
}
5769
return .success(value)
5870
}

0 commit comments

Comments
 (0)