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.biometrics
18 
19 import android.annotation.MainThread
20 import android.util.Log
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.shade.domain.interactor.ShadeInteractor
23 import dagger.Lazy
24 import javax.inject.Inject
25 import kotlinx.coroutines.CoroutineScope
26 import kotlinx.coroutines.Job
27 import kotlinx.coroutines.flow.first
28 import kotlinx.coroutines.launch
29 
30 class AuthDialogPanelInteractionDetector
31 @Inject
32 constructor(
33     @Application private val scope: CoroutineScope,
34     private val shadeInteractorLazy: Lazy<ShadeInteractor>,
35 ) {
36     private var shadeExpansionCollectorJob: Job? = null
37 
38     @MainThread
enablenull39     fun enable(onShadeInteraction: Runnable) {
40         if (shadeExpansionCollectorJob != null) {
41             Log.e(TAG, "Already enabled")
42             return
43         }
44         //TODO(b/313957306) delete this check
45         if (shadeInteractorLazy.get().isUserInteracting.value) {
46             // Workaround for b/311266890. This flow is in an error state that breaks this.
47             Log.e(TAG, "isUserInteracting already true, skipping enable")
48             return
49         }
50         shadeExpansionCollectorJob =
51             scope.launch {
52                 Log.i(TAG, "Enable detector")
53                 // wait for it to emit true once
54                 shadeInteractorLazy.get().isUserInteracting.first { it }
55                 Log.i(TAG, "Detector detected shade interaction")
56                 onShadeInteraction.run()
57             }
58         shadeExpansionCollectorJob?.invokeOnCompletion { shadeExpansionCollectorJob = null }
59     }
60 
61     @MainThread
disablenull62     fun disable() {
63         Log.i(TAG, "Disable detector")
64         shadeExpansionCollectorJob?.cancel()
65     }
66 }
67 
68 private const val TAG = "AuthDialogPanelInteractionDetector"
69