1 // Copyright 2012 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.
5 package org.chromium.content.browser;
7 import android.util.Log;
8 import android.view.MotionEvent;
10 import org.chromium.base.CalledByNative;
12 // This class converts android MotionEvent into an array of touch points so
13 // that they can be forwarded to the renderer process.
16 public static final int CONVERSION_ERROR = -1;
18 // Type of motion event to send to the native side. The values originate from their
19 // webkit WebInputEvent counterparts, and are set via initializeConstants().
20 static int TOUCH_EVENT_TYPE_START;
21 static int TOUCH_EVENT_TYPE_MOVE;
22 static int TOUCH_EVENT_TYPE_END;
23 static int TOUCH_EVENT_TYPE_CANCEL;
25 // Type of motion event to send to the native side. The values originate from their
26 // webkit WebTouchPoint counterparts, and are set via initializeConstants().
27 private static int TOUCH_POINT_STATE_UNDEFINED;
28 private static int TOUCH_POINT_STATE_RELEASED;
29 private static int TOUCH_POINT_STATE_PRESSED;
30 private static int TOUCH_POINT_STATE_MOVED;
31 private static int TOUCH_POINT_STATE_STATIONARY;
32 private static int TOUCH_POINT_STATE_CANCELLED;
34 private final int mState;
35 private final int mId;
36 private final float mX;
37 private final float mY;
38 private final float mPressure;
39 private final float mTouchMajor;
40 private final float mTouchMinor;
41 private final float mOrientation;
43 TouchPoint(int state, int id, float x, float y, float pressure,
44 float touchMajor, float touchMinor, float orientation) {
50 mTouchMajor = touchMajor;
51 mTouchMinor = touchMinor;
52 mOrientation = orientation;
55 // The following methods are called by native to parse the java TouchPoint
56 // object it has received.
57 @SuppressWarnings("unused")
59 public int getState() { return mState; }
61 @SuppressWarnings("unused")
63 public int getId() { return mId; }
65 @SuppressWarnings("unused")
67 public int getX() { return (int) mX; }
69 @SuppressWarnings("unused")
71 public int getY() { return (int) mY; }
73 @SuppressWarnings("unused")
75 public float getPressure() { return mPressure; }
77 @SuppressWarnings("unused")
79 public float getTouchMajor() { return mTouchMajor; }
81 @SuppressWarnings("unused")
83 public float getTouchMinor() { return mTouchMinor; }
85 @SuppressWarnings("unused")
87 public float getOrientation() { return mOrientation; }
89 // Converts a MotionEvent into an array of touch points.
90 // Returns the WebTouchEvent::Type for the MotionEvent and -1 for failure.
91 public static int createTouchPoints(MotionEvent event, TouchPoint[] pts) {
95 switch (event.getActionMasked()) {
96 case MotionEvent.ACTION_DOWN:
97 type = TOUCH_EVENT_TYPE_START;
98 defaultState = TOUCH_POINT_STATE_PRESSED;
100 case MotionEvent.ACTION_MOVE:
101 type = TOUCH_EVENT_TYPE_MOVE;
102 defaultState = TOUCH_POINT_STATE_MOVED;
104 case MotionEvent.ACTION_UP:
105 type = TOUCH_EVENT_TYPE_END;
106 defaultState = TOUCH_POINT_STATE_RELEASED;
108 case MotionEvent.ACTION_CANCEL:
109 type = TOUCH_EVENT_TYPE_CANCEL;
110 defaultState = TOUCH_POINT_STATE_CANCELLED;
112 case MotionEvent.ACTION_POINTER_DOWN:
113 type = TOUCH_EVENT_TYPE_START;
114 defaultState = TOUCH_POINT_STATE_STATIONARY;
116 case MotionEvent.ACTION_POINTER_UP:
117 type = TOUCH_EVENT_TYPE_END;
118 defaultState = TOUCH_POINT_STATE_STATIONARY;
121 Log.e("Chromium", "Unknown motion event action: " + event.getActionMasked());
122 return CONVERSION_ERROR;
125 for (int i = 0; i < pts.length; ++i) {
126 int state = defaultState;
127 if (defaultState == TOUCH_POINT_STATE_STATIONARY && event.getActionIndex() == i) {
128 // An additional pointer has started or ended. Map this pointer state as
129 // required, and all other pointers as "stationary".
130 state = event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN ?
131 TOUCH_POINT_STATE_PRESSED : TOUCH_POINT_STATE_RELEASED;
133 pts[i] = new TouchPoint(state, event.getPointerId(i),
134 event.getX(i), event.getY(i),
135 event.getPressure(i),
136 event.getTouchMajor(i), event.getTouchMinor(i),
137 event.getOrientation(i));
143 // This method is called by native to initialize all the constants from
144 // their counterparts in WebInputEvent and WebTouchPoint.
145 @SuppressWarnings("unused")
147 private static void initializeConstants(
148 int touchTypeStart, int touchTypeMove, int touchTypeEnd, int touchTypeCancel,
149 int touchPointUndefined, int touchPointReleased, int touchPointPressed,
150 int touchPointMoved, int touchPointStationary, int touchPointCancelled) {
151 TOUCH_EVENT_TYPE_START = touchTypeStart;
152 TOUCH_EVENT_TYPE_MOVE = touchTypeMove;
153 TOUCH_EVENT_TYPE_END = touchTypeEnd;
154 TOUCH_EVENT_TYPE_CANCEL = touchTypeCancel;
155 TOUCH_POINT_STATE_UNDEFINED = touchPointUndefined;
156 TOUCH_POINT_STATE_RELEASED = touchPointReleased;
157 TOUCH_POINT_STATE_PRESSED = touchPointPressed;
158 TOUCH_POINT_STATE_MOVED = touchPointMoved;
159 TOUCH_POINT_STATE_STATIONARY = touchPointStationary;
160 TOUCH_POINT_STATE_CANCELLED = touchPointCancelled;
164 * Initialize the constants to distinct values if they have not been initialized.
165 * During pure-Java testing, initializeConstants() may not be called by native code.
166 * Unit tests should call this method before using the values.
168 static void initializeConstantsForTesting() {
169 if (TOUCH_EVENT_TYPE_START == TOUCH_EVENT_TYPE_MOVE) {
170 initializeConstants(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);