@blotch/angular-lynx-native 22.3.1 → 22.3.3

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.
Files changed (123) hide show
  1. package/README.md +47 -15
  2. package/dist/android/project.d.ts +114 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +440 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +14 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/files.d.ts +24 -0
  12. package/dist/files.d.ts.map +1 -0
  13. package/dist/files.js +41 -0
  14. package/dist/generate.d.ts +34 -3
  15. package/dist/generate.d.ts.map +1 -1
  16. package/dist/generate.js +78 -19
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +16 -2
  20. package/dist/ios/build.d.ts.map +1 -1
  21. package/dist/ios/build.js +8 -2
  22. package/dist/ios/frameworks.d.ts +6 -5
  23. package/dist/ios/frameworks.d.ts.map +1 -1
  24. package/dist/ios/frameworks.js +46 -15
  25. package/dist/ios/project.d.ts +192 -11
  26. package/dist/ios/project.d.ts.map +1 -1
  27. package/dist/ios/project.js +823 -33
  28. package/dist/options.d.ts +208 -1
  29. package/dist/options.d.ts.map +1 -1
  30. package/dist/options.js +154 -0
  31. package/dist/permissions.d.ts +18 -1
  32. package/dist/permissions.d.ts.map +1 -1
  33. package/dist/permissions.js +79 -4
  34. package/dist/plugins.d.ts +245 -0
  35. package/dist/plugins.d.ts.map +1 -0
  36. package/dist/plugins.js +1244 -0
  37. package/package.json +1 -1
  38. package/templates/android/README.md +46 -5
  39. package/templates/android/angular-lynx.properties +2 -0
  40. package/templates/android/app/build.gradle.kts +78 -11
  41. package/templates/android/app/src/main/AndroidManifest.xml +19 -3
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  65. package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
  66. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  67. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
  68. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
  69. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  70. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
  71. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
  72. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  73. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  74. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  75. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  76. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  77. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  78. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  79. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  80. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  81. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  82. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  83. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  84. package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
  85. package/templates/android/build.gradle.kts +1 -1
  86. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  87. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  88. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  89. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  90. package/templates/ios/App/AngularLynxPlugins.swift +22 -0
  91. package/templates/ios/App/AngularLynxVideo.h +37 -0
  92. package/templates/ios/App/AngularLynxVideo.m +435 -0
  93. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  94. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  95. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  96. package/templates/ios/App/AnimationFrameRate.swift +66 -0
  97. package/templates/ios/App/AppDelegate.swift +37 -3
  98. package/templates/ios/App/AppleSignInButton.swift +106 -0
  99. package/templates/ios/App/AppleSignInModule.swift +222 -0
  100. package/templates/ios/App/AudioRecorderModule.swift +433 -0
  101. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  102. package/templates/ios/App/BiometricsModule.swift +256 -0
  103. package/templates/ios/App/BluetoothModule.swift +1086 -0
  104. package/templates/ios/App/CalendarModule.swift +158 -0
  105. package/templates/ios/App/FilePickerModule.swift +240 -0
  106. package/templates/ios/App/GeolocationModule.swift +455 -0
  107. package/templates/ios/App/HapticsModule.swift +90 -0
  108. package/templates/ios/App/HostElementUI.swift +270 -0
  109. package/templates/ios/App/ImagePickerModule.swift +160 -1
  110. package/templates/ios/App/Info.plist +2 -0
  111. package/templates/ios/App/LynxViewController.swift +274 -9
  112. package/templates/ios/App/MotionModule.swift +430 -0
  113. package/templates/ios/App/NetworkStatus.swift +41 -0
  114. package/templates/ios/App/NfcModule.swift +346 -0
  115. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  116. package/templates/ios/App/ShareTargetModule.swift +150 -0
  117. package/templates/ios/App/ShellModule.swift +17 -0
  118. package/templates/ios/App/StoreReviewModule.swift +61 -0
  119. package/templates/ios/App/TrackingModule.swift +121 -0
  120. package/templates/ios/App/WifiModule.swift +237 -0
  121. package/templates/ios/App/XElementRegistry.swift +13 -3
  122. package/templates/ios/ShareExtension/Info.plist +42 -0
  123. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -0,0 +1,331 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.annotation.SuppressLint
