2012-04-07 00:09:26 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.mozilla.gecko.Tab;
|
|
|
|
import org.mozilla.gecko.Tabs;
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.MotionEvent;
|
2013-02-08 06:13:09 -08:00
|
|
|
import android.view.View;
|
2012-07-20 10:06:01 -07:00
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.Queue;
|
2012-04-07 00:09:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class handles incoming touch events from the user and sends them to
|
|
|
|
* listeners in Gecko and/or performs the "default action" (asynchronous pan/zoom
|
|
|
|
* behaviour. EVERYTHING IN THIS CLASS MUST RUN ON THE UI THREAD.
|
|
|
|
*
|
|
|
|
* In the following code/comments, a "block" of events refers to a contiguous
|
|
|
|
* sequence of events that starts with a DOWN or POINTER_DOWN and goes up to
|
|
|
|
* but not including the next DOWN or POINTER_DOWN event.
|
|
|
|
*
|
|
|
|
* "Dispatching" an event refers to performing the default actions for the event,
|
|
|
|
* which at our level of abstraction just means sending it off to the gesture
|
|
|
|
* detectors and the pan/zoom controller.
|
|
|
|
*
|
|
|
|
* If an event is "default-prevented" that means one or more listeners in Gecko
|
|
|
|
* has called preventDefault() on the event, which means that the default action
|
|
|
|
* for that event should not occur. Usually we care about a "block" of events being
|
|
|
|
* default-prevented, which means that the DOWN/POINTER_DOWN event that started
|
|
|
|
* the block, or the first MOVE event following that, were prevent-defaulted.
|
|
|
|
*
|
|
|
|
* A "default-prevented notification" is when we here in Java-land receive a notification
|
|
|
|
* from gecko as to whether or not a block of events was default-prevented. This happens
|
|
|
|
* at some point after the first or second event in the block is processed in Gecko.
|
|
|
|
* This code assumes we get EXACTLY ONE default-prevented notification for each block
|
|
|
|
* of events.
|
2012-05-15 10:22:26 -07:00
|
|
|
*
|
|
|
|
* Note that even if all events are default-prevented, we still send specific types
|
|
|
|
* of notifications to the pan/zoom controller. The notifications are needed
|
|
|
|
* to respond to user actions a timely manner regardless of default-prevention,
|
|
|
|
* and fix issues like bug 749384.
|
2012-04-07 00:09:26 -07:00
|
|
|
*/
|
2013-02-08 06:13:09 -08:00
|
|
|
final class TouchEventHandler implements Tabs.OnTabsChangedListener {
|
2012-04-07 00:09:26 -07:00
|
|
|
private static final String LOGTAG = "GeckoTouchEventHandler";
|
|
|
|
|
|
|
|
// The time limit for listeners to respond with preventDefault on touchevents
|
|
|
|
// before we begin panning the page
|
2012-05-15 10:21:14 -07:00
|
|
|
private final int EVENT_LISTENER_TIMEOUT = 200;
|
2012-04-07 00:09:26 -07:00
|
|
|
|
2013-02-08 06:13:09 -08:00
|
|
|
private final View mView;
|
2012-04-07 00:09:26 -07:00
|
|
|
private final GestureDetector mGestureDetector;
|
|
|
|
private final SimpleScaleGestureDetector mScaleGestureDetector;
|
2013-02-08 06:12:59 -08:00
|
|
|
private final JavaPanZoomController mPanZoomController;
|
2012-04-07 00:09:26 -07:00
|
|
|
|
|
|
|
// the queue of events that we are holding on to while waiting for a preventDefault
|
|
|
|
// notification
|
|
|
|
private final Queue<MotionEvent> mEventQueue;
|
|
|
|
private final ListenerTimeoutProcessor mListenerTimeoutProcessor;
|
|
|
|
|
|
|
|
// whether or not we should wait for touch listeners to respond (this state is
|
|
|
|
// per-tab and is updated when we switch tabs).
|
|
|
|
private boolean mWaitForTouchListeners;
|
|
|
|
|
|
|
|
// true if we should hold incoming events in our queue. this is re-set for every
|
|
|
|
// block of events, this is cleared once we find out if the block has been
|
|
|
|
// default-prevented or not (or we time out waiting for that).
|
|
|
|
private boolean mHoldInQueue;
|
|
|
|
|
2014-02-28 13:26:08 -08:00
|
|
|
// true if we should dispatch incoming events to the gesture detector and the pan/zoom
|
|
|
|
// controller. if this is false, then the current block of events has been
|
|
|
|
// default-prevented, and we should not dispatch these events (although we'll still send
|
|
|
|
// them to gecko listeners).
|
|
|
|
private boolean mDispatchEvents;
|
2012-04-07 00:09:26 -07:00
|
|
|
|
|
|
|
// this next variable requires some explanation. strap yourself in.
|
|
|
|
//
|
|
|
|
// for each block of events, we do two things: (1) send the events to gecko and expect
|
|
|
|
// exactly one default-prevented notification in return, and (2) kick off a delayed
|
|
|
|
// ListenerTimeoutProcessor that triggers in case we don't hear from the listener in
|
|
|
|
// a timely fashion.
|
|
|
|
// since events are constantly coming in, we need to be able to handle more than one
|
|
|
|
// block of events in the queue.
|
|
|
|
//
|
|
|
|
// this means that there are ordering restrictions on these that we can take advantage of,
|
|
|
|
// and need to abide by. blocks of events in the queue will always be in the order that
|
|
|
|
// the user generated them. default-prevented notifications we get from gecko will be in
|
|
|
|
// the same order as the blocks of events in the queue. the ListenerTimeoutProcessors that
|
|
|
|
// have been posted will also fire in the same order as the blocks of events in the queue.
|
|
|
|
// HOWEVER, we may get multiple default-prevented notifications interleaved with multiple
|
|
|
|
// ListenerTimeoutProcessor firings, and that interleaving is not predictable.
|
|
|
|
//
|
|
|
|
// therefore, we need to make sure that for each block of events, we process the queued
|
|
|
|
// events exactly once, either when we get the default-prevented notification, or when the
|
|
|
|
// timeout expires (whichever happens first). there is no way to associate the
|
|
|
|
// default-prevented notification with a particular block of events other than via ordering,
|
|
|
|
//
|
|
|
|
// so what we do to accomplish this is to track a "processing balance", which is the number
|
|
|
|
// of default-prevented notifications that we have received, minus the number of ListenerTimeoutProcessors
|
|
|
|
// that have fired. (think "balance" as in teeter-totter balance). this value is:
|
|
|
|
// - zero when we are in a state where the next default-prevented notification we expect
|
|
|
|
// to receive and the next ListenerTimeoutProcessor we expect to fire both correspond to
|
|
|
|
// the next block of events in the queue.
|
|
|
|
// - positive when we are in a state where we have received more default-prevented notifications
|
|
|
|
// than ListenerTimeoutProcessors. This means that the next default-prevented notification
|
|
|
|
// does correspond to the block at the head of the queue, but the next n ListenerTimeoutProcessors
|
|
|
|
// need to be ignored as they are for blocks we have already processed. (n is the absolute value
|
|
|
|
// of the balance.)
|
|
|
|
// - negative when we are in a state where we have received more ListenerTimeoutProcessors than
|
|
|
|
// default-prevented notifications. This means that the next ListenerTimeoutProcessor that
|
|
|
|
// we receive does correspond to the block at the head of the queue, but the next n
|
|
|
|
// default-prevented notifications need to be ignored as they are for blocks we have already
|
|
|
|
// processed. (n is the absolute value of the balance.)
|
|
|
|
private int mProcessingBalance;
|
|
|
|
|
2013-02-08 06:13:09 -08:00
|
|
|
TouchEventHandler(Context context, View view, JavaPanZoomController panZoomController) {
|
2012-04-07 00:09:26 -07:00
|
|
|
mView = view;
|
|
|
|
|
|
|
|
mEventQueue = new LinkedList<MotionEvent>();
|
2013-02-08 06:13:09 -08:00
|
|
|
mPanZoomController = panZoomController;
|
2012-08-07 07:39:04 -07:00
|
|
|
mGestureDetector = new GestureDetector(context, mPanZoomController);
|
|
|
|
mScaleGestureDetector = new SimpleScaleGestureDetector(mPanZoomController);
|
2012-04-07 00:09:26 -07:00
|
|
|
mListenerTimeoutProcessor = new ListenerTimeoutProcessor();
|
2014-02-28 13:26:08 -08:00
|
|
|
mDispatchEvents = true;
|
2012-04-07 00:09:26 -07:00
|
|
|
|
2012-08-07 07:39:04 -07:00
|
|
|
mGestureDetector.setOnDoubleTapListener(mPanZoomController);
|
2012-05-18 08:24:27 -07:00
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
Tabs.registerOnTabsChangedListener(this);
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:13:09 -08:00
|
|
|
public void destroy() {
|
2013-01-25 10:51:41 -08:00
|
|
|
Tabs.unregisterOnTabsChangedListener(this);
|
|
|
|
}
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
/* This function MUST be called on the UI thread */
|
|
|
|
public boolean handleEvent(MotionEvent event) {
|
|
|
|
if (isDownEvent(event)) {
|
|
|
|
// this is the start of a new block of events! whee!
|
|
|
|
mHoldInQueue = mWaitForTouchListeners;
|
2012-04-20 08:40:41 -07:00
|
|
|
|
2014-02-28 13:26:08 -08:00
|
|
|
// Set mDispatchEvents to true so that we are guaranteed to either queue these
|
|
|
|
// events or dispatch them. The only time we should not do either is once we've
|
|
|
|
// heard back from content to preventDefault this block.
|
|
|
|
mDispatchEvents = true;
|
2012-04-07 00:09:26 -07:00
|
|
|
if (mHoldInQueue) {
|
2012-05-15 10:22:26 -07:00
|
|
|
// if the new block we are starting is the current block (i.e. there are no
|
|
|
|
// other blocks waiting in the queue, then we should let the pan/zoom controller
|
|
|
|
// know we are waiting for the touch listeners to run
|
|
|
|
if (mEventQueue.isEmpty()) {
|
2012-07-10 09:46:49 -07:00
|
|
|
mPanZoomController.startingNewEventBlock(event, true);
|
2012-05-15 10:22:26 -07:00
|
|
|
}
|
2012-04-17 09:29:18 -07:00
|
|
|
} else {
|
2012-06-06 10:50:01 -07:00
|
|
|
// we're not going to be holding this block of events in the queue, but we need
|
|
|
|
// a marker of some sort so that the processEventBlock loop deals with the blocks
|
|
|
|
// in the right order as notifications come in. we use a single null event in
|
|
|
|
// the queue as a placeholder for a block of events that has already been dispatched.
|
|
|
|
mEventQueue.add(null);
|
2012-07-10 09:46:49 -07:00
|
|
|
mPanZoomController.startingNewEventBlock(event, false);
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
2012-06-06 10:50:01 -07:00
|
|
|
|
|
|
|
// set the timeout so that we dispatch these events and update mProcessingBalance
|
|
|
|
// if we don't get a default-prevented notification
|
|
|
|
mView.postDelayed(mListenerTimeoutProcessor, EVENT_LISTENER_TIMEOUT);
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
|
|
|
|
2014-02-28 13:26:08 -08:00
|
|
|
// if we need to hold the events, add it to the queue. if we need to dispatch
|
|
|
|
// it directly, do that. it is possible that both mHoldInQueue and mDispatchEvents
|
|
|
|
// are false, in which case we are processing a block of events that we know
|
|
|
|
// has been default-prevented. in that case we don't keep the events as we don't
|
|
|
|
// need them (but we still pass them to the gecko listener).
|
2012-04-07 00:09:26 -07:00
|
|
|
if (mHoldInQueue) {
|
|
|
|
mEventQueue.add(MotionEvent.obtain(event));
|
2014-02-28 13:26:08 -08:00
|
|
|
} else if (mDispatchEvents) {
|
|
|
|
dispatchEvent(event);
|
|
|
|
} else if (touchFinished(event)) {
|
|
|
|
mPanZoomController.preventedTouchFinished();
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:13:09 -08:00
|
|
|
return false;
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is how gecko sends us a default-prevented notification. It is called
|
|
|
|
* once gecko knows definitively whether the block of events has had preventDefault
|
|
|
|
* called on it (either on the initial down event that starts the block, or on
|
|
|
|
* the first event following that down event).
|
|
|
|
*
|
|
|
|
* This function MUST be called on the UI thread.
|
|
|
|
*/
|
|
|
|
public void handleEventListenerAction(boolean allowDefaultAction) {
|
|
|
|
if (mProcessingBalance > 0) {
|
|
|
|
// this event listener that triggered this took too long, and the corresponding
|
|
|
|
// ListenerTimeoutProcessor runnable already ran for the event in question. the
|
|
|
|
// block of events this is for has already been processed, so we don't need to
|
|
|
|
// do anything here.
|
|
|
|
} else {
|
|
|
|
processEventBlock(allowDefaultAction);
|
|
|
|
}
|
|
|
|
mProcessingBalance--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function MUST be called on the UI thread. */
|
|
|
|
public void setWaitForTouchListeners(boolean aValue) {
|
|
|
|
mWaitForTouchListeners = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isDownEvent(MotionEvent event) {
|
|
|
|
int action = (event.getAction() & MotionEvent.ACTION_MASK);
|
|
|
|
return (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN);
|
|
|
|
}
|
|
|
|
|
2012-05-15 10:22:26 -07:00
|
|
|
private boolean touchFinished(MotionEvent event) {
|
|
|
|
int action = (event.getAction() & MotionEvent.ACTION_MASK);
|
|
|
|
return (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL);
|
|
|
|
}
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
/**
|
|
|
|
* Dispatch the event to the gesture detectors and the pan/zoom controller.
|
|
|
|
*/
|
2014-02-28 13:26:08 -08:00
|
|
|
private void dispatchEvent(MotionEvent event) {
|
2012-04-07 00:09:26 -07:00
|
|
|
if (mGestureDetector.onTouchEvent(event)) {
|
2012-06-19 12:01:44 -07:00
|
|
|
return;
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
|
|
|
mScaleGestureDetector.onTouchEvent(event);
|
|
|
|
if (mScaleGestureDetector.isInProgress()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 13:26:08 -08:00
|
|
|
mPanZoomController.handleEvent(event);
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the block of events at the head of the queue now that we know
|
|
|
|
* whether it has been default-prevented or not.
|
|
|
|
*/
|
|
|
|
private void processEventBlock(boolean allowDefaultAction) {
|
2014-02-28 13:26:08 -08:00
|
|
|
if (!allowDefaultAction) {
|
|
|
|
// if the block has been default-prevented, cancel whatever stuff we had in
|
|
|
|
// progress in the gesture detector and pan zoom controller
|
|
|
|
long now = SystemClock.uptimeMillis();
|
|
|
|
dispatchEvent(MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0));
|
|
|
|
}
|
|
|
|
|
2012-06-06 10:50:01 -07:00
|
|
|
if (mEventQueue.isEmpty()) {
|
|
|
|
Log.e(LOGTAG, "Unexpected empty event queue in processEventBlock!", new Exception());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-07 00:09:26 -07:00
|
|
|
// the odd loop condition is because the first event in the queue will
|
|
|
|
// always be a DOWN or POINTER_DOWN event, and we want to process all
|
|
|
|
// the events in the queue starting at that one, up to but not including
|
|
|
|
// the next DOWN or POINTER_DOWN event.
|
|
|
|
|
|
|
|
MotionEvent event = mEventQueue.poll();
|
|
|
|
while (true) {
|
2012-06-06 10:50:01 -07:00
|
|
|
// event being null here is valid and represents a block of events
|
|
|
|
// that has already been dispatched.
|
|
|
|
|
|
|
|
if (event != null) {
|
2014-02-28 13:26:08 -08:00
|
|
|
// for each event we process, only dispatch it if the block hasn't been
|
|
|
|
// default-prevented.
|
|
|
|
if (allowDefaultAction) {
|
|
|
|
dispatchEvent(event);
|
|
|
|
} else if (touchFinished(event)) {
|
|
|
|
mPanZoomController.preventedTouchFinished();
|
|
|
|
}
|
2012-04-07 00:09:26 -07:00
|
|
|
}
|
2012-06-06 10:50:01 -07:00
|
|
|
if (mEventQueue.isEmpty()) {
|
2012-04-07 00:09:26 -07:00
|
|
|
// we have processed the backlog of events, and are all caught up.
|
|
|
|
// now we can set clear the hold flag and set the dispatch flag so
|
|
|
|
// that the handleEvent() function can do the right thing for all
|
|
|
|
// remaining events in this block (which is still ongoing) without
|
|
|
|
// having to put them in the queue.
|
|
|
|
mHoldInQueue = false;
|
2014-02-28 13:26:08 -08:00
|
|
|
mDispatchEvents = allowDefaultAction;
|
2012-04-07 00:09:26 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 10:50:01 -07:00
|
|
|
event = mEventQueue.peek();
|
|
|
|
if (event == null || isDownEvent(event)) {
|
2012-04-07 00:09:26 -07:00
|
|
|
// we have finished processing the block we were interested in.
|
|
|
|
// now we wait for the next call to processEventBlock
|
2012-06-06 10:50:01 -07:00
|
|
|
if (event != null) {
|
2012-07-10 09:46:49 -07:00
|
|
|
mPanZoomController.startingNewEventBlock(event, true);
|
2012-06-06 10:50:01 -07:00
|
|
|
}
|
2012-04-07 00:09:26 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// pop the event we peeked above, as it is still part of the block and
|
|
|
|
// we want to keep processing
|
|
|
|
mEventQueue.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
private class ListenerTimeoutProcessor implements Runnable {
|
2012-04-07 00:09:26 -07:00
|
|
|
/* This MUST be run on the UI thread */
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-04-07 00:09:26 -07:00
|
|
|
public void run() {
|
|
|
|
if (mProcessingBalance < 0) {
|
|
|
|
// gecko already responded with default-prevented notification, and so
|
|
|
|
// the block of events this ListenerTimeoutProcessor corresponds to have
|
|
|
|
// already been removed from the queue.
|
|
|
|
} else {
|
|
|
|
processEventBlock(true);
|
|
|
|
}
|
|
|
|
mProcessingBalance++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tabs.OnTabsChangedListener implementation
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-06-11 15:18:40 -07:00
|
|
|
public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
|
2012-04-07 00:09:26 -07:00
|
|
|
if ((Tabs.getInstance().isSelectedTab(tab) && msg == Tabs.TabEvents.STOP) || msg == Tabs.TabEvents.SELECTED) {
|
|
|
|
mWaitForTouchListeners = tab.getHasTouchListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|