OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "ui/events/gesture_detection/motion_event.h" | 13 #include "ui/events/gesture_detection/motion_event.h" |
14 #include "ui/gfx/geometry/point_f.h" | 14 #include "ui/gfx/geometry/point_f.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent. | 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). |
19 class MotionEventAndroid : public ui::MotionEvent { | 21 class MotionEventAndroid : public ui::MotionEvent { |
20 public: | 22 public: |
21 // Forcing the caller to provide all cached values upon construction | 23 // Forcing the caller to provide all cached values upon construction |
22 // eliminates the need to perform a JNI call to retrieve values individually. | 24 // eliminates the need to perform a JNI call to retrieve values individually. |
23 MotionEventAndroid(JNIEnv* env, | 25 MotionEventAndroid(float pix_to_dip, |
| 26 JNIEnv* env, |
24 jobject event, | 27 jobject event, |
25 jlong time_ms, | 28 jlong time_ms, |
26 jint android_action, | 29 jint android_action, |
27 jint pointer_count, | 30 jint pointer_count, |
28 jint history_size, | 31 jint history_size, |
29 jint action_index, | 32 jint action_index, |
30 jfloat pos_x_0, | 33 jfloat pos_x_0_pixels, |
31 jfloat pos_y_0, | 34 jfloat pos_y_0_pixels, |
32 jfloat pos_x_1, | 35 jfloat pos_x_1_pixels, |
33 jfloat pos_y_1, | 36 jfloat pos_y_1_pixels, |
34 jint pointer_id_0, | 37 jint pointer_id_0, |
35 jint pointer_id_1, | 38 jint pointer_id_1, |
36 jfloat touch_major_0, | 39 jfloat touch_major_0_pixels, |
37 jfloat touch_major_1); | 40 jfloat touch_major_1_pixels); |
38 virtual ~MotionEventAndroid(); | 41 virtual ~MotionEventAndroid(); |
39 | 42 |
40 // ui::MotionEvent methods. | 43 // ui::MotionEvent methods. |
41 virtual int GetId() const OVERRIDE; | 44 virtual int GetId() const OVERRIDE; |
42 virtual Action GetAction() const OVERRIDE; | 45 virtual Action GetAction() const OVERRIDE; |
43 virtual int GetActionIndex() const OVERRIDE; | 46 virtual int GetActionIndex() const OVERRIDE; |
44 virtual size_t GetPointerCount() const OVERRIDE; | 47 virtual size_t GetPointerCount() const OVERRIDE; |
45 virtual int GetPointerId(size_t pointer_index) const OVERRIDE; | 48 virtual int GetPointerId(size_t pointer_index) const OVERRIDE; |
46 virtual float GetX(size_t pointer_index) const OVERRIDE; | 49 virtual float GetX(size_t pointer_index) const OVERRIDE; |
47 virtual float GetY(size_t pointer_index) const OVERRIDE; | 50 virtual float GetY(size_t pointer_index) const OVERRIDE; |
(...skipping 22 matching lines...) Expand all Loading... |
70 base::TimeTicks GetDownTime() const; | 73 base::TimeTicks GetDownTime() const; |
71 | 74 |
72 static bool RegisterMotionEventAndroid(JNIEnv* env); | 75 static bool RegisterMotionEventAndroid(JNIEnv* env); |
73 | 76 |
74 static base::android::ScopedJavaLocalRef<jobject> Obtain( | 77 static base::android::ScopedJavaLocalRef<jobject> Obtain( |
75 const MotionEventAndroid& event); | 78 const MotionEventAndroid& event); |
76 static base::android::ScopedJavaLocalRef<jobject> Obtain( | 79 static base::android::ScopedJavaLocalRef<jobject> Obtain( |
77 base::TimeTicks down_time, | 80 base::TimeTicks down_time, |
78 base::TimeTicks event_time, | 81 base::TimeTicks event_time, |
79 Action action, | 82 Action action, |
80 float x, | 83 float x_pixels, |
81 float y); | 84 float y_pixels); |
82 | 85 |
83 private: | 86 private: |
84 MotionEventAndroid(); | 87 MotionEventAndroid(); |
85 MotionEventAndroid(JNIEnv* env, jobject event); | 88 MotionEventAndroid(float pix_to_dip, JNIEnv* env, jobject event); |
86 MotionEventAndroid(const MotionEventAndroid&); | 89 MotionEventAndroid(const MotionEventAndroid&); |
87 MotionEventAndroid& operator=(const MotionEventAndroid&); | 90 MotionEventAndroid& operator=(const MotionEventAndroid&); |
88 | 91 |
| 92 float ToDips(float pixels) const; |
| 93 gfx::PointF ToDips(const gfx::PointF& pixels) const; |
| 94 |
89 // Cache pointer coords, id's and major lengths for the most common | 95 // Cache pointer coords, id's and major lengths for the most common |
90 // touch-related scenarios, i.e., scrolling and pinching. This prevents | 96 // touch-related scenarios, i.e., scrolling and pinching. This prevents |
91 // redundant JNI fetches for the same bits. | 97 // redundant JNI fetches for the same bits. |
92 enum { MAX_POINTERS_TO_CACHE = 2 }; | 98 enum { MAX_POINTERS_TO_CACHE = 2 }; |
93 | 99 |
94 // The Java reference to the underlying MotionEvent. | 100 // The Java reference to the underlying MotionEvent. |
95 base::android::ScopedJavaGlobalRef<jobject> event_; | 101 base::android::ScopedJavaGlobalRef<jobject> event_; |
96 | 102 |
97 base::TimeTicks cached_time_; | 103 base::TimeTicks cached_time_; |
98 Action cached_action_; | 104 Action cached_action_; |
99 size_t cached_pointer_count_; | 105 size_t cached_pointer_count_; |
100 size_t cached_history_size_; | 106 size_t cached_history_size_; |
101 int cached_action_index_; | 107 int cached_action_index_; |
102 gfx::PointF cached_positions_[MAX_POINTERS_TO_CACHE]; | 108 gfx::PointF cached_positions_[MAX_POINTERS_TO_CACHE]; |
103 int cached_pointer_ids_[MAX_POINTERS_TO_CACHE]; | 109 int cached_pointer_ids_[MAX_POINTERS_TO_CACHE]; |
104 float cached_touch_majors_[MAX_POINTERS_TO_CACHE]; | 110 float cached_touch_majors_[MAX_POINTERS_TO_CACHE]; |
105 | 111 |
| 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_; |
| 115 |
106 // Whether |event_| should be recycled on destruction. This will only be true | 116 // Whether |event_| should be recycled on destruction. This will only be true |
107 // for those events generated via |Obtain(...)|. | 117 // for those events generated via |Obtain(...)|. |
108 bool should_recycle_; | 118 bool should_recycle_; |
109 }; | 119 }; |
110 | 120 |
111 } // namespace content | 121 } // namespace content |
112 | 122 |
113 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 123 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
OLD | NEW |