4
+ import android.content.Context
5
+ import android.view.MotionEvent
6
+ import android.view.View
7
+ import android.view.ViewGroup
8
+ import android.widget.FrameLayout
9
+ import com.lynx.tasm.behavior.LynxContext
10
+ import com.lynx.tasm.behavior.TouchEventDispatcher
11
+ import com.lynx.tasm.behavior.ui.LynxBaseUI
12
+ import com.lynx.tasm.behavior.ui.LynxUI
13
+ import com.lynx.tasm.behavior.ui.UIGroup
14
+ import com.lynx.tasm.behavior.ui.UIShadowProxy
15
+ import com.lynx.tasm.behavior.ui.view.UIView
16
+
17
+ /**
18
+ * A native element that shows its Lynx children in a view outside the LynxView, such as a
19
+ * BottomSheetDialog's, a popup's or another window's, and keeps them interactive there.
20
+ *
21
+ * Touches reach Lynx through the LynxView's `dispatchTouchEvent`, which hit-tests the elements
22
+ * from the page's root, so a child whose view is moved out of the LynxView still draws but never
23
+ * gets a tap, a touch or a gesture. [showChildren] moves the element's own view, with every view
24
+ * Lynx put in it, into a frame that dispatches the touches in it from the element, with a
25
+ * [TouchEventDispatcher] of its own, as Lynx's `<overlay>` does for its dialog. Touches in the
26
+ * container then reach the element's children as they would in the LynxView: their `bindtap`,
27
+ * `bindtouchmove` and the rest. The frame also lays the children out, which the LynxView's
28
+ * layout pass no longer reaches.
29
+ *
30
+ * The element's own background and border are drawn by its view, so they go along. A `box-shadow`
31
+ * or an `outline` is drawn by the view of the `UIShadowProxy` Lynx wraps the element in, around the
32
+ * element's view, so with one the proxy's view goes into the frame instead, holding the element's.
33
+ *
34
+ * A plugin's element subclasses it rather than Lynx's `UIView`, lays itself out with CSS like any
35
+ * element (the width and height it has there are the ones it keeps in the container), and calls
36
+ * [showChildren] when its container appears and [returnChildren] once it has gone.
37
+ */
38
+ open class HostElementUI(context: LynxContext) : UIView(context) {
39
+ private var frame: HostedFrame? = null
40
+
41
+ /** Whether the element's view is in a container of the element's, rather than the LynxView. */
42
+ val isShowingChildrenElsewhere: Boolean
43
+ get() = frame != null
44
+
45
+ /**
46
+ * The proxy Lynx wraps the element in when it has a `box-shadow` or an `outline`, which draws
47
+ * them in a view of its own that holds the element's.
48
+ */
49
+ private val shadowProxy: UIShadowProxy?
50
+ get() = parent as? UIShadowProxy
51
+
52
+ /** The view that holds the element's on the page: the shadow proxy's, or else its own. */
53
+ private val outerView: View
54
+ get() = shadowProxy?.view ?: mView
55
+
56
+ /**
57
+ * Moves the element's view, with its children, to the top left of [container], and dispatches
58
+ * their touches from there. Returns the frame it put in [container], for a container that
59
+ * needs layout params of its own.
60
+ */
61
+ fun showChildren(container: ViewGroup): ViewGroup {
62
+ val hosted = frame ?: HostedFrame(mView.context, this).also { frame = it }
63
+ val outer = outerView
64
+ (outer.parent as? ViewGroup)?.removeView(outer)
65
+ (hosted.parent as? ViewGroup)?.removeView(hosted)
66
+ hosted.addView(outer, FrameLayout.LayoutParams(width, height))
67
+ container.addView(hosted)
68
+ return hosted
69
+ }
70
+
71
+ /** Puts the element's view back in the LynxView, where Lynx put it. */
72
+ fun returnChildren() {
73
+ val hosted = frame ?: return
74
+ frame = null
75
+ val outer = outerView
76
+ // From wherever it is: an element given its first box-shadow or outline while it's in the
77
+ // frame is wrapped then in a new proxy, whose view Lynx puts on the page with the
78
+ // element's inside it, and adding that view again would throw.
79
+ (outer.parent as? ViewGroup)?.removeView(outer)
80
+ (hosted.parent as? ViewGroup)?.removeView(hosted)
81
+ // With a shadow, the proxy is what Lynx put among the parent's children, in its place.
82
+ val proxy = shadowProxy
83
+ val placed: LynxBaseUI = proxy ?: this
84
+ val parent = placed.parent as? LynxBaseUI
85
+ // A flattened parent (a plain <view> Lynx draws without a view of its own) has no view
86
+ // to hold the element's: Lynx put it in the draw parent's, its nearest ancestor with a
87
+ // view (LynxUIOwner.insertIntoDrawList), so it goes back there.
88
+ val drawParent = if (parent?.isFlatten == true) parent.drawParent else parent
89
+ // Not the parent's insertView(), which counts the siblings shown elsewhere too.
90
+ if (drawParent is UIGroup<*>) addAmongAttached(placed, outer, drawParent.view)
91
+ // The proxy's view fills its parent's, with the element's at its place on the page: laid
92
+ // out and scrolled as the frame left it, the shadow and the element would be off by that
93
+ // place.
94
+ proxy?.view?.scrollTo(0, 0)
95
+ proxy?.layout()
96
+ requestLayout()
97
+ }
98
+
99
+ /**
100
+ * Puts [view], the view of [placed] (the element, or its shadow proxy), among [container]'s:
101
+ * just after the nearest sibling before it in Lynx's drawing order whose view is there, or
102
+ * else first. Lynx's own `UIGroup.insertView()` counts every sibling before it that has a
103
+ * view, including one whose view is shown elsewhere, so with two siblings shown elsewhere the
104
+ * index it gives can be past the views [container] holds, which Android rejects.
105
+ *
106
+ * The sibling's view can be in a view inside [container] rather than in it: a `<scroll-view>`'s
107
+ * `addView()` hands its children to a layout of its own. The element's view then goes into that
108
+ * same layout, beside the sibling's.
109
+ */
110
+ private fun addAmongAttached(placed: LynxBaseUI, view: View, container: ViewGroup) {
111
+ var ui = placed.previousDrawUI
112
+ while (ui != null) {
113
+ val sibling = (ui as? LynxUI<*>)?.view
114
+ val holder = sibling?.parent as? ViewGroup
115
+ if (holder != null && holder.isIn(container)) {
116
+ holder.addView(view, holder.indexOfChild(sibling) + 1)
117
+ return
118
+ }
119
+ ui = ui.previousDrawUI
120
+ }
121
+ container.addView(view, 0)
122
+ }
123
+
124
+ /**
125
+ * Whether this is [container] or a view inside it, and not, say, the frame of a sibling that's
126
+ * shown elsewhere too.
127
+ */
128
+ private fun ViewGroup.isIn(container: ViewGroup): Boolean {
129
+ var view: ViewGroup? = this
130
+ while (view != null) {
131
+ if (view === container) return true
132
+ view = view.parent as? ViewGroup
133
+ }
134
+ return false
135
+ }
136
+
137
+ override fun handleLayout() {
138
+ if (!isShowingChildrenElsewhere) {
139
+ super.handleLayout()
140
+ return
141
+ }
142
+ layOutInFrame()
143
+ }
144
+
145
+ /**
146
+ * Lays the element's view out at the frame's top left, keeping its size: Lynx lays it out at
147
+ * the element's place on the page, which means nothing in the frame.
148
+ *
149
+ * The shadow proxy draws the shadow and outline around that place on the page, in a view that
150
+ * holds the element's there. That view covers the element's frame, and is scrolled by that
151
+ * place, so the element is at the frame's top left, with its shadow and outline around it.
152
+ * Scrolled rather than moved up and to the left: with a negative place (an element pulled up
153
+ * past its parent's top), a moved view would start inside the element, and Android sends no
154
+ * touch to the part of a child outside its parent, such as a native control at the element's
155
+ * top. Lynx lays the proxy's view out again to fill the frame whenever it lays the proxy out,
156
+ * and then the element: this runs after it.
157
+ */
158
+ private fun layOutInFrame() {
159
+ val proxy = shadowProxy
160
+ if (proxy == null) {
161
+ mView.layout(0, 0, width, height)
162
+ return
163
+ }
164
+ proxy.view.layout(0, 0, width, height)
165
+ proxy.view.scrollTo(left, top)
166
+ mView.layout(left, top, left + width, top + height)
167
+ }
168
+
169
+ /**
170
+ * The element's frame outside the LynxView: it measures and lays out the element's children
171
+ * and dispatches the touches in it to Lynx, as a LynxView does for the page.
172
+ */
173
+ @SuppressLint("ViewConstructor")
174
+ private class HostedFrame(context: Context, private val host: HostElementUI) :
175
+ FrameLayout(context) {
176
+ private val dispatcher = HostedTouchEventDispatcher(host.lynxContext)
177
+
178
+ /** Whether an element took this touch's moves for itself (`consume-slide-event`). */
179
+ private var slideConsumed = false
180
+
181
+ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
182
+ host.measureChildren()
183
+ (host.outerView.layoutParams as? LayoutParams)?.let {
184
+ it.width = host.width
185
+ it.height = host.height
186
+ }
187
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec)
188
+ }
189
+
190
+ override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
191
+ host.layOutInFrame()
192
+ host.layoutChildren()
193
+ }
194
+
195
+ override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
196
+ if (dispatcher.onInterceptTouchEvent(ev)) {
197
+ parent?.requestDisallowInterceptTouchEvent(true)
198
+ return true
199
+ }
200
+ return super.onInterceptTouchEvent(ev)
201
+ }
202
+
203
+ // As LynxView's: Lynx first, then the views inside (a native control, an input) unless an
204
+ // element took the slide. The element's view is at the frame's top left, so the event's
205
+ // coordinates are the element's own, which Lynx hit-tests from.
206
+ override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
207
+ val action = ev.actionMasked
208
+ if (action == MotionEvent.ACTION_DOWN) {
209
+ slideConsumed = false
210
+ dispatcher.beginTouch()
211
+ }
212
+ try {
213
+ val consumed = dispatcher.onTouchEvent(ev, host)
214
+ if (!consumed) {
215
+ val handled = super.dispatchTouchEvent(ev)
216
+ // A DOWN nothing takes makes this frame no touch target, and the touch's UP
217
+ // or CANCEL go elsewhere: the dispatcher leaves the context now instead.
218
+ if (!handled && action == MotionEvent.ACTION_DOWN) dispatcher.endTouch()
219
+ return handled
220
+ }
221
+ if (dispatcher.blockNativeEvent(ev)) {
222
+ parent?.requestDisallowInterceptTouchEvent(true)
223
+ }
224
+ if (!slideConsumed) slideConsumed = dispatcher.consumeSlideEvent(ev)
225
+ if (!slideConsumed) super.dispatchTouchEvent(ev)
226
+ // Claimed either way, so the rest of the touch comes here too.
227
+ return true
228
+ } finally {
229
+ // In a finally, so a throw on the way can't leave this dispatcher in the context.
230
+ if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
231
+ dispatcher.endTouch()
232
+ }
233
+ }
234
+ }
235
+
236
+ // A window that goes, such as a sheet dismissed with a finger still down, may never send
237
+ // the frame its UP or CANCEL.
238
+ override fun onDetachedFromWindow() {
239
+ dispatcher.endTouch()
240
+ super.onDetachedFromWindow()
241
+ }
242
+ }
243
+
244
+ /**
245
+ * The frame's [TouchEventDispatcher], which also hears what Lynx's elements tell the
246
+ * LynxContext about the touch in the frame.
247
+ *
248
+ * A `<scroll-view>`, a `<list>` or a swiper that starts scrolling calls
249
+ * `LynxContext.onGestureRecognized`, and an element that moves under the finger calls
250
+ * `onPropsChanged`: that's what stops the touch's tap. The context passes them to the one
251
+ * dispatcher it holds, the LynxView's, so a dispatcher of the frame's own never heard them,
252
+ * and a short drag that scrolled a list in a sheet also tapped the row under the finger.
253
+ *
254
+ * While a touch in the frame lasts, from [beginTouch] to [endTouch], this dispatcher is the
255
+ * context's, and it passes each of those calls on to the dispatcher it took the place of. Each
256
+ * dispatcher is told then, whichever owns the element's touch: a frame's, the LynxView's under
257
+ * it, or another frame's when two hosts have a touch at once. A dispatcher that has no touch
258
+ * under the element is unaffected, since each starts its touch afresh, and Lynx checks the
259
+ * gesture's element against the touched element's ancestors.
260
+ */
261
+ private class HostedTouchEventDispatcher(private val context: LynxContext) :
262
+ TouchEventDispatcher(context.lynxUIOwner) {
263
+ /** The dispatcher the context held when this one took its place, until [endTouch]. */
264
+ private var below: TouchEventDispatcher? = null
265
+
266
+ /** Whether this dispatcher is the context's, or under another frame's that is. */
267
+ private var inContext = false
268
+
269
+ /** Makes this dispatcher the context's, with the one it held under it. */
270
+ fun beginTouch() {
271
+ if (inContext) return
272
+ inContext = true
273
+ below = context.touchEventDispatcher
274
+ context.touchEventDispatcher = this
275
+ }
276
+
277
+ /**
278
+ * Takes this dispatcher out of the context's, leaving the rest as they are. The touches of
279
+ * two frames can end in either order, and Lynx may have given the context the LynxView's
280
+ * dispatcher again meanwhile (`LynxUIRenderer` does when the page's config changes), so it
281
+ * takes itself out of the chain wherever it is, rather than putting back what it found.
282
+ */
283
+ fun endTouch() {
284
+ if (!inContext) return
285
+ inContext = false
286
+ val next = below
287
+ below = null
288
+ var above = context.touchEventDispatcher
289
+ if (above === this) {
290
+ context.touchEventDispatcher = next
291
+ return
292
+ }
293
+ while (above is HostedTouchEventDispatcher) {
294
+ if (above.below === this) {
295
+ above.below = next
296
+ return
297
+ }
298
+ above = above.below
299
+ }
300
+ }
301
+
302
+ override fun onGestureRecognized() {
303
+ super.onGestureRecognized()
304
+ below?.onGestureRecognized()
305
+ }
306
+
307
+ override fun onGestureRecognized(ui: LynxBaseUI) {
308
+ super.onGestureRecognized(ui)
309
+ below?.onGestureRecognized(ui)
310
+ }
311
+
312
+ override fun onGestureRecognized(sign: Int) {
313
+ super.onGestureRecognized(sign)
314
+ below?.onGestureRecognized(sign)
315
+ }
316
+
317
+ override fun onPropsChanged(ui: LynxBaseUI) {
318
+ super.onPropsChanged(ui)
319
+ below?.onPropsChanged(ui)
320
+ }
321
+
322
+ override fun isTouchMoving(): Boolean =
323
+ super.isTouchMoving() || below?.isTouchMoving() == true
324
+
325
+ // LynxContext.reset(), when the page is loaded again, is meant for the LynxView's.
326
+ override fun reset() {
327
+ super.reset()
328
+ below?.reset()
329
+ }
330
+ }
331
+ }
@@ -1,5 +1,16 @@
1
1
  package dev.angularlynx.shell
