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.scene.data.repository
18 
19 import android.os.RemoteException
20 import com.android.internal.statusbar.IStatusBarService
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.UiBackground
23 import java.util.concurrent.Executor
24 import javax.inject.Inject
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.flow.StateFlow
27 import kotlinx.coroutines.flow.asStateFlow
28 
29 /** Source of truth for the visibility of various parts of the window root view. */
30 @SysUISingleton
31 class WindowRootViewVisibilityRepository
32 @Inject
33 constructor(
34     private val statusBarService: IStatusBarService,
35     @UiBackground private val uiBgExecutor: Executor,
36 ) {
37     private val _isLockscreenOrShadeVisible = MutableStateFlow(false)
38     val isLockscreenOrShadeVisible: StateFlow<Boolean> = _isLockscreenOrShadeVisible.asStateFlow()
39 
setIsLockscreenOrShadeVisiblenull40     fun setIsLockscreenOrShadeVisible(visible: Boolean) {
41         _isLockscreenOrShadeVisible.value = visible
42     }
43 
44     /**
45      * Called when the lockscreen or shade has been shown and can be interacted with so that SysUI
46      * can notify external services.
47      */
onLockscreenOrShadeInteractivenull48     fun onLockscreenOrShadeInteractive(
49         shouldClearNotificationEffects: Boolean,
50         notificationCount: Int,
51     ) {
52         executeServiceCallOnUiBg {
53             statusBarService.onPanelRevealed(shouldClearNotificationEffects, notificationCount)
54         }
55     }
56 
57     /**
58      * Called when the lockscreen or shade no longer can be interactecd with so that SysUI can
59      * notify external services.
60      */
onLockscreenOrShadeNotInteractivenull61     fun onLockscreenOrShadeNotInteractive() {
62         executeServiceCallOnUiBg { statusBarService.onPanelHidden() }
63     }
64 
executeServiceCallOnUiBgnull65     private fun executeServiceCallOnUiBg(runnable: () -> Unit) {
66         uiBgExecutor.execute {
67             try {
68                 runnable.invoke()
69             } catch (ex: RemoteException) {
70                 // Won't fail unless the world has ended
71             }
72         }
73     }
74 }
75