Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / motion_event_android.h
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 "ui/events/gesture_detection/motion_event.h"
14 #include "ui/gfx/geometry/point_f.h"
15
16 namespace content {
17
18 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent.
19 class MotionEventAndroid : public ui::MotionEvent {
20  public:
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,
24                      jobject event,
25                      jlong time_ms,
26                      jint android_action,
27                      jint pointer_count,
28                      jint history_size,
29                      jint action_index,
30                      jfloat pos_x_0,
31                      jfloat pos_y_0,
32                      jfloat pos_x_1,
33                      jfloat pos_y_1,
34                      jint pointer_id_0,
35                      jint pointer_id_1,
36                      jfloat touch_major_0,
37                      jfloat touch_major_1);
38   virtual ~MotionEventAndroid();
39
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(
54       size_t pointer_index,
55       size_t historical_index) const OVERRIDE;
56   virtual float GetHistoricalX(
57       size_t pointer_index,
58       size_t historical_index) const OVERRIDE;
59   virtual float GetHistoricalY(
60       size_t pointer_index,
61       size_t historical_index) const OVERRIDE;
62   virtual scoped_ptr<MotionEvent> Clone() const OVERRIDE;
63   virtual scoped_ptr<MotionEvent> Cancel() const OVERRIDE;
64
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;
70
71   static bool RegisterMotionEventAndroid(JNIEnv* env);
72
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,
78       Action action,
79       float x,
80       float y);
81
82  private:
83   MotionEventAndroid();
84   MotionEventAndroid(JNIEnv* env, jobject event);
85   MotionEventAndroid(const MotionEventAndroid&);
86   MotionEventAndroid& operator=(const MotionEventAndroid&);
87
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 };
92
93   // The Java reference to the underlying MotionEvent.
94   base::android::ScopedJavaGlobalRef<jobject> event_;
95
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];
104
105   // Whether |event_| should be recycled on destruction. This will only be true
106   // for those events generated via |Obtain(...)|.
107   bool should_recycle_;
108 };
109
110 }  // namespace content
111
112 #endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_