2
2
 
3
+ // angular-lynx:image-picker-camera
4
+ import android.Manifest
5
+ import android.content.ActivityNotFoundException
6
+ import android.content.pm.PackageManager
7
+ import androidx.core.content.ContextCompat
8
+ import androidx.core.content.FileProvider
9
+ import java.io.File
10
+ import java.text.SimpleDateFormat
11
+ import java.util.Date
12
+ import java.util.Locale
13
+ // /angular-lynx:image-picker-camera
3
14
  import android.content.Context
4
15
  import android.graphics.Bitmap
5
16
  import android.graphics.BitmapFactory
@@ -38,6 +49,11 @@ import kotlin.math.roundToInt
38
49
  * runtime's relay to the main thread carry only what JSON can. Scaling it
39
50
  * down here first, with `maxDimension`, keeps that small: a 12-megapixel
40
51
  * photo is several megabytes, a 1024-pixel one a few hundred kilobytes.
52
+ *
53
+ * `takePhoto`, the camera, is only in an app whose angular.json sets
54
+ * `permissions.camera`, as on iOS: the generator takes out the lines between
55
+ * the `angular-lynx:image-picker-camera` markers in every other app, so the
56
+ * runtime finds no `takePhoto` and rejects with `unavailable`.
41
57
  */
