1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.jetpackcamera.core.camera
17 
18 import android.annotation.SuppressLint
19 import androidx.camera.core.Camera
20 import androidx.camera.core.CameraSelector
21 import androidx.camera.core.CompositionSettings
22 import androidx.camera.core.ConcurrentCamera
23 import androidx.camera.core.ConcurrentCamera.SingleCameraConfig
24 import androidx.camera.core.UseCaseGroup
25 import androidx.camera.lifecycle.ProcessCameraProvider
26 import androidx.lifecycle.Lifecycle
27 import androidx.lifecycle.LifecycleOwner
28 import androidx.lifecycle.LifecycleRegistry
29 import kotlin.coroutines.CoroutineContext
30 import kotlinx.coroutines.CoroutineScope
31 import kotlinx.coroutines.Job
32 import kotlinx.coroutines.coroutineScope
33 
34 /**
35  * Runs a camera for the duration of a coroutine.
36  *
37  * The camera selected by [cameraSelector] will run with the provided [useCases] for the
38  * duration that [block] is active. This means that [block] should suspend until the camera
39  * should be closed.
40  */
runWithnull41 suspend fun <R> ProcessCameraProvider.runWith(
42     cameraSelector: CameraSelector,
43     useCases: UseCaseGroup,
44     block: suspend CoroutineScope.(Camera) -> R
45 ): R = coroutineScope {
46     val scopedLifecycle = CoroutineLifecycleOwner(coroutineContext)
47     block(this@runWith.bindToLifecycle(scopedLifecycle, cameraSelector, useCases))
48 }
49 
50 @SuppressLint("RestrictedApi")
runWithConcurrentnull51 suspend fun <R> ProcessCameraProvider.runWithConcurrent(
52     cameraConfigs: List<Pair<CameraSelector, CompositionSettings>>,
53     useCaseGroup: UseCaseGroup,
54     block: suspend CoroutineScope.(ConcurrentCamera) -> R
55 ): R = coroutineScope {
56     val scopedLifecycle = CoroutineLifecycleOwner(coroutineContext)
57     val singleCameraConfigs = cameraConfigs.map {
58         SingleCameraConfig(it.first, useCaseGroup, it.second, scopedLifecycle)
59     }
60     block(this@runWithConcurrent.bindToLifecycle(singleCameraConfigs))
61 }
62 
63 /**
64  * A [LifecycleOwner] that follows the lifecycle of a coroutine.
65  *
66  * If the coroutine is active, the owned lifecycle will jump to a
67  * [Lifecycle.State.RESUMED] state. When the coroutine completes, the owned lifecycle will
68  * transition to a [Lifecycle.State.DESTROYED] state.
69  */
70 private class CoroutineLifecycleOwner(coroutineContext: CoroutineContext) :
71     LifecycleOwner {
72     private val lifecycleRegistry: LifecycleRegistry =
<lambda>null73         LifecycleRegistry(this).apply {
74             currentState = Lifecycle.State.INITIALIZED
75         }
76 
77     override val lifecycle: Lifecycle
78         get() = lifecycleRegistry
79 
80     init {
81         if (coroutineContext[Job]?.isActive == true) {
82             lifecycleRegistry.currentState = Lifecycle.State.RESUMED
<lambda>null83             coroutineContext[Job]?.invokeOnCompletion {
84                 lifecycleRegistry.apply {
85                     currentState = Lifecycle.State.STARTED
86                     currentState = Lifecycle.State.CREATED
87                     currentState = Lifecycle.State.DESTROYED
88                 }
89             }
90         } else {
91             lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
92         }
93     }
94 }
95