diff --git a/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt b/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt index c5c1212..8b2ceae 100644 --- a/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt +++ b/app/src/main/java/com/nova/brain/glass/ui/InspectionActivity.kt @@ -41,10 +41,12 @@ class InspectionActivity : private var isPreviewActive = false private var currentZoomLevel = 1 private var maxZoomLevel = 1 + private var previewStartAttempted = false private val listener = object : OfflineCmdListener { override fun onOfflineCmd(cmd: String) { runOnUiThread { + LogHelper.d("InspectionActivity onOfflineCmd: $cmd") when (cmd) { "退出", "返回", "退回" -> finish() "开始", "开始任务" -> startCapture() @@ -93,6 +95,7 @@ class InspectionActivity : private val previewTextureListener = object : TextureView.SurfaceTextureListener { override fun onSurfaceTextureAvailable(surface: android.graphics.SurfaceTexture, width: Int, height: Int) { + LogHelper.d("Inspection preview surface available: ${width}x$height, requested=$isPreviewRequested") if (isPreviewRequested) { startCameraPreview() } @@ -169,27 +172,43 @@ class InspectionActivity : capturePhoto() return } + LogHelper.d("Inspection startCapture: previewActive=$isPreviewActive, textureAvailable=${binding.cameraPreview.isAvailable}") binding.hint.text = "相机预览启动中,请稍后..." isPreviewRequested = true + previewStartAttempted = false SprayingPhotoManager.clear() + binding.cameraPreviewContainer.visibility = View.VISIBLE + binding.zoomText.visibility = View.VISIBLE if (binding.cameraPreview.isAvailable) { startCameraPreview() } else { - binding.cameraPreviewContainer.visibility = View.VISIBLE - binding.zoomText.visibility = View.VISIBLE + binding.cameraPreview.post { + LogHelper.d("Inspection preview post check: textureAvailable=${binding.cameraPreview.isAvailable}, requested=$isPreviewRequested") + if (isPreviewRequested && binding.cameraPreview.isAvailable) { + startCameraPreview() + } + } } } private fun startCameraPreview() { + if (!isPreviewRequested || previewStartAttempted) { + LogHelper.d("Inspection startCameraPreview skipped: requested=$isPreviewRequested attempted=$previewStartAttempted") + return + } val surfaceTexture = binding.cameraPreview.surfaceTexture ?: return + previewStartAttempted = true val surface = Surface(surfaceTexture) + LogHelper.d("Inspection startCameraPreview invoke startCameraShare") runCatching { GlassMediaServiceHelper.startCameraShare(surface, cameraSurfaceCallback) }.onFailure { isPreviewRequested = false + previewStartAttempted = false binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE binding.hint.text = "相机预览启动失败,请重试" + LogHelper.e("Inspection startCameraPreview failed: ${it.message}", it) (it.message ?: "相机预览启动失败").showMessage() } } @@ -197,8 +216,12 @@ class InspectionActivity : private fun stopCameraPreview() { isPreviewRequested = false isPreviewActive = false + previewStartAttempted = false + LogHelper.d("Inspection stopCameraPreview") runCatching { GlassMediaServiceHelper.stopCameraShare(cameraSurfaceCallback) + }.onFailure { + LogHelper.e("Inspection stopCameraPreview failed: ${it.message}", it) } binding.cameraPreviewContainer.visibility = View.GONE binding.zoomText.visibility = View.GONE