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.temporarydisplay.chipbar
18 
19 import android.content.Context
20 import android.view.MotionEvent
21 import android.view.View
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.settings.DisplayTracker
24 import com.android.systemui.statusbar.gesture.SwipeUpGestureHandler
25 import com.android.systemui.statusbar.gesture.SwipeUpGestureLogger
26 import com.android.systemui.util.boundsOnScreen
27 import javax.inject.Inject
28 
29 /**
30  * A class to detect when a user has swiped the chipbar away.
31  *
32  * Effectively [SysUISingleton]. But, this shouldn't be created if the gesture isn't enabled. See
33  * [TemporaryDisplayModule.provideSwipeChipbarAwayGestureHandler].
34  */
35 @SysUISingleton
36 class SwipeChipbarAwayGestureHandler
37 @Inject
38 constructor(
39     context: Context,
40     displayTracker: DisplayTracker,
41     logger: SwipeUpGestureLogger,
42 ) : SwipeUpGestureHandler(context, displayTracker, logger, loggerTag = LOGGER_TAG) {
43 
<lambda>null44     private var viewFetcher: () -> View? = { null }
45 
startOfGestureIsWithinBoundsnull46     override fun startOfGestureIsWithinBounds(ev: MotionEvent): Boolean {
47         val view = viewFetcher.invoke() ?: return false
48         // Since chipbar is in its own window, we need to use [boundsOnScreen] to get an accurate
49         // bottom. ([view.bottom] would be relative to its window, which would be too small.)
50         val viewBottom = view.boundsOnScreen.bottom
51         // Allow the gesture to start a bit below the chipbar
52         return ev.y <= 1.5 * viewBottom
53     }
54 
55     /**
56      * Sets a fetcher that returns the current chipbar view. The fetcher will be invoked whenever a
57      * gesture starts to determine if the gesture is near the chipbar.
58      */
setViewFetchernull59     fun setViewFetcher(fetcher: () -> View?) {
60         viewFetcher = fetcher
61     }
62 
63     /** Removes the current view fetcher. */
resetViewFetchernull64     fun resetViewFetcher() {
65         viewFetcher = { null }
66     }
67 }
68 
69 private const val LOGGER_TAG = "SwipeChipbarAway"
70