blob: 361ce3421da39c8b3686c5eafd5373450bf3a102 [file] [log] [blame]
jinsukkim5c3a1202017-03-10 00:02:141// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/android/event_forwarder.h"
6
7#include "jni/EventForwarder_jni.h"
8#include "ui/android/view_android.h"
9#include "ui/android/view_client.h"
10#include "ui/events/android/motion_event_android.h"
11
12namespace ui {
13
14using base::android::JavaParamRef;
15using base::android::ScopedJavaLocalRef;
16
17EventForwarder::EventForwarder(ViewAndroid* view) : view_(view) {}
18
19EventForwarder::~EventForwarder() {
20 if (!java_obj_.is_null()) {
21 Java_EventForwarder_destroy(base::android::AttachCurrentThread(),
22 java_obj_);
23 java_obj_.Reset();
24 }
25}
26
27ScopedJavaLocalRef<jobject> EventForwarder::GetJavaObject() {
28 if (java_obj_.is_null()) {
29 java_obj_.Reset(
30 Java_EventForwarder_create(base::android::AttachCurrentThread(),
31 reinterpret_cast<intptr_t>(this)));
32 }
33 return ScopedJavaLocalRef<jobject>(java_obj_);
34}
35
36jboolean EventForwarder::OnTouchEvent(JNIEnv* env,
37 const JavaParamRef<jobject>& obj,
38 const JavaParamRef<jobject>& motion_event,
39 jlong time_ms,
40 jint android_action,
41 jint pointer_count,
42 jint history_size,
43 jint action_index,
44 jfloat pos_x_0,
45 jfloat pos_y_0,
46 jfloat pos_x_1,
47 jfloat pos_y_1,
48 jint pointer_id_0,
49 jint pointer_id_1,
50 jfloat touch_major_0,
51 jfloat touch_major_1,
52 jfloat touch_minor_0,
53 jfloat touch_minor_1,
54 jfloat orientation_0,
55 jfloat orientation_1,
56 jfloat tilt_0,
57 jfloat tilt_1,
58 jfloat raw_pos_x,
59 jfloat raw_pos_y,
60 jint android_tool_type_0,
61 jint android_tool_type_1,
62 jint android_button_state,
63 jint android_meta_state,
64 jboolean is_touch_handle_event) {
65 ui::MotionEventAndroid::Pointer pointer0(
66 pointer_id_0, pos_x_0, pos_y_0, touch_major_0, touch_minor_0,
67 orientation_0, tilt_0, android_tool_type_0);
68 ui::MotionEventAndroid::Pointer pointer1(
69 pointer_id_1, pos_x_1, pos_y_1, touch_major_1, touch_minor_1,
70 orientation_1, tilt_1, android_tool_type_1);
71 ui::MotionEventAndroid event(
72 1.f / view_->GetDipScale(), env, motion_event.obj(), time_ms,
73 android_action, pointer_count, history_size, action_index,
74 0 /* action_button */, android_button_state, android_meta_state,
75 raw_pos_x - pos_x_0, raw_pos_y - pos_y_0, &pointer0, &pointer1);
76 return view_->OnTouchEvent(event, is_touch_handle_event);
77}
78
79void EventForwarder::OnMouseEvent(JNIEnv* env,
80 const JavaParamRef<jobject>& obj,
81 jlong time_ms,
82 jint android_action,
83 jfloat x,
84 jfloat y,
85 jint pointer_id,
86 jfloat orientation,
87 jfloat pressure,
88 jfloat tilt,
89 jint android_action_button,
90 jint android_button_state,
91 jint android_meta_state,
92 jint android_tool_type) {
93 // Construct a motion_event object minimally, only to convert the raw
94 // parameters to ui::MotionEvent values. Since we used only the cached values
95 // at index=0, it is okay to even pass a null event to the constructor.
96 ui::MotionEventAndroid::Pointer pointer(
97 pointer_id, x, y, 0.0f /* touch_major */, 0.0f /* touch_minor */,
98 orientation, tilt, android_tool_type);
99 ui::MotionEventAndroid event(
100 1.f / view_->GetDipScale(), env, nullptr /* event */, time_ms,
101 android_action, 1 /* pointer_count */, 0 /* history_size */,
102 0 /* action_index */, android_action_button, android_button_state,
103 android_meta_state, 0 /* raw_offset_x_pixels */,
104 0 /* raw_offset_y_pixels */, &pointer, nullptr);
105 view_->OnMouseEvent(event);
106}
107
108bool RegisterEventForwarder(JNIEnv* env) {
109 return RegisterNativesImpl(env);
110}
111
112} // namespace ui