44// under the terms of the GNU Lesser General Public License 3.0
55// as published by the Free Software Foundation https://fsf.org
66
7+ #if !SKIP
78import Foundation
9+ import SkipAndroidSDKBridge
810@_exported import SkipBridge
911#if canImport(FoundationNetworking)
1012@_exported import FoundationNetworking
@@ -16,6 +18,7 @@ import Foundation
1618#endif
1719
1820fileprivate let logger : Logger = Logger ( subsystem: " SkipAndroidBridge " , category: " AndroidBridgeToKotlin " )
21+ #endif
1922
2023#if os(Android)
2124public let isAndroid = true
@@ -26,64 +29,40 @@ public let isAndroid = false
2629
2730private var androidBridgeInit = false
2831
29- // SKIP @BridgeToKotlin
3032public class AndroidBridgeKotlin {
31- public init ( ) {
32- }
33-
34- }
35-
36- /// Perform all the setup that is needed to get `Foundation` idioms working with Android conventions.
37- ///
38- /// This includes:
39- /// - Using the Android certificate store for HTTPS validation
40- /// - Using the AndroidContext files locations for `FileManager.url`
41- // SKIP @BridgeToKotlin
42- func initAndroidBridge( ) throws {
43- if androidBridgeInit == true { return }
44- defer { androidBridgeInit = true }
45-
46- let start = Date . now
47- logger. log ( " initAndroidBridge started " )
48- #if os(Android)
49- #if !SKIP
50- try setupFileManagerProperties ( context: AndroidContext . shared)
51- try installSystemCertificates ( )
52- #endif
53- #endif
54- logger. log ( " initAndroidBridge done in \( Date . now. timeIntervalSince ( start) ) " )
55- }
56-
57-
58- // URL.applicationSupportDirectory exists in Darwin's Foundation but not in Android's Foundation
59- #if os(Android)
60- extension URL {
61- public static var applicationSupportDirectory : URL {
62- try ! FileManager . default. url ( for: . applicationSupportDirectory, in: . userDomainMask, appropriateFor: nil , create: false )
63- }
64-
65- public static var cachesDirectory : URL {
66- try ! FileManager . default. url ( for: . cachesDirectory, in: . userDomainMask, appropriateFor: nil , create: false )
33+ /// Perform all the setup that is needed to get `Foundation` idioms working with Android conventions.
34+ ///
35+ /// This includes:
36+ /// - Using the Android certificate store for HTTPS validation
37+ /// - Using the AndroidContext files locations for `FileManager.url`
38+ public static func initAndroidBridge( ) throws {
39+ if androidBridgeInit == true { return }
40+ defer { androidBridgeInit = true }
41+
42+ let start = Date . now
43+ logger. log ( " initAndroidBridge started " )
44+ guard let context = AndroidContext . shared as AndroidContext ? else {
45+ fatalError ( " no AndroidContext.shared " )
46+ }
47+ #if os(Android)
48+ try setupFileManagerProperties ( filesDir: context. filesDir, cacheDir: context. cacheDir)
49+ try installSystemCertificates ( )
50+ #endif
51+ logger. log ( " initAndroidBridge done in \( Date . now. timeIntervalSince ( start) ) applicationSupportDirectory= \( URL . applicationSupportDirectory. path) " )
6752 }
6853}
69- #endif
7054
71- #if !SKIP
72- private func setupFileManagerProperties( context: AndroidContext ? ) throws {
73- guard let context else { return }
74- let filesDir = context. filesDir
75- let cacheDir = context. cacheDir
7655
56+ private func setupFileManagerProperties( filesDir: String , cacheDir: String ) throws {
7757 // https://github.com/swiftlang/swift-foundation/blob/main/Sources/FoundationEssentials/FileManager/SearchPaths/FileManager%2BXDGSearchPaths.swift#L46
7858 setenv ( " XDG_CACHE_HOME " , cacheDir, 1 )
7959 // https://github.com/swiftlang/swift-foundation/blob/main/Sources/FoundationEssentials/FileManager/SearchPaths/FileManager%2BXDGSearchPaths.swift#L37
8060 setenv ( " XDG_DATA_HOME " , filesDir, 1 )
8161
82- // ensure that we can get the `.applicationSupportDirectory`, which should use the `XDG_DATA_HOME` envrionment
62+ // ensure that we can get the `.applicationSupportDirectory`, which should use the `XDG_DATA_HOME` environment
8363 //let applicationSupportDirectory = URL.applicationSupportDirectory // unavailable on Android
8464 let applicationSupportDirectory = try ! FileManager . default. url ( for: . applicationSupportDirectory, in: . userDomainMask, appropriateFor: nil , create: true )
8565 logger. debug ( " setupFileManagerProperties: applicationSupportDirectory= \( applicationSupportDirectory. path) " )
86-
8766}
8867
8968/// Collects all the certificate files from the Android certificate store and writes them to a single `cacerts.pem` file that can be used by libcurl,
@@ -136,5 +115,16 @@ private func installSystemCertificates(fromCertficateFolders certsFolders: [Stri
136115 //setenv("URLSessionCertificateAuthorityInfoFile", "/system/etc/security/cacerts/", 1) // doesn't work for directories
137116 setenv ( " URLSessionCertificateAuthorityInfoFile " , generatedCacertsURL. path, 1 )
138117}
139- #endif
140118
119+ // URL.applicationSupportDirectory exists in Darwin's Foundation but not in Android's Foundation
120+ #if os(Android)
121+ extension URL {
122+ public static var applicationSupportDirectory : URL {
123+ try ! FileManager . default. url ( for: . applicationSupportDirectory, in: . userDomainMask, appropriateFor: nil , create: false )
124+ }
125+
126+ public static var cachesDirectory : URL {
127+ try ! FileManager . default. url ( for: . cachesDirectory, in: . userDomainMask, appropriateFor: nil , create: false )
128+ }
129+ }
130+ #endif
0 commit comments