Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / browser / TouchPoint.java
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.
4
5 package org.chromium.content.browser;
6
7 import android.util.Log;
8 import android.view.MotionEvent;
9
10 import org.chromium.base.CalledByNative;
11
12 // This class converts android MotionEvent into an array of touch points so
13 // that they can be forwarded to the renderer process.
14 class TouchPoint {
15
16     public static final int CONVERSION_ERROR = -1;
17
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;
24
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;
33
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;
42
43     TouchPoint(int state, int id, float x, float y, float pressure,
44             float touchMajor, float touchMinor, float orientation) {
45         mState = state;
46         mId = id;
47         mX = x;
48         mY = y;
49         mPressure = pressure;
50         mTouchMajor = touchMajor;
51         mTouchMinor = touchMinor;
52         mOrientation = orientation;
53     }
54
55     // The following methods are called by native to parse the java TouchPoint
56     // object it has received.
57     @SuppressWarnings("unused")
58     @CalledByNative
59     public int getState() { return mState; }
60
61     @SuppressWarnings("unused")
62     @CalledByNative
63     public int getId() { return mId; }
64
65     @SuppressWarnings("unused")
66     @CalledByNative
67     public int getX() { return (int) mX; }
68
69     @SuppressWarnings("unused")
70     @CalledByNative
71     public int getY() { return (int) mY; }
72
73     @SuppressWarnings("unused")
74     @CalledByNative
75     public float getPressure() { return mPressure; }
76
77     @SuppressWarnings("unused")
78     @CalledByNative
79     public float getTouchMajor() { return mTouchMajor; }
80
81     @SuppressWarnings("unused")
82     @CalledByNative
83     public float getTouchMinor() { return mTouchMinor; }
84
85     @SuppressWarnings("unused")
86     @CalledByNative
87     public float getOrientation() { return mOrientation; }
88
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) {
92         int type;
93         int defaultState;
94
95         switch (event.getActionMasked()) {
96             case MotionEvent.ACTION_DOWN:
97                 type = TOUCH_EVENT_TYPE_START;
98                 defaultState = TOUCH_POINT_STATE_PRESSED;
99                 break;
100             case MotionEvent.ACTION_MOVE:
101                 type = TOUCH_EVENT_TYPE_MOVE;
102                 defaultState = TOUCH_POINT_STATE_MOVED;
103                 break;
104             case MotionEvent.ACTION_UP:
105                 type = TOUCH_EVENT_TYPE_END;
106                 defaultState = TOUCH_POINT_STATE_RELEASED;
107                 break;
108             case MotionEvent.ACTION_CANCEL:
109                 type = TOUCH_EVENT_TYPE_CANCEL;
110                 defaultState = TOUCH_POINT_STATE_CANCELLED;
111                 break;
112             case MotionEvent.ACTION_POINTER_DOWN:
113                 type = TOUCH_EVENT_TYPE_START;
114                 defaultState = TOUCH_POINT_STATE_STATIONARY;
115                 break;
116             case MotionEvent.ACTION_POINTER_UP:
117                 type = TOUCH_EVENT_TYPE_END;
118                 defaultState = TOUCH_POINT_STATE_STATIONARY;
119                 break;
120             default:
121                 Log.e("Chromium", "Unknown motion event action: " + event.getActionMasked());
122                 return CONVERSION_ERROR;
123         }
124
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;
132             }
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));
138         }
139
140         return type;
141     }
142
143     // This method is called by native to initialize all the constants from
144     // their counterparts in WebInputEvent and WebTouchPoint.
145     @SuppressWarnings("unused")
146     @CalledByNative
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;
161     }
162
163     /**
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.
167      */
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);
171         }
172     }
173 }