1 // Copyright 2014 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
10 #include "base/android/scoped_java_ref.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
14 #include "ui/events/gesture_detection/motion_event.h"
15 #include "ui/gfx/geometry/point_f.h"
19 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
20 // All *input* coordinates are in device pixels (as with Android MotionEvent),
21 // while all *output* coordinates are in DIPs (as with WebTouchEvent).
22 class CONTENT_EXPORT MotionEventAndroid
: public ui::MotionEvent
{
24 // Forcing the caller to provide all cached values upon construction
25 // eliminates the need to perform a JNI call to retrieve values individually.
26 MotionEventAndroid(float pix_to_dip
,
34 jfloat pos_x_0_pixels
,
35 jfloat pos_y_0_pixels
,
36 jfloat pos_x_1_pixels
,
37 jfloat pos_y_1_pixels
,
40 jfloat touch_major_0_pixels
,
41 jfloat touch_major_1_pixels
,
42 jfloat raw_pos_x_pixels
,
43 jfloat raw_pos_y_pixels
,
44 jint android_tool_type_0
,
45 jint android_tool_type_1
,
46 jint android_button_state
);
47 virtual ~MotionEventAndroid();
49 // ui::MotionEvent methods.
50 virtual int GetId() const OVERRIDE
;
51 virtual Action
GetAction() const OVERRIDE
;
52 virtual int GetActionIndex() const OVERRIDE
;
53 virtual size_t GetPointerCount() const OVERRIDE
;
54 virtual int GetPointerId(size_t pointer_index
) const OVERRIDE
;
55 virtual float GetX(size_t pointer_index
) const OVERRIDE
;
56 virtual float GetY(size_t pointer_index
) const OVERRIDE
;
57 virtual float GetRawX(size_t pointer_index
) const OVERRIDE
;
58 virtual float GetRawY(size_t pointer_index
) const OVERRIDE
;
59 virtual float GetTouchMajor(size_t pointer_index
) const OVERRIDE
;
60 virtual float GetPressure(size_t pointer_index
) const OVERRIDE
;
61 virtual base::TimeTicks
GetEventTime() const OVERRIDE
;
62 virtual size_t GetHistorySize() const OVERRIDE
;
63 virtual base::TimeTicks
GetHistoricalEventTime(
64 size_t historical_index
) const OVERRIDE
;
65 virtual float GetHistoricalTouchMajor(size_t pointer_index
,
66 size_t historical_index
) const OVERRIDE
;
67 virtual float GetHistoricalX(size_t pointer_index
,
68 size_t historical_index
) const OVERRIDE
;
69 virtual float GetHistoricalY(size_t pointer_index
,
70 size_t historical_index
) const OVERRIDE
;
71 virtual ToolType
GetToolType(size_t pointer_index
) const OVERRIDE
;
72 virtual int GetButtonState() const OVERRIDE
;
73 virtual scoped_ptr
<MotionEvent
> Clone() const OVERRIDE
;
74 virtual scoped_ptr
<MotionEvent
> Cancel() const OVERRIDE
;
76 // Additional Android MotionEvent methods.
77 float GetTouchMinor() const { return GetTouchMinor(0); }
78 float GetTouchMinor(size_t pointer_index
) const;
79 float GetOrientation() const;
80 base::TimeTicks
GetDownTime() const;
82 static bool RegisterMotionEventAndroid(JNIEnv
* env
);
84 static base::android::ScopedJavaLocalRef
<jobject
> Obtain(
85 const MotionEventAndroid
& event
);
86 static base::android::ScopedJavaLocalRef
<jobject
> Obtain(
87 base::TimeTicks down_time
,
88 base::TimeTicks event_time
,
95 MotionEventAndroid(float pix_to_dip
, JNIEnv
* env
, jobject event
);
96 MotionEventAndroid(const MotionEventAndroid
&);
97 MotionEventAndroid
& operator=(const MotionEventAndroid
&);
99 float ToDips(float pixels
) const;
100 gfx::PointF
ToDips(const gfx::PointF
& pixels
) const;
102 // Cache pointer coords, id's and major lengths for the most common
103 // touch-related scenarios, i.e., scrolling and pinching. This prevents
104 // redundant JNI fetches for the same bits.
105 enum { MAX_POINTERS_TO_CACHE
= 2 };
107 // The Java reference to the underlying MotionEvent.
108 base::android::ScopedJavaGlobalRef
<jobject
> event_
;
110 base::TimeTicks cached_time_
;
111 Action cached_action_
;
112 size_t cached_pointer_count_
;
113 size_t cached_history_size_
;
114 int cached_action_index_
;
115 gfx::PointF cached_positions_
[MAX_POINTERS_TO_CACHE
];
116 int cached_pointer_ids_
[MAX_POINTERS_TO_CACHE
];
117 float cached_touch_majors_
[MAX_POINTERS_TO_CACHE
];
118 gfx::Vector2dF cached_raw_position_offset_
;
119 ToolType cached_tool_types_
[MAX_POINTERS_TO_CACHE
];
120 int cached_button_state_
;
122 // Used to convert pixel coordinates from the Java-backed MotionEvent to
123 // DIP coordinates cached/returned by the MotionEventAndroid.
124 const float pix_to_dip_
;
126 // Whether |event_| should be recycled on destruction. This will only be true
127 // for those events generated via |Obtain(...)|.
128 bool should_recycle_
;
131 } // namespace content
133 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_