1 /*
<lambda>null2  * 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 
17 package com.android.systemui.keyguard.data.repository
18 
19 import com.android.systemui.common.coroutine.ChannelExt.trySendWithFailureLogging
20 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.Main
23 import com.android.systemui.keyguard.shared.model.DevicePosture
24 import com.android.systemui.statusbar.policy.DevicePostureController
25 import javax.inject.Inject
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.channels.awaitClose
28 import kotlinx.coroutines.flow.Flow
29 import kotlinx.coroutines.flow.flowOn
30 
31 /** Provide current device posture state. */
32 interface DevicePostureRepository {
33     /** Provides the current device posture. */
34     val currentDevicePosture: Flow<DevicePosture>
35 }
36 
37 @SysUISingleton
38 class DevicePostureRepositoryImpl
39 @Inject
40 constructor(
41     private val postureController: DevicePostureController,
42     @Main private val mainDispatcher: CoroutineDispatcher
43 ) : DevicePostureRepository {
44     override val currentDevicePosture: Flow<DevicePosture>
45         get() =
<lambda>null46             conflatedCallbackFlow {
47                     val sendPostureUpdate = { posture: Int ->
48                         val currentDevicePosture = DevicePosture.toPosture(posture)
49                         trySendWithFailureLogging(
50                             currentDevicePosture,
51                             TAG,
52                             "Error sending posture update to $currentDevicePosture"
53                         )
54                     }
55                     val callback = DevicePostureController.Callback { sendPostureUpdate(it) }
56                     postureController.addCallback(callback)
57                     sendPostureUpdate(postureController.devicePosture)
58 
59                     awaitClose { postureController.removeCallback(callback) }
60                 }
61                 .flowOn(mainDispatcher) // DevicePostureController requirement
62 
63     companion object {
64         const val TAG = "PostureRepositoryImpl"
65     }
66 }
67