From e4a5fe365e9e3d14fbbc47aab483ea01cc458980 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 1 Mar 2024 16:28:58 +0100 Subject: [PATCH] fix: Fix focus not running on UI Thread --- .../main/java/com/mrousavy/camera/CameraView+Focus.kt | 5 ++++- .../main/java/com/mrousavy/camera/core/CameraSession.kt | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/CameraView+Focus.kt b/package/android/src/main/java/com/mrousavy/camera/CameraView+Focus.kt index 90b775054c..1c871282b8 100644 --- a/package/android/src/main/java/com/mrousavy/camera/CameraView+Focus.kt +++ b/package/android/src/main/java/com/mrousavy/camera/CameraView+Focus.kt @@ -1,11 +1,14 @@ package com.mrousavy.camera import com.facebook.react.bridge.ReadableMap +import com.mrousavy.camera.utils.runOnUiThreadAndWait suspend fun CameraView.focus(pointMap: ReadableMap) { val x = pointMap.getDouble("x") val y = pointMap.getDouble("y") - val point = previewView.meteringPointFactory.createPoint(x.toFloat(), y.toFloat()) + val point = runOnUiThreadAndWait { + previewView.meteringPointFactory.createPoint(x.toFloat(), y.toFloat()) + } cameraSession.focus(point) } diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt index 40a8044183..ad1a491ad5 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt @@ -508,6 +508,7 @@ class CameraSession(private val context: Context, private val callback: Callback recording.resume() } + @SuppressLint("RestrictedApi") suspend fun focus(meteringPoint: MeteringPoint) { val camera = camera ?: throw CameraNotReadyError() @@ -517,8 +518,14 @@ class CameraSession(private val context: Context, private val callback: Callback } try { + Log.i(TAG, "Focusing to ${action.meteringPointsAf.joinToString { "(${it.x}, ${it.y})" }}...") val future = camera.cameraControl.startFocusAndMetering(action) - future.await(CameraQueues.cameraExecutor) + val result = future.await(CameraQueues.cameraExecutor) + if (result.isFocusSuccessful) { + Log.i(TAG, "Focused successfully!") + } else { + Log.i(TAG, "Focus failed.") + } } catch (e: CameraControl.OperationCanceledException) { throw FocusCanceledError() }