Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: content/browser/renderer_host/input/motion_event_android.cc

Issue 348813002: Extend MotionEvent to have tool type and button state (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 int ToAndroidToolType(MotionEventAndroid::ToolType tool_type) {
58 switch (tool_type) {
59 case MotionEventAndroid::TOOL_TYPE_UNKNOWN:
60 return TOOL_TYPE_UNKNOWN;
61 case MotionEventAndroid::TOOL_TYPE_FINGER:
62 return TOOL_TYPE_FINGER;
63 case MotionEventAndroid::TOOL_TYPE_STYLUS:
64 return TOOL_TYPE_STYLUS;
65 case MotionEventAndroid::TOOL_TYPE_MOUSE:
66 return TOOL_TYPE_MOUSE;
67 default:
68 NOTREACHED() << "Invalid MotionEvent tool type: " << tool_type;
69 };
70 return TOOL_TYPE_UNKNOWN;
71 }
72
73 MotionEventAndroid::ToolType FromAndroidToolType(int android_tool_type) {
74 switch (android_tool_type) {
75 case TOOL_TYPE_UNKNOWN:
76 return MotionEventAndroid::TOOL_TYPE_UNKNOWN;
77 case TOOL_TYPE_FINGER:
78 return MotionEventAndroid::TOOL_TYPE_FINGER;
79 case TOOL_TYPE_STYLUS:
80 return MotionEventAndroid::TOOL_TYPE_STYLUS;
81 case TOOL_TYPE_MOUSE:
82 return MotionEventAndroid::TOOL_TYPE_MOUSE;
83 default:
84 NOTREACHED() << "Invalid Android MotionEvent tool type: "
85 << android_tool_type;
86 };
87 return MotionEventAndroid::TOOL_TYPE_UNKNOWN;
88 }
89
jdduke (slow) 2014/06/20 15:33:25 I would say drop the "ToAndroidToolType" and "ToAn
Changwan Ryu 2014/06/20 21:00:34 Done.
90 int ToAndroidButtonState(int button_state) {
91 int result = 0;
92 if ((button_state & MotionEventAndroid::BUTTON_BACK) != 0)
93 result |= BUTTON_BACK;
94 if ((button_state & MotionEventAndroid::BUTTON_FORWARD) != 0)
95 result |= BUTTON_FORWARD;
96 if ((button_state & MotionEventAndroid::BUTTON_PRIMARY) != 0)
97 result |= BUTTON_PRIMARY;
98 if ((button_state & MotionEventAndroid::BUTTON_SECONDARY) != 0)
99 result |= BUTTON_SECONDARY;
100 if ((button_state & MotionEventAndroid::BUTTON_TERTIARY) != 0)
101 result |= BUTTON_TERTIARY;
102 return result;
103 }
104
105 int FromAndroidButtonState(int button_state) {
106 int result = 0;
107 if ((button_state & BUTTON_BACK) != 0)
108 result |= MotionEventAndroid::BUTTON_BACK;
109 if ((button_state & BUTTON_FORWARD) != 0)
110 result |= MotionEventAndroid::BUTTON_FORWARD;
111 if ((button_state & BUTTON_PRIMARY) != 0)
112 result |= MotionEventAndroid::BUTTON_PRIMARY;
113 if ((button_state & BUTTON_SECONDARY) != 0)
114 result |= MotionEventAndroid::BUTTON_SECONDARY;
115 if ((button_state & BUTTON_TERTIARY) != 0)
116 result |= MotionEventAndroid::BUTTON_TERTIARY;
117 return result;
118 }
119
57 int64 ToAndroidTime(base::TimeTicks time) { 120 int64 ToAndroidTime(base::TimeTicks time) {
58 return (time - base::TimeTicks()).InMilliseconds(); 121 return (time - base::TimeTicks()).InMilliseconds();
59 } 122 }
60 123
61 base::TimeTicks FromAndroidTime(int64 time_ms) { 124 base::TimeTicks FromAndroidTime(int64 time_ms) {
62 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); 125 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms);
63 } 126 }
64 127
65 } // namespace 128 } // namespace
66 129
67 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, 130 MotionEventAndroid::MotionEventAndroid(float pix_to_dip,
68 JNIEnv* env, 131 JNIEnv* env,
69 jobject event, 132 jobject event,
70 jlong time_ms, 133 jlong time_ms,
71 jint android_action, 134 jint android_action,
72 jint pointer_count, 135 jint pointer_count,
73 jint history_size, 136 jint history_size,
74 jint action_index, 137 jint action_index,
75 jfloat pos_x_0_pixels, 138 jfloat pos_x_0_pixels,
76 jfloat pos_y_0_pixels, 139 jfloat pos_y_0_pixels,
77 jfloat pos_x_1_pixels, 140 jfloat pos_x_1_pixels,
78 jfloat pos_y_1_pixels, 141 jfloat pos_y_1_pixels,
79 jint pointer_id_0, 142 jint pointer_id_0,
80 jint pointer_id_1, 143 jint pointer_id_1,
81 jfloat touch_major_0_pixels, 144 jfloat touch_major_0_pixels,
82 jfloat touch_major_1_pixels, 145 jfloat touch_major_1_pixels,
83 jfloat raw_pos_x_pixels, 146 jfloat raw_pos_x_pixels,
84 jfloat raw_pos_y_pixels) 147 jfloat raw_pos_y_pixels,
148 jint android_tool_type_0,
149 jint android_tool_type_1,
150 jint android_button_state)
85 : cached_time_(FromAndroidTime(time_ms)), 151 : cached_time_(FromAndroidTime(time_ms)),
86 cached_action_(FromAndroidAction(android_action)), 152 cached_action_(FromAndroidAction(android_action)),
87 cached_pointer_count_(pointer_count), 153 cached_pointer_count_(pointer_count),
88 cached_history_size_(history_size), 154 cached_history_size_(history_size),
89 cached_action_index_(action_index), 155 cached_action_index_(action_index),
156 cached_button_state_(FromAndroidButtonState(android_button_state)),
90 pix_to_dip_(pix_to_dip), 157 pix_to_dip_(pix_to_dip),
91 should_recycle_(false) { 158 should_recycle_(false) {
92 DCHECK_GT(pointer_count, 0); 159 DCHECK_GT(pointer_count, 0);
93 DCHECK_GE(history_size, 0); 160 DCHECK_GE(history_size, 0);
94 161
95 event_.Reset(env, event); 162 event_.Reset(env, event);
96 DCHECK(event_.obj()); 163 DCHECK(event_.obj());
97 164
98 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); 165 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)); 166 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels));
100 cached_pointer_ids_[0] = pointer_id_0; 167 cached_pointer_ids_[0] = pointer_id_0;
101 cached_pointer_ids_[1] = pointer_id_1; 168 cached_pointer_ids_[1] = pointer_id_1;
102 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); 169 cached_touch_majors_[0] = ToDips(touch_major_0_pixels);
103 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); 170 cached_touch_majors_[1] = ToDips(touch_major_1_pixels);
104 cached_raw_position_offset_ = 171 cached_raw_position_offset_ =
105 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - 172 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) -
106 cached_positions_[0]; 173 cached_positions_[0];
174 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0);
175 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1);
107 } 176 }
108 177
109 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, 178 MotionEventAndroid::MotionEventAndroid(float pix_to_dip,
110 JNIEnv* env, 179 JNIEnv* env,
111 jobject event) 180 jobject event)
112 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), 181 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))),
113 cached_action_( 182 cached_action_(
114 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), 183 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))),
115 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), 184 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)),
116 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), 185 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)),
117 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), 186 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)),
187 cached_button_state_(
188 FromAndroidButtonState(Java_MotionEvent_getButtonState(env, event))),
118 pix_to_dip_(pix_to_dip), 189 pix_to_dip_(pix_to_dip),
119 should_recycle_(true) { 190 should_recycle_(true) {
120 event_.Reset(env, event); 191 event_.Reset(env, event);
121 DCHECK(event_.obj()); 192 DCHECK(event_.obj());
122 193
123 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { 194 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) {
124 if (i < cached_pointer_count_) { 195 if (i < cached_pointer_count_) {
125 cached_positions_[i] = 196 cached_positions_[i] =
126 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), 197 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i),
127 Java_MotionEvent_getYF_I(env, event, i))); 198 Java_MotionEvent_getYF_I(env, event, i)));
128 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); 199 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i);
129 cached_touch_majors_[i] = 200 cached_touch_majors_[i] =
130 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); 201 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i));
202 cached_tool_types_[i] =
203 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i));
131 } else { 204 } else {
132 cached_pointer_ids_[i] = 0; 205 cached_pointer_ids_[i] = 0;
133 cached_touch_majors_[i] = 0.f; 206 cached_touch_majors_[i] = 0.f;
207 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN;
134 } 208 }
135 } 209 }
136 210
137 cached_raw_position_offset_ = 211 cached_raw_position_offset_ =
138 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), 212 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event),
139 Java_MotionEvent_getRawY(env, event))) - 213 Java_MotionEvent_getRawY(env, event))) -
140 cached_positions_[0]; 214 cached_positions_[0];
141 } 215 }
142 216
143 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) 217 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other)
144 : event_(Obtain(other)), 218 : event_(Obtain(other)),
145 cached_time_(other.cached_time_), 219 cached_time_(other.cached_time_),
146 cached_action_(other.cached_action_), 220 cached_action_(other.cached_action_),
147 cached_pointer_count_(other.cached_pointer_count_), 221 cached_pointer_count_(other.cached_pointer_count_),
148 cached_history_size_(other.cached_history_size_), 222 cached_history_size_(other.cached_history_size_),
149 cached_action_index_(other.cached_action_index_), 223 cached_action_index_(other.cached_action_index_),
150 cached_raw_position_offset_(other.cached_raw_position_offset_), 224 cached_raw_position_offset_(other.cached_raw_position_offset_),
225 cached_button_state_(other.cached_button_state_),
151 pix_to_dip_(other.pix_to_dip_), 226 pix_to_dip_(other.pix_to_dip_),
152 should_recycle_(true) { 227 should_recycle_(true) {
153 DCHECK(event_.obj()); 228 DCHECK(event_.obj());
154 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { 229 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) {
155 cached_positions_[i] = other.cached_positions_[i]; 230 cached_positions_[i] = other.cached_positions_[i];
156 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; 231 cached_pointer_ids_[i] = other.cached_pointer_ids_[i];
157 cached_touch_majors_[i] = other.cached_touch_majors_[i]; 232 cached_touch_majors_[i] = other.cached_touch_majors_[i];
233 cached_tool_types_[i] = other.cached_tool_types_[i];
158 } 234 }
159 } 235 }
160 236
161 MotionEventAndroid::~MotionEventAndroid() { 237 MotionEventAndroid::~MotionEventAndroid() {
162 if (should_recycle_) 238 if (should_recycle_)
163 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); 239 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj());
164 } 240 }
165 241
166 int MotionEventAndroid::GetId() const { 242 int MotionEventAndroid::GetId() const {
167 return 0; 243 return 0;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( 327 return ToDips(Java_MotionEvent_getHistoricalXF_I_I(
252 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); 328 AttachCurrentThread(), event_.obj(), pointer_index, historical_index));
253 } 329 }
254 330
255 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, 331 float MotionEventAndroid::GetHistoricalY(size_t pointer_index,
256 size_t historical_index) const { 332 size_t historical_index) const {
257 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( 333 return ToDips(Java_MotionEvent_getHistoricalYF_I_I(
258 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); 334 AttachCurrentThread(), event_.obj(), pointer_index, historical_index));
259 } 335 }
260 336
337 ui::MotionEvent::ToolType MotionEventAndroid::GetToolType(
338 size_t pointer_index) const {
339 DCHECK_LT(pointer_index, cached_pointer_count_);
340 if (pointer_index < MAX_POINTERS_TO_CACHE)
341 return cached_tool_types_[pointer_index];
342 return FromAndroidToolType(Java_MotionEvent_getToolType(
343 AttachCurrentThread(), event_.obj(), pointer_index));
344 }
345
346 int MotionEventAndroid::GetButtonState() const {
347 return cached_button_state_;
348 }
349
261 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { 350 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const {
262 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); 351 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this));
263 } 352 }
264 353
265 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { 354 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const {
266 // The input coordinates to |MotionEventAndroid| are always in device pixels, 355 // The input coordinates to |MotionEventAndroid| are always in device pixels,
267 // but the cached coordinates are in DIPs. 356 // but the cached coordinates are in DIPs.
268 const gfx::PointF position_pixels = 357 const gfx::PointF position_pixels =
269 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); 358 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_);
270 return scoped_ptr<MotionEvent>( 359 return scoped_ptr<MotionEvent>(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), 410 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(),
322 ToAndroidTime(down_time), 411 ToAndroidTime(down_time),
323 ToAndroidTime(event_time), 412 ToAndroidTime(event_time),
324 ToAndroidAction(action), 413 ToAndroidAction(action),
325 x_pixels, 414 x_pixels,
326 y_pixels, 415 y_pixels,
327 0); 416 0);
328 } 417 }
329 418
330 } // namespace content 419 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698