OLD | NEW |
| (Empty) |
1 | |
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. | |
5 | |
6 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | |
7 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | |
8 | |
9 #include <jni.h> | |
10 | |
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" | |
17 | |
18 namespace content { | |
19 | |
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 { | |
24 public: | |
25 struct Pointer { | |
26 Pointer(jint id, | |
27 jfloat pos_x_pixels, | |
28 jfloat pos_y_pixels, | |
29 jfloat touch_major_pixels, | |
30 jfloat touch_minor_pixels, | |
31 jfloat orientation_rad, | |
32 jfloat tilt_rad, | |
33 jint tool_type); | |
34 jint id; | |
35 jfloat pos_x_pixels; | |
36 jfloat pos_y_pixels; | |
37 jfloat touch_major_pixels; | |
38 jfloat touch_minor_pixels; | |
39 jfloat orientation_rad; | |
40 jfloat tilt_rad; | |
41 jint tool_type; | |
42 }; | |
43 | |
44 // Forcing the caller to provide all cached values upon construction | |
45 // eliminates the need to perform a JNI call to retrieve values individually. | |
46 MotionEventAndroid(float pix_to_dip, | |
47 JNIEnv* env, | |
48 jobject event, | |
49 jlong time_ms, | |
50 jint android_action, | |
51 jint pointer_count, | |
52 jint history_size, | |
53 jint action_index, | |
54 jint android_button_state, | |
55 jint meta_state, | |
56 jfloat raw_offset_x_pixels, | |
57 jfloat raw_offset_y_pixels, | |
58 const Pointer& pointer0, | |
59 const Pointer& pointer1); | |
60 ~MotionEventAndroid() override; | |
61 | |
62 // ui::MotionEvent methods. | |
63 uint32 GetUniqueEventId() const override; | |
64 Action GetAction() const override; | |
65 int GetActionIndex() const override; | |
66 size_t GetPointerCount() const override; | |
67 int GetPointerId(size_t pointer_index) const override; | |
68 float GetX(size_t pointer_index) const override; | |
69 float GetY(size_t pointer_index) const override; | |
70 float GetRawX(size_t pointer_index) const override; | |
71 float GetRawY(size_t pointer_index) const override; | |
72 float GetTouchMajor(size_t pointer_index) const override; | |
73 float GetTouchMinor(size_t pointer_index) const override; | |
74 float GetOrientation(size_t pointer_index) const override; | |
75 float GetPressure(size_t pointer_index) const override; | |
76 float GetTilt(size_t pointer_index) const override; | |
77 base::TimeTicks GetEventTime() const override; | |
78 size_t GetHistorySize() const override; | |
79 base::TimeTicks GetHistoricalEventTime( | |
80 size_t historical_index) const override; | |
81 float GetHistoricalTouchMajor(size_t pointer_index, | |
82 size_t historical_index) const override; | |
83 float GetHistoricalX(size_t pointer_index, | |
84 size_t historical_index) const override; | |
85 float GetHistoricalY(size_t pointer_index, | |
86 size_t historical_index) const override; | |
87 ToolType GetToolType(size_t pointer_index) const override; | |
88 int GetButtonState() const override; | |
89 int GetFlags() const override; | |
90 | |
91 static bool RegisterMotionEventAndroid(JNIEnv* env); | |
92 | |
93 private: | |
94 struct CachedPointer; | |
95 | |
96 float ToDips(float pixels) const; | |
97 CachedPointer FromAndroidPointer(const Pointer& pointer) const; | |
98 | |
99 // Cache pointer coords, id's and major lengths for the most common | |
100 // touch-related scenarios, i.e., scrolling and pinching. This prevents | |
101 // redundant JNI fetches for the same bits. | |
102 enum { MAX_POINTERS_TO_CACHE = 2 }; | |
103 | |
104 // The Java reference to the underlying MotionEvent. | |
105 base::android::ScopedJavaGlobalRef<jobject> event_; | |
106 | |
107 // Used to convert pixel coordinates from the Java-backed MotionEvent to | |
108 // DIP coordinates cached/returned by the MotionEventAndroid. | |
109 const float pix_to_dip_; | |
110 | |
111 const base::TimeTicks cached_time_; | |
112 const Action cached_action_; | |
113 const size_t cached_pointer_count_; | |
114 const size_t cached_history_size_; | |
115 const int cached_action_index_; | |
116 const int cached_button_state_; | |
117 const int cached_flags_; | |
118 const gfx::Vector2dF cached_raw_position_offset_; | |
119 struct CachedPointer { | |
120 CachedPointer(); | |
121 int id; | |
122 gfx::PointF position; | |
123 float touch_major; | |
124 float touch_minor; | |
125 float orientation; | |
126 float tilt; | |
127 ToolType tool_type; | |
128 } cached_pointers_[MAX_POINTERS_TO_CACHE]; | |
129 | |
130 // A unique identifier for the Android motion event. | |
131 const uint32 unique_event_id_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(MotionEventAndroid); | |
134 }; | |
135 | |
136 } // namespace content | |
137 | |
138 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | |
OLD | NEW |