@blotch/angular-lynx-native 22.3.2 → 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.
- package/README.md +8 -2
- package/dist/android/project.d.ts +27 -3
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +80 -9
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +1 -0
- package/dist/files.d.ts +24 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +41 -0
- package/dist/generate.d.ts +17 -2
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +68 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/ios/project.d.ts +21 -2
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +58 -2
- package/dist/options.d.ts +68 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +27 -0
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +18 -3
- package/dist/plugins.d.ts +245 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +1244 -0
- package/package.json +1 -1
- package/templates/android/README.md +12 -4
- package/templates/android/app/build.gradle.kts +27 -9
- package/templates/android/app/src/main/AndroidManifest.xml +5 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +153 -9
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +10 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +203 -16
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +1 -8
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +11 -1
- package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
- package/templates/ios/App/AngularLynxPlugins.swift +22 -0
- package/templates/ios/App/AnimationFrameRate.swift +66 -0
- package/templates/ios/App/AppDelegate.swift +3 -1
- package/templates/ios/App/AudioRecorderModule.swift +433 -0
- package/templates/ios/App/GeolocationModule.swift +455 -0
- package/templates/ios/App/HostElementUI.swift +270 -0
- package/templates/ios/App/ImagePickerModule.swift +160 -1
- package/templates/ios/App/LynxViewController.swift +66 -5
- package/templates/ios/App/NetworkStatus.swift +41 -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
|
+
}
|