Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close camera on backgrounding the app #58

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface CameraUseCase {
currentCameraSettings: CameraAppSettings
)

suspend fun unbindAll()

suspend fun takePicture()

suspend fun startVideoRecording()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ constructor(
}
}

override suspend fun unbindAll() {
cameraProvider.unbindAll()
}

override suspend fun takePicture() {
val imageDeferred = CompletableDeferred<ImageProxy>()

Expand Down Expand Up @@ -245,7 +249,7 @@ constructor(
Log.d(
TAG,
"Changing CaptureMode: singleStreamCaptureEnabled:" +
(captureMode == CaptureMode.SINGLE_STREAM)
(captureMode == CaptureMode.SINGLE_STREAM)
)
updateUseCaseGroup()
rebindUseCases()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class FakeCameraUseCase : CameraUseCase {
previewStarted = true
}

override suspend fun unbindAll() {
useCasesBinded = false
previewStarted = false
}

override suspend fun takePicture() {
if (!useCasesBinded) {
throw IllegalStateException("Usecases not binded")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class PreviewViewModel @Inject constructor(
cancel()
}
}
viewModelScope.launch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should handle this from inside the CameraUseCase without needing the ViewModel's manual intervention.

When runningCameraJob.cancel() is called I think we'll get to awaitCancellation() in CameraXCameraUseCase.

From there we can do a finally block, and call cameraProvider.unbindAll()

Lets wait to hear from @temcguir on this too.

cameraUseCase.unbindAll()
}
}

fun setFlash(flashModeStatus: FlashModeStatus) {
Expand Down Expand Up @@ -168,8 +171,8 @@ class PreviewViewModel @Inject constructor(
if (previewUiState.value.currentCameraSettings.isBackCameraAvailable
&& previewUiState.value.currentCameraSettings.isFrontCameraAvailable
) {

viewModelScope.launch {
stopCamera()
runningCameraJob = viewModelScope.launch {
_previewUiState.emit(
previewUiState.value.copy(
currentCameraSettings =
Expand Down