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.back.domain.interactor
18 
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.plugins.statusbar.StatusBarStateController
21 import com.android.systemui.shade.QuickSettingsController
22 import com.android.systemui.shade.ShadeController
23 import com.android.systemui.shade.ShadeViewController
24 import com.android.systemui.statusbar.StatusBarState
25 import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager
26 import javax.inject.Inject
27 
28 /** Handles requests to go back either from a button or gesture. */
29 @SysUISingleton
30 class BackActionInteractor
31 @Inject
32 constructor(
33     private val statusBarStateController: StatusBarStateController,
34     private val statusBarKeyguardViewManager: StatusBarKeyguardViewManager,
35     private val shadeController: ShadeController
36 ) {
37     private lateinit var shadeViewController: ShadeViewController
38     private lateinit var qsController: QuickSettingsController
39 
40     fun setup(qsController: QuickSettingsController, svController: ShadeViewController) {
41         this.qsController = qsController
42         this.shadeViewController = svController
43     }
44 
45     fun shouldBackBeHandled(): Boolean {
46         return statusBarStateController.state != StatusBarState.KEYGUARD &&
47             statusBarStateController.state != StatusBarState.SHADE_LOCKED &&
48             !statusBarKeyguardViewManager.isBouncerShowingOverDream
49     }
50 
51     fun onBackRequested(): Boolean {
52         if (statusBarKeyguardViewManager.canHandleBackPressed()) {
53             statusBarKeyguardViewManager.onBackPressed()
54             return true
55         }
56         if (qsController.isCustomizing) {
57             qsController.closeQsCustomizer()
58             return true
59         }
60         if (qsController.expanded) {
61             shadeViewController.animateCollapseQs(false)
62             return true
63         }
64         if (shadeViewController.closeUserSwitcherIfOpen()) {
65             return true
66         }
67         if (shouldBackBeHandled()) {
68             if (shadeViewController.canBeCollapsed()) {
69                 // this is the Shade dismiss animation, so make sure QQS closes when it ends.
70                 shadeViewController.onBackPressed()
71                 shadeController.animateCollapseShade()
72             }
73             return true
74         }
75         return false
76     }
77 }
78