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.statusbar.gesture 18 19 import android.content.Context 20 import android.view.GestureDetector 21 import android.view.InputEvent 22 import android.view.MotionEvent 23 import com.android.systemui.dagger.SysUISingleton 24 import com.android.systemui.settings.DisplayTracker 25 import javax.inject.Inject 26 27 /** 28 * A class to detect when a user taps the screen. To be notified when the tap is detected, add a 29 * callback via [addOnGestureDetectedCallback]. 30 */ 31 @SysUISingleton 32 class TapGestureDetector @Inject constructor( 33 private val context: Context, 34 displayTracker: DisplayTracker 35 ) : GenericGestureDetector( 36 TapGestureDetector::class.simpleName!!, 37 displayTracker.defaultDisplayId 38 ) { 39 40 private val gestureListener = object : GestureDetector.SimpleOnGestureListener() { onSingleTapUpnull41 override fun onSingleTapUp(e: MotionEvent): Boolean { 42 onGestureDetected(e) 43 return true 44 } 45 } 46 47 private var gestureDetector: GestureDetector? = null 48 onInputEventnull49 override fun onInputEvent(ev: InputEvent) { 50 if (ev !is MotionEvent) { 51 return 52 } 53 // Pass all events to [gestureDetector], which will then notify [gestureListener] when a tap 54 // is detected. 55 gestureDetector!!.onTouchEvent(ev) 56 } 57 58 /** Start listening for the tap gesture. */ startGestureListeningnull59 override fun startGestureListening() { 60 super.startGestureListening() 61 gestureDetector = GestureDetector(context, gestureListener) 62 } 63 64 /** Stop listening for the swipe gesture. */ stopGestureListeningnull65 override fun stopGestureListening() { 66 super.stopGestureListening() 67 gestureDetector = null 68 } 69 } 70