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