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.
4
5#include "content/browser/renderer_host/input/synthetic_gesture_target_android.h"
6
7#include "content/browser/android/content_view_core_impl.h"
8#include "content/browser/renderer_host/render_widget_host_impl.h"
9#include "jni/TouchEventSynthesizer_jni.h"
10#include "third_party/WebKit/public/web/WebInputEvent.h"
11#include "ui/gfx/android/view_configuration.h"
12#include "ui/gfx/screen.h"
13
14using blink::WebTouchEvent;
15
16namespace content {
17
18SyntheticGestureTargetAndroid::SyntheticGestureTargetAndroid(
19    RenderWidgetHostImpl* host,
20    base::android::ScopedJavaLocalRef<jobject> touch_event_synthesizer)
21    : SyntheticGestureTargetBase(host),
22      touch_event_synthesizer_(touch_event_synthesizer) {
23  DCHECK(!touch_event_synthesizer_.is_null());
24}
25
26SyntheticGestureTargetAndroid::~SyntheticGestureTargetAndroid() {
27}
28
29bool SyntheticGestureTargetAndroid::RegisterTouchEventSynthesizer(JNIEnv* env) {
30  return RegisterNativesImpl(env);
31}
32
33void SyntheticGestureTargetAndroid::TouchSetPointer(
34    JNIEnv* env, int index, int x, int y, int id) {
35  TRACE_EVENT0("input", "SyntheticGestureTargetAndroid::TouchSetPointer");
36  Java_TouchEventSynthesizer_setPointer(env, touch_event_synthesizer_.obj(),
37                                      index, x, y, id);
38}
39
40void SyntheticGestureTargetAndroid::TouchInject(
41    JNIEnv* env, Action action, int pointer_count, int64 time_in_ms) {
42  TRACE_EVENT0("input", "SyntheticGestureTargetAndroid::TouchInject");
43  Java_TouchEventSynthesizer_inject(env, touch_event_synthesizer_.obj(),
44                                    static_cast<int>(action), pointer_count,
45                                    time_in_ms);
46}
47
48void SyntheticGestureTargetAndroid::DispatchWebTouchEventToPlatform(
49    const blink::WebTouchEvent& web_touch, const ui::LatencyInfo&) {
50  JNIEnv* env = base::android::AttachCurrentThread();
51
52  SyntheticGestureTargetAndroid::Action action =
53      SyntheticGestureTargetAndroid::ActionInvalid;
54  switch (web_touch.type) {
55    case blink::WebInputEvent::TouchStart:
56      action = SyntheticGestureTargetAndroid::ActionStart;
57      break;
58    case blink::WebInputEvent::TouchMove:
59      action = SyntheticGestureTargetAndroid::ActionMove;
60      break;
61    case blink::WebInputEvent::TouchCancel:
62      action = SyntheticGestureTargetAndroid::ActionCancel;
63      break;
64    case blink::WebInputEvent::TouchEnd:
65      action = SyntheticGestureTargetAndroid::ActionEnd;
66      break;
67    default:
68      NOTREACHED();
69  }
70  const unsigned num_touches = web_touch.touchesLength;
71  for (unsigned i = 0; i < num_touches; ++i) {
72    const blink::WebTouchPoint* point = &web_touch.touches[i];
73    TouchSetPointer(env, i, point->position.x, point->position.y, point->id);
74  }
75
76  TouchInject(env, action, num_touches,
77              static_cast<int64>(web_touch.timeStampSeconds * 1000.0));
78}
79
80void SyntheticGestureTargetAndroid::DispatchWebMouseWheelEventToPlatform(
81    const blink::WebMouseWheelEvent& web_wheel, const ui::LatencyInfo&) {
82  CHECK(false);
83}
84
85void SyntheticGestureTargetAndroid::DispatchWebMouseEventToPlatform(
86    const blink::WebMouseEvent& web_mouse, const ui::LatencyInfo&) {
87  CHECK(false);
88}
89
90SyntheticGestureParams::GestureSourceType
91SyntheticGestureTargetAndroid::GetDefaultSyntheticGestureSourceType() const {
92  return SyntheticGestureParams::TOUCH_INPUT;
93}
94
95float SyntheticGestureTargetAndroid::GetTouchSlopInDips() const {
96  float device_scale_factor =
97      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().device_scale_factor();
98  return gfx::ViewConfiguration::GetTouchSlopInPixels() / device_scale_factor;
99}
100
101float SyntheticGestureTargetAndroid::GetMinScalingSpanInDips() const {
102  float device_scale_factor =
103      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().device_scale_factor();
104  return
105      gfx::ViewConfiguration::GetMinScalingSpanInPixels() / device_scale_factor;
106}
107
108}  // namespace content
109