michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.mozilla.gecko.GeckoAppShell; michael@0: import org.mozilla.gecko.GeckoEvent; michael@0: import org.mozilla.gecko.EventDispatcher; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.graphics.PointF; michael@0: import android.os.Handler; michael@0: import android.util.Log; michael@0: michael@0: class SubdocumentScrollHelper implements GeckoEventListener { michael@0: private static final String LOGTAG = "GeckoSubdocScroll"; michael@0: michael@0: private static String MESSAGE_PANNING_OVERRIDE = "Panning:Override"; michael@0: private static String MESSAGE_CANCEL_OVERRIDE = "Panning:CancelOverride"; michael@0: private static String MESSAGE_SCROLL = "Gesture:Scroll"; michael@0: private static String MESSAGE_SCROLL_ACK = "Gesture:ScrollAck"; michael@0: michael@0: private final Handler mUiHandler; michael@0: private final EventDispatcher mEventDispatcher; michael@0: michael@0: /* This is the amount of displacement we have accepted but not yet sent to JS; this is michael@0: * only valid when mOverrideScrollPending is true. */ michael@0: private final PointF mPendingDisplacement; michael@0: michael@0: /* When this is true, we're sending scroll events to JS to scroll the active subdocument. */ michael@0: private boolean mOverridePanning; michael@0: michael@0: /* When this is true, we have received an ack for the last scroll event we sent to JS, and michael@0: * are ready to send the next scroll event. Note we only ever have one scroll event inflight michael@0: * at a time. */ michael@0: private boolean mOverrideScrollAck; michael@0: michael@0: /* When this is true, we have a pending scroll that we need to send to JS; we were unable michael@0: * to send it when it was initially requested because mOverrideScrollAck was not true. */ michael@0: private boolean mOverrideScrollPending; michael@0: michael@0: /* When this is true, the last scroll event we sent actually did some amount of scrolling on michael@0: * the subdocument; we use this to decide when we have reached the end of the subdocument. */ michael@0: private boolean mScrollSucceeded; michael@0: michael@0: SubdocumentScrollHelper(EventDispatcher eventDispatcher) { michael@0: // mUiHandler will be bound to the UI thread since that's where this constructor runs michael@0: mUiHandler = new Handler(); michael@0: mPendingDisplacement = new PointF(); michael@0: michael@0: mEventDispatcher = eventDispatcher; michael@0: registerEventListener(MESSAGE_PANNING_OVERRIDE); michael@0: registerEventListener(MESSAGE_CANCEL_OVERRIDE); michael@0: registerEventListener(MESSAGE_SCROLL_ACK); michael@0: } michael@0: michael@0: void destroy() { michael@0: unregisterEventListener(MESSAGE_PANNING_OVERRIDE); michael@0: unregisterEventListener(MESSAGE_CANCEL_OVERRIDE); michael@0: unregisterEventListener(MESSAGE_SCROLL_ACK); michael@0: } michael@0: michael@0: private void registerEventListener(String event) { michael@0: mEventDispatcher.registerEventListener(event, this); michael@0: } michael@0: michael@0: private void unregisterEventListener(String event) { michael@0: mEventDispatcher.unregisterEventListener(event, this); michael@0: } michael@0: michael@0: boolean scrollBy(PointF displacement) { michael@0: if (! mOverridePanning) { michael@0: return false; michael@0: } michael@0: michael@0: if (! mOverrideScrollAck) { michael@0: mOverrideScrollPending = true; michael@0: mPendingDisplacement.x += displacement.x; michael@0: mPendingDisplacement.y += displacement.y; michael@0: return true; michael@0: } michael@0: michael@0: JSONObject json = new JSONObject(); michael@0: try { michael@0: json.put("x", displacement.x); michael@0: json.put("y", displacement.y); michael@0: } catch (JSONException e) { michael@0: Log.e(LOGTAG, "Error forming subwindow scroll message: ", e); michael@0: } michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(MESSAGE_SCROLL, json.toString())); michael@0: michael@0: mOverrideScrollAck = false; michael@0: mOverrideScrollPending = false; michael@0: // clear the |mPendingDisplacement| after serializing |displacement| to michael@0: // JSON because they might be the same object michael@0: mPendingDisplacement.x = 0; michael@0: mPendingDisplacement.y = 0; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void cancel() { michael@0: mOverridePanning = false; michael@0: } michael@0: michael@0: boolean scrolling() { michael@0: return mOverridePanning; michael@0: } michael@0: michael@0: boolean lastScrollSucceeded() { michael@0: return mScrollSucceeded; michael@0: } michael@0: michael@0: // GeckoEventListener implementation michael@0: michael@0: @Override michael@0: public void handleMessage(final String event, final JSONObject message) { michael@0: // This comes in on the Gecko thread; hand off the handling to the UI thread. michael@0: mUiHandler.post(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: try { michael@0: if (MESSAGE_PANNING_OVERRIDE.equals(event)) { michael@0: mOverridePanning = true; michael@0: mOverrideScrollAck = true; michael@0: mOverrideScrollPending = false; michael@0: mScrollSucceeded = true; michael@0: } else if (MESSAGE_CANCEL_OVERRIDE.equals(event)) { michael@0: mOverridePanning = false; michael@0: } else if (MESSAGE_SCROLL_ACK.equals(event)) { michael@0: mOverrideScrollAck = true; michael@0: mScrollSucceeded = message.getBoolean("scrolled"); michael@0: if (mOverridePanning && mOverrideScrollPending) { michael@0: scrollBy(mPendingDisplacement); michael@0: } michael@0: } michael@0: } catch (Exception e) { michael@0: Log.e(LOGTAG, "Exception handling message", e); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: }