fix(camera): 修复检查活动中相机预览启动逻辑

- 添加了 previewStartAttempted 标志防止重复启动相机预览
- 在多个关键位置增加了调试日志用于追踪相机状态
- 优化了相机预览可用性检测和启动时机
- 修复了相机预览启动失败后的状态重置逻辑
- 改进了相机预览启动时的 UI 可见性控制
- 添加了相机预览停止时的异常处理机制
这个提交包含在:
徐勤民 2026-04-21 17:54:18 +08:00
父节点 10ca6f3a37
当前提交 92674a5b93

查看文件

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