9
9
import Android
10
10
import AndroidNDK
11
11
#endif
12
+ import JavaKit
12
13
13
14
/// Android API Level
14
15
public struct AndroidAPI : RawRepresentable , Equatable , Hashable , Codable , Sendable {
@@ -25,34 +26,45 @@ public extension AndroidAPI {
25
26
26
27
/// Available since API level 24. Returns the API level of the device we're actually running on.
27
28
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 ( ) )
35
31
}
36
32
}
37
33
}
38
34
39
35
internal extension AndroidAPI {
40
36
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
+
41
45
/// `Build.VERSION.SDK_INT`
42
46
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
+ }
45
57
}
46
58
47
-
59
+ /// Available since API level 24. Returns the API level of the device we're actually running on.
48
60
static func ndkValue( ) -> Result < Int32 , Exception > {
49
61
#if os(Android) && canImport(AndroidNDK)
50
62
let value = android_get_device_api_level ( )
51
63
#else
52
64
let value : Int32 = - 1
53
65
#endif
54
66
guard value != - 1 else {
55
- throw Exception ( )
67
+ return . failure ( Exception ( ) )
56
68
}
57
69
return . success( value)
58
70
}
0 commit comments