@@ -45,6 +45,8 @@ public class CameraStateAdapter @Inject constructor() {
4545
4646 @GuardedBy(" lock" ) private var currentGraph: CameraGraph ? = null
4747
48+ @GuardedBy(" lock" ) private var closedGraph: CameraGraph ? = null
49+
4850 @GuardedBy(" lock" ) private var currentCameraInternalState = CameraInternal .State .CLOSED
4951
5052 @GuardedBy(" lock" ) private var currentCameraStateError: CameraState .StateError ? = null
@@ -77,6 +79,7 @@ public class CameraStateAdapter @Inject constructor() {
7779
7880 // Clear the graph reference as it's no longer valid.
7981 currentGraph = null
82+ closedGraph = null
8083 }
8184 }
8285
@@ -88,7 +91,28 @@ public class CameraStateAdapter @Inject constructor() {
8891 postCameraState(CameraInternal .State .CLOSED )
8992 }
9093 currentGraph = cameraGraph
94+ closedGraph = null
9195 currentCameraInternalState = CameraInternal .State .CLOSED
96+ currentCameraStateError = null
97+ }
98+
99+ /* *
100+ * Signals that a specific CameraGraph has been explicitly closed by the application layer. This
101+ * acts as the definitive "User requested close" signal.
102+ */
103+ public fun onGraphClosed (cameraGraph : CameraGraph ): Unit =
104+ synchronized(lock) {
105+ if (currentGraph == cameraGraph) {
106+ // If we are still in a non-closed state, force a transition to CLOSED.
107+ if (currentCameraInternalState != CameraInternal .State .CLOSED ) {
108+ // Transition to closing state and we will wait for CameraGraph to stop
109+ // and then transition to CLOSED state.
110+ postCameraState(CameraInternal .State .CLOSING )
111+ postCameraState(CameraInternal .State .CLOSED )
112+ }
113+ closedGraph = cameraGraph
114+ currentCameraInternalState = CameraInternal .State .CLOSED
115+ }
92116 }
93117
94118 public fun onGraphStateUpdated (cameraGraph : CameraGraph , graphState : GraphState ): Unit =
@@ -111,7 +135,14 @@ public class CameraStateAdapter @Inject constructor() {
111135 return
112136 }
113137
114- val nextComboState = calculateNextState(currentCameraInternalState, graphState)
138+ val isGraphActive = currentGraph != null && currentGraph != closedGraph
139+ val nextComboState =
140+ calculateNextState(
141+ currentCameraInternalState,
142+ graphState,
143+ currentCameraStateError,
144+ isGraphActive,
145+ )
115146 if (nextComboState == null ) {
116147 Camera2Logger .warn {
117148 " Impermissible state transition: " +
@@ -120,11 +151,22 @@ public class CameraStateAdapter @Inject constructor() {
120151 }
121152 return
122153 }
154+ // Fill Missing Transition: PENDING/CLOSED -> OPENING -> OPEN
155+ if (nextComboState.state == CameraInternal .State .OPEN ) {
156+ if (
157+ currentCameraInternalState == CameraInternal .State .CLOSED ||
158+ currentCameraInternalState == CameraInternal .State .PENDING_OPEN
159+ ) {
160+ postCameraState(CameraInternal .State .OPENING )
161+ }
162+ }
123163 currentCameraInternalState = nextComboState.state
124164 currentCameraStateError = nextComboState.error
125165
126166 // Now that the current graph state is updated, post the latest states.
127- Camera2Logger .debug { " Updated current camera internal state to $nextComboState " }
167+ Camera2Logger .debug {
168+ " Updated current camera internal state: $currentCameraInternalState to $nextComboState "
169+ }
128170 postCameraState(currentCameraInternalState, currentCameraStateError)
129171 }
130172
@@ -152,6 +194,8 @@ public class CameraStateAdapter @Inject constructor() {
152194 internal fun calculateNextState (
153195 currentState : CameraInternal .State ,
154196 graphState : GraphState ,
197+ currentError : CameraState .StateError ? ,
198+ isGraphActive : Boolean ,
155199 ): CombinedCameraState ? =
156200 when (currentState) {
157201 CameraInternal .State .CLOSED ->
@@ -164,49 +208,22 @@ public class CameraStateAdapter @Inject constructor() {
164208 when (graphState) {
165209 GraphStateStarted -> CombinedCameraState (CameraInternal .State .OPEN )
166210 is GraphStateError ->
167- if (graphState.willAttemptRetry) {
168- CombinedCameraState (
169- CameraInternal .State .OPENING ,
170- graphState.cameraError.toCameraStateError(),
171- )
172- } else {
173- if (isRecoverableError(graphState.cameraError)) {
174- CombinedCameraState (
175- CameraInternal .State .PENDING_OPEN ,
176- graphState.cameraError.toCameraStateError(),
177- )
178- } else {
179- CombinedCameraState (
180- CameraInternal .State .CLOSING ,
181- graphState.cameraError.toCameraStateError(),
182- )
183- }
184- }
185- GraphStateStopping -> CombinedCameraState (CameraInternal .State .CLOSING )
186- GraphStateStopped -> CombinedCameraState (CameraInternal .State .CLOSED )
211+ resolveErrorEvent(graphState, CameraInternal .State .OPENING )
212+ GraphStateStopping ,
213+ GraphStateStopped -> graphState.handleStateStop(isGraphActive, currentError)
187214 else -> null
188215 }
189216 CameraInternal .State .OPEN ->
190217 when (graphState) {
191- GraphStateStopping -> CombinedCameraState ( CameraInternal . State . CLOSING )
192- GraphStateStopped -> CombinedCameraState ( CameraInternal . State . CLOSED )
218+ GraphStateStopping ,
219+ GraphStateStopped -> graphState.handleStateStop(isGraphActive, currentError )
193220 is GraphStateError ->
194- if (isRecoverableError(graphState.cameraError)) {
195- CombinedCameraState (
196- CameraInternal .State .PENDING_OPEN ,
197- graphState.cameraError.toCameraStateError(),
198- )
199- } else {
200- CombinedCameraState (
201- CameraInternal .State .CLOSED ,
202- graphState.cameraError.toCameraStateError(),
203- )
204- }
221+ resolveErrorEvent(graphState, CameraInternal .State .OPENING )
205222 else -> null
206223 }
207224 CameraInternal .State .CLOSING ->
208225 when (graphState) {
209- GraphStateStopped -> CombinedCameraState ( CameraInternal . State . CLOSED )
226+ GraphStateStopped -> graphState.handleStateStop(isGraphActive, currentError )
210227 GraphStateStarting -> CombinedCameraState (CameraInternal .State .OPENING )
211228 is GraphStateError ->
212229 CombinedCameraState (
@@ -219,18 +236,15 @@ public class CameraStateAdapter @Inject constructor() {
219236 when (graphState) {
220237 GraphStateStarting -> CombinedCameraState (CameraInternal .State .OPENING )
221238 GraphStateStarted -> CombinedCameraState (CameraInternal .State .OPEN )
222- is GraphStateError ->
223- if (isRecoverableError(graphState.cameraError)) {
224- CombinedCameraState (
225- CameraInternal .State .PENDING_OPEN ,
226- graphState.cameraError.toCameraStateError(),
227- )
228- } else {
229- CombinedCameraState (
230- CameraInternal .State .CLOSED ,
231- graphState.cameraError.toCameraStateError(),
232- )
233- }
239+ is GraphStateError -> {
240+ // Calculate the error code (downgrading critical to recoverable if needed)
241+ val (_, newError) =
242+ resolveErrorEvent(graphState, CameraInternal .State .OPENING )
243+ // STAY in PENDING_OPEN. Do not go back to OPENING.
244+ CombinedCameraState (CameraInternal .State .PENDING_OPEN , newError)
245+ }
246+ GraphStateStopping ,
247+ GraphStateStopped -> graphState.handleStateStop(isGraphActive, currentError)
234248 else -> null
235249 }
236250 else -> null
@@ -250,6 +264,54 @@ public class CameraStateAdapter @Inject constructor() {
250264 )
251265
252266 public companion object {
267+ private fun GraphState.handleStateStop (
268+ isGraphActive : Boolean ,
269+ currentError : CameraState .StateError ? ,
270+ ): CombinedCameraState ? {
271+ if (isGraphActive && currentError != null ) {
272+ return CombinedCameraState (CameraInternal .State .PENDING_OPEN , currentError)
273+ }
274+
275+ // If the graph is not active (User closed it), strictly close.
276+ return when (this ) {
277+ GraphStateStopping ->
278+ CombinedCameraState (CameraInternal .State .CLOSING , currentError)
279+ GraphStateStopped -> CombinedCameraState (CameraInternal .State .CLOSED , currentError)
280+ else -> null
281+ }
282+ }
283+
284+ /* * Resolves a GraphStateError into a CombinedCameraState. */
285+ private fun resolveErrorEvent (
286+ graphStateError : GraphStateError ,
287+ retryState : CameraInternal .State ,
288+ ): CombinedCameraState {
289+ val cameraError = graphStateError.cameraError
290+
291+ // 1. Recoverable via Retry or Native Recoverability
292+ if (isRecoverableError(cameraError) || graphStateError.willAttemptRetry) {
293+ // If retrying, force "Recoverable" type to align with CameraX contract
294+ val stateError =
295+ if (graphStateError.willAttemptRetry && ! isRecoverableError(cameraError)) {
296+ CameraState .StateError .create(CameraState .ERROR_OTHER_RECOVERABLE_ERROR )
297+ } else {
298+ cameraError.toCameraStateError()
299+ }
300+ val nextState =
301+ if (! graphStateError.willAttemptRetry) {
302+ CameraInternal .State .PENDING_OPEN
303+ } else {
304+ retryState
305+ }
306+ return CombinedCameraState (nextState, stateError)
307+ }
308+
309+ return CombinedCameraState (
310+ CameraInternal .State .CLOSING ,
311+ cameraError.toCameraStateError(),
312+ )
313+ }
314+
253315 internal fun CameraError.toCameraStateError (): CameraState .StateError =
254316 CameraState .StateError .create(
255317 when (this ) {
0 commit comments