[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / renderer_host / input / motion_event_android.h
blobf4e8c9422ddd7778292257818911c2661c033aac
2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
7 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
9 #include <jni.h>
11 #include "base/android/scoped_java_ref.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "content/common/content_export.h"
15 #include "ui/events/gesture_detection/motion_event.h"
16 #include "ui/gfx/geometry/point_f.h"
18 namespace content {
20 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
21 // All *input* coordinates are in device pixels (as with Android MotionEvent),
22 // while all *output* coordinates are in DIPs (as with WebTouchEvent).
23 class CONTENT_EXPORT MotionEventAndroid : public ui::MotionEvent {
24 public:
25 struct Pointer {
26 Pointer(jint id,
27 jfloat pos_x_pixels,
28 jfloat pos_y_pixels,
29 jfloat touch_major_pixels,
30 jfloat touch_minor_pixels,
31 jfloat orientation_rad,
32 jint tool_type);
33 jint id;
34 jfloat pos_x_pixels;
35 jfloat pos_y_pixels;
36 jfloat touch_major_pixels;
37 jfloat touch_minor_pixels;
38 jfloat orientation_rad;
39 jint tool_type;
42 // Forcing the caller to provide all cached values upon construction
43 // eliminates the need to perform a JNI call to retrieve values individually.
44 MotionEventAndroid(float pix_to_dip,
45 JNIEnv* env,
46 jobject event,
47 jlong time_ms,
48 jint android_action,
49 jint pointer_count,
50 jint history_size,
51 jint action_index,
52 jint android_button_state,
53 jint meta_state,
54 jfloat raw_offset_x_pixels,
55 jfloat raw_offset_y_pixels,
56 const Pointer& pointer0,
57 const Pointer& pointer1);
58 ~MotionEventAndroid() override;
60 // ui::MotionEvent methods.
61 int GetId() const override;
62 Action GetAction() const override;
63 int GetActionIndex() const override;
64 size_t GetPointerCount() const override;
65 int GetPointerId(size_t pointer_index) const override;
66 float GetX(size_t pointer_index) const override;
67 float GetY(size_t pointer_index) const override;
68 float GetRawX(size_t pointer_index) const override;
69 float GetRawY(size_t pointer_index) const override;
70 float GetTouchMajor(size_t pointer_index) const override;
71 float GetTouchMinor(size_t pointer_index) const override;
72 float GetOrientation(size_t pointer_index) const override;
73 float GetPressure(size_t pointer_index) const override;
74 base::TimeTicks GetEventTime() const override;
75 size_t GetHistorySize() const override;
76 base::TimeTicks GetHistoricalEventTime(
77 size_t historical_index) const override;
78 float GetHistoricalTouchMajor(size_t pointer_index,
79 size_t historical_index) const override;
80 float GetHistoricalX(size_t pointer_index,
81 size_t historical_index) const override;
82 float GetHistoricalY(size_t pointer_index,
83 size_t historical_index) const override;
84 ToolType GetToolType(size_t pointer_index) const override;
85 int GetButtonState() const override;
86 int GetFlags() const override;
88 static bool RegisterMotionEventAndroid(JNIEnv* env);
90 private:
91 struct CachedPointer;
93 float ToDips(float pixels) const;
94 CachedPointer FromAndroidPointer(const Pointer& pointer) const;
96 // Cache pointer coords, id's and major lengths for the most common
97 // touch-related scenarios, i.e., scrolling and pinching. This prevents
98 // redundant JNI fetches for the same bits.
99 enum { MAX_POINTERS_TO_CACHE = 2 };
101 // The Java reference to the underlying MotionEvent.
102 base::android::ScopedJavaGlobalRef<jobject> event_;
104 // Used to convert pixel coordinates from the Java-backed MotionEvent to
105 // DIP coordinates cached/returned by the MotionEventAndroid.
106 const float pix_to_dip_;
108 const base::TimeTicks cached_time_;
109 const Action cached_action_;
110 const size_t cached_pointer_count_;
111 const size_t cached_history_size_;
112 const int cached_action_index_;
113 const int cached_button_state_;
114 const int cached_flags_;
115 const gfx::Vector2dF cached_raw_position_offset_;
116 struct CachedPointer {
117 CachedPointer();
118 int id;
119 gfx::PointF position;
120 float touch_major;
121 float touch_minor;
122 float orientation;
123 ToolType tool_type;
124 } cached_pointers_[MAX_POINTERS_TO_CACHE];
126 DISALLOW_COPY_AND_ASSIGN(MotionEventAndroid);
129 } // namespace content
131 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_