| Index: content/public/android/java/src/org/chromium/content/browser/EventForwarderImpl.java
|
| diff --git a/content/public/android/java/src/org/chromium/content/browser/EventForwarderImpl.java b/content/public/android/java/src/org/chromium/content/browser/EventForwarderImpl.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7c65af500562a536e339122b9d2bd670075b5124
|
| --- /dev/null
|
| +++ b/content/public/android/java/src/org/chromium/content/browser/EventForwarderImpl.java
|
| @@ -0,0 +1,164 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.content.browser;
|
| +
|
| +import android.content.Context;
|
| +import android.os.Build;
|
| +import android.view.MotionEvent;
|
| +
|
| +import org.chromium.base.TraceEvent;
|
| +import org.chromium.base.annotations.JNINamespace;
|
| +import org.chromium.content_public.browser.EventForwarder;
|
| +import org.chromium.content_public.browser.WebContents;
|
| +
|
| +/**
|
| + * Implementation of {@link EventForwarder}. Works as a bridge for Java layer
|
| + * events for pass through to reach native content.
|
| + */
|
| +@JNINamespace("content")
|
| +public class EventForwarderImpl implements EventForwarder {
|
| + private final WebContents mWebContents;
|
| + private long mNativeEventForwarderImpl;
|
| +
|
| + // TODO: Remove the dependency on ContentViewCore once the all the event forwarding
|
| + // stuff is migrated here.
|
| + private ContentViewCore mContentViewCore;
|
| + private Context mContext;
|
| +
|
| + // Offsets for the events that passes through.
|
| + private float mCurrentTouchOffsetX;
|
| + private float mCurrentTouchOffsetY;
|
| +
|
| + public static EventForwarderImpl create(WebContents webContents) {
|
| + EventForwarderImpl eventForwarder = new EventForwarderImpl(webContents);
|
| + eventForwarder.init();
|
| + return eventForwarder;
|
| + }
|
| +
|
| + private EventForwarderImpl(WebContents webContents) {
|
| + mWebContents = webContents;
|
| + }
|
| +
|
| + private void init() {
|
| + mNativeEventForwarderImpl = nativeInit(mWebContents);
|
| + }
|
| +
|
| + @Override
|
| + public void setCurrentTouchEventOffsets(float dx, float dy) {
|
| + mCurrentTouchOffsetX = dx;
|
| + mCurrentTouchOffsetY = dy;
|
| +
|
| + // Update also ContentViewCore during the migration phase.
|
| + getContentViewCore().setCurrentTouchEventOffsets(dx, dy);
|
| + }
|
| +
|
| + private MotionEvent createOffsetMotionEvent(MotionEvent src) {
|
| + MotionEvent dst = MotionEvent.obtain(src);
|
| + dst.offsetLocation(mCurrentTouchOffsetX, mCurrentTouchOffsetY);
|
| + return dst;
|
| + }
|
| +
|
| + private ContentViewCore getContentViewCore() {
|
| + if (mContentViewCore == null) {
|
| + mContentViewCore = ContentViewCore.fromWebContents(mWebContents);
|
| + }
|
| + return mContentViewCore;
|
| + }
|
| +
|
| + private Context getContext() {
|
| + if (mContext == null) mContext = getContentViewCore().getContext();
|
| + return mContext;
|
| + }
|
| +
|
| + @Override
|
| + public boolean onTouchEvent(MotionEvent event) {
|
| + // TODO(mustaq): Should we include MotionEvent.TOOL_TYPE_STYLUS here?
|
| + // crbug.com/592082
|
| + if (event.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
|
| + // Mouse button info is incomplete on L and below
|
| + int apiVersion = Build.VERSION.SDK_INT;
|
| + if (apiVersion >= android.os.Build.VERSION_CODES.M) {
|
| + return getContentViewCore().sendMouseEvent(event);
|
| + }
|
| + }
|
| +
|
| + final boolean isTouchHandleEvent = false;
|
| + return sendTouchEvent(event, isTouchHandleEvent);
|
| + }
|
| +
|
| + @Override
|
| + public boolean onTouchHandleEvent(MotionEvent event) {
|
| + return sendTouchEvent(event, true);
|
| + }
|
| +
|
| + private boolean sendTouchEvent(MotionEvent event, boolean isTouchHandleEvent) {
|
| + TraceEvent.begin("sendTouchEvent");
|
| + try {
|
| + int eventAction = event.getActionMasked();
|
| +
|
| + if (eventAction == MotionEvent.ACTION_DOWN) {
|
| + getContentViewCore().cancelRequestToScrollFocusedEditableNodeIntoView();
|
| + }
|
| +
|
| + if (SPenSupport.isSPenSupported(getContext())) {
|
| + eventAction = SPenSupport.convertSPenEventAction(eventAction);
|
| + }
|
| + if (!ContentViewCore.isValidTouchEventActionForNative(eventAction)) return false;
|
| +
|
| + if (mNativeEventForwarderImpl == 0) return false;
|
| +
|
| + // A zero offset is quite common, in which case the unnecessary copy should be avoided.
|
| + MotionEvent offset = null;
|
| + if (mCurrentTouchOffsetX != 0 || mCurrentTouchOffsetY != 0) {
|
| + offset = createOffsetMotionEvent(event);
|
| + event = offset;
|
| + }
|
| +
|
| + final int pointerCount = event.getPointerCount();
|
| +
|
| + float[] touchMajor = {
|
| + event.getTouchMajor(), pointerCount > 1 ? event.getTouchMajor(1) : 0};
|
| + float[] touchMinor = {
|
| + event.getTouchMinor(), pointerCount > 1 ? event.getTouchMinor(1) : 0};
|
| +
|
| + for (int i = 0; i < 2; i++) {
|
| + if (touchMajor[i] < touchMinor[i]) {
|
| + float tmp = touchMajor[i];
|
| + touchMajor[i] = touchMinor[i];
|
| + touchMinor[i] = tmp;
|
| + }
|
| + }
|
| +
|
| + final boolean consumed = nativeOnTouchEvent(mNativeEventForwarderImpl, event,
|
| + event.getEventTime(), eventAction, pointerCount, event.getHistorySize(),
|
| + event.getActionIndex(), event.getX(), event.getY(),
|
| + pointerCount > 1 ? event.getX(1) : 0, pointerCount > 1 ? event.getY(1) : 0,
|
| + event.getPointerId(0), pointerCount > 1 ? event.getPointerId(1) : -1,
|
| + touchMajor[0], touchMajor[1], touchMinor[0], touchMinor[1],
|
| + event.getOrientation(), pointerCount > 1 ? event.getOrientation(1) : 0,
|
| + event.getAxisValue(MotionEvent.AXIS_TILT),
|
| + pointerCount > 1 ? event.getAxisValue(MotionEvent.AXIS_TILT, 1) : 0,
|
| + event.getRawX(), event.getRawY(), event.getToolType(0),
|
| + pointerCount > 1 ? event.getToolType(1) : MotionEvent.TOOL_TYPE_UNKNOWN,
|
| + event.getButtonState(), event.getMetaState(), isTouchHandleEvent);
|
| +
|
| + if (offset != null) offset.recycle();
|
| + return consumed;
|
| + } finally {
|
| + TraceEvent.end("sendTouchEvent");
|
| + }
|
| + }
|
| +
|
| + private native long nativeInit(WebContents webContents);
|
| +
|
| + // All touch events (including flings, scrolls etc) accept coordinates in physical pixels.
|
| + private native boolean nativeOnTouchEvent(long nativeEventForwarderImpl, MotionEvent event,
|
| + long timeMs, int action, int pointerCount, int historySize, int actionIndex, float x0,
|
| + float y0, float x1, float y1, int pointerId0, int pointerId1, float touchMajor0,
|
| + float touchMajor1, float touchMinor0, float touchMinor1, float orientation0,
|
| + float orientation1, float tilt0, float tilt1, float rawX, float rawY,
|
| + int androidToolType0, int androidToolType1, int androidButtonState,
|
| + int androidMetaState, boolean isTouchHandleEvent);
|
| +}
|
|
|