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 "ui/events/gesture_detection/motion_event.h"
14 #include "ui/gfx/geometry/point_f.h"
18 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
19 // All *input* coordinates are in device pixels (as with Android MotionEvent),
20 // while all *output* coordinates are in DIPs (as with WebTouchEvent).
21 class MotionEventAndroid
: public ui::MotionEvent
{
23 // Forcing the caller to provide all cached values upon construction
24 // eliminates the need to perform a JNI call to retrieve values individually.
25 MotionEventAndroid(float pix_to_dip
,
33 jfloat pos_x_0_pixels
,
34 jfloat pos_y_0_pixels
,
35 jfloat pos_x_1_pixels
,
36 jfloat pos_y_1_pixels
,
39 jfloat touch_major_0_pixels
,
40 jfloat touch_major_1_pixels
);
41 virtual ~MotionEventAndroid();
43 // ui::MotionEvent methods.
44 virtual int GetId() const OVERRIDE
;
45 virtual Action
GetAction() const OVERRIDE
;
46 virtual int GetActionIndex() const OVERRIDE
;
47 virtual size_t GetPointerCount() const OVERRIDE
;
48 virtual int GetPointerId(size_t pointer_index
) const OVERRIDE
;
49 virtual float GetX(size_t pointer_index
) const OVERRIDE
;
50 virtual float GetY(size_t pointer_index
) const OVERRIDE
;
51 virtual float GetTouchMajor(size_t pointer_index
) const OVERRIDE
;
52 virtual float GetPressure(size_t pointer_index
) const OVERRIDE
;
53 virtual base::TimeTicks
GetEventTime() const OVERRIDE
;
54 virtual size_t GetHistorySize() const OVERRIDE
;
55 virtual base::TimeTicks
GetHistoricalEventTime(
56 size_t historical_index
) const OVERRIDE
;
57 virtual float GetHistoricalTouchMajor(
59 size_t historical_index
) const OVERRIDE
;
60 virtual float GetHistoricalX(
62 size_t historical_index
) const OVERRIDE
;
63 virtual float GetHistoricalY(
65 size_t historical_index
) const OVERRIDE
;
66 virtual scoped_ptr
<MotionEvent
> Clone() const OVERRIDE
;
67 virtual scoped_ptr
<MotionEvent
> Cancel() const OVERRIDE
;
69 // Additional Android MotionEvent methods.
70 float GetTouchMinor() const { return GetTouchMinor(0); }
71 float GetTouchMinor(size_t pointer_index
) const;
72 float GetOrientation() const;
73 base::TimeTicks
GetDownTime() const;
75 static bool RegisterMotionEventAndroid(JNIEnv
* env
);
77 static base::android::ScopedJavaLocalRef
<jobject
> Obtain(
78 const MotionEventAndroid
& event
);
79 static base::android::ScopedJavaLocalRef
<jobject
> Obtain(
80 base::TimeTicks down_time
,
81 base::TimeTicks event_time
,
88 MotionEventAndroid(float pix_to_dip
, JNIEnv
* env
, jobject event
);
89 MotionEventAndroid(const MotionEventAndroid
&);
90 MotionEventAndroid
& operator=(const MotionEventAndroid
&);
92 float ToDips(float pixels
) const;
93 gfx::PointF
ToDips(const gfx::PointF
& pixels
) const;
95 // Cache pointer coords, id's and major lengths for the most common
96 // touch-related scenarios, i.e., scrolling and pinching. This prevents
97 // redundant JNI fetches for the same bits.
98 enum { MAX_POINTERS_TO_CACHE
= 2 };
100 // The Java reference to the underlying MotionEvent.
101 base::android::ScopedJavaGlobalRef
<jobject
> event_
;
103 base::TimeTicks cached_time_
;
104 Action cached_action_
;
105 size_t cached_pointer_count_
;
106 size_t cached_history_size_
;
107 int cached_action_index_
;
108 gfx::PointF cached_positions_
[MAX_POINTERS_TO_CACHE
];
109 int cached_pointer_ids_
[MAX_POINTERS_TO_CACHE
];
110 float cached_touch_majors_
[MAX_POINTERS_TO_CACHE
];
112 // Used to convert pixel coordinates from the Java-backed MotionEvent to
113 // DIP coordinates cached/returned by the MotionEventAndroid.
114 const float pix_to_dip_
;
116 // Whether |event_| should be recycled on destruction. This will only be true
117 // for those events generated via |Obtain(...)|.
118 bool should_recycle_
;
121 } // namespace content
123 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_