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