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

Unified Diff: ui/android/event_handler.cc

Issue 2708613002: Add EventForwarder for routing touch events (Closed)
Patch Set: EventHandler Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: ui/android/event_handler.cc
diff --git a/ui/android/event_handler.cc b/ui/android/event_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..64112e3350885102ea460399f73c280a49a050da
--- /dev/null
+++ b/ui/android/event_handler.cc
@@ -0,0 +1,125 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/android/event_handler.h"
+
+#include "base/android/jni_android.h"
+#include "base/metrics/histogram_macros.h"
+#include "jni/EventHandler_jni.h"
+#include "ui/android/view_android.h"
+#include "ui/events/android/motion_event_android.h"
+
+namespace ui {
+
+using base::android::JavaParamRef;
+using base::android::ScopedJavaLocalRef;
+
+namespace {
+
+void RecordToolTypeForActionDown(const MotionEventAndroid& event) {
+ MotionEventAndroid::Action action = event.GetAction();
+ if (action == MotionEventAndroid::ACTION_DOWN ||
+ action == MotionEventAndroid::ACTION_POINTER_DOWN ||
+ action == MotionEventAndroid::ACTION_BUTTON_PRESS) {
+ UMA_HISTOGRAM_ENUMERATION("Event.AndroidActionDown.ToolType",
+ event.GetToolType(0),
+ MotionEventAndroid::TOOL_TYPE_LAST + 1);
+ }
+}
+
+} // namespace
+
+EventHandler* EventHandler::Create(ViewAndroid* view) {
+ return new EventHandler(view);
+}
+
+EventHandler::EventHandler(ViewAndroid* view)
+ : view_(view), dip_scale_(view->GetDipScale()) {}
boliu 2017/02/27 19:21:56 don't cache this scale, since it can change at run
Jinsuk Kim 2017/02/28 06:56:04 Done.
+
+ScopedJavaLocalRef<jobject> EventHandler::CreateJavaObject() {
+ return Java_EventHandler_create(base::android::AttachCurrentThread(),
+ reinterpret_cast<intptr_t>(this));
+}
+
+jboolean EventHandler::OnTouchEvent(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ const JavaParamRef<jobject>& motion_event,
+ jlong time_ms,
+ jint android_action,
+ jint pointer_count,
+ jint history_size,
+ jint action_index,
+ jfloat pos_x_0,
+ jfloat pos_y_0,
+ jfloat pos_x_1,
+ jfloat pos_y_1,
+ jint pointer_id_0,
+ jint pointer_id_1,
+ jfloat touch_major_0,
+ jfloat touch_major_1,
+ jfloat touch_minor_0,
+ jfloat touch_minor_1,
+ jfloat orientation_0,
+ jfloat orientation_1,
+ jfloat tilt_0,
+ jfloat tilt_1,
+ jfloat raw_pos_x,
+ jfloat raw_pos_y,
+ jint android_tool_type_0,
+ jint android_tool_type_1,
+ jint android_button_state,
+ jint android_meta_state,
+ jboolean is_touch_handle_event) {
+ MotionEventAndroid::Pointer pointer0(
+ pointer_id_0, pos_x_0, pos_y_0, touch_major_0, touch_minor_0,
+ orientation_0, tilt_0, android_tool_type_0);
+ MotionEventAndroid::Pointer pointer1(
+ pointer_id_1, pos_x_1, pos_y_1, touch_major_1, touch_minor_1,
+ orientation_1, tilt_1, android_tool_type_1);
+ MotionEventAndroid event(1.f / dip_scale_, env, motion_event.obj(), time_ms,
+ android_action, pointer_count, history_size,
+ action_index, android_button_state,
+ android_meta_state, raw_pos_x - pos_x_0,
+ raw_pos_y - pos_y_0, &pointer0, &pointer1);
+
+ RecordToolTypeForActionDown(event);
+ return view_->OnTouchEvent(event, is_touch_handle_event);
+}
+
+jboolean EventHandler::OnMouseEvent(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ jlong time_ms,
+ jint android_action,
+ jfloat x,
+ jfloat y,
+ jint pointer_id,
+ jfloat pressure,
+ jfloat orientation,
+ jfloat tilt,
+ jint android_action_button,
+ jint android_button_state,
+ jint android_meta_state,
+ jint android_tool_type) {
+ // Construct a motion_event object minimally, only to convert the raw
+ // parameters to ui::MotionEvent values. Since we used only the cached values
+ // at index=0, it is okay to even pass a null event to the constructor.
+ MotionEventAndroid::Pointer pointer0(pointer_id, x, y, 0.0f /* touch_major */,
+ 0.0f /* touch_minor */, orientation,
+ tilt, android_tool_type);
+
+ MotionEventAndroid event(
+ 1.f / dip_scale_, env, nullptr /* event */, time_ms, android_action,
+ 1 /* pointer_count */, 0 /* history_size */, 0 /* action_index */,
+ android_button_state, android_meta_state, 0 /* raw_offset_x_pixels */,
+ 0 /* raw_offset_y_pixels */, &pointer0, nullptr);
+
+ RecordToolTypeForActionDown(event);
+ return view_->OnMouseEvent(event, android_action_button);
+}
+
+bool RegisterEventHandler(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698