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
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
6#define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
7
8#include <jni.h>
9
10#include "base/android/scoped_java_ref.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/time/time.h"
13#include "content/common/content_export.h"
14#include "ui/events/gesture_detection/motion_event.h"
15#include "ui/gfx/geometry/point_f.h"
16
17namespace content {
18
19// Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
20// All *input* coordinates are in device pixels (as with Android MotionEvent),
21// while all *output* coordinates are in DIPs (as with WebTouchEvent).
22class CONTENT_EXPORT MotionEventAndroid : public ui::MotionEvent {
23 public:
24  // Forcing the caller to provide all cached values upon construction
25  // eliminates the need to perform a JNI call to retrieve values individually.
26  MotionEventAndroid(float pix_to_dip,
27                     JNIEnv* env,
28                     jobject event,
29                     jlong time_ms,
30                     jint android_action,
31                     jint pointer_count,
32                     jint history_size,
33                     jint action_index,
34                     jfloat pos_x_0_pixels,
35                     jfloat pos_y_0_pixels,
36                     jfloat pos_x_1_pixels,
37                     jfloat pos_y_1_pixels,
38                     jint pointer_id_0,
39                     jint pointer_id_1,
40                     jfloat touch_major_0_pixels,
41                     jfloat touch_major_1_pixels,
42                     jfloat touch_minor_0_pixels,
43                     jfloat touch_minor_1_pixels,
44                     jfloat orientation_0_rad,
45                     jfloat orientation_1_rad,
46                     jfloat raw_pos_x_pixels,
47                     jfloat raw_pos_y_pixels,
48                     jint android_tool_type_0,
49                     jint android_tool_type_1,
50                     jint android_button_state,
51                     jint meta_state);
52  virtual ~MotionEventAndroid();
53
54  // ui::MotionEvent methods.
55  virtual int GetId() const OVERRIDE;
56  virtual Action GetAction() const OVERRIDE;
57  virtual int GetActionIndex() const OVERRIDE;
58  virtual size_t GetPointerCount() const OVERRIDE;
59  virtual int GetPointerId(size_t pointer_index) const OVERRIDE;
60  virtual float GetX(size_t pointer_index) const OVERRIDE;
61  virtual float GetY(size_t pointer_index) const OVERRIDE;
62  virtual float GetRawX(size_t pointer_index) const OVERRIDE;
63  virtual float GetRawY(size_t pointer_index) const OVERRIDE;
64  virtual float GetTouchMajor(size_t pointer_index) const OVERRIDE;
65  virtual float GetTouchMinor(size_t pointer_index) const OVERRIDE;
66  virtual float GetOrientation(size_t pointer_index) const OVERRIDE;
67  virtual float GetPressure(size_t pointer_index) const OVERRIDE;
68  virtual base::TimeTicks GetEventTime() const OVERRIDE;
69  virtual size_t GetHistorySize() const OVERRIDE;
70  virtual base::TimeTicks GetHistoricalEventTime(
71      size_t historical_index) const OVERRIDE;
72  virtual float GetHistoricalTouchMajor(size_t pointer_index,
73                                        size_t historical_index) const OVERRIDE;
74  virtual float GetHistoricalX(size_t pointer_index,
75                               size_t historical_index) const OVERRIDE;
76  virtual float GetHistoricalY(size_t pointer_index,
77                               size_t historical_index) const OVERRIDE;
78  virtual ToolType GetToolType(size_t pointer_index) const OVERRIDE;
79  virtual int GetButtonState() const OVERRIDE;
80  virtual int GetFlags() const OVERRIDE;
81  virtual scoped_ptr<MotionEvent> Clone() const OVERRIDE;
82  virtual scoped_ptr<MotionEvent> Cancel() const OVERRIDE;
83
84  // Additional Android MotionEvent methods.
85  base::TimeTicks GetDownTime() const;
86
87  static bool RegisterMotionEventAndroid(JNIEnv* env);
88
89  static base::android::ScopedJavaLocalRef<jobject> Obtain(
90      const MotionEventAndroid& event);
91  static base::android::ScopedJavaLocalRef<jobject> Obtain(
92      base::TimeTicks down_time,
93      base::TimeTicks event_time,
94      Action action,
95      float x_pixels,
96      float y_pixels);
97
98 private:
99  MotionEventAndroid();
100  MotionEventAndroid(float pix_to_dip, JNIEnv* env, jobject event);
101  MotionEventAndroid(const MotionEventAndroid&);
102  MotionEventAndroid& operator=(const MotionEventAndroid&);
103
104  float ToDips(float pixels) const;
105  gfx::PointF ToDips(const gfx::PointF& pixels) const;
106
107  // Cache pointer coords, id's and major lengths for the most common
108  // touch-related scenarios, i.e., scrolling and pinching.  This prevents
109  // redundant JNI fetches for the same bits.
110  enum { MAX_POINTERS_TO_CACHE = 2 };
111
112  // The Java reference to the underlying MotionEvent.
113  base::android::ScopedJavaGlobalRef<jobject> event_;
114
115  base::TimeTicks cached_time_;
116  Action cached_action_;
117  size_t cached_pointer_count_;
118  size_t cached_history_size_;
119  int cached_action_index_;
120  gfx::PointF cached_positions_[MAX_POINTERS_TO_CACHE];
121  int cached_pointer_ids_[MAX_POINTERS_TO_CACHE];
122  float cached_touch_majors_[MAX_POINTERS_TO_CACHE];
123  float cached_touch_minors_[MAX_POINTERS_TO_CACHE];
124  float cached_orientations_[MAX_POINTERS_TO_CACHE];
125  gfx::Vector2dF cached_raw_position_offset_;
126  ToolType cached_tool_types_[MAX_POINTERS_TO_CACHE];
127  int cached_button_state_;
128  int cached_flags_;
129
130  // Used to convert pixel coordinates from the Java-backed MotionEvent to
131  // DIP coordinates cached/returned by the MotionEventAndroid.
132  const float pix_to_dip_;
133
134  // Whether |event_| should be recycled on destruction. This will only be true
135  // for those events generated via |Obtain(...)|.
136  bool should_recycle_;
137};
138
139}  // namespace content
140
141#endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
142