| 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 "jni/MotionEvent_jni.h" | 8 #include "jni/MotionEvent_jni.h" |
| 9 | 9 |
| 10 using base::android::AttachCurrentThread; | 10 using base::android::AttachCurrentThread; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 return MotionEventAndroid::ACTION_POINTER_DOWN; | 47 return MotionEventAndroid::ACTION_POINTER_DOWN; |
| 48 case ACTION_POINTER_UP: | 48 case ACTION_POINTER_UP: |
| 49 return MotionEventAndroid::ACTION_POINTER_UP; | 49 return MotionEventAndroid::ACTION_POINTER_UP; |
| 50 default: | 50 default: |
| 51 NOTREACHED() << "Invalid Android MotionEvent type for gesture detection: " | 51 NOTREACHED() << "Invalid Android MotionEvent type for gesture detection: " |
| 52 << android_action; | 52 << android_action; |
| 53 }; | 53 }; |
| 54 return MotionEventAndroid::ACTION_CANCEL; | 54 return MotionEventAndroid::ACTION_CANCEL; |
| 55 } | 55 } |
| 56 | 56 |
| 57 MotionEventAndroid::ToolType FromAndroidToolType(int android_tool_type) { |
| 58 switch (android_tool_type) { |
| 59 case TOOL_TYPE_UNKNOWN: |
| 60 return MotionEventAndroid::TOOL_TYPE_UNKNOWN; |
| 61 case TOOL_TYPE_FINGER: |
| 62 return MotionEventAndroid::TOOL_TYPE_FINGER; |
| 63 case TOOL_TYPE_STYLUS: |
| 64 return MotionEventAndroid::TOOL_TYPE_STYLUS; |
| 65 case TOOL_TYPE_MOUSE: |
| 66 return MotionEventAndroid::TOOL_TYPE_MOUSE; |
| 67 default: |
| 68 NOTREACHED() << "Invalid Android MotionEvent tool type: " |
| 69 << android_tool_type; |
| 70 }; |
| 71 return MotionEventAndroid::TOOL_TYPE_UNKNOWN; |
| 72 } |
| 73 |
| 74 int FromAndroidButtonState(int button_state) { |
| 75 int result = 0; |
| 76 if ((button_state & BUTTON_BACK) != 0) |
| 77 result |= MotionEventAndroid::BUTTON_BACK; |
| 78 if ((button_state & BUTTON_FORWARD) != 0) |
| 79 result |= MotionEventAndroid::BUTTON_FORWARD; |
| 80 if ((button_state & BUTTON_PRIMARY) != 0) |
| 81 result |= MotionEventAndroid::BUTTON_PRIMARY; |
| 82 if ((button_state & BUTTON_SECONDARY) != 0) |
| 83 result |= MotionEventAndroid::BUTTON_SECONDARY; |
| 84 if ((button_state & BUTTON_TERTIARY) != 0) |
| 85 result |= MotionEventAndroid::BUTTON_TERTIARY; |
| 86 return result; |
| 87 } |
| 88 |
| 57 int64 ToAndroidTime(base::TimeTicks time) { | 89 int64 ToAndroidTime(base::TimeTicks time) { |
| 58 return (time - base::TimeTicks()).InMilliseconds(); | 90 return (time - base::TimeTicks()).InMilliseconds(); |
| 59 } | 91 } |
| 60 | 92 |
| 61 base::TimeTicks FromAndroidTime(int64 time_ms) { | 93 base::TimeTicks FromAndroidTime(int64 time_ms) { |
| 62 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 94 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
| 63 } | 95 } |
| 64 | 96 |
| 65 } // namespace | 97 } // namespace |
| 66 | 98 |
| 67 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 99 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 68 JNIEnv* env, | 100 JNIEnv* env, |
| 69 jobject event, | 101 jobject event, |
| 70 jlong time_ms, | 102 jlong time_ms, |
| 71 jint android_action, | 103 jint android_action, |
| 72 jint pointer_count, | 104 jint pointer_count, |
| 73 jint history_size, | 105 jint history_size, |
| 74 jint action_index, | 106 jint action_index, |
| 75 jfloat pos_x_0_pixels, | 107 jfloat pos_x_0_pixels, |
| 76 jfloat pos_y_0_pixels, | 108 jfloat pos_y_0_pixels, |
| 77 jfloat pos_x_1_pixels, | 109 jfloat pos_x_1_pixels, |
| 78 jfloat pos_y_1_pixels, | 110 jfloat pos_y_1_pixels, |
| 79 jint pointer_id_0, | 111 jint pointer_id_0, |
| 80 jint pointer_id_1, | 112 jint pointer_id_1, |
| 81 jfloat touch_major_0_pixels, | 113 jfloat touch_major_0_pixels, |
| 82 jfloat touch_major_1_pixels, | 114 jfloat touch_major_1_pixels, |
| 83 jfloat raw_pos_x_pixels, | 115 jfloat raw_pos_x_pixels, |
| 84 jfloat raw_pos_y_pixels) | 116 jfloat raw_pos_y_pixels, |
| 117 jint android_tool_type_0, |
| 118 jint android_tool_type_1, |
| 119 jint android_button_state) |
| 85 : cached_time_(FromAndroidTime(time_ms)), | 120 : cached_time_(FromAndroidTime(time_ms)), |
| 86 cached_action_(FromAndroidAction(android_action)), | 121 cached_action_(FromAndroidAction(android_action)), |
| 87 cached_pointer_count_(pointer_count), | 122 cached_pointer_count_(pointer_count), |
| 88 cached_history_size_(history_size), | 123 cached_history_size_(history_size), |
| 89 cached_action_index_(action_index), | 124 cached_action_index_(action_index), |
| 125 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 90 pix_to_dip_(pix_to_dip), | 126 pix_to_dip_(pix_to_dip), |
| 91 should_recycle_(false) { | 127 should_recycle_(false) { |
| 92 DCHECK_GT(pointer_count, 0); | 128 DCHECK_GT(pointer_count, 0); |
| 93 DCHECK_GE(history_size, 0); | 129 DCHECK_GE(history_size, 0); |
| 94 | 130 |
| 95 event_.Reset(env, event); | 131 event_.Reset(env, event); |
| 96 DCHECK(event_.obj()); | 132 DCHECK(event_.obj()); |
| 97 | 133 |
| 98 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 134 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); |
| 99 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 135 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
| 100 cached_pointer_ids_[0] = pointer_id_0; | 136 cached_pointer_ids_[0] = pointer_id_0; |
| 101 cached_pointer_ids_[1] = pointer_id_1; | 137 cached_pointer_ids_[1] = pointer_id_1; |
| 102 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | 138 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); |
| 103 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | 139 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); |
| 104 cached_raw_position_offset_ = | 140 cached_raw_position_offset_ = |
| 105 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - | 141 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - |
| 106 cached_positions_[0]; | 142 cached_positions_[0]; |
| 143 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); |
| 144 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); |
| 107 } | 145 } |
| 108 | 146 |
| 109 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 147 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 110 JNIEnv* env, | 148 JNIEnv* env, |
| 111 jobject event) | 149 jobject event) |
| 112 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), | 150 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), |
| 113 cached_action_( | 151 cached_action_( |
| 114 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), | 152 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), |
| 115 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), | 153 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), |
| 116 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), | 154 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), |
| 117 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), | 155 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), |
| 156 cached_button_state_( |
| 157 FromAndroidButtonState(Java_MotionEvent_getButtonState(env, event))), |
| 118 pix_to_dip_(pix_to_dip), | 158 pix_to_dip_(pix_to_dip), |
| 119 should_recycle_(true) { | 159 should_recycle_(true) { |
| 120 event_.Reset(env, event); | 160 event_.Reset(env, event); |
| 121 DCHECK(event_.obj()); | 161 DCHECK(event_.obj()); |
| 122 | 162 |
| 123 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 163 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 124 if (i < cached_pointer_count_) { | 164 if (i < cached_pointer_count_) { |
| 125 cached_positions_[i] = | 165 cached_positions_[i] = |
| 126 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 166 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
| 127 Java_MotionEvent_getYF_I(env, event, i))); | 167 Java_MotionEvent_getYF_I(env, event, i))); |
| 128 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | 168 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
| 129 cached_touch_majors_[i] = | 169 cached_touch_majors_[i] = |
| 130 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | 170 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); |
| 171 cached_tool_types_[i] = |
| 172 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); |
| 131 } else { | 173 } else { |
| 132 cached_pointer_ids_[i] = 0; | 174 cached_pointer_ids_[i] = 0; |
| 133 cached_touch_majors_[i] = 0.f; | 175 cached_touch_majors_[i] = 0.f; |
| 176 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; |
| 134 } | 177 } |
| 135 } | 178 } |
| 136 | 179 |
| 137 cached_raw_position_offset_ = | 180 cached_raw_position_offset_ = |
| 138 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), | 181 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), |
| 139 Java_MotionEvent_getRawY(env, event))) - | 182 Java_MotionEvent_getRawY(env, event))) - |
| 140 cached_positions_[0]; | 183 cached_positions_[0]; |
| 141 } | 184 } |
| 142 | 185 |
| 143 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 186 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
| 144 : event_(Obtain(other)), | 187 : event_(Obtain(other)), |
| 145 cached_time_(other.cached_time_), | 188 cached_time_(other.cached_time_), |
| 146 cached_action_(other.cached_action_), | 189 cached_action_(other.cached_action_), |
| 147 cached_pointer_count_(other.cached_pointer_count_), | 190 cached_pointer_count_(other.cached_pointer_count_), |
| 148 cached_history_size_(other.cached_history_size_), | 191 cached_history_size_(other.cached_history_size_), |
| 149 cached_action_index_(other.cached_action_index_), | 192 cached_action_index_(other.cached_action_index_), |
| 150 cached_raw_position_offset_(other.cached_raw_position_offset_), | 193 cached_raw_position_offset_(other.cached_raw_position_offset_), |
| 194 cached_button_state_(other.cached_button_state_), |
| 151 pix_to_dip_(other.pix_to_dip_), | 195 pix_to_dip_(other.pix_to_dip_), |
| 152 should_recycle_(true) { | 196 should_recycle_(true) { |
| 153 DCHECK(event_.obj()); | 197 DCHECK(event_.obj()); |
| 154 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 198 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 155 cached_positions_[i] = other.cached_positions_[i]; | 199 cached_positions_[i] = other.cached_positions_[i]; |
| 156 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 200 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 157 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 201 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 202 cached_tool_types_[i] = other.cached_tool_types_[i]; |
| 158 } | 203 } |
| 159 } | 204 } |
| 160 | 205 |
| 161 MotionEventAndroid::~MotionEventAndroid() { | 206 MotionEventAndroid::~MotionEventAndroid() { |
| 162 if (should_recycle_) | 207 if (should_recycle_) |
| 163 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 208 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
| 164 } | 209 } |
| 165 | 210 |
| 166 int MotionEventAndroid::GetId() const { | 211 int MotionEventAndroid::GetId() const { |
| 167 return 0; | 212 return 0; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( | 296 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( |
| 252 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 297 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 253 } | 298 } |
| 254 | 299 |
| 255 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, | 300 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, |
| 256 size_t historical_index) const { | 301 size_t historical_index) const { |
| 257 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( | 302 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( |
| 258 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 303 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 259 } | 304 } |
| 260 | 305 |
| 306 ui::MotionEvent::ToolType MotionEventAndroid::GetToolType( |
| 307 size_t pointer_index) const { |
| 308 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 309 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 310 return cached_tool_types_[pointer_index]; |
| 311 return FromAndroidToolType(Java_MotionEvent_getToolType( |
| 312 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 313 } |
| 314 |
| 315 int MotionEventAndroid::GetButtonState() const { |
| 316 return cached_button_state_; |
| 317 } |
| 318 |
| 261 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { | 319 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { |
| 262 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); | 320 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); |
| 263 } | 321 } |
| 264 | 322 |
| 265 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { | 323 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { |
| 266 // The input coordinates to |MotionEventAndroid| are always in device pixels, | 324 // The input coordinates to |MotionEventAndroid| are always in device pixels, |
| 267 // but the cached coordinates are in DIPs. | 325 // but the cached coordinates are in DIPs. |
| 268 const gfx::PointF position_pixels = | 326 const gfx::PointF position_pixels = |
| 269 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); | 327 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); |
| 270 return scoped_ptr<MotionEvent>( | 328 return scoped_ptr<MotionEvent>( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 379 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
| 322 ToAndroidTime(down_time), | 380 ToAndroidTime(down_time), |
| 323 ToAndroidTime(event_time), | 381 ToAndroidTime(event_time), |
| 324 ToAndroidAction(action), | 382 ToAndroidAction(action), |
| 325 x_pixels, | 383 x_pixels, |
| 326 y_pixels, | 384 y_pixels, |
| 327 0); | 385 0); |
| 328 } | 386 } |
| 329 | 387 |
| 330 } // namespace content | 388 } // namespace content |
| OLD | NEW |