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