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 
17 package com.android.systemui.statusbar.data.repository
18 
19 import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.statusbar.NotificationRemoteInputManager
22 import com.android.systemui.statusbar.RemoteInputController
23 import dagger.Binds
24 import dagger.Module
25 import javax.inject.Inject
26 import kotlinx.coroutines.channels.awaitClose
27 import kotlinx.coroutines.flow.Flow
28 
29 /**
30  * Repository used for tracking the state of notification remote input (e.g. when the user presses
31  * "reply" on a notification and the keyboard opens).
32  */
33 interface RemoteInputRepository {
34     /** Whether remote input is currently active for any notification. */
35     val isRemoteInputActive: Flow<Boolean>
36 }
37 
38 @SysUISingleton
39 class RemoteInputRepositoryImpl
40 @Inject
41 constructor(
42     private val notificationRemoteInputManager: NotificationRemoteInputManager,
43 ) : RemoteInputRepository {
<lambda>null44     override val isRemoteInputActive: Flow<Boolean> = conflatedCallbackFlow {
45         trySend(false) // initial value is false
46         val callback =
47             object : RemoteInputController.Callback {
48                 override fun onRemoteInputActive(active: Boolean) {
49                     trySend(active)
50                 }
51             }
52         notificationRemoteInputManager.addControllerCallback(callback)
53         awaitClose { notificationRemoteInputManager.removeControllerCallback(callback) }
54     }
55 }
56 
57 @Module
58 interface RemoteInputRepositoryModule {
bindImplnull59     @Binds fun bindImpl(impl: RemoteInputRepositoryImpl): RemoteInputRepository
60 }
61