catalyst-core 0.1.0-canary.8 → 0.2.0-beta.1

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 (127) hide show
  1. package/README.md +1 -1
  2. package/changelog.md +5 -0
  3. package/dist/native/androidProject/app/build.gradle.kts +9 -0
  4. package/dist/native/androidProject/app/src/main/AndroidManifest.xml +2 -1
  5. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/CustomWebview.kt +19 -2
  6. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/MainActivity.kt +13 -0
  7. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/NativeBridge.kt +148 -0
  8. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/NativeCameraManager.kt +337 -0
  9. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/TransitionManager.kt +202 -0
  10. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/WebCacheManager.kt +22 -15
  11. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/BarcodeDetector.kt +123 -0
  12. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/CameraSessionManager.kt +244 -0
  13. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/HoldController.kt +70 -0
  14. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/TorchController.kt +57 -0
  15. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/VideoStreamState.kt +41 -0
  16. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/VideoStreamStateMachine.kt +40 -0
  17. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/ViewfinderMapper.kt +75 -0
  18. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/camera/ZoomController.kt +156 -0
  19. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/security/FridaDetector.kt +16 -5
  20. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/BridgeUtils.kt +42 -5
  21. package/dist/native/androidProject/app/src/main/java/io/yourname/androidproject/utils/FrameworkServerUtils.kt +7 -3
  22. package/dist/native/androidProject/app/src/main/res/drawable/debug_viewfinder_border.xml +8 -0
  23. package/dist/native/androidProject/app/src/main/res/layout/activity_main.xml +37 -0
  24. package/dist/native/androidSetup.js +1 -0
  25. package/dist/native/bridge/constants/NativeInterfaces.js +9 -5
  26. package/dist/native/bridge/hooks/index.js +1 -0
  27. package/dist/native/bridge/hooks/useCamera.js +9 -0
  28. package/dist/native/bridge/hooks/useDataProtection.js +2 -0
  29. package/dist/native/bridge/hooks/useDeviceInfo.js +2 -0
  30. package/dist/native/bridge/hooks/useFilePicker.js +2 -0
  31. package/dist/native/bridge/hooks/useGoogleSignIn.js +1 -0
  32. package/dist/native/bridge/hooks/useHapticFeedback.js +1 -0
  33. package/dist/native/bridge/hooks/useIntent.js +1 -0
  34. package/dist/native/bridge/hooks/useNativeTransition.js +43 -0
  35. package/dist/native/bridge/hooks/useNetworkStatus.js +3 -0
  36. package/dist/native/bridge/hooks/useNotification.js +2 -0
  37. package/dist/native/bridge/hooks/useSafeArea.js +2 -0
  38. package/dist/native/bridge/hooks/useVideoStream.js +6 -0
  39. package/dist/native/bridge/hooks/utils.js +1 -0
  40. package/dist/native/bridge/hooks.js +1 -137
  41. package/dist/native/bridge/useBaseHook.js +12 -7
  42. package/dist/native/bridge/utils/NativeBridge.js +41 -6
  43. package/dist/native/buildAndroid/assets.js +52 -0
  44. package/dist/native/buildAndroid/build.js +19 -0
  45. package/dist/native/buildAndroid/config.js +1 -0
  46. package/dist/native/buildAndroid/index.js +1 -0
  47. package/dist/native/buildAppAndroid.js +2 -150
  48. package/dist/native/buildAppIos.js +2 -305
  49. package/dist/native/buildIos/assets.js +8 -0
  50. package/dist/native/buildIos/build.js +40 -0
  51. package/dist/native/buildIos/config.js +71 -0
  52. package/dist/native/buildIos/index.js +6 -0
  53. package/dist/native/buildIos/plugins.js +45 -0
  54. package/dist/native/internalPluginUtils.js +7 -1
  55. package/dist/native/iosnativeWebView/Package.resolved +36 -0
  56. package/dist/native/iosnativeWebView/Sources/Core/Camera/BarcodeDetector.swift +116 -0
  57. package/dist/native/iosnativeWebView/Sources/Core/Camera/CameraPreviewView.swift +58 -0
  58. package/dist/native/iosnativeWebView/Sources/Core/Camera/CameraSessionManager.swift +308 -0
  59. package/dist/native/iosnativeWebView/Sources/Core/Camera/HoldController.swift +63 -0
  60. package/dist/native/iosnativeWebView/Sources/Core/Camera/NativeCameraManager.swift +194 -0
  61. package/dist/native/iosnativeWebView/Sources/Core/Camera/TorchController.swift +60 -0
  62. package/dist/native/iosnativeWebView/Sources/Core/Camera/VideoStreamState.swift +44 -0
  63. package/dist/native/iosnativeWebView/Sources/Core/Camera/VideoStreamStateMachine.swift +60 -0
  64. package/dist/native/iosnativeWebView/Sources/Core/Camera/ZoomController.swift +130 -0
  65. package/dist/native/iosnativeWebView/Sources/Core/Constants/CatalystConstants.swift +11 -1
  66. package/dist/native/iosnativeWebView/Sources/Core/Constants/ConfigConstants.swift +2 -1
  67. package/dist/native/iosnativeWebView/Sources/Core/WebView/BridgeMessageValidator.swift +4 -1
  68. package/dist/native/iosnativeWebView/Sources/Core/WebView/NativeBridge.swift +214 -2
  69. package/dist/native/iosnativeWebView/Sources/Core/WebView/TransitionManager.swift +169 -0
  70. package/dist/native/iosnativeWebView/Sources/Core/WebView/WebView.swift +32 -7
  71. package/dist/native/iosnativeWebView/Sources/Core/WebView/WebViewNavigationDelegate.swift +6 -2
  72. package/dist/native/iosnativeWebView/iosnativeWebView/ContentView.swift +30 -11
  73. package/dist/native/iosnativeWebView/iosnativeWebView/Info.plist +11 -17
  74. package/dist/native/iosnativeWebView/iosnativeWebView.xcodeproj/project.pbxproj +4 -2
  75. package/dist/native/iosnativeWebView/iosnativeWebViewTests/BootTimingUtilityTests.swift +1 -1
  76. package/dist/native/iosnativeWebView/iosnativeWebViewTests/ConfigMappingTests.swift +4 -2
  77. package/dist/native/pluginComposerAndroid.js +23 -4
  78. package/dist/native/pluginComposerIos.js +11 -2
  79. package/dist/native/renameAndroidProject.js +3 -3
  80. package/dist/native/setupEmulatorIos.js +6 -4
  81. package/dist/native/setupServer.js +2 -3
  82. package/dist/native/utils.js +4 -1
  83. package/dist/otel.js +70 -2
  84. package/dist/scripts/build.js +1 -1
  85. package/dist/scripts/cleanCache.js +1 -0
  86. package/dist/scripts/devBuild.js +1 -1
  87. package/dist/scripts/devServe.js +1 -1
  88. package/dist/scripts/loadEnvironmentVariables.js +1 -2
  89. package/dist/scripts/registerAliases.js +6 -2
  90. package/dist/scripts/scriptUtils.js +2 -1
  91. package/dist/scripts/serve.js +2 -1
  92. package/dist/scripts/start.js +7 -2
  93. package/dist/server/expressServer.js +3 -8
  94. package/dist/server/renderer/document/Body.js +4 -4
  95. package/dist/server/renderer/document/Head.js +2 -2
  96. package/dist/server/renderer/extract.js +31 -27
  97. package/dist/server/renderer/handler.js +47 -24
  98. package/dist/server/renderer/index.js +1 -1
  99. package/dist/server/startServer.js +57 -7
  100. package/dist/server/utils/userAgentUtil.js +9 -3
  101. package/dist/server/utils/validator.js +1 -1
  102. package/dist/webpack/babel-plugins/remove-client.plugin.js +2 -2
  103. package/dist/webpack/babel-plugins/remove-ssr.plugin.js +2 -2
  104. package/dist/webpack/babel.config.client.js +1 -1
  105. package/dist/webpack/babel.config.ssr.js +1 -1
  106. package/dist/webpack/base.babel.js +7 -8
  107. package/dist/webpack/development.client.babel.js +7 -16
  108. package/dist/webpack/production.client.babel.js +1 -1
  109. package/dist/webpack/production.ssr.babel.js +5 -1
  110. package/mcp_v2/knowledge-base.json +193 -100
  111. package/mcp_v2/lib/helpers.js +52 -12
  112. package/mcp_v2/lib/seed.js +88 -0
  113. package/mcp_v2/mcp.js +154 -7
  114. package/mcp_v2/setup.js +34 -195
  115. package/mcp_v2/tools/build.js +1 -1
  116. package/mcp_v2/tools/config.js +16 -4
  117. package/mcp_v2/tools/conversion.js +11 -11
  118. package/mcp_v2/tools/github.js +1117 -0
  119. package/mcp_v2/tools/knowledge.js +16 -3
  120. package/mcp_v2/tools/sync.js +179 -10
  121. package/mcp_v2/tools/tasks.js +13 -4
  122. package/package.json +25 -21
  123. package/.release-meta.json +0 -4
  124. package/dist/native/internal-plugins/device-info-plugin/android/DeviceInfoPlugin.kt +0 -43
  125. package/dist/native/internal-plugins/device-info-plugin/ios/DeviceInfoPlugin.swift +0 -28
  126. package/dist/native/internal-plugins/device-info-plugin/manifest.json +0 -19
  127. package/license +0 -10
