@onekeyfe/react-native-qr-scanner 6.8.0-32 → 6.8.0-33

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.
@@ -1,8 +1,9 @@
1
1
  package com.margelo.nitro.qrscanner
2
2
 
3
3
  import android.Manifest
4
- import android.content.pm.PackageManager
5
4
  import android.annotation.SuppressLint
5
+ import android.content.Context
6
+ import android.content.pm.PackageManager
6
7
  import android.graphics.Color
7
8
  import android.graphics.drawable.GradientDrawable
8
9
  import android.os.Handler
@@ -43,6 +44,40 @@ import com.google.mlkit.vision.common.InputImage
43
44
  import java.util.concurrent.Executors
44
45
  import java.util.concurrent.TimeUnit
45
46
 
47
+ /**
48
+ * React Native drives layout from Yoga and suppresses Android's native
49
+ * `requestLayout()` pass for children added or updated after initial layout.
50
+ * Re-measuring on `requestLayout()` keeps the dynamically replaced CameraX
51
+ * `PreviewView` and its internal `TextureView` sized to this container.
52
+ */
53
+ internal class QrScannerContainer(context: Context) : FrameLayout(context) {
54
+ private var layoutPosted = false
55
+ private val reLayout = Runnable {
56
+ layoutPosted = false
57
+ if (width > 0 && height > 0) {
58
+ measure(
59
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
60
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
61
+ )
62
+ layout(left, top, right, bottom)
63
+ }
64
+ }
65
+
66
+ override fun requestLayout() {
67
+ super.requestLayout()
68
+ if (!layoutPosted && width > 0 && height > 0) {
69
+ layoutPosted = true
70
+ post(reLayout)
71
+ }
72
+ }
73
+
74
+ override fun onDetachedFromWindow() {
75
+ removeCallbacks(reLayout)
76
+ layoutPosted = false
77
+ super.onDetachedFromWindow()
78
+ }
79
+ }
80
+
46
81
  /**
47
82
  * CameraX preview plus ML Kit QR detection. Native code reports raw text only;
48
83
  * mode handling and BC-UR assembly run in JS through `./ur`.
@@ -80,7 +115,8 @@ class HybridQrScanner(val context: ThemedReactContext) : HybridQrScannerSpec(),
80
115
  alpha = 0f
81
116
  isClickable = false
82
117
  }
83
- private val container = FrameLayout(context).apply {
118
+ private val container = QrScannerContainer(context).apply {
119
+ setBackgroundColor(Color.BLACK)
84
120
  addView(previewView, FrameLayout.LayoutParams(
85
121
  FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
86
122
  ))
@@ -0,0 +1,118 @@
1
+ package com.margelo.nitro.qrscanner
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.os.Looper
6
+ import android.view.View
7
+ import android.widget.FrameLayout
8
+ import org.junit.Assert.assertEquals
9
+ import org.junit.Assert.assertTrue
10
+ import org.junit.Test
11
+ import org.junit.runner.RunWith
12
+ import org.robolectric.Robolectric
13
+ import org.robolectric.RobolectricTestRunner
14
+ import org.robolectric.Shadows.shadowOf
15
+ import org.robolectric.annotation.Config
16
+
17
+ @RunWith(RobolectricTestRunner::class)
18
+ @Config(sdk = [34])
19
+ class QrScannerContainerTest {
20
+ /**
21
+ * Simulates a React Native host view that suppresses Android's native
22
+ * requestLayout() traversal after Yoga performs the initial layout.
23
+ */
24
+ private class SwallowingHost(context: Context) : FrameLayout(context) {
25
+ var suppressNativeRelayout = false
26
+ var swallowedRequestCount = 0
27
+
28
+ override fun requestLayout() {
29
+ if (suppressNativeRelayout) {
30
+ swallowedRequestCount++
31
+ return
32
+ }
33
+ super.requestLayout()
34
+ }
35
+ }
36
+
37
+ private fun mountInSwallowingHost(
38
+ container: QrScannerContainer,
39
+ width: Int,
40
+ height: Int,
41
+ ): SwallowingHost {
42
+ val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
43
+ val host = SwallowingHost(activity)
44
+ host.addView(container, FrameLayout.LayoutParams(width, height))
45
+ activity.setContentView(host)
46
+ host.measure(
47
+ View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
48
+ View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY),
49
+ )
50
+ host.layout(0, 0, width, height)
51
+ shadowOf(Looper.getMainLooper()).idle()
52
+ host.suppressNativeRelayout = true
53
+ return host
54
+ }
55
+
56
+ @Test
57
+ fun reMeasuresDynamicallyReplacedChildAfterInitialLayout() {
58
+ val activity = Robolectric.buildActivity(Activity::class.java).create().get()
59
+ val container = QrScannerContainer(activity)
60
+ val initialPreview = FrameLayout(activity)
61
+ container.addView(
62
+ initialPreview,
63
+ FrameLayout.LayoutParams(
64
+ FrameLayout.LayoutParams.MATCH_PARENT,
65
+ FrameLayout.LayoutParams.MATCH_PARENT,
66
+ ),
67
+ )
68
+ val host = mountInSwallowingHost(container, 400, 300)
69
+ assertEquals(400, initialPreview.width)
70
+ assertEquals(300, initialPreview.height)
71
+
72
+ val replacementPreview = FrameLayout(activity)
73
+ container.removeView(initialPreview)
74
+ container.addView(
75
+ replacementPreview,
76
+ 0,
77
+ FrameLayout.LayoutParams(
78
+ FrameLayout.LayoutParams.MATCH_PARENT,
79
+ FrameLayout.LayoutParams.MATCH_PARENT,
80
+ ),
81
+ )
82
+ shadowOf(Looper.getMainLooper()).idle()
83
+
84
+ assertTrue(host.swallowedRequestCount > 0)
85
+ assertEquals(400, replacementPreview.width)
86
+ assertEquals(300, replacementPreview.height)
87
+ }
88
+
89
+ @Test
90
+ fun reMeasuresGrandchildAddedInsidePreviewChild() {
91
+ val activity = Robolectric.buildActivity(Activity::class.java).create().get()
92
+ val container = QrScannerContainer(activity)
93
+ val previewHost = FrameLayout(activity)
94
+ container.addView(
95
+ previewHost,
96
+ FrameLayout.LayoutParams(
97
+ FrameLayout.LayoutParams.MATCH_PARENT,
98
+ FrameLayout.LayoutParams.MATCH_PARENT,
99
+ ),
100
+ )
101
+ val host = mountInSwallowingHost(container, 360, 360)
102
+
103
+ // Simulates PreviewView adding its internal TextureView when CameraX binds.
104
+ val textureChild = View(activity)
105
+ previewHost.addView(
106
+ textureChild,
107
+ FrameLayout.LayoutParams(
108
+ FrameLayout.LayoutParams.MATCH_PARENT,
109
+ FrameLayout.LayoutParams.MATCH_PARENT,
110
+ ),
111
+ )
112
+ shadowOf(Looper.getMainLooper()).idle()
113
+
114
+ assertTrue(host.swallowedRequestCount > 0)
115
+ assertEquals(360, textureChild.width)
116
+ assertEquals(360, textureChild.height)
117
+ }
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-qr-scanner",
3
- "version": "6.8.0-32",
3
+ "version": "6.8.0-33",
4
4
  "description": "react-native-qr-scanner",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",