| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import android.content.Context; |
| 9 |
import android.view.GestureDetector; |
| 10 |
import android.view.MotionEvent; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class handles incoming touch events from the user and sends them to |
| 14 |
* listeners in Gecko and/or performs the "default action" (asynchronous pan/zoom |
| 15 |
* behaviour. EVERYTHING IN THIS CLASS MUST RUN ON THE UI THREAD. |
| 16 |
* |
| 17 |
* "Dispatching" an event refers to performing the default actions for the event, |
| 18 |
* which at our level of abstraction just means sending it off to the gesture |
| 19 |
* detectors and the pan/zoom controller. |
| 20 |
*/ |
| 21 |
public final class TouchEventHandler { |
| 22 |
private static final String LOGTAG = "GeckoTouchEventHandler"; |
| 23 |
|
| 24 |
private final GestureDetector mGestureDetector; |
| 25 |
private final SimpleScaleGestureDetector mScaleGestureDetector; |
| 26 |
private final JavaPanZoomController mPanZoomController; |
| 27 |
|
| 28 |
TouchEventHandler(Context context, JavaPanZoomController panZoomController) { |
| 29 |
mPanZoomController = panZoomController; |
| 30 |
mGestureDetector = new GestureDetector(context, mPanZoomController); |
| 31 |
mScaleGestureDetector = new SimpleScaleGestureDetector(mPanZoomController); |
| 32 |
|
| 33 |
mGestureDetector.setOnDoubleTapListener(mPanZoomController); |
| 34 |
} |
| 35 |
|
| 36 |
/* This function MUST be called on the UI thread */ |
| 37 |
public boolean handleEvent(MotionEvent event) { |
| 38 |
dispatchEvent(event); |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Dispatch the event to the gesture detectors and the pan/zoom controller. |
| 44 |
*/ |
| 45 |
private void dispatchEvent(MotionEvent event) { |
| 46 |
if (mGestureDetector.onTouchEvent(event)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
mScaleGestureDetector.onTouchEvent(event); |
| 50 |
if (mScaleGestureDetector.isInProgress()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
mPanZoomController.handleEvent(event); |
| 54 |
} |
| 55 |
} |