fix inconsistencies when user switches camera mode fast

- debounce switching by 300ms when toggling between video/photo capture
- setCameraAvailable only when there is no error and the camera state is Type.OPEN
This commit is contained in:
darthpaul 2022-11-11 00:44:53 +00:00
parent 7b7838331e
commit c858e5b908
2 changed files with 29 additions and 15 deletions

View File

@ -52,6 +52,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
const val PHOTO_MODE_INDEX = 1
const val VIDEO_MODE_INDEX = 0
private const val MIN_SWIPE_DISTANCE_X = 100
private const val DELAY_BETWEEN_MODE_SWITCH = 300L
}
lateinit var mTimerHandler: Handler
@ -67,10 +68,14 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
private var mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0
var mLastHandledOrientation = 0
private val togglePhotoVideoRunnable = Runnable {
handleTogglePhotoVideo()
}
private val tabSelectedListener = object : TabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
handleTogglePhotoVideo()
camera_mode_tab.removeCallbacks(togglePhotoVideoRunnable)
camera_mode_tab.postDelayed(togglePhotoVideoRunnable, DELAY_BETWEEN_MODE_SWITCH)
}
}
@ -382,7 +387,13 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
@SuppressLint("ClickableViewAccessibility")
private fun initModeSwitcher() {
val gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
val gestureDetector = GestureDetectorCompat(this, object : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent): Boolean {
// we have to return true here so ACTION_UP
// (and onFling) can be dispatched
return true
}
override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
// these can be null even if the docs say they cannot
if (event1 == null || event2 == null) {
@ -404,7 +415,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
}
})
camera_mode_tab.setOnTouchListener { _, event ->
bottom_overlay.setOnTouchListener { _, event ->
gestureDetector.onTouchEvent(event)
}
}

View File

@ -244,20 +244,23 @@ class CameraXPreview(
listener.onChangeCamera(isFrontCameraInUse())
camera?.cameraInfo?.cameraState?.observe(activity) { cameraState ->
when (cameraState.type) {
CameraState.Type.OPEN,
CameraState.Type.OPENING -> {
listener.setHasFrontAndBackCamera(hasFrontCamera() && hasBackCamera())
listener.setCameraAvailable(true)
}
CameraState.Type.PENDING_OPEN,
CameraState.Type.CLOSING,
CameraState.Type.CLOSED -> {
listener.setCameraAvailable(false)
if (cameraState.error == null) {
when (cameraState.type) {
CameraState.Type.OPEN-> {
listener.setHasFrontAndBackCamera(hasFrontCamera() && hasBackCamera())
listener.setCameraAvailable(true)
}
CameraState.Type.OPENING,
CameraState.Type.PENDING_OPEN,
CameraState.Type.CLOSING,
CameraState.Type.CLOSED -> {
listener.setCameraAvailable(false)
}
}
} else {
listener.setCameraAvailable(false)
cameraErrorHandler.handleCameraError(cameraState.error)
}
cameraErrorHandler.handleCameraError(cameraState?.error)
}
}