42
58
  class ImagePickerModule(context: Context, param: Any?) : LynxModule(context, param) {
43
59
  private val host = param as Host
@@ -50,6 +66,19 @@ class ImagePickerModule(context: Context, param: Any?) : LynxModule(context, par
50
66
  * opening it, when it's already open.
51
67
  */
52
68
  fun pickImage(done: (Uri?) -> Unit): Boolean
69
+
70
+ // angular-lynx:image-picker-camera
71
+ /**
72
+ * Opens the phone's camera app to take a photo into [output], and
73
+ * calls [done] once with whether it took one. Returns false, without
74
+ * opening it, when the camera or the picker is already open. Throws
75
+ * ActivityNotFoundException on a phone without a camera app.
76
+ */
77
+ fun takePicture(output: Uri, done: (Boolean) -> Unit): Boolean
78
+
79
+ /** Android's permission prompt, for the camera. */
80
+ val permissions: PermissionHost
81
+ // /angular-lynx:image-picker-camera
53
82
  }
54
83
 
55
84
  /**
@@ -78,6 +107,108 @@ class ImagePickerModule(context: Context, param: Any?) : LynxModule(context, par
78
107
  }
79
108
  }
80
109
 
110
+ // angular-lynx:image-picker-camera
111
+ /**
112
+ * Opens the camera and calls back once with `{ base64, name, type }`, a
113
+ * JPEG of the photo taken, `{ canceled: true }` when the person closes
114
+ * it, or `{ error, message }`: `unavailable` on a phone without a camera,
115
+ * `denied` when the person doesn't let the app use it, or `failed`, as
116
+ * the iOS module does.
117
+ */
118
+ @LynxMethod
119
+ fun takePhoto(options: ReadableMap, callback: Callback) {
120
+ val maxDimension = if (options.hasKey("maxDimension")) options.getDouble("maxDimension").toInt() else null
121
+ val quality = if (options.hasKey("quality")) options.getDouble("quality") else null
122
+ main.post {
123
+ if (!mContext.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
124
+ callback.invoke(error("unavailable", "This device has no camera."))
125
+ return@post
126
+ }
127
+ // The manifest declares CAMERA (angular.json's
128
+ // permissions.camera), and Android then refuses
129
+ // ACTION_IMAGE_CAPTURE, with a SecurityException, to an app that
130
+ // hasn't been granted it, though the camera app takes the photo.
131
+ if (cameraGranted()) {
132
+ capture(maxDimension, quality, callback)
133
+ return@post
134
+ }
135
+ val asked = host.permissions.requestPermissions(arrayOf(Manifest.permission.CAMERA)) {
136
+ if (cameraGranted()) {
137
+ capture(maxDimension, quality, callback)
138
+ } else {
139
+ callback.invoke(error("denied", "The app isn't allowed to use the camera."))
140
+ }
141
+ }
142
+ if (!asked) callback.invoke(failed("Another permission prompt is open."))
143
+ }
144
+ }
145
+
146
+ private fun cameraGranted() =
147
+ ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
148
+
149
+ /** Opens the camera app, on the main thread, with the permission granted. */
150
+ private fun capture(maxDimension: Int?, quality: Double?, callback: Callback) {
151
+ // The camera app writes the photo into a file of ours, which it
152
+ // reaches through the FileProvider LynxShare's files go through
153
+ // (res/xml/share_paths.xml), with a grant for this one URI.
154
+ val folder = File(mContext.cacheDir, CAMERA_FOLDER)
155
+ val file = File(folder, "${System.currentTimeMillis()}.jpg")
156
+ val uri = try {
157
+ folder.mkdirs()
158
+ FileProvider.getUriForFile(mContext, "${mContext.packageName}$AUTHORITY_SUFFIX", file)
159
+ } catch (_: IllegalArgumentException) {
160
+ callback.invoke(failed("There's nowhere to keep the photo."))
161
+ return
162
+ }
163
+ val opened = try {
164
+ host.takePicture(uri) { taken ->
165
+ // A camera app that closes without a photo leaves the file
166
+ // empty, or doesn't make it.
167
+ if (!taken || file.length() == 0L) {
168
+ file.delete()
169
+ callback.invoke(JavaOnlyMap.from(mapOf("canceled" to true)))
170
+ } else {
171
+ worker.execute {
172
+ try {
173
+ callback.invoke(prepareTaken(file, maxDimension, quality))
174
+ } finally {
175
+ file.delete()
176
+ }
177
+ }
178
+ }
179
+ }
180
+ } catch (_: ActivityNotFoundException) {
181
+ callback.invoke(error("unavailable", "This device has no camera app."))
182
+ return
183
+ }
184
+ if (!opened) callback.invoke(failed("A picker is already open."))
185
+ }
186
+
187
+ /**
188
+ * The photo the camera app took, always re-encoded as a JPEG, as on iOS:
189
+ * upright (camera apps store it turned, with an orientation tag), scaled
190
+ * down to [maxDimension], and without the camera's metadata, such as
191
+ * where it was taken.
192
+ */
193
+ private fun prepareTaken(file: File, maxDimension: Int?, quality: Double?): JavaOnlyMap {
194
+ val bytes = try {
195
+ file.readBytes()
196
+ } catch (_: IOException) {
197
+ return failed("The photo couldn't be read.")
198
+ }
199
+ val jpeg = try {
200
+ toJpeg(bytes, maxDimension, quality ?: DEFAULT_QUALITY)
201
+ } catch (_: OutOfMemoryError) {
202
+ null
203
+ } ?: return failed("The photo couldn't be decoded.")
204
+ val name = "IMG_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(Date(file.lastModified()))}.jpg"
205
+ return answer(jpeg, name, "image/jpeg")
206
+ }
207
+
208
+ private fun error(code: String, message: String) =
209
+ JavaOnlyMap.from(mapOf("error" to code, "message" to message))
210
+ // /angular-lynx:image-picker-camera
211
+
81
212
  /**
82
213
  * The callback's answer for the picked image: as it was picked, or
83
214
  * re-encoded as a JPEG.
@@ -124,6 +255,18 @@ class ImagePickerModule(context: Context, param: Any?) : LynxModule(context, par
124
255
  companion object {
125
256
  const val NAME = "AngularLynxImagePicker"
126
257
 
258
+ // angular-lynx:image-picker-camera
259
+ /**
260
+ * The cache folder the camera app writes the photo to, which
261
+ * res/xml/share_paths.xml lets FileProvider share. The photo is
262
+ * deleted once it's read.
263
+ */
264
+ private const val CAMERA_FOLDER = "angular-lynx-camera"
265
+
266
+ /** The FileProvider's, in AndroidManifest.xml (as ShareModule's). */
267
+ private const val AUTHORITY_SUFFIX = ".angularlynx.share"
268
+ // /angular-lynx:image-picker-camera
269
+
127
270
  /** What a re-encoded image gets when the app doesn't choose a quality. */
128
271
  private const val DEFAULT_QUALITY = 0.9
129
272
 
@@ -0,0 +1,83 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.content.Context
4
+ import android.graphics.BitmapFactory
5
+ import android.system.Os
6
+ import android.system.OsConstants
7
+ import com.facebook.imageformat.DefaultImageFormats
8
+ import com.facebook.imagepipeline.bitmaps.SimpleBitmapReleaser
9
+ import com.facebook.imagepipeline.common.ImageDecodeOptions
10
+ import com.facebook.imagepipeline.core.ImagePipelineConfig
11
+ import com.facebook.imagepipeline.core.MemoryChunkType
12
+ import com.facebook.imagepipeline.decoder.DecodeException
13
+ import com.facebook.imagepipeline.decoder.ImageDecoder
14
+ import com.facebook.imagepipeline.decoder.ImageDecoderConfig
15
+ import com.facebook.imagepipeline.image.CloseableImage
16
+ import com.facebook.imagepipeline.image.CloseableStaticBitmap
17
+ import com.facebook.imagepipeline.image.EncodedImage
18
+ import com.facebook.imagepipeline.image.ImmutableQualityInfo
19
+ import com.facebook.imagepipeline.image.QualityInfo
20
+ import com.facebook.imagepipeline.memory.PoolConfig
21
+ import com.facebook.imagepipeline.memory.PoolFactory
22
+
23
+ /**
24
+ * The configuration Fresco, which draws `<image>` for Lynx's image service,
25
+ * starts with.
26
+ *
27
+ * Lynx's image service is compiled against Fresco 2.3, and Fresco 3 isn't
28
+ * binary compatible with it: `CloseableImage` and `CloseableBitmap` became
29
+ * interfaces, so the service's first decoded bitmap fails verification.
30
+ * Fresco's native libraries before 3.4 don't load with 16 KB memory pages.
31
+ * Their arm64 builds have a 64 KB `p_align`, which Google Play's check reads,
32
+ * but each puts its RELRO and its writable data in one segment, with the
33
+ * RELRO ending on a 4 or 8 KB boundary; Android 17's linker refuses them
34
+ * (`program alignment (8192) cannot be smaller than system page size`),
35
+ * and the `UnsatisfiedLinkError` ends the app on a Fresco thread.
36
+ *
37
+ * So on a phone with pages larger than 4 KB, Fresco keeps encoded images in
38
+ * Java memory instead of `libimagepipeline.so`'s, transcodes without
39
+ * `libnative-imagetranscoder.so`, and decodes GIF and animated WebP, which
40
+ * need `libgifimage.so` and `libstatic-webp.so`, as their first frame with
41
+ * Android's decoder. Everything else decodes as it does with 4 KB pages.
42
+ * Lynx's own `blur-radius` still copies the bitmap with
43
+ * `libimagepipeline.so`, so it fails on those phones. A phone with 4 KB pages
44
+ * gets Fresco's defaults. Drop this once Lynx's image service is built
45
+ * against a Fresco with 16 KB-safe libraries (3.4 or later).
46
+ */
47
+ internal fun imagePipelineConfig(context: Context): ImagePipelineConfig {
48
+ val builder = ImagePipelineConfig.newBuilder(context)
49
+ .setPoolFactory(PoolFactory(PoolConfig.newBuilder().build()))
50
+ if (Os.sysconf(OsConstants._SC_PAGESIZE) > 4096) {
51
+ builder.setMemoryChunkType(MemoryChunkType.BUFFER_MEMORY)
52
+ builder.experiment().setNativeCodeDisabled(true)
53
+ builder.setImageDecoderConfig(
54
+ ImageDecoderConfig.newBuilder()
55
+ .overrideDecoder(DefaultImageFormats.GIF, FirstFrameDecoder)
56
+ .overrideDecoder(DefaultImageFormats.WEBP_ANIMATED, FirstFrameDecoder)
57
+ .build(),
58
+ )
59
+ }
60
+ return builder.build()
61
+ }
62
+
63
+ /**
64
+ * Decodes an animated image's first frame with Android's BitmapFactory, which
65
+ * reads GIF and animated WebP without Fresco's native libraries.
66
+ */
67
+ private object FirstFrameDecoder : ImageDecoder {
68
+ override fun decode(
69
+ encodedImage: EncodedImage,
70
+ length: Int,
71
+ qualityInfo: QualityInfo,
72
+ options: ImageDecodeOptions,
73
+ ): CloseableImage {
74
+ val bitmap = encodedImage.inputStream?.use { BitmapFactory.decodeStream(it) }
75
+ ?: throw DecodeException("Android couldn't decode the image's first frame", encodedImage)
76
+ return CloseableStaticBitmap(
77
+ bitmap,
78
+ SimpleBitmapReleaser.getInstance(),
79
+ ImmutableQualityInfo.FULL_QUALITY,
80
+ 0,
81
+ )
82
+ }
83
+ }