@blotch/angular-lynx-native 22.2.14 → 22.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/android/project.d.ts +9 -2
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +19 -2
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +3 -0
- package/dist/options.d.ts +18 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +27 -0
- package/package.json +1 -1
- package/templates/android/README.md +22 -1
- package/templates/android/app/build.gradle.kts +7 -1
- package/templates/android/app/src/main/AndroidManifest.xml +22 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ClipboardModule.kt +80 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +219 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/LinkingModule.kt +149 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +35 -1
- package/templates/ios/App/ClipboardModule.swift +51 -0
- package/templates/ios/App/ImagePickerModule.swift +227 -0
- package/templates/ios/App/LinkingModule.swift +146 -0
- package/templates/ios/App/LynxViewController.swift +11 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.content.ActivityNotFoundException
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.net.Uri
|
|
7
|
+
import android.os.Handler
|
|
8
|
+
import android.os.Looper
|
|
9
|
+
import androidx.activity.ComponentActivity
|
|
10
|
+
import androidx.browser.customtabs.CustomTabColorSchemeParams
|
|
11
|
+
import androidx.browser.customtabs.CustomTabsIntent
|
|
12
|
+
import androidx.lifecycle.DefaultLifecycleObserver
|
|
13
|
+
import androidx.lifecycle.LifecycleOwner
|
|
14
|
+
import com.lynx.jsbridge.LynxMethod
|
|
15
|
+
import com.lynx.jsbridge.LynxModule
|
|
16
|
+
import com.lynx.react.bridge.Callback
|
|
17
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
18
|
+
import com.lynx.react.bridge.ReadableMap
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The native module behind the runtime's `LynxLinking`, which calls it as
|
|
22
|
+
* `NativeModules.AngularLynxLinking` (from the background thread, where
|
|
23
|
+
* NativeModules lives). MainActivity registers it with itself as the param:
|
|
24
|
+
* the module starts activities from it and watches its lifecycle.
|
|
25
|
+
*
|
|
26
|
+
* Lynx has no window.open or `<a href>`, so a link can't leave the app on its
|
|
27
|
+
* own. `openURL` sends an ACTION_VIEW intent, which the browser, the mail app
|
|
28
|
+
* or the app that claims the URL opens, and `openInApp` shows the page in a
|
|
29
|
+
* Custom Tab over the app. Each method calls back once: with `{}` when it's
|
|
30
|
+
* done, `{ value }` for `canOpenURL`, or `{ error: "failed", message }`, as
|
|
31
|
+
* the iOS module does.
|
|
32
|
+
*/
|
|
33
|
+
class LinkingModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
34
|
+
private val activity = param as ComponentActivity
|
|
35
|
+
private val main = Handler(Looper.getMainLooper())
|
|
36
|
+
|
|
37
|
+
// The open Custom Tab's watcher, or null when none is open. Only touched
|
|
38
|
+
// on the main thread.
|
|
39
|
+
private var sheet: DefaultLifecycleObserver? = null
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Calls back once Android has started the activity that opens [url].
|
|
43
|
+
* Starting it needs no `<queries>` entry: only [canOpenURL] does.
|
|
44
|
+
*/
|
|
45
|
+
@LynxMethod
|
|
46
|
+
fun openURL(url: String, callback: Callback) {
|
|
47
|
+
// Lynx calls modules off the main thread, and the activity starts
|
|
48
|
+
// others from it.
|
|
49
|
+
main.post {
|
|
50
|
+
try {
|
|
51
|
+
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
|
|
52
|
+
callback.invoke(JavaOnlyMap())
|
|
53
|
+
} catch (_: ActivityNotFoundException) {
|
|
54
|
+
callback.invoke(failed("Nothing on this device can open $url."))
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Calls back with `{ value }`: whether an activity on the phone opens
|
|
61
|
+
* [url]. From Android 11, the app sees only the apps its manifest's
|
|
62
|
+
* `<queries>` asks about, which the generator writes from angular.json's
|
|
63
|
+
* `canOpenURLSchemes`, so for any other scheme the answer is `false`.
|
|
64
|
+
*/
|
|
65
|
+
@LynxMethod
|
|
66
|
+
fun canOpenURL(url: String, callback: Callback) {
|
|
67
|
+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
|
68
|
+
val found = intent.resolveActivity(activity.packageManager) != null
|
|
69
|
+
callback.invoke(JavaOnlyMap.from(mapOf("value" to found)))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Shows [url] in a Custom Tab and calls back once the app is back in
|
|
74
|
+
* front. Android says nothing when a Custom Tab closes, so the module
|
|
75
|
+
* watches the activity instead: it pauses as the tab opens, and resumes
|
|
76
|
+
* when the person closes the tab or comes back to the app another way.
|
|
77
|
+
* [options] may hold `toolbarColor`, a `#` and eight hex digits (red,
|
|
78
|
+
* green, blue, alpha); the tab picks its icons' color to contrast with it.
|
|
79
|
+
*/
|
|
80
|
+
@LynxMethod
|
|
81
|
+
fun openInApp(url: String, options: ReadableMap, callback: Callback) {
|
|
82
|
+
val uri = Uri.parse(url)
|
|
83
|
+
val toolbarColor = color(options.getString("toolbarColor", ""))
|
|
84
|
+
main.post {
|
|
85
|
+
if (sheet != null) {
|
|
86
|
+
callback.invoke(failed("A browser sheet is already open."))
|
|
87
|
+
return@post
|
|
88
|
+
}
|
|
89
|
+
// A Custom Tab falls back to any app that opens the intent, which
|
|
90
|
+
// for another scheme isn't a browser.
|
|
91
|
+
if (uri.scheme?.lowercase() !in setOf("http", "https")) {
|
|
92
|
+
callback.invoke(failed("The browser sheet opens http and https URLs only, not $url."))
|
|
93
|
+
return@post
|
|
94
|
+
}
|
|
95
|
+
val tab = CustomTabsIntent.Builder().apply {
|
|
96
|
+
if (toolbarColor != null) {
|
|
97
|
+
setDefaultColorSchemeParams(
|
|
98
|
+
CustomTabColorSchemeParams.Builder().setToolbarColor(toolbarColor).build(),
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
}.build()
|
|
102
|
+
val watcher = object : DefaultLifecycleObserver {
|
|
103
|
+
var left = false
|
|
104
|
+
|
|
105
|
+
override fun onPause(owner: LifecycleOwner) {
|
|
106
|
+
left = true
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Also called as soon as the watcher is added, with the
|
|
110
|
+
// activity already resumed, before the tab opens.
|
|
111
|
+
override fun onResume(owner: LifecycleOwner) {
|
|
112
|
+
if (!left) return
|
|
113
|
+
closed(this)
|
|
114
|
+
callback.invoke(JavaOnlyMap())
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
sheet = watcher
|
|
118
|
+
activity.lifecycle.addObserver(watcher)
|
|
119
|
+
try {
|
|
120
|
+
tab.launchUrl(activity, uri)
|
|
121
|
+
} catch (_: ActivityNotFoundException) {
|
|
122
|
+
closed(watcher)
|
|
123
|
+
callback.invoke(failed("No browser on this device can open $url."))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private fun closed(watcher: DefaultLifecycleObserver) {
|
|
129
|
+
activity.lifecycle.removeObserver(watcher)
|
|
130
|
+
sheet = null
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A `#` and eight hex digits, red, green, blue and alpha, as the runtime
|
|
135
|
+
* writes every color it's given, as an ARGB Int.
|
|
136
|
+
*/
|
|
137
|
+
private fun color(hex: String): Int? {
|
|
138
|
+
if (hex.length != 9 || !hex.startsWith("#")) return null
|
|
139
|
+
val rgba = hex.substring(1).toLongOrNull(16) ?: return null
|
|
140
|
+
return ((rgba and 0xFF) shl 24 or (rgba shr 8)).toInt()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private fun failed(message: String) =
|
|
144
|
+
JavaOnlyMap.from(mapOf("error" to "failed", "message" to message))
|
|
145
|
+
|
|
146
|
+
companion object {
|
|
147
|
+
const val NAME = "AngularLynxLinking"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -17,6 +17,8 @@ import android.view.accessibility.AccessibilityManager
|
|
|
17
17
|
import android.widget.FrameLayout
|
|
18
18
|
import androidx.activity.ComponentActivity
|
|
19
19
|
import androidx.activity.OnBackPressedCallback
|
|
20
|
+
import androidx.activity.result.PickVisualMediaRequest
|
|
21
|
+
import androidx.activity.result.contract.ActivityResultContracts
|
|
20
22
|
import androidx.activity.enableEdgeToEdge
|
|
21
23
|
import androidx.browser.customtabs.CustomTabsIntent
|
|
22
24
|
import androidx.core.graphics.Insets
|
|
@@ -73,6 +75,25 @@ class MainActivity : ComponentActivity(), ShellModule.Host, WebAuthModule.Host {
|
|
|
73
75
|
}
|
|
74
76
|
private var webAuthSession: WebAuthSession? = null
|
|
75
77
|
|
|
78
|
+
// The Photo Picker (see ImagePickerModule), and who to tell what was
|
|
79
|
+
// picked. An activity registers for results as it's created, before it
|
|
80
|
+
// starts, so a result that arrives after Android recreated it still
|
|
81
|
+
// finds the launcher; the property initializer runs then.
|
|
82
|
+
private var imagePicked: ((Uri?) -> Unit)? = null
|
|
83
|
+
private val imagePicker = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
|
|
84
|
+
val done = imagePicked ?: return@registerForActivityResult
|
|
85
|
+
imagePicked = null
|
|
86
|
+
done(uri)
|
|
87
|
+
}
|
|
88
|
+
private val imagePickerHost = object : ImagePickerModule.Host {
|
|
89
|
+
override fun pickImage(done: (Uri?) -> Unit): Boolean {
|
|
90
|
+
if (imagePicked != null) return false
|
|
91
|
+
imagePicked = done
|
|
92
|
+
imagePicker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
76
97
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
77
98
|
// Android 15+ always draws apps edge to edge; opting in on older
|
|
78
99
|
// versions too keeps the layout the same everywhere.
|
|
@@ -89,6 +110,14 @@ class MainActivity : ComponentActivity(), ShellModule.Host, WebAuthModule.Host {
|
|
|
89
110
|
registerModule(WebAuthModule.NAME, WebAuthModule::class.java, this@MainActivity)
|
|
90
111
|
registerModule(LocalStorageModule.NAME, LocalStorageModule::class.java, null)
|
|
91
112
|
registerModule(SecureStorageModule.NAME, SecureStorageModule::class.java, null)
|
|
113
|
+
// LynxClipboard's text (ClipboardModule.kt).
|
|
114
|
+
registerModule(ClipboardModule.NAME, ClipboardModule::class.java, null)
|
|
115
|
+
// Links out of the app (LinkingModule.kt), which it opens from this
|
|
116
|
+
// activity.
|
|
117
|
+
registerModule(LinkingModule.NAME, LinkingModule::class.java, this@MainActivity)
|
|
118
|
+
// The photo picker behind LynxImagePicker (ImagePickerModule.kt), which
|
|
119
|
+
// this activity launches.
|
|
120
|
+
registerModule(ImagePickerModule.NAME, ImagePickerModule::class.java, imagePickerHost)
|
|
92
121
|
// <canvas>'s WebGL (WebGLModule.kt), which the runtime calls from
|
|
93
122
|
// the main thread, where Angular runs: main-thread modules let
|
|
94
123
|
// it get its answers synchronously, with lynx.module(name).
|
|
@@ -230,7 +259,8 @@ class MainActivity : ComponentActivity(), ShellModule.Host, WebAuthModule.Host {
|
|
|
230
259
|
|
|
231
260
|
/**
|
|
232
261
|
* The global props the runtime reads: `theme` (LynxTheme), `isNotchScreen`
|
|
233
|
-
* (LynxSafeArea), `appLocale` (LynxLocale), the
|
|
262
|
+
* (LynxSafeArea), `appLocale` (LynxLocale), `appTimeZone` (the `intl`
|
|
263
|
+
* build option's Intl.DateTimeFormat), the accessibility settings
|
|
234
264
|
* (LynxAccessibilitySettings) and `osVersion` (LynxSystemInfo), plus the
|
|
235
265
|
* safe-area insets in dp and `edgeToEdge`, which env(safe-area-inset-*)
|
|
236
266
|
* reads. The iOS shell sends the same keys, and Lynx Explorer sends theme
|
|
@@ -261,6 +291,10 @@ class MainActivity : ComponentActivity(), ShellModule.Host, WebAuthModule.Host {
|
|
|
261
291
|
)
|
|
262
292
|
// The user's language as a BCP 47 tag, such as "fr-FR".
|
|
263
293
|
ConfigurationCompat.getLocales(config)[0]?.let { props["appLocale"] = it.toLanguageTag() }
|
|
294
|
+
// The time zone's IANA name, such as "Europe/London". The `intl` build
|
|
295
|
+
// option's Intl.DateTimeFormat formats in the device's zone by default,
|
|
296
|
+
// which it reads from the engine's local time, and names it with this.
|
|
297
|
+
props["appTimeZone"] = java.util.TimeZone.getDefault().id
|
|
264
298
|
DynamicColors.read(this)?.let { props["dynamicColors"] = it }
|
|
265
299
|
return props
|
|
266
300
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Lynx
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
/// The native module behind the runtime's `LynxClipboard`, which calls it as
|
|
5
|
+
/// `NativeModules.AngularLynxClipboard` (from the background thread, where
|
|
6
|
+
/// NativeModules lives).
|
|
7
|
+
///
|
|
8
|
+
/// Text goes on the general pasteboard, the one every app shares. Each method
|
|
9
|
+
/// calls back once: `setString` with `{}`, `getString` with `{ value }`.
|
|
10
|
+
final class ClipboardModule: NSObject, LynxModule {
|
|
11
|
+
static var name: String { "AngularLynxClipboard" }
|
|
12
|
+
|
|
13
|
+
static var methodLookup: [String: String] {
|
|
14
|
+
[
|
|
15
|
+
"setString": NSStringFromSelector(#selector(setString(_:callback:))),
|
|
16
|
+
"getString": NSStringFromSelector(#selector(getString(_:))),
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
override init() {
|
|
21
|
+
super.init()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
25
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
26
|
+
// ignores it.
|
|
27
|
+
init(param: Any) {
|
|
28
|
+
super.init()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Lynx calls modules off the main thread, and UIPasteboard is UIKit's: on
|
|
32
|
+
// iOS 16 and later a read can show the paste prompt.
|
|
33
|
+
@objc func setString(_ text: String, callback: @escaping LynxCallbackBlock) {
|
|
34
|
+
DispatchQueue.main.async {
|
|
35
|
+
UIPasteboard.general.string = text
|
|
36
|
+
callback([:])
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// The pasteboard's text, or `""` when it holds none. Reading text another
|
|
41
|
+
/// app copied makes iOS 16 and later ask the person first, and a refusal
|
|
42
|
+
/// reads as none. `hasStrings` doesn't ask, so an empty pasteboard, or one
|
|
43
|
+
/// holding only an image, never shows the prompt.
|
|
44
|
+
@objc func getString(_ callback: @escaping LynxCallbackBlock) {
|
|
45
|
+
DispatchQueue.main.async {
|
|
46
|
+
let pasteboard = UIPasteboard.general
|
|
47
|
+
let text = pasteboard.hasStrings ? pasteboard.string : nil
|
|
48
|
+
callback(["value": text ?? ""])
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import ImageIO
|
|
2
|
+
import Lynx
|
|
3
|
+
import PhotosUI
|
|
4
|
+
import UIKit
|
|
5
|
+
import UniformTypeIdentifiers
|
|
6
|
+
|
|
7
|
+
/// The native module behind the runtime's `LynxImagePicker`, which calls it as
|
|
8
|
+
/// `NativeModules.AngularLynxImagePicker` (from the background thread, where
|
|
9
|
+
/// NativeModules lives).
|
|
10
|
+
///
|
|
11
|
+
/// Lynx has no `<input type="file">`. PHPickerViewController lets the person
|
|
12
|
+
/// pick a photo instead: it runs outside the app and hands over only the photo
|
|
13
|
+
/// picked, so the app needs no photo library permission (no
|
|
14
|
+
/// NSPhotoLibraryUsageDescription).
|
|
15
|
+
///
|
|
16
|
+
/// The image goes back as base64, since the module's callback and the
|
|
17
|
+
/// runtime's relay to the main thread carry only what JSON can. Scaling it
|
|
18
|
+
/// down here first, with `maxDimension`, keeps that small: a 12-megapixel
|
|
19
|
+
/// photo is several megabytes, a 1024-pixel one a few hundred kilobytes.
|
|
20
|
+
final class ImagePickerModule: NSObject, LynxModule, PHPickerViewControllerDelegate,
|
|
21
|
+
UIAdaptivePresentationControllerDelegate
|
|
22
|
+
{
|
|
23
|
+
static var name: String { "AngularLynxImagePicker" }
|
|
24
|
+
|
|
25
|
+
static var methodLookup: [String: String] {
|
|
26
|
+
["pick": NSStringFromSelector(#selector(pick(_:callback:)))]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// What a re-encoded image gets when the app doesn't choose a quality.
|
|
30
|
+
private static let defaultQuality = 0.9
|
|
31
|
+
|
|
32
|
+
/// The formats other platforms can show, which go back as they were picked
|
|
33
|
+
/// unless the app asks for a smaller or re-compressed image. Anything else,
|
|
34
|
+
/// such as an iPhone's HEIC photos, is re-encoded as a JPEG.
|
|
35
|
+
private static let portableTypes: Set<String> = [
|
|
36
|
+
"image/jpeg", "image/png", "image/gif", "image/webp",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
/// The open picker's request and who to tell, until it closes.
|
|
40
|
+
private struct Request {
|
|
41
|
+
let maxDimension: Int?
|
|
42
|
+
let quality: Double?
|
|
43
|
+
let callback: LynxCallbackBlock
|
|
44
|
+
}
|
|
45
|
+
private var open: Request?
|
|
46
|
+
|
|
47
|
+
override init() {
|
|
48
|
+
super.init()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
52
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
53
|
+
// ignores it.
|
|
54
|
+
init(param: Any) {
|
|
55
|
+
super.init()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Opens the picker and calls back once with `{ base64, name, type }`,
|
|
59
|
+
/// `{ canceled: true }` when the person closes it, or
|
|
60
|
+
/// `{ error: "failed", message }`.
|
|
61
|
+
@objc func pick(_ options: [String: Any], callback: @escaping LynxCallbackBlock) {
|
|
62
|
+
// Lynx calls modules off the main thread, and the picker is UI.
|
|
63
|
+
DispatchQueue.main.async {
|
|
64
|
+
self.present(options, callback)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private func present(_ options: [String: Any], _ callback: @escaping LynxCallbackBlock) {
|
|
69
|
+
guard open == nil else {
|
|
70
|
+
callback(["error": "failed", "message": "A picker is already open."])
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
guard let presenter = topViewController() else {
|
|
74
|
+
callback(["error": "failed", "message": "There's no screen to open the picker over."])
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
var configuration = PHPickerConfiguration()
|
|
78
|
+
configuration.filter = .images
|
|
79
|
+
configuration.selectionLimit = 1
|
|
80
|
+
// The photo as it's stored, rather than one the system converts first:
|
|
81
|
+
// this module converts only what it must, once.
|
|
82
|
+
configuration.preferredAssetRepresentationMode = .current
|
|
83
|
+
let picker = PHPickerViewController(configuration: configuration)
|
|
84
|
+
picker.delegate = self
|
|
85
|
+
open = Request(
|
|
86
|
+
maxDimension: (options["maxDimension"] as? NSNumber)?.intValue,
|
|
87
|
+
quality: (options["quality"] as? NSNumber)?.doubleValue,
|
|
88
|
+
callback: callback)
|
|
89
|
+
presenter.present(picker, animated: true)
|
|
90
|
+
// Swiping the sheet down closes it without didFinishPicking.
|
|
91
|
+
picker.presentationController?.delegate = self
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
|
95
|
+
picker.dismiss(animated: true)
|
|
96
|
+
guard let request = open else { return }
|
|
97
|
+
open = nil
|
|
98
|
+
guard let provider = results.first?.itemProvider else {
|
|
99
|
+
request.callback(["canceled": true])
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
// The first of the photo's representations that's an image, which is
|
|
103
|
+
// the one it's stored in: public.heic for most iPhone photos.
|
|
104
|
+
guard
|
|
105
|
+
let identifier = provider.registeredTypeIdentifiers.first(where: {
|
|
106
|
+
UTType($0)?.conforms(to: .image) == true
|
|
107
|
+
})
|
|
108
|
+
else {
|
|
109
|
+
request.callback(["error": "failed", "message": "The picked item isn't an image."])
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
let name = provider.suggestedName ?? "image"
|
|
113
|
+
provider.loadDataRepresentation(forTypeIdentifier: identifier) { data, error in
|
|
114
|
+
// Called on a background queue, which is where the image work belongs.
|
|
115
|
+
guard let data else {
|
|
116
|
+
request.callback([
|
|
117
|
+
"error": "failed",
|
|
118
|
+
"message": error?.localizedDescription ?? "The picked image couldn't be read.",
|
|
119
|
+
])
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
request.callback(
|
|
123
|
+
Self.prepare(data, type: UTType(identifier), name: name, request: request))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
|
|
128
|
+
guard let request = open else { return }
|
|
129
|
+
open = nil
|
|
130
|
+
request.callback(["canceled": true])
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// The callback's answer for the picked image: as it was picked, or
|
|
134
|
+
/// re-encoded as a JPEG.
|
|
135
|
+
private static func prepare(_ data: Data, type: UTType?, name: String, request: Request)
|
|
136
|
+
-> [String: Any]
|
|
137
|
+
{
|
|
138
|
+
let mime = type?.preferredMIMEType ?? "application/octet-stream"
|
|
139
|
+
if request.maxDimension == nil, request.quality == nil, portableTypes.contains(mime) {
|
|
140
|
+
return [
|
|
141
|
+
"base64": data.base64EncodedString(),
|
|
142
|
+
"name": "\(name).\(type?.preferredFilenameExtension ?? "jpg")",
|
|
143
|
+
"type": mime,
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
guard
|
|
147
|
+
let jpeg = toJpeg(
|
|
148
|
+
data, maxDimension: request.maxDimension,
|
|
149
|
+
quality: request.quality ?? defaultQuality)
|
|
150
|
+
else {
|
|
151
|
+
return ["error": "failed", "message": "The picked image couldn't be decoded."]
|
|
152
|
+
}
|
|
153
|
+
return ["base64": jpeg.base64EncodedString(), "name": "\(name).jpg", "type": "image/jpeg"]
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// The image as a JPEG: upright, whichever way the camera stored it, no
|
|
157
|
+
/// larger than `maxDimension` on its longest side, and without the
|
|
158
|
+
/// original's metadata, such as where it was taken. ImageIO decodes it
|
|
159
|
+
/// straight to the target size, so a 12-megapixel photo is never held in
|
|
160
|
+
/// memory at full size when it's scaled down.
|
|
161
|
+
private static func toJpeg(_ data: Data, maxDimension: Int?, quality: Double) -> Data? {
|
|
162
|
+
guard let source = CGImageSourceCreateWithData(data as CFData, nil),
|
|
163
|
+
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any],
|
|
164
|
+
let width = properties[kCGImagePropertyPixelWidth] as? Int,
|
|
165
|
+
let height = properties[kCGImagePropertyPixelHeight] as? Int
|
|
166
|
+
else { return nil }
|
|
167
|
+
// Never larger than the image: ImageIO would scale it up.
|
|
168
|
+
let longest = max(width, height)
|
|
169
|
+
let size = min(maxDimension ?? longest, longest)
|
|
170
|
+
let options: [CFString: Any] = [
|
|
171
|
+
kCGImageSourceCreateThumbnailFromImageAlways: true,
|
|
172
|
+
kCGImageSourceCreateThumbnailWithTransform: true,
|
|
173
|
+
kCGImageSourceThumbnailMaxPixelSize: size,
|
|
174
|
+
kCGImageSourceShouldCacheImmediately: true,
|
|
175
|
+
]
|
|
176
|
+
guard var image = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary)
|
|
177
|
+
else { return nil }
|
|
178
|
+
if let opaque = flattened(image) {
|
|
179
|
+
image = opaque
|
|
180
|
+
}
|
|
181
|
+
let output = NSMutableData()
|
|
182
|
+
guard
|
|
183
|
+
let destination = CGImageDestinationCreateWithData(
|
|
184
|
+
output, UTType.jpeg.identifier as CFString, 1, nil)
|
|
185
|
+
else { return nil }
|
|
186
|
+
CGImageDestinationAddImage(
|
|
187
|
+
destination, image,
|
|
188
|
+
[kCGImageDestinationLossyCompressionQuality: quality] as CFDictionary)
|
|
189
|
+
return CGImageDestinationFinalize(destination) ? output as Data : nil
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// The image drawn over white, when it has transparency: a JPEG has none,
|
|
193
|
+
/// and would show it black. Nil for an image that's already opaque.
|
|
194
|
+
private static func flattened(_ image: CGImage) -> CGImage? {
|
|
195
|
+
switch image.alphaInfo {
|
|
196
|
+
case .none, .noneSkipFirst, .noneSkipLast: return nil
|
|
197
|
+
default: break
|
|
198
|
+
}
|
|
199
|
+
// The image's own color space keeps a Display P3 photo's colors.
|
|
200
|
+
let space =
|
|
201
|
+
image.colorSpace.flatMap { $0.model == .rgb ? $0 : nil }
|
|
202
|
+
?? CGColorSpace(name: CGColorSpace.sRGB)!
|
|
203
|
+
guard
|
|
204
|
+
let context = CGContext(
|
|
205
|
+
data: nil, width: image.width, height: image.height, bitsPerComponent: 8,
|
|
206
|
+
bytesPerRow: 0, space: space, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
|
|
207
|
+
else { return nil }
|
|
208
|
+
let bounds = CGRect(x: 0, y: 0, width: image.width, height: image.height)
|
|
209
|
+
context.setFillColor(CGColor(gray: 1, alpha: 1))
|
|
210
|
+
context.fill(bounds)
|
|
211
|
+
context.draw(image, in: bounds)
|
|
212
|
+
return context.makeImage()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// The view controller on top, which the picker opens over.
|
|
216
|
+
private func topViewController() -> UIViewController? {
|
|
217
|
+
let window = UIApplication.shared.connectedScenes
|
|
218
|
+
.compactMap { $0 as? UIWindowScene }
|
|
219
|
+
.flatMap(\.windows)
|
|
220
|
+
.first(where: \.isKeyWindow)
|
|
221
|
+
var top = window?.rootViewController
|
|
222
|
+
while let presented = top?.presentedViewController {
|
|
223
|
+
top = presented
|
|
224
|
+
}
|
|
225
|
+
return top
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import Lynx
|
|
2
|
+
import SafariServices
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
/// The native module behind the runtime's `LynxLinking`, which calls it as
|
|
6
|
+
/// `NativeModules.AngularLynxLinking` (from the background thread, where
|
|
7
|
+
/// NativeModules lives).
|
|
8
|
+
///
|
|
9
|
+
/// Lynx has no window.open or `<a href>`, so a link can't leave the app on
|
|
10
|
+
/// its own. `openURL` hands it to iOS, which opens it in Safari, Mail or the
|
|
11
|
+
/// app that claims it, and `openInApp` shows it in SFSafariViewController over
|
|
12
|
+
/// the app. Each method calls back once: with `{}` when it's done, `{ value }`
|
|
13
|
+
/// for `canOpenURL`, or `{ error: "failed", message }`.
|
|
14
|
+
final class LinkingModule: NSObject, LynxModule, SFSafariViewControllerDelegate {
|
|
15
|
+
static var name: String { "AngularLynxLinking" }
|
|
16
|
+
|
|
17
|
+
static var methodLookup: [String: String] {
|
|
18
|
+
[
|
|
19
|
+
"openURL": NSStringFromSelector(#selector(openURL(_:callback:))),
|
|
20
|
+
"openInApp": NSStringFromSelector(#selector(openInApp(_:options:callback:))),
|
|
21
|
+
"canOpenURL": NSStringFromSelector(#selector(canOpenURL(_:callback:))),
|
|
22
|
+
]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// Who to tell when the open sheet closes, or nil when none is open.
|
|
26
|
+
private var sheetClosed: LynxCallbackBlock?
|
|
27
|
+
|
|
28
|
+
override init() {
|
|
29
|
+
super.init()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
33
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
34
|
+
// ignores it.
|
|
35
|
+
init(param: Any) {
|
|
36
|
+
super.init()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// Calls back once iOS has handed `address` to the app that opens it.
|
|
40
|
+
/// UIApplication.open needs no LSApplicationQueriesSchemes entry: only
|
|
41
|
+
/// canOpenURL asks for one.
|
|
42
|
+
@objc func openURL(_ address: String, callback: @escaping LynxCallbackBlock) {
|
|
43
|
+
// Lynx calls modules off the main thread, and UIApplication is
|
|
44
|
+
// main-thread only.
|
|
45
|
+
DispatchQueue.main.async {
|
|
46
|
+
guard let url = URL(string: address) else {
|
|
47
|
+
callback(Self.failed("\(address) is not a valid URL."))
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
UIApplication.shared.open(url) { opened in
|
|
51
|
+
callback(opened ? [:] : Self.failed("Nothing on this device can open \(address)."))
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Calls back with `{ value }`: whether an app on the phone opens
|
|
57
|
+
/// `address`. iOS answers `false` for a scheme the app doesn't list in
|
|
58
|
+
/// Info.plist's LSApplicationQueriesSchemes, which the generator writes
|
|
59
|
+
/// from angular.json's `canOpenURLSchemes`.
|
|
60
|
+
@objc func canOpenURL(_ address: String, callback: @escaping LynxCallbackBlock) {
|
|
61
|
+
DispatchQueue.main.async {
|
|
62
|
+
guard let url = URL(string: address) else {
|
|
63
|
+
callback(Self.failed("\(address) is not a valid URL."))
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
callback(["value": UIApplication.shared.canOpenURL(url)])
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Shows `address` in SFSafariViewController and calls back once the
|
|
71
|
+
/// person closes it. `options` may hold `toolbarColor` and `controlsColor`,
|
|
72
|
+
/// each a `#` and eight hex digits: red, green, blue and alpha.
|
|
73
|
+
@objc func openInApp(
|
|
74
|
+
_ address: String, options: [String: Any], callback: @escaping LynxCallbackBlock
|
|
75
|
+
) {
|
|
76
|
+
DispatchQueue.main.async {
|
|
77
|
+
self.present(address, options, callback)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private func present(
|
|
82
|
+
_ address: String, _ options: [String: Any], _ callback: @escaping LynxCallbackBlock
|
|
83
|
+
) {
|
|
84
|
+
guard sheetClosed == nil else {
|
|
85
|
+
callback(Self.failed("A browser sheet is already open."))
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
// SFSafariViewController throws an Objective-C exception, which Swift
|
|
89
|
+
// can't catch, for any other scheme.
|
|
90
|
+
guard let url = URL(string: address), let scheme = url.scheme?.lowercased(),
|
|
91
|
+
scheme == "http" || scheme == "https"
|
|
92
|
+
else {
|
|
93
|
+
callback(Self.failed("The browser sheet opens http and https URLs only, not \(address)."))
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
guard let presenter = Self.topViewController() else {
|
|
97
|
+
callback(Self.failed("The app has no screen to show the browser sheet over."))
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
let safari = SFSafariViewController(url: url)
|
|
101
|
+
safari.delegate = self
|
|
102
|
+
if let color = Self.color(options["toolbarColor"]) {
|
|
103
|
+
safari.preferredBarTintColor = color
|
|
104
|
+
}
|
|
105
|
+
if let color = Self.color(options["controlsColor"]) {
|
|
106
|
+
safari.preferredControlTintColor = color
|
|
107
|
+
}
|
|
108
|
+
sheetClosed = callback
|
|
109
|
+
presenter.present(safari, animated: true)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/// The person tapped Done, or swiped the sheet away.
|
|
113
|
+
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
|
|
114
|
+
let closed = sheetClosed
|
|
115
|
+
sheetClosed = nil
|
|
116
|
+
closed?([:])
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/// The view controller on top of the app's window: the LynxViewController,
|
|
120
|
+
/// or whatever it's presenting, such as a sign-in sheet.
|
|
121
|
+
private static func topViewController() -> UIViewController? {
|
|
122
|
+
var top = UIApplication.shared.connectedScenes
|
|
123
|
+
.compactMap { $0 as? UIWindowScene }
|
|
124
|
+
.flatMap(\.windows)
|
|
125
|
+
.first(where: \.isKeyWindow)?
|
|
126
|
+
.rootViewController
|
|
127
|
+
while let presented = top?.presentedViewController, !presented.isBeingDismissed {
|
|
128
|
+
top = presented
|
|
129
|
+
}
|
|
130
|
+
return top
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/// A `#` and eight hex digits, red, green, blue and alpha, as the runtime
|
|
134
|
+
/// writes every color it's given.
|
|
135
|
+
private static func color(_ value: Any?) -> UIColor? {
|
|
136
|
+
guard let hex = value as? String, hex.count == 9, hex.hasPrefix("#"),
|
|
137
|
+
let rgba = UInt32(hex.dropFirst(), radix: 16)
|
|
138
|
+
else { return nil }
|
|
139
|
+
let channel = { (shift: UInt32) in CGFloat((rgba >> shift) & 0xFF) / 255 }
|
|
140
|
+
return UIColor(red: channel(24), green: channel(16), blue: channel(8), alpha: channel(0))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private static func failed(_ message: String) -> [String: Any] {
|
|
144
|
+
["error": "failed", "message": message]
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -25,6 +25,11 @@ final class LynxViewController: UIViewController {
|
|
|
25
25
|
config.register(WebAuthModule.self)
|
|
26
26
|
config.register(LocalStorageModule.self)
|
|
27
27
|
config.register(SecureStorageModule.self)
|
|
28
|
+
// LynxClipboard's text (ClipboardModule).
|
|
29
|
+
config.register(ClipboardModule.self)
|
|
30
|
+
config.register(LinkingModule.self)
|
|
31
|
+
// The photo picker behind LynxImagePicker (ImagePickerModule).
|
|
32
|
+
config.register(ImagePickerModule.self)
|
|
28
33
|
// <canvas>'s WebGL (AngularLynxWebGL.h), which the runtime calls from
|
|
29
34
|
// the main thread. It's Objective-C, which Swift reaches without a
|
|
30
35
|
// bridging header by name.
|
|
@@ -131,7 +136,8 @@ final class LynxViewController: UIViewController {
|
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
/// The global props the runtime reads: `theme` (LynxTheme), `isNotchScreen`
|
|
134
|
-
/// (LynxSafeArea), `appLocale` (LynxLocale), the
|
|
139
|
+
/// (LynxSafeArea), `appLocale` (LynxLocale), `appTimeZone` (the `intl`
|
|
140
|
+
/// build option's Intl.DateTimeFormat), the accessibility settings
|
|
135
141
|
/// (LynxAccessibilitySettings) and `osVersion` (LynxSystemInfo), plus the
|
|
136
142
|
/// safe-area insets and `edgeToEdge`, which env(safe-area-inset-*) reads.
|
|
137
143
|
/// The Android shell sends the same keys, and Lynx Explorer sends theme and
|
|
@@ -168,6 +174,10 @@ final class LynxViewController: UIViewController {
|
|
|
168
174
|
if let language = Locale.preferredLanguages.first {
|
|
169
175
|
props["appLocale"] = language
|
|
170
176
|
}
|
|
177
|
+
// The time zone's IANA name, such as "Europe/London". The `intl` build
|
|
178
|
+
// option's Intl.DateTimeFormat formats in the device's zone by default,
|
|
179
|
+
// which it reads from the engine's local time, and names it with this.
|
|
180
|
+
props["appTimeZone"] = TimeZone.current.identifier
|
|
171
181
|
// Without useBoolLiterals, Lynx turns a Bool into 1 or 0.
|
|
172
182
|
return LynxTemplateData(dictionary: props, useBoolLiterals: true)
|
|
173
183
|
}
|