1 // Copyright 2013 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.
5 #include "content/browser/renderer_host/input/synthetic_gesture_target_android.h"
7 #include "content/browser/android/content_view_core_impl.h"
8 #include "content/browser/renderer_host/render_widget_host_impl.h"
9 #include "jni/MotionEventSynthesizer_jni.h"
10 #include "third_party/WebKit/public/web/WebInputEvent.h"
11 #include "ui/gfx/android/view_configuration.h"
13 using blink::WebTouchEvent
;
17 SyntheticGestureTargetAndroid::SyntheticGestureTargetAndroid(
18 RenderWidgetHostImpl
* host
,
19 base::android::ScopedJavaLocalRef
<jobject
> touch_event_synthesizer
)
20 : SyntheticGestureTargetBase(host
),
21 touch_event_synthesizer_(touch_event_synthesizer
) {
22 DCHECK(!touch_event_synthesizer_
.is_null());
25 SyntheticGestureTargetAndroid::~SyntheticGestureTargetAndroid() {
28 bool SyntheticGestureTargetAndroid::RegisterMotionEventSynthesizer(
30 return RegisterNativesImpl(env
);
33 void SyntheticGestureTargetAndroid::TouchSetPointer(
34 JNIEnv
* env
, int index
, int x
, int y
, int id
) {
35 TRACE_EVENT0("input", "SyntheticGestureTargetAndroid::TouchSetPointer");
36 Java_MotionEventSynthesizer_setPointer(env
, touch_event_synthesizer_
.obj(),
40 void SyntheticGestureTargetAndroid::TouchSetScrollDeltas(
41 JNIEnv
* env
, int x
, int y
, int dx
, int dy
) {
42 TRACE_EVENT0("input", "SyntheticGestureTargetAndroid::TouchSetPointer");
43 Java_MotionEventSynthesizer_setScrollDeltas(
44 env
, touch_event_synthesizer_
.obj(), x
, y
, dx
, dy
);
47 void SyntheticGestureTargetAndroid::TouchInject(
48 JNIEnv
* env
, Action action
, int pointer_count
, int64 time_in_ms
) {
49 TRACE_EVENT0("input", "SyntheticGestureTargetAndroid::TouchInject");
50 Java_MotionEventSynthesizer_inject(env
, touch_event_synthesizer_
.obj(),
51 static_cast<int>(action
), pointer_count
,
55 void SyntheticGestureTargetAndroid::DispatchWebTouchEventToPlatform(
56 const blink::WebTouchEvent
& web_touch
, const ui::LatencyInfo
&) {
57 JNIEnv
* env
= base::android::AttachCurrentThread();
59 SyntheticGestureTargetAndroid::Action action
=
60 SyntheticGestureTargetAndroid::ActionInvalid
;
61 switch (web_touch
.type
) {
62 case blink::WebInputEvent::TouchStart
:
63 action
= SyntheticGestureTargetAndroid::ActionStart
;
65 case blink::WebInputEvent::TouchMove
:
66 action
= SyntheticGestureTargetAndroid::ActionMove
;
68 case blink::WebInputEvent::TouchCancel
:
69 action
= SyntheticGestureTargetAndroid::ActionCancel
;
71 case blink::WebInputEvent::TouchEnd
:
72 action
= SyntheticGestureTargetAndroid::ActionEnd
;
77 const unsigned num_touches
= web_touch
.touchesLength
;
78 for (unsigned i
= 0; i
< num_touches
; ++i
) {
79 const blink::WebTouchPoint
* point
= &web_touch
.touches
[i
];
80 TouchSetPointer(env
, i
, point
->position
.x
, point
->position
.y
, point
->id
);
83 TouchInject(env
, action
, num_touches
,
84 static_cast<int64
>(web_touch
.timeStampSeconds
* 1000.0));
87 void SyntheticGestureTargetAndroid::DispatchWebMouseWheelEventToPlatform(
88 const blink::WebMouseWheelEvent
& web_wheel
, const ui::LatencyInfo
&) {
89 JNIEnv
* env
= base::android::AttachCurrentThread();
90 TouchSetScrollDeltas(env
, web_wheel
.x
, web_wheel
.y
, web_wheel
.deltaX
,
92 Java_MotionEventSynthesizer_inject(
93 env
, touch_event_synthesizer_
.obj(),
94 static_cast<int>(SyntheticGestureTargetAndroid::ActionScroll
),
95 1, static_cast<int64
>(web_wheel
.timeStampSeconds
* 1000.0));
98 void SyntheticGestureTargetAndroid::DispatchWebMouseEventToPlatform(
99 const blink::WebMouseEvent
& web_mouse
, const ui::LatencyInfo
&) {
103 SyntheticGestureParams::GestureSourceType
104 SyntheticGestureTargetAndroid::GetDefaultSyntheticGestureSourceType() const {
105 return SyntheticGestureParams::TOUCH_INPUT
;
108 float SyntheticGestureTargetAndroid::GetTouchSlopInDips() const {
109 // TODO(jdduke): Have all targets use the same ui::GestureConfiguration
111 return gfx::ViewConfiguration::GetTouchSlopInDips();
114 float SyntheticGestureTargetAndroid::GetMinScalingSpanInDips() const {
115 // TODO(jdduke): Have all targets use the same ui::GestureConfiguration
117 return gfx::ViewConfiguration::GetMinScalingSpanInDips();
120 } // namespace content