@@ -0,0 +1,70 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ import android.os.Handler
4
+ import android.os.Looper
5
+ import android.util.Log
6
+ import java.util.concurrent.atomic.AtomicReference
7
+
8
+ /**
9
+ * Manages the QR hold state — after each detection, results are suppressed for HOLD_DURATION_MS
10
+ * to prevent the same QR from firing repeatedly and to let auto-zoom relax.
11
+ *
12
+ * Uses a suppress flag in BarcodeDetector instead of unbind/rebind — camera pipeline
13
+ * stays bound the whole time, so there is no flicker.
14
+ */
15
+ class HoldController(
16
+ private val stateMachine: VideoStreamStateMachine,
17
+ private val barcodeDetector: BarcodeDetector
18
+ ) {
19
+
20
+ private val TAG = "HoldController"
21
+ private val HOLD_DURATION_MS = 200L
22
+
23
+ private val handler = Handler(Looper.getMainLooper())
24
+ @Volatile private var resumeRunnable: Runnable? = null
25
+
26
+ private val lastDetectedValueRef = AtomicReference<String?>(null)
27
+ val lastDetectedValue: String? get() = lastDetectedValueRef.get()
28
+
29
+ /**
30
+ * Call after a new QR value is confirmed. Suppresses results for HOLD_DURATION_MS,
31
+ * then re-enables and transitions back to STREAMING.
32
+ */
33
+ fun startHold() {
34
+ if (!stateMachine.transition(VideoStreamState.HOLD)) return
35
+
36
+ barcodeDetector.suppressResults = true
37
+ Log.d(TAG, "Hold started — results suppressed for ${HOLD_DURATION_MS}ms")
38
+
39
+ resumeRunnable?.let { handler.removeCallbacks(it) }
40
+ val runnable = Runnable {
41
+ if (!stateMachine.state.isActive) return@Runnable
42
+ barcodeDetector.suppressResults = false
43
+ stateMachine.transition(VideoStreamState.STREAMING)
44
+ Log.d(TAG, "Hold ended — results resumed")
45
+ }
46
+ resumeRunnable = runnable
47
+ handler.postDelayed(runnable, HOLD_DURATION_MS)
48
+ }
49
+
50
+ /**
51
+ * Call on stop() or flip() to cancel any pending hold and clear detection memory.
52
+ */
53
+ fun reset() {
54
+ resumeRunnable?.let { handler.removeCallbacks(it) }
55
+ resumeRunnable = null
56
+ barcodeDetector.suppressResults = false
57
+ lastDetectedValueRef.set(null)
58
+ Log.d(TAG, "Hold state reset")
59
+ }
60
+
61
+ /**
62
+ * Returns true if this value is new (not a repeat of the last detected value).
63
+ * Also updates lastDetectedValue if it is new.
64
+ */
65
+ fun isNewValue(value: String): Boolean {
66
+ // compareAndSet atomically checks-and-updates — eliminates TOCTOU race
67
+ // between the read and the write on lastDetectedValue.
68
+ return lastDetectedValueRef.getAndSet(value) != value
69
+ }
70
+ }
@@ -0,0 +1,57 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ import android.content.Context
4
+ import android.util.Log
5
+ import androidx.camera.core.Camera
6
+ import androidx.core.content.ContextCompat
7
+
8
+ /**
9
+ * Manages torch (flashlight) state.
10
+ * Guards against front-camera torch calls.
11
+ * Fires ON_TORCH_CHANGED via [onTorchChanged] callback only after the
12
+ * hardware operation succeeds (ListenableFuture resolved on main thread).
13
+ */
14
+ class TorchController(
15
+ private val context: Context,
16
+ private val onTorchChanged: (enabled: Boolean) -> Unit
17
+ ) {
18
+
19
+ private val TAG = "TorchController"
20
+
21
+ private var camera: Camera? = null
22
+ private var currentFacing: String = "back"
23
+
24
+ fun attachCamera(cam: Camera, facing: String) {
25
+ camera = cam
26
+ currentFacing = facing
27
+ }
28
+
29
+ fun detachCamera() {
30
+ camera = null
31
+ }
32
+
33
+ fun setTorch(on: Boolean) {
34
+ val cam = camera ?: run {
35
+ Log.w(TAG, "setTorch($on) — camera is null, skipping")
36
+ return
37
+ }
38
+ if (currentFacing == "front") {
39
+ Log.w(TAG, "setTorch($on) — front camera has no torch, ignoring")
40
+ return
41
+ }
42
+ Log.d(TAG, "setTorch($on)")
43
+ cam.cameraControl.enableTorch(on).addListener(
44
+ {
45
+ Log.d(TAG, "enableTorch($on) resolved — notifying")
46
+ onTorchChanged(on)
47
+ },
48
+ ContextCompat.getMainExecutor(context)
49
+ )
50
+ }
51
+
52
+ /** Called after every bindCamera — torch always resets to off on session start. */
53
+ fun notifyReset() {
54
+ onTorchChanged(false)
55
+ Log.d(TAG, "Torch reset to off (new session)")
56
+ }
57
+ }
@@ -0,0 +1,41 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ /**
4
+ * State machine for the video stream lifecycle.
5
+ *
6
+ * Valid transitions:
7
+ * IDLE → STARTING (start() called, permission check begins)
8
+ * STARTING → STREAMING (camera bound successfully)
9
+ * STARTING → IDLE (permission denied or bind failure)
10
+ * STREAMING → HOLD (QR detected — analyzer unbound for hold duration)
11
+ * HOLD → STREAMING (hold expired — analyzer rebound)
12
+ * STREAMING → FLIPPING (flip() called)
13
+ * FLIPPING → STREAMING (rebind complete after flip)
14
+ * STREAMING → STOPPING (stop() called)
15
+ * FLIPPING → STOPPING (stop() called mid-flip)
16
+ * HOLD → STOPPING (stop() called during hold)
17
+ * STOPPING → IDLE (unbind complete)
18
+ */
19
+ enum class VideoStreamState {
20
+ IDLE,
21
+ STARTING,
22
+ STREAMING,
23
+ HOLD,
24
+ FLIPPING,
25
+ STOPPING;
26
+
27
+ fun canTransitionTo(next: VideoStreamState): Boolean = when (this) {
28
+ IDLE -> next == STARTING
29
+ STARTING -> next == STREAMING || next == IDLE
30
+ STREAMING -> next == HOLD || next == FLIPPING || next == STOPPING
31
+ HOLD -> next == STREAMING || next == STOPPING
32
+ FLIPPING -> next == STREAMING || next == STOPPING
33
+ STOPPING -> next == IDLE
34
+ }
35
+
36
+ val isActive: Boolean get() = this == STREAMING || this == HOLD || this == FLIPPING
37
+ }
38
+
39
+ interface VideoStreamStateListener {
40
+ fun onStateChanged(prev: VideoStreamState, next: VideoStreamState)
41
+ }
@@ -0,0 +1,40 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ import android.util.Log
4
+ import java.util.concurrent.CopyOnWriteArrayList
5
+
6
+ class VideoStreamStateMachine {
7
+
8
+ private val TAG = "VideoStreamStateMachine"
9
+
10
+ @Volatile
11
+ var state: VideoStreamState = VideoStreamState.IDLE
12
+ private set
13
+
14
+ private val listeners = CopyOnWriteArrayList<VideoStreamStateListener>()
15
+
16
+ fun addListener(listener: VideoStreamStateListener) {
17
+ if (!listeners.contains(listener)) listeners.add(listener)
18
+ }
19
+
20
+ fun removeListener(listener: VideoStreamStateListener) {
21
+ listeners.remove(listener)
22
+ }
23
+
24
+ @Synchronized
25
+ fun transition(next: VideoStreamState): Boolean {
26
+ val prev = state
27
+ if (!prev.canTransitionTo(next)) {
28
+ Log.w(TAG, "Invalid transition: $prev → $next, ignoring")
29
+ return false
30
+ }
31
+ state = next
32
+ Log.d(TAG, "State: $prev → $next")
33
+ // Notify outside the synchronized block to prevent deadlock if a listener
34
+ // calls back into transition() on the same thread.
35
+ val snapshot = listeners.toList()
36
+ return true.also { snapshot.forEach { it.onStateChanged(prev, next) } }
37
+ }
38
+
39
+ val isActive: Boolean get() = state.isActive
40
+ }
@@ -0,0 +1,75 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ import android.graphics.RectF
4
+ import android.util.Log
5
+ import android.webkit.WebView
6
+ import org.json.JSONObject
7
+
8
+ /**
9
+ * Pure coordinate math — converts JS viewfinder rect → screen-absolute rect
10
+ * and maps ML Kit barcode bounding boxes from image space → screen space.
11
+ * Uses WebView for origin offset and PreviewView dimensions for scale.
12
+ */
13
+ object ViewfinderMapper {
14
+
15
+ private const val TAG = "ViewfinderMapper"
16
+
17
+ /**
18
+ * Parse viewfinder rect from JS JSON and convert to screen-absolute coordinates.
19
+ * JS sends coords relative to WebView top-left; we add the WebView's on-screen origin.
20
+ *
21
+ * @return Pair(screenAbsoluteRect, jsRelativeRect) or null if parsing fails.
22
+ */
23
+ fun parseViewfinderRect(json: JSONObject, webView: WebView): Pair<RectF, RectF>? {
24
+ return try {
25
+ val x = json.getDouble("x").toFloat()
26
+ val y = json.getDouble("y").toFloat()
27
+ val w = json.getDouble("width").toFloat()
28
+ val h = json.getDouble("height").toFloat()
29
+
30
+ val jsRect = RectF(x, y, x + w, y + h)
31
+
32
+ val loc = IntArray(2)
33
+ webView.getLocationOnScreen(loc)
34
+ val originX = loc[0].toFloat()
35
+ val originY = loc[1].toFloat()
36
+
37
+ val screenRect = RectF(x + originX, y + originY, x + originX + w, y + originY + h)
38
+ Log.d(TAG, "viewfinderScreenRect: left=${screenRect.left} top=${screenRect.top} right=${screenRect.right} bottom=${screenRect.bottom}")
39
+ Pair(screenRect, jsRect)
40
+ } catch (e: Exception) {
41
+ Log.w(TAG, "Failed to parse viewfinderRect: ${e.message}")
42
+ null
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Map a barcode bounding box from ML Kit image space → screen space.
48
+ *
49
+ * ML Kit boxes are in post-rotation image space (after imgW/imgH swap).
50
+ * PreviewView uses FILL_CENTER — compute uniform scale + center offset.
51
+ */
52
+ fun mapBarcodeToScreen(
53
+ boxLeft: Int, boxTop: Int, boxRight: Int, boxBottom: Int,
54
+ imageWidth: Int, imageHeight: Int,
55
+ previewWidth: Float, previewHeight: Float
56
+ ): RectF? {
57
+ if (previewWidth == 0f || previewHeight == 0f) {
58
+ Log.w(TAG, "previewView has zero size — cannot map barcode to screen")
59
+ return null
60
+ }
61
+ if (imageWidth == 0 || imageHeight == 0) {
62
+ Log.w(TAG, "imageWidth/imageHeight is zero — cannot map barcode to screen")
63
+ return null
64
+ }
65
+ val scale = maxOf(previewWidth / imageWidth.toFloat(), previewHeight / imageHeight.toFloat())
66
+ val offsetX = (previewWidth - imageWidth * scale) / 2f
67
+ val offsetY = (previewHeight - imageHeight * scale) / 2f
68
+ return RectF(
69
+ boxLeft * scale + offsetX,
70
+ boxTop * scale + offsetY,
71
+ boxRight * scale + offsetX,
72
+ boxBottom * scale + offsetY
73
+ )
74
+ }
75
+ }
@@ -0,0 +1,156 @@
1
+ package io.yourname.androidproject.camera
2
+
3
+ import android.animation.ValueAnimator
4
+ import android.util.Log
5
+ import android.view.MotionEvent
6
+ import android.view.ScaleGestureDetector
7
+ import android.view.animation.DecelerateInterpolator
8
+ import androidx.camera.core.Camera
9
+ import android.content.Context
10
+
11
+ /**
12
+ * Handles all zoom operations:
13
+ * - setZoom(multiplier: Float) via bridge — 1.0 = 1x, 2.0 = 2x
14
+ * - pinch-to-zoom via ScaleGestureDetector
15
+ * - auto-zoom suggestion callback (wired from BarcodeDetector)
16
+ *
17
+ * Fires ON_ZOOM_CHANGED via [onZoomChanged] callback after each zoom change.
18
+ * Payload: zoomLevel (Float multiplier, e.g. 1.5), minZoom, maxZoom.
19
+ */
20
+ class ZoomController(
21
+ context: Context,
22
+ private val stateMachine: VideoStreamStateMachine,
23
+ private val onZoomChanged: (zoomLevel: Float, minZoom: Float, maxZoom: Float) -> Unit
24
+ ) {
25
+
26
+ private val TAG = "ZoomController"
27
+
28
+ private var camera: Camera? = null
29
+
30
+ // Smooth auto-zoom state
31
+ private var zoomAnimator: ValueAnimator? = null
32
+ private var lastSuggestedRatio: Float = -1f
33
+ private var consecutiveSuggestionCount: Int = 0
34
+ private val SUGGESTION_CONFIRM_FRAMES = 2 // frames before committing to a zoom target
35
+ private val SUGGESTION_TOLERANCE = 0.1f // ratio delta considered "same target"
36
+ private val ZOOM_ANIMATION_DURATION_MS = 300L
37
+
38
+ private val scaleGestureDetector = ScaleGestureDetector(
39
+ context,
40
+ object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
41
+ override fun onScale(detector: ScaleGestureDetector): Boolean {
42
+ if (!stateMachine.isActive) return true
43
+ val cam = camera ?: return true
44
+ val zoomState = cam.cameraInfo.zoomState.value ?: return true
45
+ val newRatio = (zoomState.zoomRatio * detector.scaleFactor)
46
+ .coerceIn(zoomState.minZoomRatio, zoomState.maxZoomRatio)
47
+ cam.cameraControl.setZoomRatio(newRatio)
48
+ onZoomChanged(newRatio, zoomState.minZoomRatio, zoomState.maxZoomRatio)
49
+ return true
50
+ }
51
+ }
52
+ )
53
+
54
+ fun attachCamera(cam: Camera) {
55
+ camera = cam
56
+ }
57
+
58
+ fun detachCamera() {
59
+ cancelZoomAnimation()
60
+ camera = null
61
+ }
62
+
63
+ fun onTouchEvent(event: MotionEvent): Boolean {
64
+ return if (stateMachine.isActive) scaleGestureDetector.onTouchEvent(event) else false
65
+ }
66
+
67
+ /** Called from bridge: multiplier e.g. 1.0=1x, 2.0=2x */
68
+ fun setZoom(multiplier: Float) {
69
+ Log.d(TAG, "setZoom(${multiplier}x) called")
70
+ applyZoomMultiplier(multiplier)
71
+ }
72
+
73
+ fun applyZoomMultiplier(multiplier: Float) {
74
+ val cam = camera ?: run {
75
+ Log.w(TAG, "applyZoomMultiplier(${multiplier}x) — camera is null, skipping")
76
+ return
77
+ }
78
+ val zoomState = cam.cameraInfo.zoomState.value ?: run {
79
+ Log.w(TAG, "applyZoomMultiplier(${multiplier}x) — zoomState is null, skipping")
80
+ return
81
+ }
82
+ val min = zoomState.minZoomRatio
83
+ val max = zoomState.maxZoomRatio
84
+ val ratio = multiplier.coerceIn(min, max)
85
+ Log.d(TAG, "applyZoomMultiplier(${multiplier}x) → ratio=$ratio (min=$min max=$max)")
86
+ cam.cameraControl.setZoomRatio(ratio)
87
+ onZoomChanged(ratio, min, max)
88
+ }
89
+
90
+ /**
91
+ * Auto-zoom suggestion from ML Kit ZoomSuggestionOptions.
92
+ * Zoom-only-up guard: never apply a suggestion that would zoom out.
93
+ * Debounce: only commit after SUGGESTION_CONFIRM_FRAMES consecutive frames
94
+ * with the same target (within SUGGESTION_TOLERANCE).
95
+ * Animation: smoothly interpolates from current ratio to target over ZOOM_ANIMATION_DURATION_MS.
96
+ */
97
+ fun onAutoZoomSuggestion(suggestedRatio: Float): Boolean {
98
+ val cam = camera ?: return false
99
+ val zoomState = cam.cameraInfo.zoomState.value ?: return false
100
+ val clamped = suggestedRatio.coerceIn(zoomState.minZoomRatio, zoomState.maxZoomRatio)
101
+
102
+ // Zoom-only-up guard
103
+ if (clamped <= zoomState.zoomRatio) {
104
+ Log.d(TAG, "auto-zoom suggestion=$suggestedRatio ignored — would zoom out")
105
+ consecutiveSuggestionCount = 0
106
+ lastSuggestedRatio = -1f
107
+ return false
108
+ }
109
+
110
+ // Debounce: count consecutive frames pointing at the same target
111
+ if (Math.abs(clamped - lastSuggestedRatio) <= SUGGESTION_TOLERANCE) {
112
+ consecutiveSuggestionCount++
113
+ } else {
114
+ consecutiveSuggestionCount = 1
115
+ lastSuggestedRatio = clamped
116
+ }
117
+
118
+ if (consecutiveSuggestionCount < SUGGESTION_CONFIRM_FRAMES) {
119
+ Log.d(TAG, "auto-zoom debounce: $consecutiveSuggestionCount/$SUGGESTION_CONFIRM_FRAMES for target=$clamped")
120
+ return false
121
+ }
122
+
123
+ // Confirmed — reset counters and animate
124
+ consecutiveSuggestionCount = 0
125
+ lastSuggestedRatio = -1f
126
+
127
+ val fromRatio = zoomState.zoomRatio
128
+ val minZoom = zoomState.minZoomRatio
129
+ val maxZoom = zoomState.maxZoomRatio
130
+ Log.d(TAG, "auto-zoom: animating $fromRatio → $clamped")
131
+
132
+ zoomAnimator?.cancel()
133
+ zoomAnimator = ValueAnimator.ofFloat(fromRatio, clamped).apply {
134
+ duration = ZOOM_ANIMATION_DURATION_MS
135
+ interpolator = DecelerateInterpolator()
136
+ addUpdateListener { animator ->
137
+ val ratio = animator.animatedValue as Float
138
+ cam.cameraControl.setZoomRatio(ratio)
139
+ onZoomChanged(ratio, minZoom, maxZoom)
140
+ }
141
+ start()
142
+ }
143
+ return true
144
+ }
145
+
146
+ fun cancelZoomAnimation() {
147
+ zoomAnimator?.cancel()
148
+ zoomAnimator = null
149
+ consecutiveSuggestionCount = 0
150
+ lastSuggestedRatio = -1f
151
+ }
152
+
153
+ fun currentMaxZoomRatio(): Float {
154
+ return camera?.cameraInfo?.zoomState?.value?.maxZoomRatio ?: 8.0f
155
+ }
156
+ }
@@ -5,7 +5,8 @@ import io.yourname.androidproject.utils.BridgeUtils
5
5
  import java.io.BufferedReader
6
6
  import java.io.File
7
7
  import java.io.InputStreamReader
8
- import java.net.Socket
8
+ import java.net.InetSocketAddress
9
+ import java.nio.channels.SocketChannel
9
10
 
10
11
  /**
11
12
  * Local Frida and hooking framework detection.
@@ -68,10 +69,20 @@ object FridaDetector {
68
69
  private fun checkFridaPorts(): Boolean {
69
70
  return FRIDA_PORTS.any { port ->
70
71
  try {
71
- Socket().use { socket ->
72
- val address = java.net.InetSocketAddress("127.0.0.1", port)
73
- socket.connect(address, 2000) // 2 second timeout
74
- socket.isConnected
72
+ SocketChannel.open().use { channel ->
73
+ val address = InetSocketAddress("127.0.0.1", port)
74
+ val deadline = System.currentTimeMillis() + 2000
75
+ channel.configureBlocking(false)
76
+ channel.connect(address)
77
+
78
+ while (!channel.finishConnect()) {
79
+ if (System.currentTimeMillis() >= deadline) {
80
+ return@use false
81
+ }
82
+ Thread.sleep(25)
83
+ }
84
+
85
+ true
75
86
  }
76
87
  } catch (e: Exception) {
77
88
  // Connection failed or timed out - no Frida on this port
@@ -76,12 +76,25 @@ object BridgeUtils {
76
76
  // Safe area events
77
77
  ON_SAFE_AREA_INSETS_UPDATED("ON_SAFE_AREA_INSETS_UPDATED"),
78
78
 
79
+ // Video stream events
80
+ ON_VIDEO_STREAM_READY("ON_VIDEO_STREAM_READY"),
81
+ ON_VIDEO_STREAM_STOPPED("ON_VIDEO_STREAM_STOPPED"),
82
+ ON_QR_DETECTED("ON_QR_DETECTED"),
83
+ ON_TORCH_CHANGED("ON_TORCH_CHANGED"),
84
+ ON_ZOOM_CHANGED("ON_ZOOM_CHANGED"),
85
+
79
86
  // Security events
80
87
  ON_SCREEN_SECURE_SET("ON_SCREEN_SECURE_SET"),
81
88
  ON_SCREEN_SECURE_STATUS("ON_SCREEN_SECURE_STATUS"),
82
89
  ON_SCREEN_SECURE_ERROR("ON_SCREEN_SECURE_ERROR"),
83
90
  ON_WEB_DATA_CLEARED("ON_WEB_DATA_CLEARED"),
84
91
  ON_WEB_DATA_CLEAR_ERROR("ON_WEB_DATA_CLEAR_ERROR"),
92
+
93
+ // Transition events
94
+ ON_TRANSITION_COMMITTED("ON_TRANSITION_COMMITTED"),
95
+ ON_TRANSITION_CANCELLED("ON_TRANSITION_CANCELLED"),
96
+ ON_TRANSITION_TIMEOUT("ON_TRANSITION_TIMEOUT"),
97
+
85
98
  }
86
99
 
87
100
  /**
@@ -99,13 +112,25 @@ object BridgeUtils {
99
112
  }
100
113
 
101
114
  try {
102
- val safeData = data?.replace("'", "\\'") ?: ""
103
115
  val jsCode = if (data != null) {
104
- "window.WebBridge.callback('${event.eventName}', '$safeData')"
116
+ // If data looks like a JSON object or array, inject it raw (with U+2028/U+2029
117
+ // escaping) so the JS side receives a live value rather than a double-encoded string.
118
+ // Otherwise wrap with JSONObject.quote() to produce a safe JS string literal.
119
+ val trimmed = data.trimStart()
120
+ if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
121
+ (trimmed.startsWith("[") && trimmed.endsWith("]"))) {
122
+ val safeData = data
123
+ .replace("
", "\\u2028")
124
+ .replace("
", "\\u2029")
125
+ "window.WebBridge.callback('${event.eventName}', $safeData)"
126
+ } else {
127
+ val quotedData = org.json.JSONObject.quote(data)
128
+ "window.WebBridge.callback('${event.eventName}', $quotedData)"
129
+ }
105
130
  } else {
106
131
  "window.WebBridge.callback('${event.eventName}', null)"
107
132
  }
108
-
133
+
109
134
  webView.evaluateJavascript(jsCode, null)
110
135
  Log.d(TAG, "✅ Web notification sent: ${event.eventName}")
111
136
  } catch (e: Exception) {
@@ -146,9 +171,21 @@ object BridgeUtils {
146
171
  * @param jsonData The JSON object to send
147
172
  */
148
173
  fun notifyWebJson(webView: WebView, event: WebEvents, jsonData: JSONObject) {
174
+ // Ensure WebView interactions happen on the main thread
175
+ if (Looper.myLooper() != Looper.getMainLooper()) {
176
+ webView.post { notifyWebJson(webView, event, jsonData) }
177
+ return
178
+ }
179
+
149
180
  try {
150
- val jsonString = jsonData.toString().replace("'", "\\'")
151
- notifyWeb(webView, event, jsonString)
181
+ // Inject JSON directly into the JS call. U+2028/U+2029 are valid JSON whitespace but
182
+ // are treated as line terminators by V8, causing a SyntaxError — escape them first.
183
+ val safeJson = jsonData.toString()
184
+ .replace("
", "\\u2028")
185
+ .replace("
", "\\u2029")
186
+ val jsCode = "window.WebBridge.callback('${event.eventName}', $safeJson)"
187
+ webView.evaluateJavascript(jsCode, null)
188
+ Log.d(TAG, "✅ Web JSON notification sent: ${event.eventName}")
152
189
  } catch (e: Exception) {
153
190
  Log.e(TAG, "❌ Failed to send JSON to web: ${e.message}")
154
191
  notifyWebError(webView, event, "Error processing data: ${e.message}")
@@ -12,7 +12,8 @@ import io.ktor.server.routing.*
12
12
  import kotlinx.coroutines.*
13
13
  import java.io.File
14
14
  import java.io.IOException
15
- import java.net.ServerSocket
15
+ import java.net.InetSocketAddress
16
+ import java.nio.channels.ServerSocketChannel
16
17
  import java.security.SecureRandom
17
18
  import java.util.*
18
19
  import java.util.concurrent.ConcurrentHashMap
@@ -279,7 +280,10 @@ object FrameworkServerUtils {
279
280
 
280
281
  private fun isPortAvailable(port: Int): Boolean {
281
282
  return try {
282
- ServerSocket(port).use { true }
283
+ ServerSocketChannel.open().use { channel ->
284
+ channel.bind(InetSocketAddress("127.0.0.1", port))
285
+ true
286
+ }
283
287
  } catch (e: IOException) {
284
288
  false
285
289
  }
@@ -408,4 +412,4 @@ object FrameworkServerUtils {
408
412
  }
409
413
  }
410
414
  }
411
- }
415
+ }
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <shape xmlns:android="http://schemas.android.com/apk/res/android"
3
+ android:shape="rectangle">
4
+ <stroke
5
+ android:width="3dp"
6
+ android:color="#FF0000" />
7
+ <solid android:color="#33FF0000" />
8
+ </shape>
@@ -13,6 +13,12 @@
13
13
  app:layout_constraintStart_toStartOf="parent"
14
14
  app:layout_constraintEnd_toEndOf="parent">
15
15
 
16
+ <androidx.camera.view.PreviewView
17
+ android:id="@+id/camera_preview"
18
+ android:layout_width="match_parent"
19
+ android:layout_height="match_parent"
20
+ android:visibility="invisible" />
21
+
16
22
  <WebView
17
23
  android:id="@+id/webview"
18
24
  android:layout_width="match_parent"
@@ -24,6 +30,37 @@
24
30
  android:layout_width="wrap_content"
25
31
  android:layout_height="wrap_content"
26
32
  android:visibility="gone" />
33
+
34
+ <RelativeLayout
35
+ android:id="@+id/debug_viewfinder_overlay"
36
+ android:layout_width="0dp"
37
+ android:layout_height="0dp"
38
+ android:background="@null"
39
+ android:visibility="gone">
40
+
41
+ <TextView
42
+ android:id="@+id/debug_qr_status"
43
+ android:layout_width="wrap_content"
44
+ android:layout_height="wrap_content"
45
+ android:layout_alignParentBottom="true"
46
+ android:layout_centerHorizontal="true"
47
+ android:layout_marginBottom="8dp"
48
+ android:paddingHorizontal="10dp"
49
+ android:paddingVertical="4dp"
50
+ android:background="#CC000000"
51
+ android:textColor="#FFFFFF"
52
+ android:textSize="12sp"
53
+ android:text="⬤ –" />
54
+
55
+ </RelativeLayout>
56
+
57
+ <!-- Debug: shows where native thinks the barcode box is in screen space -->
58
+ <View
59
+ android:id="@+id/debug_barcode_overlay"
60
+ android:layout_width="0dp"
61
+ android:layout_height="0dp"
62
+ android:background="#5500FF00"
63
+ android:visibility="gone" />
27
64
  </RelativeLayout>
28
65
 
29
66
  </androidx.constraintlayout.widget.ConstraintLayout>
@@ -4,5 +4,6 @@ if(!_fs.default.existsSync(javaHome)){throw new Error(`Java installation not fou
4
4
  const homeDir=process.env.HOME;const shellProfile=`${homeDir}/.zshrc`;const exportCommand=`\n# Java Configuration\nexport JAVA_HOME=${javaHome}\nexport PATH=$JAVA_HOME/bin:$PATH\n`;try{_fs.default.appendFileSync(shellProfile,exportCommand);}catch(error){throw new Error(`Failed to update ${shellProfile}: ${error.message}. Please add the following lines manually:\n${exportCommand}`);}// Set for current process
5
5
  process.env.JAVA_HOME=javaHome;process.env.PATH=`${javaHome}/bin:${process.env.PATH}`;// Verify Java configuration
6
6
  try{const javaVersion=(0,_utils.runCommand)("java -version");progress.log("Java installed and configured successfully","success");progress.log(`Java Version: ${javaVersion}`,"info");progress.log(`IMPORTANT: Please run the following commands to complete setup:`,"warning");progress.log(`1. source ${shellProfile}`,"info");progress.log(`2. echo $JAVA_HOME`,"info");return true;}catch(error){throw new Error(`Java installation verified but environment not configured. Please run:\n1. source ${shellProfile}\n2. echo $JAVA_HOME`);}}catch(error){progress.log(error.message,"error");progress.log("Manual Setup Instructions:","info");progress.log("1. Run: brew reinstall --cask zulu@17","info");progress.log("2. Add to ~/.zshrc:","info");progress.log(" export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home","info");progress.log(" export PATH=$JAVA_HOME/bin:$PATH","info");progress.log("3. Run: source ~/.zshrc","info");throw error;}}async function validateJavaEnvironment(){progress.start("java");const hasJava=await checkJavaInstallation();if(!hasJava){progress.log("Java installation required. Starting installation process...");const javaInstalled=await installJava();if(!javaInstalled){progress.fail("java","Failed to set up Java environment");throw new Error("Failed to set up Java environment");}}progress.complete("java");}async function initializeConfig(){const configFile=_fs.default.readFileSync(configPath,"utf8");const config=JSON.parse(configFile);const{WEBVIEW_CONFIG}=config;if(!WEBVIEW_CONFIG||Object.keys(WEBVIEW_CONFIG).length===0){progress.log("WebView Config missing in "+configPath,"error");process.exit(1);}if(!WEBVIEW_CONFIG.android){WEBVIEW_CONFIG.android={};}return{WEBVIEW_CONFIG};}async function saveConfig(newConfig){try{const existingConfigFile=_fs.default.readFileSync(configPath,"utf8");const existingConfig=JSON.parse(existingConfigFile);const updatedConfig={...existingConfig,WEBVIEW_CONFIG:{...existingConfig.WEBVIEW_CONFIG,android:newConfig.WEBVIEW_CONFIG.android}};_fs.default.writeFileSync(configPath,JSON.stringify(updatedConfig,null,2));progress.log("Configuration saved successfully","success");}catch(error){progress.log("Failed to save configuration: "+error.message,"error");process.exit(1);}}function validateAndroidTools(androidConfig){const ANDROID_SDK=androidConfig.sdkPath;const EMULATOR_PATH=`${ANDROID_SDK}/emulator/emulator`;const ADB_PATH=`${ANDROID_SDK}/platform-tools/adb`;if(!ANDROID_SDK){throw new Error("Android SDK path is not configured");}if(!_fs.default.existsSync(ANDROID_SDK)){throw new Error(`Android SDK path does not exist: ${ANDROID_SDK}`);}if(!_fs.default.existsSync(ADB_PATH)){throw new Error(`ADB not found at: ${ADB_PATH}`);}try{(0,_utils.runCommand)(`${ADB_PATH} version`);progress.log("ADB is valid","success");}catch(error){throw new Error(`ADB is not working properly: ${error.message}`);}if(!_fs.default.existsSync(EMULATOR_PATH)){throw new Error(`Emulator not found at: ${EMULATOR_PATH}`);}try{(0,_utils.runCommand)(`${EMULATOR_PATH} -version`);progress.log("Emulator is valid","success");}catch(error){throw new Error(`Emulator is not working properly: ${error.message}`);}try{const avdOutput=(0,_utils.runCommand)(`${EMULATOR_PATH} -list-avds`);if(!avdOutput.includes(androidConfig.emulatorName)){throw new Error(`Specified emulator "${androidConfig.emulatorName}" not found in available AVDs`);}progress.log(`Emulator "${androidConfig.emulatorName}" exists`,"success");}catch(error){throw new Error(`Error checking emulator AVD: ${error.message}`);}progress.log("Android tools validation completed successfully!","success");return{EMULATOR_PATH,ADB_PATH};}async function checkEmulator(ADB_PATH){try{const devices=(0,_utils.runCommand)(`${ADB_PATH} devices`);return devices.includes("emulator");}catch(error){progress.log(`Error checking emulator: ${error}`,"error");return false;}}async function startEmulator(EMULATOR_PATH,androidConfig){progress.log(`Starting emulator: ${androidConfig.emulatorName}...`);// eslint-disable-next-line security/detect-child-process
7
+ // nosemgrep: javascript.lang.security.detect-child-process.detect-child-process - EMULATOR_PATH is derived from SDK configuration and emulatorName is local native setup config.
7
8
  (0,_child_process.exec)(`${EMULATOR_PATH} -avd ${androidConfig.emulatorName} -read-only > /dev/null &`,error=>{if(error){progress.log(`Error starting emulator: ${error}`,"error");}});}async function updateLocalProperties(sdkPath){progress.start("properties");try{(0,_utils.runCommand)(`cd ${pwd}/androidProject && ./gradlew updateSdkPath -PsdkPath="${sdkPath}"`);progress.log("Updated local.properties successfully","success");progress.complete("properties");}catch(error){progress.fail("properties",`Failed to update local.properties: ${error.message}`);throw error;}}async function setupAndroidEnvironment(){try{await validateJavaEnvironment();progress.start("config");await initializeConfig();const config=await(0,_utils.validateAndCompleteConfig)("android",configPath);progress.complete("config");progress.start("androidTools");const{EMULATOR_PATH,ADB_PATH}=validateAndroidTools(config.android);progress.complete("androidTools");progress.start("emulator");const emulatorRunning=await checkEmulator(ADB_PATH);if(!emulatorRunning){progress.log("No emulator running, attempting to start one...");await startEmulator(EMULATOR_PATH,config.android);}else{progress.log("Emulator already running, proceeding with installation...","success");}progress.complete("emulator");if(config.android?.sdkPath){await updateLocalProperties(config.android.sdkPath);}progress.start("saveConfig");await saveConfig({WEBVIEW_CONFIG:config});progress.complete("saveConfig");progress.start("setupServer");await(0,_setupServer.setupServer)(configPath);progress.complete("setupServer");progress.printTreeContent("Configuration Explanation",["WEBVIEW_CONFIG: Main configuration object for the WebView setup",{text:"port: Port number for the WebView server",indent:1,prefix:"├─ ",color:"gray"},{text:"android: Android-specific configuration",indent:1,prefix:"└─ ",color:"gray"},{text:"buildType: Build type (debug/release)",indent:2,prefix:"├─ ",color:"gray"},{text:"sdkPath: Android SDK path",indent:2,prefix:"├─ ",color:"gray"},{text:"emulatorName: Selected Android emulator name",indent:2,prefix:"└─ ",color:"gray"}]);progress.printTreeContent("Final Configuration",[JSON.stringify(config,null,2)]);process.exit(0);}catch(error){if(progress.currentStep){progress.fail(progress.currentStep.id,error.message);}process.exit(1);}}// Execute the main setup
8
9
  setupAndroidEnvironment();