• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package com.android.systemui.unfold.util
16 
17 import java.lang.ref.WeakReference
18 import javax.inject.Inject
19 import javax.inject.Singleton
20 
21 interface UnfoldKeyguardVisibilityProvider {
22     /**
23      * True when the keyguard is visible.
24      *
25      * Might be [null] when it is not known.
26      */
27     val isKeyguardVisible: Boolean?
28 }
29 
30 /** Used to notify keyguard visibility. */
31 interface UnfoldKeyguardVisibilityManager {
32     /** Sets the delegate. [delegate] should return true when the keyguard is visible. */
setKeyguardVisibleDelegatenull33     fun setKeyguardVisibleDelegate(delegate: () -> Boolean)
34 }
35 
36 /**
37  * Keeps a [WeakReference] for the keyguard visibility provider.
38  *
39  * It is a weak reference because this is in the global scope, while the delegate might be set from
40  * another subcomponent (that might have shorter lifespan).
41  */
42 @Singleton
43 class UnfoldKeyguardVisibilityManagerImpl @Inject constructor() :
44     UnfoldKeyguardVisibilityProvider, UnfoldKeyguardVisibilityManager {
45 
46     private var delegatedProvider: WeakReference<() -> Boolean?>? = null
47 
48     override fun setKeyguardVisibleDelegate(delegate: () -> Boolean) {
49         delegatedProvider = WeakReference(delegate)
50     }
51 
52     override val isKeyguardVisible: Boolean?
53         get() = delegatedProvider?.get()?.invoke()
54 }
55