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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_
10 #include "base/android/scoped_java_ref.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "ui/events/gesture_detection/motion_event.h"
14 #include "ui/gfx/geometry/point_f.h"
18 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
19 class MotionEventAndroid : public ui::MotionEvent {
21 // Forcing the caller to provide all cached values upon construction
22 // eliminates the need to perform a JNI call to retrieve values individually.
23 MotionEventAndroid(JNIEnv* env,
37 jfloat touch_major_1);
38 virtual ~MotionEventAndroid();
40 // ui::MotionEvent methods.
41 virtual Action GetAction() const OVERRIDE;
42 virtual int GetActionIndex() const OVERRIDE;
43 virtual size_t GetPointerCount() const OVERRIDE;
44 virtual int GetPointerId(size_t pointer_index) const OVERRIDE;
45 virtual float GetX(size_t pointer_index) const OVERRIDE;
46 virtual float GetY(size_t pointer_index) const OVERRIDE;
47 virtual float GetTouchMajor(size_t pointer_index) const OVERRIDE;
48 virtual float GetPressure(size_t pointer_index) const OVERRIDE;
49 virtual base::TimeTicks GetEventTime() const OVERRIDE;
50 virtual size_t GetHistorySize() const OVERRIDE;
51 virtual base::TimeTicks GetHistoricalEventTime(
52 size_t historical_index) const OVERRIDE;
53 virtual float GetHistoricalTouchMajor(
55 size_t historical_index) const OVERRIDE;
56 virtual float GetHistoricalX(
58 size_t historical_index) const OVERRIDE;
59 virtual float GetHistoricalY(
61 size_t historical_index) const OVERRIDE;
62 virtual scoped_ptr<MotionEvent> Clone() const OVERRIDE;
63 virtual scoped_ptr<MotionEvent> Cancel() const OVERRIDE;
65 // Additional Android MotionEvent methods.
66 float GetTouchMinor() const { return GetTouchMinor(0); }
67 float GetTouchMinor(size_t pointer_index) const;
68 float GetOrientation() const;
69 base::TimeTicks GetDownTime() const;
71 static bool RegisterMotionEventAndroid(JNIEnv* env);
73 static base::android::ScopedJavaLocalRef<jobject> Obtain(
74 const MotionEventAndroid& event);
75 static base::android::ScopedJavaLocalRef<jobject> Obtain(
76 base::TimeTicks down_time,
77 base::TimeTicks event_time,
84 MotionEventAndroid(JNIEnv* env, jobject event);
85 MotionEventAndroid(const MotionEventAndroid&);
86 MotionEventAndroid& operator=(const MotionEventAndroid&);
88 // Cache pointer coords, id's and major lengths for the most common
89 // touch-related scenarios, i.e., scrolling and pinching. This prevents
90 // redundant JNI fetches for the same bits.
91 enum { MAX_POINTERS_TO_CACHE = 2 };
93 // The Java reference to the underlying MotionEvent.
94 base::android::ScopedJavaGlobalRef<jobject> event_;
96 base::TimeTicks cached_time_;
97 Action cached_action_;
98 size_t cached_pointer_count_;
99 size_t cached_history_size_;
100 int cached_action_index_;
101 gfx::PointF cached_positions_[MAX_POINTERS_TO_CACHE];
102 int cached_pointer_ids_[MAX_POINTERS_TO_CACHE];
103 float cached_touch_majors_[MAX_POINTERS_TO_CACHE];
105 // Whether |event_| should be recycled on destruction. This will only be true
106 // for those events generated via |Obtain(...)|.
107 bool should_recycle_;
110 } // namespace content
112 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_