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.shade
18 
19 import com.android.systemui.assist.AssistManager
20 import com.android.systemui.statusbar.CommandQueue
21 import com.android.systemui.statusbar.NotificationPresenter
22 import com.android.systemui.statusbar.NotificationShadeWindowController
23 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager
24 import dagger.Lazy
25 import kotlinx.coroutines.ExperimentalCoroutinesApi
26 
27 /** A base class for non-empty implementations of ShadeController. */
28 @OptIn(ExperimentalCoroutinesApi::class)
29 abstract class BaseShadeControllerImpl(
30     protected val commandQueue: CommandQueue,
31     protected val statusBarKeyguardViewManager: StatusBarKeyguardViewManager,
32     protected val notificationShadeWindowController: NotificationShadeWindowController,
33     protected val assistManagerLazy: Lazy<AssistManager>
34 ) : ShadeController {
35     protected lateinit var notifPresenter: NotificationPresenter
36     /** Runnables to run after completing a collapse of the shade. */
37     private val postCollapseActions = ArrayList<Runnable>()
38 
startnull39     override fun start() {
40         // Do nothing by default
41     }
42 
animateExpandShadenull43     final override fun animateExpandShade() {
44         if (isShadeEnabled) {
45             expandToNotifications()
46         }
47     }
48 
49     /** Expand the shade with notifications visible. */
expandToNotificationsnull50     protected abstract fun expandToNotifications()
51 
52     final override fun animateExpandQs() {
53         if (isShadeEnabled) {
54             expandToQs()
55         }
56     }
57 
58     /** Expand the shade showing only quick settings. */
expandToQsnull59     protected abstract fun expandToQs()
60 
61     final override fun addPostCollapseAction(action: Runnable) {
62         postCollapseActions.add(action)
63     }
64 
runPostCollapseActionsnull65     protected fun runPostCollapseActions() {
66         val clonedList: ArrayList<Runnable> = ArrayList(postCollapseActions)
67         postCollapseActions.clear()
68         for (r in clonedList) {
69             r.run()
70         }
71         statusBarKeyguardViewManager.readyForKeyguardDone()
72     }
73 
onLaunchAnimationEndnull74     final override fun onLaunchAnimationEnd(launchIsFullScreen: Boolean) {
75         if (!this.notifPresenter.isCollapsing()) {
76             onClosingFinished()
77         }
78         if (launchIsFullScreen) {
79             instantCollapseShade()
80         }
81     }
onLaunchAnimationCancellednull82     final override fun onLaunchAnimationCancelled(isLaunchForActivity: Boolean) {
83         if (
84             notifPresenter.isPresenterFullyCollapsed() &&
85                 !notifPresenter.isCollapsing() &&
86                 isLaunchForActivity
87         ) {
88             onClosingFinished()
89         } else {
90             collapseShade(true /* animate */)
91         }
92     }
93 
onClosingFinishednull94     protected fun onClosingFinished() {
95         runPostCollapseActions()
96         if (!this.notifPresenter.isPresenterFullyCollapsed()) {
97             // if we set it not to be focusable when collapsing, we have to undo it when we aborted
98             // the closing
99             notificationShadeWindowController.setNotificationShadeFocusable(true)
100         }
101     }
102 
setNotificationPresenternull103     override fun setNotificationPresenter(presenter: NotificationPresenter) {
104         notifPresenter = presenter
105     }
106 }
107