Skip to content

Commit

Permalink
CameraProcessFragment
Browse files Browse the repository at this point in the history
- More gracefully handles the bad situation of the camera being disconnected when the fragment has been deactivated
  • Loading branch information
lessthanoptimal committed Sep 9, 2023
1 parent 24adbee commit 8a7a73c
Showing 1 changed file with 7 additions and 3 deletions.
Expand Up @@ -77,7 +77,7 @@ abstract class CameraProcessFragment : Fragment() {
/**
* Stops all camera capture sessions and cleans up
*/
protected fun closeAllCameras() = lifecycleScope.launch(Dispatchers.Main){
protected fun closeAllCameras() = lifecycleScope.launch(Dispatchers.Main) {
for (cam in cameraDevices.values) {
cam.session?.stopRepeating()
cam.device.close()
Expand Down Expand Up @@ -211,8 +211,8 @@ abstract class CameraProcessFragment : Fragment() {
}

override fun onDisconnected(camera: CameraDevice) {
Log.w(TAG, "Camera $cameraID has been disconnected")
requireActivity().finish()
Log.w(TAG, "Camera $cameraID has been disconnected. null-activity=${activity == null}")
activity?.finish()
results.finished = true
}

Expand Down Expand Up @@ -240,6 +240,10 @@ abstract class CameraProcessFragment : Fragment() {
while (!results.finished && System.currentTimeMillis() < startTime + 10_000L) {
Thread.yield()
}

if (results.camera == null)
Log.e(TAG, "Failed to get camera object when opening camera.")

return results.camera!!
}

Expand Down

0 comments on commit 8a7a73c

Please sign in to comment.