mobile/android/base/gfx/SubdocumentScrollHelper.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.gfx;
michael@0 7
michael@0 8 import org.mozilla.gecko.GeckoAppShell;
michael@0 9 import org.mozilla.gecko.GeckoEvent;
michael@0 10 import org.mozilla.gecko.EventDispatcher;
michael@0 11 import org.mozilla.gecko.util.GeckoEventListener;
michael@0 12
michael@0 13 import org.json.JSONException;
michael@0 14 import org.json.JSONObject;
michael@0 15
michael@0 16 import android.graphics.PointF;
michael@0 17 import android.os.Handler;
michael@0 18 import android.util.Log;
michael@0 19
michael@0 20 class SubdocumentScrollHelper implements GeckoEventListener {
michael@0 21 private static final String LOGTAG = "GeckoSubdocScroll";
michael@0 22
michael@0 23 private static String MESSAGE_PANNING_OVERRIDE = "Panning:Override";
michael@0 24 private static String MESSAGE_CANCEL_OVERRIDE = "Panning:CancelOverride";
michael@0 25 private static String MESSAGE_SCROLL = "Gesture:Scroll";
michael@0 26 private static String MESSAGE_SCROLL_ACK = "Gesture:ScrollAck";
michael@0 27
michael@0 28 private final Handler mUiHandler;
michael@0 29 private final EventDispatcher mEventDispatcher;
michael@0 30
michael@0 31 /* This is the amount of displacement we have accepted but not yet sent to JS; this is
michael@0 32 * only valid when mOverrideScrollPending is true. */
michael@0 33 private final PointF mPendingDisplacement;
michael@0 34
michael@0 35 /* When this is true, we're sending scroll events to JS to scroll the active subdocument. */
michael@0 36 private boolean mOverridePanning;
michael@0 37
michael@0 38 /* When this is true, we have received an ack for the last scroll event we sent to JS, and
michael@0 39 * are ready to send the next scroll event. Note we only ever have one scroll event inflight
michael@0 40 * at a time. */
michael@0 41 private boolean mOverrideScrollAck;
michael@0 42
michael@0 43 /* When this is true, we have a pending scroll that we need to send to JS; we were unable
michael@0 44 * to send it when it was initially requested because mOverrideScrollAck was not true. */
michael@0 45 private boolean mOverrideScrollPending;
michael@0 46
michael@0 47 /* When this is true, the last scroll event we sent actually did some amount of scrolling on
michael@0 48 * the subdocument; we use this to decide when we have reached the end of the subdocument. */
michael@0 49 private boolean mScrollSucceeded;
michael@0 50
michael@0 51 SubdocumentScrollHelper(EventDispatcher eventDispatcher) {
michael@0 52 // mUiHandler will be bound to the UI thread since that's where this constructor runs
michael@0 53 mUiHandler = new Handler();
michael@0 54 mPendingDisplacement = new PointF();
michael@0 55
michael@0 56 mEventDispatcher = eventDispatcher;
michael@0 57 registerEventListener(MESSAGE_PANNING_OVERRIDE);
michael@0 58 registerEventListener(MESSAGE_CANCEL_OVERRIDE);
michael@0 59 registerEventListener(MESSAGE_SCROLL_ACK);
michael@0 60 }
michael@0 61
michael@0 62 void destroy() {
michael@0 63 unregisterEventListener(MESSAGE_PANNING_OVERRIDE);
michael@0 64 unregisterEventListener(MESSAGE_CANCEL_OVERRIDE);
michael@0 65 unregisterEventListener(MESSAGE_SCROLL_ACK);
michael@0 66 }
michael@0 67
michael@0 68 private void registerEventListener(String event) {
michael@0 69 mEventDispatcher.registerEventListener(event, this);
michael@0 70 }
michael@0 71
michael@0 72 private void unregisterEventListener(String event) {
michael@0 73 mEventDispatcher.unregisterEventListener(event, this);
michael@0 74 }
michael@0 75
michael@0 76 boolean scrollBy(PointF displacement) {
michael@0 77 if (! mOverridePanning) {
michael@0 78 return false;
michael@0 79 }
michael@0 80
michael@0 81 if (! mOverrideScrollAck) {
michael@0 82 mOverrideScrollPending = true;
michael@0 83 mPendingDisplacement.x += displacement.x;
michael@0 84 mPendingDisplacement.y += displacement.y;
michael@0 85 return true;
michael@0 86 }
michael@0 87
michael@0 88 JSONObject json = new JSONObject();
michael@0 89 try {
michael@0 90 json.put("x", displacement.x);
michael@0 91 json.put("y", displacement.y);
michael@0 92 } catch (JSONException e) {
michael@0 93 Log.e(LOGTAG, "Error forming subwindow scroll message: ", e);
michael@0 94 }
michael@0 95 GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(MESSAGE_SCROLL, json.toString()));
michael@0 96
michael@0 97 mOverrideScrollAck = false;
michael@0 98 mOverrideScrollPending = false;
michael@0 99 // clear the |mPendingDisplacement| after serializing |displacement| to
michael@0 100 // JSON because they might be the same object
michael@0 101 mPendingDisplacement.x = 0;
michael@0 102 mPendingDisplacement.y = 0;
michael@0 103
michael@0 104 return true;
michael@0 105 }
michael@0 106
michael@0 107 void cancel() {
michael@0 108 mOverridePanning = false;
michael@0 109 }
michael@0 110
michael@0 111 boolean scrolling() {
michael@0 112 return mOverridePanning;
michael@0 113 }
michael@0 114
michael@0 115 boolean lastScrollSucceeded() {
michael@0 116 return mScrollSucceeded;
michael@0 117 }
michael@0 118
michael@0 119 // GeckoEventListener implementation
michael@0 120
michael@0 121 @Override
michael@0 122 public void handleMessage(final String event, final JSONObject message) {
michael@0 123 // This comes in on the Gecko thread; hand off the handling to the UI thread.
michael@0 124 mUiHandler.post(new Runnable() {
michael@0 125 @Override
michael@0 126 public void run() {
michael@0 127 try {
michael@0 128 if (MESSAGE_PANNING_OVERRIDE.equals(event)) {
michael@0 129 mOverridePanning = true;
michael@0 130 mOverrideScrollAck = true;
michael@0 131 mOverrideScrollPending = false;
michael@0 132 mScrollSucceeded = true;
michael@0 133 } else if (MESSAGE_CANCEL_OVERRIDE.equals(event)) {
michael@0 134 mOverridePanning = false;
michael@0 135 } else if (MESSAGE_SCROLL_ACK.equals(event)) {
michael@0 136 mOverrideScrollAck = true;
michael@0 137 mScrollSucceeded = message.getBoolean("scrolled");
michael@0 138 if (mOverridePanning && mOverrideScrollPending) {
michael@0 139 scrollBy(mPendingDisplacement);
michael@0 140 }
michael@0 141 }
michael@0 142 } catch (Exception e) {
michael@0 143 Log.e(LOGTAG, "Exception handling message", e);
michael@0 144 }
michael@0 145 }
michael@0 146 });
michael@0 147 }
michael@0 148 }

mercurial