1 /*
<lambda>null2  * Copyright (C) 2024 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 
17 package com.android.settings.biometrics.fingerprint2.domain.interactor
18 
19 import android.content.Context
20 import android.content.res.Configuration
21 import com.android.systemui.unfold.compat.ScreenSizeFoldProvider
22 import com.android.systemui.unfold.updates.FoldProvider
23 import kotlinx.coroutines.channels.awaitClose
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.callbackFlow
26 
27 interface FoldStateInteractor {
28   /** A flow that contains the fold state info */
29   val isFolded: Flow<Boolean>
30 
31   /**
32    * Indicates a configuration change has occurred, and the repo should update the [isFolded] flow.
33    */
34   fun onConfigurationChange(newConfig: Configuration)
35 }
36 
37 /** Interactor which handles fold state */
38 class FoldStateInteractorImpl(context: Context) : FoldStateInteractor {
39   private val screenSizeFoldProvider = ScreenSizeFoldProvider(context)
<lambda>null40   override val isFolded: Flow<Boolean> = callbackFlow {
41     val foldStateListener = FoldProvider.FoldCallback { isFolded -> trySend(isFolded) }
42     screenSizeFoldProvider.registerCallback(foldStateListener, context.mainExecutor)
43     awaitClose { screenSizeFoldProvider.unregisterCallback(foldStateListener) }
44   }
45 
46   /**
47    * This function is called by the root activity, indicating an orientation event has occurred.
48    * When this happens, the [ScreenSizeFoldProvider] is notified and it will re-compute if the
49    * device is folded or not, and notify the [FoldProvider.FoldCallback]
50    */
onConfigurationChangenull51   override fun onConfigurationChange(newConfig: Configuration) {
52     screenSizeFoldProvider.onConfigurationChange(newConfig)
53   }
54 }
55