| 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 : event_(e.event_), |
| 225 pix_to_dip_(e.pix_to_dip_), |
| 226 cached_time_(e.cached_time_), |
| 227 cached_action_(e.cached_action_), |
| 228 cached_pointer_count_(e.cached_pointer_count_), |
| 229 cached_history_size_(e.cached_history_size_), |
| 230 cached_action_index_(e.cached_action_index_), |
| 231 cached_action_button_(e.cached_action_button_), |
| 232 cached_button_state_(e.cached_button_state_), |
| 233 cached_flags_(e.cached_flags_), |
| 234 cached_raw_position_offset_(e.cached_raw_position_offset_), |
| 235 unique_event_id_(ui::GetNextTouchEventId()) { |
| 236 for (size_t i = 0; i < cached_pointer_count_; i++) |
| 237 cached_pointers_[i] = e.cached_pointers_[i]; |
| 238 } |
| 239 |
| 240 std::unique_ptr<MotionEventAndroid> MotionEventAndroid::Offset(float x, |
| 241 float y) const { |
| 242 std::unique_ptr<MotionEventAndroid> event(new MotionEventAndroid(*this)); |
| 243 for (size_t i = 0; i < cached_pointer_count_; i++) { |
| 244 event->cached_pointers_[i] = OffsetCachedPointer(cached_pointers_[i], x, y); |
| 245 } |
| 246 return event; |
| 247 } |
| 248 |
| 220 MotionEventAndroid::~MotionEventAndroid() { | 249 MotionEventAndroid::~MotionEventAndroid() { |
| 221 } | 250 } |
| 222 | 251 |
| 223 uint32_t MotionEventAndroid::GetUniqueEventId() const { | 252 uint32_t MotionEventAndroid::GetUniqueEventId() const { |
| 224 return unique_event_id_; | 253 return unique_event_id_; |
| 225 } | 254 } |
| 226 | 255 |
| 227 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { | 256 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { |
| 228 return cached_action_; | 257 return cached_action_; |
| 229 } | 258 } |
| 230 | 259 |
| 260 int MotionEventAndroid::GetActionButton() const { |
| 261 return cached_action_button_; |
| 262 } |
| 263 |
| 264 ScopedJavaLocalRef<jobject> MotionEventAndroid::GetJavaObject() const { |
| 265 return ScopedJavaLocalRef<jobject>(event_); |
| 266 } |
| 267 |
| 231 int MotionEventAndroid::GetActionIndex() const { | 268 int MotionEventAndroid::GetActionIndex() const { |
| 232 DCHECK(cached_action_ == MotionEvent::ACTION_POINTER_UP || | 269 DCHECK(cached_action_ == MotionEvent::ACTION_POINTER_UP || |
| 233 cached_action_ == MotionEvent::ACTION_POINTER_DOWN) | 270 cached_action_ == MotionEvent::ACTION_POINTER_DOWN) |
| 234 << "Invalid action for GetActionIndex(): " << cached_action_; | 271 << "Invalid action for GetActionIndex(): " << cached_action_; |
| 235 DCHECK_GE(cached_action_index_, 0); | 272 DCHECK_GE(cached_action_index_, 0); |
| 236 DCHECK_LT(cached_action_index_, static_cast<int>(cached_pointer_count_)); | 273 DCHECK_LT(cached_action_index_, static_cast<int>(cached_pointer_count_)); |
| 237 return cached_action_index_; | 274 return cached_action_index_; |
| 238 } | 275 } |
| 239 | 276 |
| 240 size_t MotionEventAndroid::GetPointerCount() const { | 277 size_t MotionEventAndroid::GetPointerCount() const { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 result.position = | 418 result.position = |
| 382 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); | 419 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); |
| 383 result.touch_major = ToDips(pointer.touch_major_pixels); | 420 result.touch_major = ToDips(pointer.touch_major_pixels); |
| 384 result.touch_minor = ToDips(pointer.touch_minor_pixels); | 421 result.touch_minor = ToDips(pointer.touch_minor_pixels); |
| 385 result.orientation = ToValidFloat(pointer.orientation_rad); | 422 result.orientation = ToValidFloat(pointer.orientation_rad); |
| 386 result.tilt = ToValidFloat(pointer.tilt_rad); | 423 result.tilt = ToValidFloat(pointer.tilt_rad); |
| 387 result.tool_type = FromAndroidToolType(pointer.tool_type); | 424 result.tool_type = FromAndroidToolType(pointer.tool_type); |
| 388 return result; | 425 return result; |
| 389 } | 426 } |
| 390 | 427 |
| 391 } // namespace content | 428 MotionEventAndroid::CachedPointer MotionEventAndroid::OffsetCachedPointer( |
| 429 const CachedPointer& pointer, |
| 430 float x, |
| 431 float y) const { |
| 432 CachedPointer result; |
| 433 result.id = pointer.id; |
| 434 result.position = gfx::PointF(pointer.position.x() + ToDips(x), |
| 435 pointer.position.y() + ToDips(y)); |
| 436 result.touch_major = pointer.touch_major; |
| 437 result.touch_minor = pointer.touch_minor; |
| 438 result.orientation = pointer.orientation; |
| 439 result.tilt = pointer.tilt; |
| 440 result.tool_type = pointer.tool_type; |
| 441 return result; |
| 442 } |
| 443 |
| 444 } // namespace ui |
| OLD | NEW |