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
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.bouncer.domain.interactor
18 
19 import android.view.View
20 import com.android.systemui.bouncer.shared.constants.KeyguardBouncerConstants.EXPANSION_HIDDEN
21 import com.android.systemui.bouncer.shared.constants.KeyguardBouncerConstants.EXPANSION_VISIBLE
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.util.ListenerSet
24 import javax.inject.Inject
25 
26 /** Interactor to add and remove callbacks for the bouncer. */
27 @SysUISingleton
28 class PrimaryBouncerCallbackInteractor @Inject constructor() {
29     private var resetCallbacks = ListenerSet<KeyguardResetCallback>()
30     private var expansionCallbacks = ArrayList<PrimaryBouncerExpansionCallback>()
31 
32     /** Add a KeyguardResetCallback. */
addKeyguardResetCallbacknull33     fun addKeyguardResetCallback(callback: KeyguardResetCallback) {
34         resetCallbacks.addIfAbsent(callback)
35     }
36 
37     /** Remove a KeyguardResetCallback. */
removeKeyguardResetCallbacknull38     fun removeKeyguardResetCallback(callback: KeyguardResetCallback) {
39         resetCallbacks.remove(callback)
40     }
41 
42     /** Adds a callback to listen to bouncer expansion updates. */
addBouncerExpansionCallbacknull43     fun addBouncerExpansionCallback(callback: PrimaryBouncerExpansionCallback) {
44         if (!expansionCallbacks.contains(callback)) {
45             expansionCallbacks.add(callback)
46         }
47     }
48 
49     /**
50      * Removes a previously added callback. If the callback was never added, this method does
51      * nothing.
52      */
removeBouncerExpansionCallbacknull53     fun removeBouncerExpansionCallback(callback: PrimaryBouncerExpansionCallback) {
54         expansionCallbacks.remove(callback)
55     }
56 
57     /** Propagate fully shown to bouncer expansion callbacks. */
dispatchFullyShownnull58     fun dispatchFullyShown() {
59         for (callback in expansionCallbacks) {
60             callback.onFullyShown()
61         }
62     }
63 
64     /** Propagate starting to hide to bouncer expansion callbacks. */
dispatchStartingToHidenull65     fun dispatchStartingToHide() {
66         for (callback in expansionCallbacks) {
67             callback.onStartingToHide()
68         }
69     }
70 
71     /** Propagate starting to show to bouncer expansion callbacks. */
dispatchStartingToShownull72     fun dispatchStartingToShow() {
73         for (callback in expansionCallbacks) {
74             callback.onStartingToShow()
75         }
76     }
77 
78     /** Propagate fully hidden to bouncer expansion callbacks. */
dispatchFullyHiddennull79     fun dispatchFullyHidden() {
80         for (callback in expansionCallbacks) {
81             callback.onFullyHidden()
82         }
83     }
84 
85     /** Propagate expansion changes to bouncer expansion callbacks. */
dispatchExpansionChangednull86     fun dispatchExpansionChanged(expansion: Float) {
87         for (callback in expansionCallbacks) {
88             callback.onExpansionChanged(expansion)
89         }
90     }
91     /** Propagate visibility changes to bouncer expansion callbacks. */
dispatchVisibilityChangednull92     fun dispatchVisibilityChanged(visibility: Int) {
93         for (callback in expansionCallbacks) {
94             callback.onVisibilityChanged(visibility == View.VISIBLE)
95         }
96     }
97 
98     /** Propagate keyguard reset. */
dispatchResetnull99     fun dispatchReset() {
100         for (callback in resetCallbacks) {
101             callback.onKeyguardReset()
102         }
103     }
104 
105     /** Callback updated when the primary bouncer's show and hide states change. */
106     interface PrimaryBouncerExpansionCallback {
107         /**
108          * Invoked when the bouncer expansion reaches [EXPANSION_VISIBLE]. This is NOT called each
109          * time the bouncer is shown, but rather only when the fully shown amount has changed based
110          * on the panel expansion. The bouncer's visibility can still change when the expansion
111          * amount hasn't changed. See [PrimaryBouncerInteractor.isFullyShowing] for the checks for
112          * the bouncer showing state.
113          */
onFullyShownnull114         fun onFullyShown() {}
115 
116         /** Invoked when the bouncer is starting to transition to a hidden state. */
onStartingToHidenull117         fun onStartingToHide() {}
118 
119         /** Invoked when the bouncer is starting to transition to a visible state. */
onStartingToShownull120         fun onStartingToShow() {}
121 
122         /** Invoked when the bouncer expansion reaches [EXPANSION_HIDDEN]. */
onFullyHiddennull123         fun onFullyHidden() {}
124 
125         /**
126          * From 0f [EXPANSION_VISIBLE] when fully visible to 1f [EXPANSION_HIDDEN] when fully hidden
127          */
onExpansionChangednull128         fun onExpansionChanged(bouncerHideAmount: Float) {}
129 
130         /**
131          * Invoked when visibility of KeyguardBouncer has changed. Note the bouncer expansion can be
132          * [EXPANSION_VISIBLE], but the view's visibility can be [View.INVISIBLE].
133          */
onVisibilityChangednull134         fun onVisibilityChanged(isVisible: Boolean) {}
135     }
136 
137     interface KeyguardResetCallback {
onKeyguardResetnull138         fun onKeyguardReset()
139     }
140 }
141