| 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 <android/input.h> | 7 #include <android/input.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| 11 #include "jni/MotionEvent_jni.h" | 11 #include "jni/MotionEvent_jni.h" |
| 12 #include "ui/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
| 13 | 13 |
| 14 using base::android::AttachCurrentThread; | 14 using base::android::AttachCurrentThread; |
| 15 using namespace JNI_MotionEvent; | 15 using namespace JNI_MotionEvent; |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 int ToAndroidAction(MotionEventAndroid::Action action) { | |
| 21 switch (action) { | |
| 22 case MotionEventAndroid::ACTION_DOWN: | |
| 23 return ACTION_DOWN; | |
| 24 case MotionEventAndroid::ACTION_UP: | |
| 25 return ACTION_UP; | |
| 26 case MotionEventAndroid::ACTION_MOVE: | |
| 27 return ACTION_MOVE; | |
| 28 case MotionEventAndroid::ACTION_CANCEL: | |
| 29 return ACTION_CANCEL; | |
| 30 case MotionEventAndroid::ACTION_POINTER_DOWN: | |
| 31 return ACTION_POINTER_DOWN; | |
| 32 case MotionEventAndroid::ACTION_POINTER_UP: | |
| 33 return ACTION_POINTER_UP; | |
| 34 }; | |
| 35 NOTREACHED() << "Invalid Android MotionEvent type for gesture detection: " | |
| 36 << action; | |
| 37 return ACTION_CANCEL; | |
| 38 } | |
| 39 | |
| 40 MotionEventAndroid::Action FromAndroidAction(int android_action) { | 20 MotionEventAndroid::Action FromAndroidAction(int android_action) { |
| 41 switch (android_action) { | 21 switch (android_action) { |
| 42 case ACTION_DOWN: | 22 case ACTION_DOWN: |
| 43 return MotionEventAndroid::ACTION_DOWN; | 23 return MotionEventAndroid::ACTION_DOWN; |
| 44 case ACTION_UP: | 24 case ACTION_UP: |
| 45 return MotionEventAndroid::ACTION_UP; | 25 return MotionEventAndroid::ACTION_UP; |
| 46 case ACTION_MOVE: | 26 case ACTION_MOVE: |
| 47 return MotionEventAndroid::ACTION_MOVE; | 27 return MotionEventAndroid::ACTION_MOVE; |
| 48 case ACTION_CANCEL: | 28 case ACTION_CANCEL: |
| 49 return MotionEventAndroid::ACTION_CANCEL; | 29 return MotionEventAndroid::ACTION_CANCEL; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 flags |= ui::EF_CONTROL_DOWN; | 80 flags |= ui::EF_CONTROL_DOWN; |
| 101 if ((meta_state & AMETA_ALT_ON) != 0) | 81 if ((meta_state & AMETA_ALT_ON) != 0) |
| 102 flags |= ui::EF_ALT_DOWN; | 82 flags |= ui::EF_ALT_DOWN; |
| 103 if ((meta_state & AMETA_META_ON) != 0) | 83 if ((meta_state & AMETA_META_ON) != 0) |
| 104 flags |= ui::EF_COMMAND_DOWN; | 84 flags |= ui::EF_COMMAND_DOWN; |
| 105 if ((meta_state & AMETA_CAPS_LOCK_ON) != 0) | 85 if ((meta_state & AMETA_CAPS_LOCK_ON) != 0) |
| 106 flags |= ui::EF_CAPS_LOCK_DOWN; | 86 flags |= ui::EF_CAPS_LOCK_DOWN; |
| 107 return flags; | 87 return flags; |
| 108 } | 88 } |
| 109 | 89 |
| 110 int64 ToAndroidTime(base::TimeTicks time) { | |
| 111 return (time - base::TimeTicks()).InMilliseconds(); | |
| 112 } | |
| 113 | |
| 114 base::TimeTicks FromAndroidTime(int64 time_ms) { | 90 base::TimeTicks FromAndroidTime(int64 time_ms) { |
| 115 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 91 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
| 116 } | 92 } |
| 117 | 93 |
| 118 float ToValidFloat(float x) { | 94 float ToValidFloat(float x) { |
| 119 if (base::IsNaN(x)) | 95 if (base::IsNaN(x)) |
| 120 return 0.f; | 96 return 0.f; |
| 121 | 97 |
| 122 // Wildly large orientation values have been observed in the wild after device | 98 // Wildly large orientation values have been observed in the wild after device |
| 123 // rotation. There's not much we can do in that case other than simply | 99 // rotation. There's not much we can do in that case other than simply |
| 124 // sanitize results beyond an absurd and arbitrary threshold. | 100 // sanitize results beyond an absurd and arbitrary threshold. |
| 125 if (std::abs(x) > 1e5f) | 101 if (std::abs(x) > 1e5f) |
| 126 return 0.f; | 102 return 0.f; |
| 127 | 103 |
| 128 return x; | 104 return x; |
| 129 } | 105 } |
| 130 | 106 |
| 131 } // namespace | 107 } // namespace |
| 132 | 108 |
| 109 MotionEventAndroid::Pointer::Pointer(jint id, |
| 110 jfloat pos_x_pixels, |
| 111 jfloat pos_y_pixels, |
| 112 jfloat touch_major_pixels, |
| 113 jfloat touch_minor_pixels, |
| 114 jfloat orientation_rad, |
| 115 jint tool_type) |
| 116 : id(id), |
| 117 pos_x_pixels(pos_x_pixels), |
| 118 pos_y_pixels(pos_y_pixels), |
| 119 touch_major_pixels(touch_major_pixels), |
| 120 touch_minor_pixels(touch_minor_pixels), |
| 121 orientation_rad(orientation_rad), |
| 122 tool_type(tool_type) { |
| 123 } |
| 124 |
| 125 MotionEventAndroid::CachedPointer::CachedPointer() |
| 126 : id(0), |
| 127 touch_major(0), |
| 128 touch_minor(0), |
| 129 orientation(0), |
| 130 tool_type(TOOL_TYPE_UNKNOWN) { |
| 131 } |
| 132 |
| 133 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 133 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 134 JNIEnv* env, | 134 JNIEnv* env, |
| 135 jobject event, | 135 jobject event, |
| 136 jlong time_ms, | 136 jlong time_ms, |
| 137 jint android_action, | 137 jint android_action, |
| 138 jint pointer_count, | 138 jint pointer_count, |
| 139 jint history_size, | 139 jint history_size, |
| 140 jint action_index, | 140 jint action_index, |
| 141 jfloat pos_x_0_pixels, | |
| 142 jfloat pos_y_0_pixels, | |
| 143 jfloat pos_x_1_pixels, | |
| 144 jfloat pos_y_1_pixels, | |
| 145 jint pointer_id_0, | |
| 146 jint pointer_id_1, | |
| 147 jfloat touch_major_0_pixels, | |
| 148 jfloat touch_major_1_pixels, | |
| 149 jfloat touch_minor_0_pixels, | |
| 150 jfloat touch_minor_1_pixels, | |
| 151 jfloat orientation_0_rad, | |
| 152 jfloat orientation_1_rad, | |
| 153 jfloat raw_pos_x_pixels, | |
| 154 jfloat raw_pos_y_pixels, | |
| 155 jint android_tool_type_0, | |
| 156 jint android_tool_type_1, | |
| 157 jint android_button_state, | 141 jint android_button_state, |
| 158 jint meta_state) | 142 jint meta_state, |
| 159 : cached_time_(FromAndroidTime(time_ms)), | 143 jfloat raw_offset_x_pixels, |
| 144 jfloat raw_offset_y_pixels, |
| 145 const Pointer& pointer0, |
| 146 const Pointer& pointer1) |
| 147 : pix_to_dip_(pix_to_dip), |
| 148 cached_time_(FromAndroidTime(time_ms)), |
| 160 cached_action_(FromAndroidAction(android_action)), | 149 cached_action_(FromAndroidAction(android_action)), |
| 161 cached_pointer_count_(pointer_count), | 150 cached_pointer_count_(pointer_count), |
| 162 cached_history_size_(history_size), | 151 cached_history_size_(history_size), |
| 163 cached_action_index_(action_index), | 152 cached_action_index_(action_index), |
| 164 cached_button_state_(FromAndroidButtonState(android_button_state)), | 153 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 165 cached_flags_(FromAndroidMetaState(meta_state)), | 154 cached_flags_(FromAndroidMetaState(meta_state)), |
| 166 pix_to_dip_(pix_to_dip), | 155 cached_raw_position_offset_(ToDips(raw_offset_x_pixels), |
| 167 should_recycle_(false) { | 156 ToDips(raw_offset_y_pixels)) { |
| 168 DCHECK_GT(pointer_count, 0); | 157 DCHECK_GT(pointer_count, 0); |
| 169 DCHECK_GE(history_size, 0); | 158 DCHECK_GE(history_size, 0); |
| 170 | 159 |
| 171 event_.Reset(env, event); | 160 event_.Reset(env, event); |
| 172 DCHECK(event_.obj()); | 161 if (cached_pointer_count_ > MAX_POINTERS_TO_CACHE || history_size > 0) |
| 162 DCHECK(event_.obj()); |
| 173 | 163 |
| 174 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 164 cached_pointers_[0] = FromAndroidPointer(pointer0); |
| 175 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 165 cached_pointers_[1] = FromAndroidPointer(pointer1); |
| 176 cached_pointer_ids_[0] = pointer_id_0; | |
| 177 cached_pointer_ids_[1] = pointer_id_1; | |
| 178 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | |
| 179 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | |
| 180 cached_touch_minors_[0] = ToDips(touch_minor_0_pixels); | |
| 181 cached_touch_minors_[1] = ToDips(touch_minor_1_pixels); | |
| 182 cached_orientations_[0] = ToValidFloat(orientation_0_rad); | |
| 183 cached_orientations_[1] = ToValidFloat(orientation_1_rad); | |
| 184 cached_raw_position_offset_ = | |
| 185 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - | |
| 186 cached_positions_[0]; | |
| 187 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); | |
| 188 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); | |
| 189 } | |
| 190 | |
| 191 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | |
| 192 JNIEnv* env, | |
| 193 jobject event) | |
| 194 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), | |
| 195 cached_action_( | |
| 196 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), | |
| 197 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), | |
| 198 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), | |
| 199 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), | |
| 200 cached_button_state_( | |
| 201 FromAndroidButtonState(Java_MotionEvent_getButtonState(env, event))), | |
| 202 cached_flags_( | |
| 203 FromAndroidMetaState(Java_MotionEvent_getMetaState(env, event))), | |
| 204 pix_to_dip_(pix_to_dip), | |
| 205 should_recycle_(true) { | |
| 206 event_.Reset(env, event); | |
| 207 DCHECK(event_.obj()); | |
| 208 | |
| 209 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | |
| 210 if (i < cached_pointer_count_) { | |
| 211 cached_positions_[i] = | |
| 212 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | |
| 213 Java_MotionEvent_getYF_I(env, event, i))); | |
| 214 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | |
| 215 cached_touch_majors_[i] = | |
| 216 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | |
| 217 cached_touch_minors_[i] = | |
| 218 ToDips(Java_MotionEvent_getTouchMinorF_I(env, event, i)); | |
| 219 cached_orientations_[i] = | |
| 220 ToValidFloat(Java_MotionEvent_getOrientationF_I(env, event, i)); | |
| 221 cached_tool_types_[i] = | |
| 222 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); | |
| 223 } else { | |
| 224 cached_pointer_ids_[i] = 0; | |
| 225 cached_touch_majors_[i] = 0.f; | |
| 226 cached_touch_minors_[i] = 0.f; | |
| 227 cached_orientations_[i] = 0.f; | |
| 228 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 cached_raw_position_offset_ = | |
| 233 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), | |
| 234 Java_MotionEvent_getRawY(env, event))) - | |
| 235 cached_positions_[0]; | |
| 236 } | |
| 237 | |
| 238 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | |
| 239 : event_(Obtain(other)), | |
| 240 cached_time_(other.cached_time_), | |
| 241 cached_action_(other.cached_action_), | |
| 242 cached_pointer_count_(other.cached_pointer_count_), | |
| 243 cached_history_size_(other.cached_history_size_), | |
| 244 cached_action_index_(other.cached_action_index_), | |
| 245 cached_raw_position_offset_(other.cached_raw_position_offset_), | |
| 246 cached_button_state_(other.cached_button_state_), | |
| 247 cached_flags_(other.cached_flags_), | |
| 248 pix_to_dip_(other.pix_to_dip_), | |
| 249 should_recycle_(true) { | |
| 250 DCHECK(event_.obj()); | |
| 251 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | |
| 252 cached_positions_[i] = other.cached_positions_[i]; | |
| 253 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | |
| 254 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | |
| 255 cached_touch_minors_[i] = other.cached_touch_minors_[i]; | |
| 256 cached_orientations_[i] = other.cached_orientations_[i]; | |
| 257 cached_tool_types_[i] = other.cached_tool_types_[i]; | |
| 258 } | |
| 259 } | 166 } |
| 260 | 167 |
| 261 MotionEventAndroid::~MotionEventAndroid() { | 168 MotionEventAndroid::~MotionEventAndroid() { |
| 262 if (should_recycle_) | |
| 263 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | |
| 264 } | 169 } |
| 265 | 170 |
| 266 int MotionEventAndroid::GetId() const { | 171 int MotionEventAndroid::GetId() const { |
| 267 return 0; | 172 return 0; |
| 268 } | 173 } |
| 269 | 174 |
| 270 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { | 175 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { |
| 271 return cached_action_; | 176 return cached_action_; |
| 272 } | 177 } |
| 273 | 178 |
| 274 int MotionEventAndroid::GetActionIndex() const { | 179 int MotionEventAndroid::GetActionIndex() const { |
| 275 return cached_action_index_; | 180 return cached_action_index_; |
| 276 } | 181 } |
| 277 | 182 |
| 278 size_t MotionEventAndroid::GetPointerCount() const { | 183 size_t MotionEventAndroid::GetPointerCount() const { |
| 279 return cached_pointer_count_; | 184 return cached_pointer_count_; |
| 280 } | 185 } |
| 281 | 186 |
| 282 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { | 187 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { |
| 283 DCHECK_LT(pointer_index, cached_pointer_count_); | 188 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 284 if (pointer_index < MAX_POINTERS_TO_CACHE) | 189 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 285 return cached_pointer_ids_[pointer_index]; | 190 return cached_pointers_[pointer_index].id; |
| 286 return Java_MotionEvent_getPointerId( | 191 return Java_MotionEvent_getPointerId( |
| 287 AttachCurrentThread(), event_.obj(), pointer_index); | 192 AttachCurrentThread(), event_.obj(), pointer_index); |
| 288 } | 193 } |
| 289 | 194 |
| 290 float MotionEventAndroid::GetX(size_t pointer_index) const { | 195 float MotionEventAndroid::GetX(size_t pointer_index) const { |
| 291 DCHECK_LT(pointer_index, cached_pointer_count_); | 196 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 292 if (pointer_index < MAX_POINTERS_TO_CACHE) | 197 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 293 return cached_positions_[pointer_index].x(); | 198 return cached_pointers_[pointer_index].position.x(); |
| 294 return ToDips(Java_MotionEvent_getXF_I( | 199 return ToDips(Java_MotionEvent_getXF_I( |
| 295 AttachCurrentThread(), event_.obj(), pointer_index)); | 200 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 296 } | 201 } |
| 297 | 202 |
| 298 float MotionEventAndroid::GetY(size_t pointer_index) const { | 203 float MotionEventAndroid::GetY(size_t pointer_index) const { |
| 299 DCHECK_LT(pointer_index, cached_pointer_count_); | 204 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 300 if (pointer_index < MAX_POINTERS_TO_CACHE) | 205 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 301 return cached_positions_[pointer_index].y(); | 206 return cached_pointers_[pointer_index].position.y(); |
| 302 return ToDips(Java_MotionEvent_getYF_I( | 207 return ToDips(Java_MotionEvent_getYF_I( |
| 303 AttachCurrentThread(), event_.obj(), pointer_index)); | 208 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 304 } | 209 } |
| 305 | 210 |
| 306 float MotionEventAndroid::GetRawX(size_t pointer_index) const { | 211 float MotionEventAndroid::GetRawX(size_t pointer_index) const { |
| 307 return GetX(pointer_index) + cached_raw_position_offset_.x(); | 212 return GetX(pointer_index) + cached_raw_position_offset_.x(); |
| 308 } | 213 } |
| 309 | 214 |
| 310 float MotionEventAndroid::GetRawY(size_t pointer_index) const { | 215 float MotionEventAndroid::GetRawY(size_t pointer_index) const { |
| 311 return GetY(pointer_index) + cached_raw_position_offset_.y(); | 216 return GetY(pointer_index) + cached_raw_position_offset_.y(); |
| 312 } | 217 } |
| 313 | 218 |
| 314 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 219 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
| 315 DCHECK_LT(pointer_index, cached_pointer_count_); | 220 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 316 if (pointer_index < MAX_POINTERS_TO_CACHE) | 221 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 317 return cached_touch_majors_[pointer_index]; | 222 return cached_pointers_[pointer_index].touch_major; |
| 318 return ToDips(Java_MotionEvent_getTouchMajorF_I( | 223 return ToDips(Java_MotionEvent_getTouchMajorF_I( |
| 319 AttachCurrentThread(), event_.obj(), pointer_index)); | 224 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 320 } | 225 } |
| 321 | 226 |
| 322 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | 227 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { |
| 323 DCHECK_LT(pointer_index, cached_pointer_count_); | 228 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 324 if (pointer_index < MAX_POINTERS_TO_CACHE) | 229 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 325 return cached_touch_minors_[pointer_index]; | 230 return cached_pointers_[pointer_index].touch_minor; |
| 326 return ToDips(Java_MotionEvent_getTouchMinorF_I(AttachCurrentThread(), | 231 return ToDips(Java_MotionEvent_getTouchMinorF_I( |
| 327 event_.obj(), pointer_index)); | 232 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 328 } | 233 } |
| 329 | 234 |
| 330 float MotionEventAndroid::GetOrientation(size_t pointer_index) const { | 235 float MotionEventAndroid::GetOrientation(size_t pointer_index) const { |
| 331 DCHECK_LT(pointer_index, cached_pointer_count_); | 236 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 332 if (pointer_index < MAX_POINTERS_TO_CACHE) | 237 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 333 return cached_orientations_[pointer_index]; | 238 return cached_pointers_[pointer_index].orientation; |
| 334 return ToValidFloat(Java_MotionEvent_getOrientationF_I( | 239 return ToValidFloat(Java_MotionEvent_getOrientationF_I( |
| 335 AttachCurrentThread(), event_.obj(), pointer_index)); | 240 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 336 } | 241 } |
| 337 | 242 |
| 338 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 243 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
| 339 DCHECK_LT(pointer_index, cached_pointer_count_); | 244 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 245 // Note that this early return is a special case exercised only in testing, as |
| 246 // caching the pressure values is not a worthwhile optimization (they're |
| 247 // accessed at most once per event instance). |
| 248 if (!event_.obj()) |
| 249 return 0.f; |
| 340 return Java_MotionEvent_getPressureF_I( | 250 return Java_MotionEvent_getPressureF_I( |
| 341 AttachCurrentThread(), event_.obj(), pointer_index); | 251 AttachCurrentThread(), event_.obj(), pointer_index); |
| 342 } | 252 } |
| 343 | 253 |
| 344 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 254 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
| 345 return cached_time_; | 255 return cached_time_; |
| 346 } | 256 } |
| 347 | 257 |
| 348 size_t MotionEventAndroid::GetHistorySize() const { | 258 size_t MotionEventAndroid::GetHistorySize() const { |
| 349 return cached_history_size_; | 259 return cached_history_size_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 371 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, | 281 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, |
| 372 size_t historical_index) const { | 282 size_t historical_index) const { |
| 373 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( | 283 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( |
| 374 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 284 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 375 } | 285 } |
| 376 | 286 |
| 377 ui::MotionEvent::ToolType MotionEventAndroid::GetToolType( | 287 ui::MotionEvent::ToolType MotionEventAndroid::GetToolType( |
| 378 size_t pointer_index) const { | 288 size_t pointer_index) const { |
| 379 DCHECK_LT(pointer_index, cached_pointer_count_); | 289 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 380 if (pointer_index < MAX_POINTERS_TO_CACHE) | 290 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 381 return cached_tool_types_[pointer_index]; | 291 return cached_pointers_[pointer_index].tool_type; |
| 382 return FromAndroidToolType(Java_MotionEvent_getToolType( | 292 return FromAndroidToolType(Java_MotionEvent_getToolType( |
| 383 AttachCurrentThread(), event_.obj(), pointer_index)); | 293 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 384 } | 294 } |
| 385 | 295 |
| 386 int MotionEventAndroid::GetButtonState() const { | 296 int MotionEventAndroid::GetButtonState() const { |
| 387 return cached_button_state_; | 297 return cached_button_state_; |
| 388 } | 298 } |
| 389 | 299 |
| 390 int MotionEventAndroid::GetFlags() const { | 300 int MotionEventAndroid::GetFlags() const { |
| 391 return cached_flags_; | 301 return cached_flags_; |
| 392 } | 302 } |
| 393 | 303 |
| 394 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { | |
| 395 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); | |
| 396 } | |
| 397 | |
| 398 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { | |
| 399 // The input coordinates to |MotionEventAndroid| are always in device pixels, | |
| 400 // but the cached coordinates are in DIPs. | |
| 401 const gfx::PointF position_pixels = | |
| 402 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); | |
| 403 return scoped_ptr<MotionEvent>( | |
| 404 new MotionEventAndroid(pix_to_dip_, | |
| 405 AttachCurrentThread(), | |
| 406 Obtain(GetDownTime(), | |
| 407 GetEventTime(), | |
| 408 MotionEventAndroid::ACTION_CANCEL, | |
| 409 position_pixels.x(), | |
| 410 position_pixels.y()).obj())); | |
| 411 } | |
| 412 | |
| 413 base::TimeTicks MotionEventAndroid::GetDownTime() const { | |
| 414 return FromAndroidTime( | |
| 415 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); | |
| 416 } | |
| 417 | |
| 418 float MotionEventAndroid::ToDips(float pixels) const { | 304 float MotionEventAndroid::ToDips(float pixels) const { |
| 419 return pixels * pix_to_dip_; | 305 return pixels * pix_to_dip_; |
| 420 } | 306 } |
| 421 | 307 |
| 422 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { | 308 MotionEventAndroid::CachedPointer MotionEventAndroid::FromAndroidPointer( |
| 423 return gfx::ScalePoint(point_pixels, pix_to_dip_); | 309 const Pointer& pointer) const { |
| 310 CachedPointer result; |
| 311 result.id = pointer.id; |
| 312 result.position = |
| 313 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); |
| 314 result.touch_major = ToDips(pointer.touch_major_pixels); |
| 315 result.touch_minor = ToDips(pointer.touch_minor_pixels); |
| 316 result.orientation = ToValidFloat(pointer.orientation_rad); |
| 317 result.tool_type = FromAndroidToolType(pointer.tool_type); |
| 318 return result; |
| 424 } | 319 } |
| 425 | 320 |
| 426 // static | 321 // static |
| 427 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { | 322 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { |
| 428 return JNI_MotionEvent::RegisterNativesImpl(env); | 323 return JNI_MotionEvent::RegisterNativesImpl(env); |
| 429 } | 324 } |
| 430 | 325 |
| 431 // static | |
| 432 base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain( | |
| 433 const MotionEventAndroid& event) { | |
| 434 return Java_MotionEvent_obtainAVME_AVME(AttachCurrentThread(), | |
| 435 event.event_.obj()); | |
| 436 } | |
| 437 | |
| 438 // static | |
| 439 base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain( | |
| 440 base::TimeTicks down_time, | |
| 441 base::TimeTicks event_time, | |
| 442 Action action, | |
| 443 float x_pixels, | |
| 444 float y_pixels) { | |
| 445 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | |
| 446 ToAndroidTime(down_time), | |
| 447 ToAndroidTime(event_time), | |
| 448 ToAndroidAction(action), | |
| 449 x_pixels, | |
| 450 y_pixels, | |
| 451 0); | |
| 452 } | |
| 453 | |
| 454 } // namespace content | 326 } // namespace content |
| OLD | NEW |