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 #include "content/browser/renderer_host/input/motion_event_android.h" | 5 #include "content/browser/renderer_host/input/motion_event_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/float_util.h" |
8 #include "jni/MotionEvent_jni.h" | 9 #include "jni/MotionEvent_jni.h" |
9 | 10 |
10 using base::android::AttachCurrentThread; | 11 using base::android::AttachCurrentThread; |
11 using namespace JNI_MotionEvent; | 12 using namespace JNI_MotionEvent; |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 namespace { | 15 namespace { |
15 | 16 |
16 int ToAndroidAction(MotionEventAndroid::Action action) { | 17 int ToAndroidAction(MotionEventAndroid::Action action) { |
17 switch (action) { | 18 switch (action) { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 90 } |
90 | 91 |
91 int64 ToAndroidTime(base::TimeTicks time) { | 92 int64 ToAndroidTime(base::TimeTicks time) { |
92 return (time - base::TimeTicks()).InMilliseconds(); | 93 return (time - base::TimeTicks()).InMilliseconds(); |
93 } | 94 } |
94 | 95 |
95 base::TimeTicks FromAndroidTime(int64 time_ms) { | 96 base::TimeTicks FromAndroidTime(int64 time_ms) { |
96 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 97 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
97 } | 98 } |
98 | 99 |
| 100 float ToValidFloat(float x) { |
| 101 return base::IsNaN(x) ? 0.f : x; |
| 102 } |
| 103 |
99 } // namespace | 104 } // namespace |
100 | 105 |
101 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 106 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
102 JNIEnv* env, | 107 JNIEnv* env, |
103 jobject event, | 108 jobject event, |
104 jlong time_ms, | 109 jlong time_ms, |
105 jint android_action, | 110 jint android_action, |
106 jint pointer_count, | 111 jint pointer_count, |
107 jint history_size, | 112 jint history_size, |
108 jint action_index, | 113 jint action_index, |
109 jfloat pos_x_0_pixels, | 114 jfloat pos_x_0_pixels, |
110 jfloat pos_y_0_pixels, | 115 jfloat pos_y_0_pixels, |
111 jfloat pos_x_1_pixels, | 116 jfloat pos_x_1_pixels, |
112 jfloat pos_y_1_pixels, | 117 jfloat pos_y_1_pixels, |
113 jint pointer_id_0, | 118 jint pointer_id_0, |
114 jint pointer_id_1, | 119 jint pointer_id_1, |
115 jfloat touch_major_0_pixels, | 120 jfloat touch_major_0_pixels, |
116 jfloat touch_major_1_pixels, | 121 jfloat touch_major_1_pixels, |
| 122 jfloat touch_minor_0_pixels, |
| 123 jfloat touch_minor_1_pixels, |
| 124 jfloat orientation_0_rad, |
| 125 jfloat orientation_1_rad, |
117 jfloat raw_pos_x_pixels, | 126 jfloat raw_pos_x_pixels, |
118 jfloat raw_pos_y_pixels, | 127 jfloat raw_pos_y_pixels, |
119 jint android_tool_type_0, | 128 jint android_tool_type_0, |
120 jint android_tool_type_1, | 129 jint android_tool_type_1, |
121 jint android_button_state) | 130 jint android_button_state) |
122 : cached_time_(FromAndroidTime(time_ms)), | 131 : cached_time_(FromAndroidTime(time_ms)), |
123 cached_action_(FromAndroidAction(android_action)), | 132 cached_action_(FromAndroidAction(android_action)), |
124 cached_pointer_count_(pointer_count), | 133 cached_pointer_count_(pointer_count), |
125 cached_history_size_(history_size), | 134 cached_history_size_(history_size), |
126 cached_action_index_(action_index), | 135 cached_action_index_(action_index), |
127 cached_button_state_(FromAndroidButtonState(android_button_state)), | 136 cached_button_state_(FromAndroidButtonState(android_button_state)), |
128 pix_to_dip_(pix_to_dip), | 137 pix_to_dip_(pix_to_dip), |
129 should_recycle_(false) { | 138 should_recycle_(false) { |
130 DCHECK_GT(pointer_count, 0); | 139 DCHECK_GT(pointer_count, 0); |
131 DCHECK_GE(history_size, 0); | 140 DCHECK_GE(history_size, 0); |
132 | 141 |
133 event_.Reset(env, event); | 142 event_.Reset(env, event); |
134 DCHECK(event_.obj()); | 143 DCHECK(event_.obj()); |
135 | 144 |
136 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 145 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); |
137 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 146 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
138 cached_pointer_ids_[0] = pointer_id_0; | 147 cached_pointer_ids_[0] = pointer_id_0; |
139 cached_pointer_ids_[1] = pointer_id_1; | 148 cached_pointer_ids_[1] = pointer_id_1; |
140 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | 149 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); |
141 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | 150 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); |
| 151 cached_touch_minors_[0] = ToDips(touch_minor_0_pixels); |
| 152 cached_touch_minors_[1] = ToDips(touch_minor_1_pixels); |
| 153 cached_orientations_[0] = ToValidFloat(orientation_0_rad); |
| 154 cached_orientations_[1] = ToValidFloat(orientation_1_rad); |
142 cached_raw_position_offset_ = | 155 cached_raw_position_offset_ = |
143 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - | 156 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - |
144 cached_positions_[0]; | 157 cached_positions_[0]; |
145 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); | 158 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); |
146 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); | 159 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); |
147 } | 160 } |
148 | 161 |
149 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 162 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
150 JNIEnv* env, | 163 JNIEnv* env, |
151 jobject event) | 164 jobject event) |
(...skipping 11 matching lines...) Expand all Loading... |
163 DCHECK(event_.obj()); | 176 DCHECK(event_.obj()); |
164 | 177 |
165 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 178 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
166 if (i < cached_pointer_count_) { | 179 if (i < cached_pointer_count_) { |
167 cached_positions_[i] = | 180 cached_positions_[i] = |
168 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 181 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
169 Java_MotionEvent_getYF_I(env, event, i))); | 182 Java_MotionEvent_getYF_I(env, event, i))); |
170 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | 183 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
171 cached_touch_majors_[i] = | 184 cached_touch_majors_[i] = |
172 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | 185 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); |
| 186 cached_touch_minors_[i] = |
| 187 ToDips(Java_MotionEvent_getTouchMinorF_I(env, event, i)); |
| 188 cached_orientations_[i] = |
| 189 ToValidFloat(Java_MotionEvent_getOrientationF_I(env, event, i)); |
173 cached_tool_types_[i] = | 190 cached_tool_types_[i] = |
174 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); | 191 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); |
175 } else { | 192 } else { |
176 cached_pointer_ids_[i] = 0; | 193 cached_pointer_ids_[i] = 0; |
177 cached_touch_majors_[i] = 0.f; | 194 cached_touch_majors_[i] = 0.f; |
| 195 cached_touch_minors_[i] = 0.f; |
| 196 cached_orientations_[i] = 0.f; |
178 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; | 197 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; |
179 } | 198 } |
180 } | 199 } |
181 | 200 |
182 cached_raw_position_offset_ = | 201 cached_raw_position_offset_ = |
183 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), | 202 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), |
184 Java_MotionEvent_getRawY(env, event))) - | 203 Java_MotionEvent_getRawY(env, event))) - |
185 cached_positions_[0]; | 204 cached_positions_[0]; |
186 } | 205 } |
187 | 206 |
188 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 207 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
189 : event_(Obtain(other)), | 208 : event_(Obtain(other)), |
190 cached_time_(other.cached_time_), | 209 cached_time_(other.cached_time_), |
191 cached_action_(other.cached_action_), | 210 cached_action_(other.cached_action_), |
192 cached_pointer_count_(other.cached_pointer_count_), | 211 cached_pointer_count_(other.cached_pointer_count_), |
193 cached_history_size_(other.cached_history_size_), | 212 cached_history_size_(other.cached_history_size_), |
194 cached_action_index_(other.cached_action_index_), | 213 cached_action_index_(other.cached_action_index_), |
195 cached_raw_position_offset_(other.cached_raw_position_offset_), | 214 cached_raw_position_offset_(other.cached_raw_position_offset_), |
196 cached_button_state_(other.cached_button_state_), | 215 cached_button_state_(other.cached_button_state_), |
197 pix_to_dip_(other.pix_to_dip_), | 216 pix_to_dip_(other.pix_to_dip_), |
198 should_recycle_(true) { | 217 should_recycle_(true) { |
199 DCHECK(event_.obj()); | 218 DCHECK(event_.obj()); |
200 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 219 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
201 cached_positions_[i] = other.cached_positions_[i]; | 220 cached_positions_[i] = other.cached_positions_[i]; |
202 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 221 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
203 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 222 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 223 cached_touch_minors_[i] = other.cached_touch_minors_[i]; |
| 224 cached_orientations_[i] = other.cached_orientations_[i]; |
204 cached_tool_types_[i] = other.cached_tool_types_[i]; | 225 cached_tool_types_[i] = other.cached_tool_types_[i]; |
205 } | 226 } |
206 } | 227 } |
207 | 228 |
208 MotionEventAndroid::~MotionEventAndroid() { | 229 MotionEventAndroid::~MotionEventAndroid() { |
209 if (should_recycle_) | 230 if (should_recycle_) |
210 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 231 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
211 } | 232 } |
212 | 233 |
213 int MotionEventAndroid::GetId() const { | 234 int MotionEventAndroid::GetId() const { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 } | 280 } |
260 | 281 |
261 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 282 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
262 DCHECK_LT(pointer_index, cached_pointer_count_); | 283 DCHECK_LT(pointer_index, cached_pointer_count_); |
263 if (pointer_index < MAX_POINTERS_TO_CACHE) | 284 if (pointer_index < MAX_POINTERS_TO_CACHE) |
264 return cached_touch_majors_[pointer_index]; | 285 return cached_touch_majors_[pointer_index]; |
265 return ToDips(Java_MotionEvent_getTouchMajorF_I( | 286 return ToDips(Java_MotionEvent_getTouchMajorF_I( |
266 AttachCurrentThread(), event_.obj(), pointer_index)); | 287 AttachCurrentThread(), event_.obj(), pointer_index)); |
267 } | 288 } |
268 | 289 |
| 290 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { |
| 291 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 292 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 293 return cached_touch_minors_[pointer_index]; |
| 294 return ToDips(Java_MotionEvent_getTouchMinorF_I(AttachCurrentThread(), |
| 295 event_.obj(), pointer_index)); |
| 296 } |
| 297 |
| 298 float MotionEventAndroid::GetOrientation(size_t pointer_index) const { |
| 299 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 300 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 301 return cached_orientations_[pointer_index]; |
| 302 return ToValidFloat(Java_MotionEvent_getOrientationF_I( |
| 303 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 304 } |
| 305 |
269 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 306 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
270 DCHECK_LT(pointer_index, cached_pointer_count_); | 307 DCHECK_LT(pointer_index, cached_pointer_count_); |
271 return Java_MotionEvent_getPressureF_I( | 308 return Java_MotionEvent_getPressureF_I( |
272 AttachCurrentThread(), event_.obj(), pointer_index); | 309 AttachCurrentThread(), event_.obj(), pointer_index); |
273 } | 310 } |
274 | 311 |
275 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 312 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
276 return cached_time_; | 313 return cached_time_; |
277 } | 314 } |
278 | 315 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 return scoped_ptr<MotionEvent>( | 367 return scoped_ptr<MotionEvent>( |
331 new MotionEventAndroid(pix_to_dip_, | 368 new MotionEventAndroid(pix_to_dip_, |
332 AttachCurrentThread(), | 369 AttachCurrentThread(), |
333 Obtain(GetDownTime(), | 370 Obtain(GetDownTime(), |
334 GetEventTime(), | 371 GetEventTime(), |
335 MotionEventAndroid::ACTION_CANCEL, | 372 MotionEventAndroid::ACTION_CANCEL, |
336 position_pixels.x(), | 373 position_pixels.x(), |
337 position_pixels.y()).obj())); | 374 position_pixels.y()).obj())); |
338 } | 375 } |
339 | 376 |
340 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | |
341 return ToDips(Java_MotionEvent_getTouchMinorF_I( | |
342 AttachCurrentThread(), event_.obj(), pointer_index)); | |
343 } | |
344 | |
345 float MotionEventAndroid::GetOrientation() const { | |
346 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); | |
347 } | |
348 | |
349 base::TimeTicks MotionEventAndroid::GetDownTime() const { | 377 base::TimeTicks MotionEventAndroid::GetDownTime() const { |
350 return FromAndroidTime( | 378 return FromAndroidTime( |
351 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); | 379 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); |
352 } | 380 } |
353 | 381 |
354 float MotionEventAndroid::ToDips(float pixels) const { | 382 float MotionEventAndroid::ToDips(float pixels) const { |
355 return pixels * pix_to_dip_; | 383 return pixels * pix_to_dip_; |
356 } | 384 } |
357 | 385 |
358 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { | 386 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { |
(...skipping 22 matching lines...) Expand all Loading... |
381 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 409 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
382 ToAndroidTime(down_time), | 410 ToAndroidTime(down_time), |
383 ToAndroidTime(event_time), | 411 ToAndroidTime(event_time), |
384 ToAndroidAction(action), | 412 ToAndroidAction(action), |
385 x_pixels, | 413 x_pixels, |
386 y_pixels, | 414 y_pixels, |
387 0); | 415 0); |
388 } | 416 } |
389 | 417 |
390 } // namespace content | 418 } // namespace content |
OLD | NEW |