2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
7 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
11 #include "base/android/scoped_java_ref.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "content/common/content_export.h"
15 #include "ui/events/gesture_detection/motion_event.h"
16 #include "ui/gfx/geometry/point_f.h"
20 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
21 // All *input* coordinates are in device pixels (as with Android MotionEvent),
22 // while all *output* coordinates are in DIPs (as with WebTouchEvent).
23 class CONTENT_EXPORT MotionEventAndroid
: public ui::MotionEvent
{
29 jfloat touch_major_pixels
,
30 jfloat touch_minor_pixels
,
31 jfloat orientation_rad
,
36 jfloat touch_major_pixels
;
37 jfloat touch_minor_pixels
;
38 jfloat orientation_rad
;
42 // Forcing the caller to provide all cached values upon construction
43 // eliminates the need to perform a JNI call to retrieve values individually.
44 MotionEventAndroid(float pix_to_dip
,
52 jint android_button_state
,
54 jfloat raw_offset_x_pixels
,
55 jfloat raw_offset_y_pixels
,
56 const Pointer
& pointer0
,
57 const Pointer
& pointer1
);
58 ~MotionEventAndroid() override
;
60 // ui::MotionEvent methods.
61 uint32
GetUniqueEventId() const override
;
62 Action
GetAction() const override
;
63 int GetActionIndex() const override
;
64 size_t GetPointerCount() const override
;
65 int GetPointerId(size_t pointer_index
) const override
;
66 float GetX(size_t pointer_index
) const override
;
67 float GetY(size_t pointer_index
) const override
;
68 float GetRawX(size_t pointer_index
) const override
;
69 float GetRawY(size_t pointer_index
) const override
;
70 float GetTouchMajor(size_t pointer_index
) const override
;
71 float GetTouchMinor(size_t pointer_index
) const override
;
72 float GetOrientation(size_t pointer_index
) const override
;
73 float GetPressure(size_t pointer_index
) const override
;
74 base::TimeTicks
GetEventTime() const override
;
75 size_t GetHistorySize() const override
;
76 base::TimeTicks
GetHistoricalEventTime(
77 size_t historical_index
) const override
;
78 float GetHistoricalTouchMajor(size_t pointer_index
,
79 size_t historical_index
) const override
;
80 float GetHistoricalX(size_t pointer_index
,
81 size_t historical_index
) const override
;
82 float GetHistoricalY(size_t pointer_index
,
83 size_t historical_index
) const override
;
84 ToolType
GetToolType(size_t pointer_index
) const override
;
85 int GetButtonState() const override
;
86 int GetFlags() const override
;
88 static bool RegisterMotionEventAndroid(JNIEnv
* env
);
93 float ToDips(float pixels
) const;
94 CachedPointer
FromAndroidPointer(const Pointer
& pointer
) const;
96 // Cache pointer coords, id's and major lengths for the most common
97 // touch-related scenarios, i.e., scrolling and pinching. This prevents
98 // redundant JNI fetches for the same bits.
99 enum { MAX_POINTERS_TO_CACHE
= 2 };
101 // The Java reference to the underlying MotionEvent.
102 base::android::ScopedJavaGlobalRef
<jobject
> event_
;
104 // Used to convert pixel coordinates from the Java-backed MotionEvent to
105 // DIP coordinates cached/returned by the MotionEventAndroid.
106 const float pix_to_dip_
;
108 const base::TimeTicks cached_time_
;
109 const Action cached_action_
;
110 const size_t cached_pointer_count_
;
111 const size_t cached_history_size_
;
112 const int cached_action_index_
;
113 const int cached_button_state_
;
114 const int cached_flags_
;
115 const gfx::Vector2dF cached_raw_position_offset_
;
116 struct CachedPointer
{
119 gfx::PointF position
;
124 } cached_pointers_
[MAX_POINTERS_TO_CACHE
];
126 // A unique identifier for the Android motion event.
127 const uint32 unique_event_id_
;
129 DISALLOW_COPY_AND_ASSIGN(MotionEventAndroid
);
132 } // namespace content
134 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_