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.view.accessibility.AccessibilityManager
20 import kotlinx.coroutines.CoroutineScope
21 import kotlinx.coroutines.channels.awaitClose
22 import kotlinx.coroutines.flow.Flow
23 import kotlinx.coroutines.flow.SharingStarted
24 import kotlinx.coroutines.flow.callbackFlow
25 import kotlinx.coroutines.flow.stateIn
26 
27 /** Represents all of the information on accessibility state. */
28 interface AccessibilityInteractor {
29   /** A flow that contains whether or not accessibility is enabled */
30   val isAccessibilityEnabled: Flow<Boolean>
31 }
32 
33 class AccessibilityInteractorImpl(
34   accessibilityManager: AccessibilityManager,
35   applicationScope: CoroutineScope,
36 ) : AccessibilityInteractor {
37   /** A flow that contains whether or not accessibility is enabled */
38   override val isAccessibilityEnabled: Flow<Boolean> =
<lambda>null39     callbackFlow {
40         val listener =
41           AccessibilityManager.AccessibilityStateChangeListener { enabled -> trySend(enabled) }
42         accessibilityManager.addAccessibilityStateChangeListener(listener)
43 
44         // This clause will be called when no one is listening to the flow
45         awaitClose { accessibilityManager.removeAccessibilityStateChangeListener(listener) }
46       }
47       .stateIn(
48         applicationScope, // This is going to tied to the activity scope
49         SharingStarted.WhileSubscribed(), // When no longer subscribed, we removeTheListener
50         false,
51       )
52 }
53