OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.ui.base; |
| 6 |
| 7 import android.view.MotionEvent; |
| 8 |
| 9 import org.chromium.base.annotations.JNINamespace; |
| 10 |
| 11 /** |
| 12 * Class used to forward view, input events down to native. |
| 13 * TODO(jinsukkim): Hook this up to UI event forwarding flow and substitute Wind
owAndroid. |
| 14 */ |
| 15 @JNINamespace("ui") |
| 16 public class ViewRoot { |
| 17 |
| 18 private final WindowAndroid mWindowAndroid; |
| 19 |
| 20 // The corresponding native instance. This class can only be used while |
| 21 // the native instance is alive. |
| 22 // This is initialized lazily. Use {@link getNativePtr()} rather than |
| 23 // accessing it directly. |
| 24 private long mNativeView; |
| 25 |
| 26 public static ViewRoot create(WindowAndroid window) { |
| 27 if (window == null) throw new IllegalArgumentException("WindowAndroid sh
ould not be null"); |
| 28 return new ViewRoot(window); |
| 29 } |
| 30 |
| 31 private ViewRoot(WindowAndroid window) { |
| 32 mWindowAndroid = window; |
| 33 } |
| 34 |
| 35 public WindowAndroid getWindowAndroid() { |
| 36 return mWindowAndroid; |
| 37 } |
| 38 |
| 39 public boolean onTouchEvent(MotionEvent event, boolean isTouchHandleEvent) { |
| 40 final int pointerCount = event.getPointerCount(); |
| 41 |
| 42 float[] touchMajor = {event.getTouchMajor(), |
| 43 pointerCount > 1 ? event.getTouchMajor(1) : 0}; |
| 44 float[] touchMinor = {event.getTouchMinor(), |
| 45 pointerCount > 1 ? event.getTouchMinor(1) : 0}; |
| 46 |
| 47 for (int i = 0; i < 2; i++) { |
| 48 if (touchMajor[i] < touchMinor[i]) { |
| 49 float tmp = touchMajor[i]; |
| 50 touchMajor[i] = touchMinor[i]; |
| 51 touchMinor[i] = tmp; |
| 52 } |
| 53 } |
| 54 |
| 55 return nativeOnTouchEvent(getNativePtr(), event, |
| 56 event.getEventTime(), event.getActionMasked(), |
| 57 pointerCount, event.getHistorySize(), event.getActionIndex(), |
| 58 event.getX(), event.getY(), |
| 59 pointerCount > 1 ? event.getX(1) : 0, |
| 60 pointerCount > 1 ? event.getY(1) : 0, |
| 61 event.getPointerId(0), pointerCount > 1 ? event.getPointerId(1)
: -1, |
| 62 touchMajor[0], touchMajor[1], |
| 63 touchMinor[0], touchMinor[1], |
| 64 event.getOrientation(), pointerCount > 1 ? event.getOrientation(
1) : 0, |
| 65 event.getAxisValue(MotionEvent.AXIS_TILT), |
| 66 pointerCount > 1 ? event.getAxisValue(MotionEvent.AXIS_TILT, 1)
: 0, |
| 67 event.getRawX(), event.getRawY(), |
| 68 event.getToolType(0), |
| 69 pointerCount > 1 ? event.getToolType(1) : MotionEvent.TOOL_TYPE_
UNKNOWN, |
| 70 event.getButtonState(), |
| 71 event.getMetaState(), |
| 72 isTouchHandleEvent); |
| 73 } |
| 74 |
| 75 public long getNativePtr() { |
| 76 if (mNativeView == 0) mNativeView = nativeInit(mWindowAndroid.getNativeP
ointer()); |
| 77 return mNativeView; |
| 78 } |
| 79 |
| 80 public void destroy() { |
| 81 if (mNativeView != 0) { |
| 82 nativeDestroy(mNativeView); |
| 83 mNativeView = 0; |
| 84 } |
| 85 } |
| 86 |
| 87 private native long nativeInit(long windowNativePointer); |
| 88 private native void nativeDestroy(long nativeViewRoot); |
| 89 |
| 90 // All touch events (including flings, scrolls etc) accept coordinates in ph
ysical pixels. |
| 91 private native boolean nativeOnTouchEvent( |
| 92 long nativeViewRoot, MotionEvent event, |
| 93 long timeMs, int action, int pointerCount, int historySize, int acti
onIndex, |
| 94 float x0, float y0, float x1, float y1, |
| 95 int pointerId0, int pointerId1, |
| 96 float touchMajor0, float touchMajor1, |
| 97 float touchMinor0, float touchMinor1, |
| 98 float orientation0, float orientation1, |
| 99 float tilt0, float tilt1, |
| 100 float rawX, float rawY, |
| 101 int androidToolType0, int androidToolType1, |
| 102 int androidButtonState, int androidMetaState, |
| 103 boolean isTouchHandleEvent); |
| 104 } |
OLD | NEW |