| 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 "ui/events/android/motion_event_android.h" | 5 #include "ui/events/android/motion_event_android.h" |
| 6 | 6 |
| 7 #include <android/input.h> | 7 #include <android/input.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "jni/MotionEvent_jni.h" | 12 #include "jni/MotionEvent_jni.h" |
| 13 #include "ui/events/base_event_utils.h" | 13 #include "ui/events/base_event_utils.h" |
| 14 #include "ui/events/event_constants.h" | 14 #include "ui/events/event_constants.h" |
| 15 #include "ui/events/event_utils.h" | 15 #include "ui/events/event_utils.h" |
| 16 | 16 |
| 17 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
| 18 using base::android::ScopedJavaLocalRef; |
| 18 using namespace JNI_MotionEvent; | 19 using namespace JNI_MotionEvent; |
| 19 | 20 |
| 20 namespace ui { | 21 namespace ui { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 MotionEventAndroid::Action FromAndroidAction(int android_action) { | 24 MotionEventAndroid::Action FromAndroidAction(int android_action) { |
| 24 switch (android_action) { | 25 switch (android_action) { |
| 25 case ACTION_DOWN: | 26 case ACTION_DOWN: |
| 26 return MotionEventAndroid::ACTION_DOWN; | 27 return MotionEventAndroid::ACTION_DOWN; |
| 27 case ACTION_UP: | 28 case ACTION_UP: |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 } | 182 } |
| 182 | 183 |
| 183 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 184 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 184 JNIEnv* env, | 185 JNIEnv* env, |
| 185 jobject event, | 186 jobject event, |
| 186 jlong time_ms, | 187 jlong time_ms, |
| 187 jint android_action, | 188 jint android_action, |
| 188 jint pointer_count, | 189 jint pointer_count, |
| 189 jint history_size, | 190 jint history_size, |
| 190 jint action_index, | 191 jint action_index, |
| 192 jint android_action_button, |
| 191 jint android_button_state, | 193 jint android_button_state, |
| 192 jint android_meta_state, | 194 jint android_meta_state, |
| 193 jfloat raw_offset_x_pixels, | 195 jfloat raw_offset_x_pixels, |
| 194 jfloat raw_offset_y_pixels, | 196 jfloat raw_offset_y_pixels, |
| 195 const Pointer* const pointer0, | 197 const Pointer* const pointer0, |
| 196 const Pointer* const pointer1) | 198 const Pointer* const pointer1) |
| 197 : pix_to_dip_(pix_to_dip), | 199 : pix_to_dip_(pix_to_dip), |
| 198 cached_time_(FromAndroidTime(time_ms)), | 200 cached_time_(FromAndroidTime(time_ms)), |
| 199 cached_action_(FromAndroidAction(android_action)), | 201 cached_action_(FromAndroidAction(android_action)), |
| 200 cached_pointer_count_(pointer_count), | 202 cached_pointer_count_(pointer_count), |
| 201 cached_history_size_(ToValidHistorySize(history_size, cached_action_)), | 203 cached_history_size_(ToValidHistorySize(history_size, cached_action_)), |
| 202 cached_action_index_(action_index), | 204 cached_action_index_(action_index), |
| 205 cached_action_button_(android_action_button), |
| 203 cached_button_state_(FromAndroidButtonState(android_button_state)), | 206 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 204 cached_flags_(ToEventFlags(android_meta_state, android_button_state)), | 207 cached_flags_(ToEventFlags(android_meta_state, android_button_state)), |
| 205 cached_raw_position_offset_(ToDips(raw_offset_x_pixels), | 208 cached_raw_position_offset_(ToDips(raw_offset_x_pixels), |
| 206 ToDips(raw_offset_y_pixels)), | 209 ToDips(raw_offset_y_pixels)), |
| 207 unique_event_id_(ui::GetNextTouchEventId()) { | 210 unique_event_id_(ui::GetNextTouchEventId()) { |
| 208 DCHECK_GT(cached_pointer_count_, 0U); | 211 DCHECK_GT(cached_pointer_count_, 0U); |
| 209 DCHECK(cached_pointer_count_ == 1 || pointer1); | 212 DCHECK(cached_pointer_count_ == 1 || pointer1); |
| 210 | 213 |
| 211 event_.Reset(env, event); | 214 event_.Reset(env, event); |
| 212 if (cached_pointer_count_ > MAX_POINTERS_TO_CACHE || cached_history_size_ > 0) | 215 if (cached_pointer_count_ > MAX_POINTERS_TO_CACHE || cached_history_size_ > 0) |
| 213 DCHECK(event_.obj()); | 216 DCHECK(event_.obj()); |
| 214 | 217 |
| 215 cached_pointers_[0] = FromAndroidPointer(*pointer0); | 218 cached_pointers_[0] = FromAndroidPointer(*pointer0); |
| 216 if (cached_pointer_count_ > 1) | 219 if (cached_pointer_count_ > 1) |
| 217 cached_pointers_[1] = FromAndroidPointer(*pointer1); | 220 cached_pointers_[1] = FromAndroidPointer(*pointer1); |
| 218 } | 221 } |
| 219 | 222 |
| 223 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& e, |
| 224 float x, |
| 225 float y) |
| 226 : event_(e.event_), |
| 227 pix_to_dip_(e.pix_to_dip_), |
| 228 cached_time_(e.cached_time_), |
| 229 cached_action_(e.cached_action_), |
| 230 cached_pointer_count_(e.cached_pointer_count_), |
| 231 cached_history_size_(e.cached_history_size_), |
| 232 cached_action_index_(e.cached_action_index_), |
| 233 cached_action_button_(e.cached_action_button_), |
| 234 cached_button_state_(e.cached_button_state_), |
| 235 cached_flags_(e.cached_flags_), |
| 236 cached_raw_position_offset_(e.cached_raw_position_offset_), |
| 237 unique_event_id_(ui::GetNextTouchEventId()) { |
| 238 for (size_t i = 0; i < cached_pointer_count_; i++) { |
| 239 cached_pointers_[i] = FromCachedPointer(e.cached_pointers_[i], x, y); |
| 240 } |
| 241 } |
| 242 |
| 220 MotionEventAndroid::~MotionEventAndroid() { | 243 MotionEventAndroid::~MotionEventAndroid() { |
| 221 } | 244 } |
| 222 | 245 |
| 223 uint32_t MotionEventAndroid::GetUniqueEventId() const { | 246 uint32_t MotionEventAndroid::GetUniqueEventId() const { |
| 224 return unique_event_id_; | 247 return unique_event_id_; |
| 225 } | 248 } |
| 226 | 249 |
| 227 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { | 250 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { |
| 228 return cached_action_; | 251 return cached_action_; |
| 229 } | 252 } |
| 230 | 253 |
| 254 int MotionEventAndroid::GetActionButton() const { |
| 255 return cached_action_button_; |
| 256 } |
| 257 |
| 258 ScopedJavaLocalRef<jobject> MotionEventAndroid::GetJavaObject() const { |
| 259 return ScopedJavaLocalRef<jobject>(event_); |
| 260 } |
| 261 |
| 231 int MotionEventAndroid::GetActionIndex() const { | 262 int MotionEventAndroid::GetActionIndex() const { |
| 232 DCHECK(cached_action_ == MotionEvent::ACTION_POINTER_UP || | 263 DCHECK(cached_action_ == MotionEvent::ACTION_POINTER_UP || |
| 233 cached_action_ == MotionEvent::ACTION_POINTER_DOWN) | 264 cached_action_ == MotionEvent::ACTION_POINTER_DOWN) |
| 234 << "Invalid action for GetActionIndex(): " << cached_action_; | 265 << "Invalid action for GetActionIndex(): " << cached_action_; |
| 235 DCHECK_GE(cached_action_index_, 0); | 266 DCHECK_GE(cached_action_index_, 0); |
| 236 DCHECK_LT(cached_action_index_, static_cast<int>(cached_pointer_count_)); | 267 DCHECK_LT(cached_action_index_, static_cast<int>(cached_pointer_count_)); |
| 237 return cached_action_index_; | 268 return cached_action_index_; |
| 238 } | 269 } |
| 239 | 270 |
| 240 size_t MotionEventAndroid::GetPointerCount() const { | 271 size_t MotionEventAndroid::GetPointerCount() const { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 result.position = | 412 result.position = |
| 382 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); | 413 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); |
| 383 result.touch_major = ToDips(pointer.touch_major_pixels); | 414 result.touch_major = ToDips(pointer.touch_major_pixels); |
| 384 result.touch_minor = ToDips(pointer.touch_minor_pixels); | 415 result.touch_minor = ToDips(pointer.touch_minor_pixels); |
| 385 result.orientation = ToValidFloat(pointer.orientation_rad); | 416 result.orientation = ToValidFloat(pointer.orientation_rad); |
| 386 result.tilt = ToValidFloat(pointer.tilt_rad); | 417 result.tilt = ToValidFloat(pointer.tilt_rad); |
| 387 result.tool_type = FromAndroidToolType(pointer.tool_type); | 418 result.tool_type = FromAndroidToolType(pointer.tool_type); |
| 388 return result; | 419 return result; |
| 389 } | 420 } |
| 390 | 421 |
| 422 MotionEventAndroid::CachedPointer MotionEventAndroid::FromCachedPointer( |
| 423 const CachedPointer& pointer, |
| 424 float x, |
| 425 float y) const { |
| 426 CachedPointer result; |
| 427 result.id = pointer.id; |
| 428 result.position = gfx::PointF(pointer.position.x() + ToDips(x), |
| 429 pointer.position.y() + ToDips(y)); |
| 430 result.touch_major = pointer.touch_major; |
| 431 result.touch_minor = pointer.touch_minor; |
| 432 result.orientation = pointer.orientation; |
| 433 result.tilt = pointer.tilt; |
| 434 result.tool_type = pointer.tool_type; |
| 435 return result; |
| 436 } |
| 437 |
| 391 } // namespace content | 438 } // namespace content |
| OLD | NEW |