OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/input/motion_event_android.h" | 5 #include "content/browser/renderer_host/input/motion_event_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "jni/MotionEvent_jni.h" | 8 #include "jni/MotionEvent_jni.h" |
9 | 9 |
10 using base::android::AttachCurrentThread; | 10 using base::android::AttachCurrentThread; |
(...skipping 21 matching lines...) Expand all Loading... |
32 int64 ToAndroidTime(base::TimeTicks time) { | 32 int64 ToAndroidTime(base::TimeTicks time) { |
33 return (time - base::TimeTicks()).InMilliseconds(); | 33 return (time - base::TimeTicks()).InMilliseconds(); |
34 } | 34 } |
35 | 35 |
36 base::TimeTicks FromAndroidTime(int64 time_ms) { | 36 base::TimeTicks FromAndroidTime(int64 time_ms) { |
37 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 37 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
38 } | 38 } |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 MotionEventAndroid::MotionEventAndroid(jobject event) | 42 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, |
43 : should_recycle_(false) { | 43 jobject event, |
44 event_.Reset(AttachCurrentThread(), event); | 44 bool should_recycle) |
| 45 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), |
| 46 cached_action_(FromAndroidAction( |
| 47 Java_MotionEvent_getActionMasked(env, event))), |
| 48 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), |
| 49 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), |
| 50 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), |
| 51 cached_x_(Java_MotionEvent_getXF(env, event)), |
| 52 cached_y_(Java_MotionEvent_getYF(env, event)), |
| 53 should_recycle_(should_recycle) { |
| 54 event_.Reset(env, event); |
45 DCHECK(event_.obj()); | 55 DCHECK(event_.obj()); |
46 } | 56 } |
47 | 57 |
48 MotionEventAndroid::MotionEventAndroid( | 58 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
49 const base::android::ScopedJavaLocalRef<jobject>& event, | 59 : event_(other.event_), |
50 bool should_recycle) | 60 cached_time_(other.cached_time_), |
51 : event_(event), | 61 cached_action_(other.cached_action_), |
52 should_recycle_(should_recycle) { | 62 cached_pointer_count_(other.cached_pointer_count_), |
53 DCHECK(event_.obj()); | 63 cached_history_size_(other.cached_history_size_), |
| 64 cached_action_index_(other.cached_action_index_), |
| 65 cached_x_(other.cached_x_), |
| 66 cached_y_(other.cached_y_), |
| 67 should_recycle_(false) { |
| 68 // An event with a pending recycle should never be copied (only cloned). |
| 69 // TODO(jdduke): Determine if we should support this, and if so, go ahead |
| 70 // perform the implicit clone. |
| 71 DCHECK(!other.should_recycle_); |
| 72 } |
| 73 |
| 74 MotionEventAndroid& MotionEventAndroid::operator=( |
| 75 const MotionEventAndroid& other) { |
| 76 if (should_recycle_) |
| 77 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
| 78 |
| 79 DCHECK(!other.should_recycle_); |
| 80 event_.Reset(other.event_); |
| 81 cached_time_ = other.cached_time_; |
| 82 cached_action_ = other.cached_action_; |
| 83 cached_pointer_count_ = other.cached_pointer_count_; |
| 84 cached_history_size_ = other.cached_history_size_; |
| 85 cached_action_index_ = other.cached_action_index_; |
| 86 cached_x_ = other.cached_x_; |
| 87 cached_y_ = other.cached_y_; |
| 88 should_recycle_ = false; |
| 89 return *this; |
54 } | 90 } |
55 | 91 |
56 MotionEventAndroid::~MotionEventAndroid() { | 92 MotionEventAndroid::~MotionEventAndroid() { |
57 if (should_recycle_) | 93 if (should_recycle_) |
58 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 94 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
59 } | 95 } |
60 | 96 |
61 MotionEventAndroid::Action MotionEventAndroid::GetActionMasked() const { | 97 MotionEventAndroid::Action MotionEventAndroid::GetActionMasked() const { |
62 return FromAndroidAction( | 98 return cached_action_; |
63 Java_MotionEvent_getActionMasked(AttachCurrentThread(), event_.obj())); | |
64 } | 99 } |
65 | 100 |
66 size_t MotionEventAndroid::GetActionIndex() const { | 101 int MotionEventAndroid::GetActionIndex() const { |
67 return Java_MotionEvent_getActionIndex(AttachCurrentThread(), event_.obj()); | 102 return cached_action_index_; |
68 } | 103 } |
69 | 104 |
70 size_t MotionEventAndroid::GetPointerCount() const { | 105 size_t MotionEventAndroid::GetPointerCount() const { |
71 return Java_MotionEvent_getPointerCount(AttachCurrentThread(), event_.obj()); | 106 return cached_pointer_count_; |
72 } | 107 } |
73 | 108 |
74 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { | 109 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { |
75 return Java_MotionEvent_getPointerId( | 110 return Java_MotionEvent_getPointerId( |
76 AttachCurrentThread(), event_.obj(), pointer_index); | 111 AttachCurrentThread(), event_.obj(), pointer_index); |
77 } | 112 } |
78 | 113 |
79 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 114 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
80 return Java_MotionEvent_getPressureF_I( | 115 return Java_MotionEvent_getPressureF_I( |
81 AttachCurrentThread(), event_.obj(), pointer_index); | 116 AttachCurrentThread(), event_.obj(), pointer_index); |
82 } | 117 } |
83 | 118 |
84 float MotionEventAndroid::GetX(size_t pointer_index) const { | 119 float MotionEventAndroid::GetX(size_t pointer_index) const { |
| 120 if (pointer_index == 0) |
| 121 return cached_x_; |
85 return Java_MotionEvent_getXF_I( | 122 return Java_MotionEvent_getXF_I( |
86 AttachCurrentThread(), event_.obj(), pointer_index); | 123 AttachCurrentThread(), event_.obj(), pointer_index); |
87 } | 124 } |
88 | 125 |
89 float MotionEventAndroid::GetY(size_t pointer_index) const { | 126 float MotionEventAndroid::GetY(size_t pointer_index) const { |
| 127 if (pointer_index == 0) |
| 128 return cached_y_; |
90 return Java_MotionEvent_getYF_I( | 129 return Java_MotionEvent_getYF_I( |
91 AttachCurrentThread(), event_.obj(), pointer_index); | 130 AttachCurrentThread(), event_.obj(), pointer_index); |
92 } | 131 } |
93 | 132 |
94 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 133 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
95 return Java_MotionEvent_getTouchMajorF_I( | 134 return Java_MotionEvent_getTouchMajorF_I( |
96 AttachCurrentThread(), event_.obj(), pointer_index); | 135 AttachCurrentThread(), event_.obj(), pointer_index); |
97 } | 136 } |
98 | 137 |
99 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | 138 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { |
100 return Java_MotionEvent_getTouchMinorF_I( | 139 return Java_MotionEvent_getTouchMinorF_I( |
101 AttachCurrentThread(), event_.obj(), pointer_index); | 140 AttachCurrentThread(), event_.obj(), pointer_index); |
102 } | 141 } |
103 | 142 |
104 float MotionEventAndroid::GetOrientation() const { | 143 float MotionEventAndroid::GetOrientation() const { |
105 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); | 144 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); |
106 } | 145 } |
107 | 146 |
108 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 147 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
109 return FromAndroidTime( | 148 return cached_time_; |
110 Java_MotionEvent_getEventTime(AttachCurrentThread(), event_.obj())); | |
111 } | 149 } |
112 | 150 |
113 base::TimeTicks MotionEventAndroid::GetDownTime() const { | 151 base::TimeTicks MotionEventAndroid::GetDownTime() const { |
114 return FromAndroidTime( | 152 return FromAndroidTime( |
115 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); | 153 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); |
116 } | 154 } |
117 | 155 |
118 size_t MotionEventAndroid::GetHistorySize() const { | 156 size_t MotionEventAndroid::GetHistorySize() const { |
119 return Java_MotionEvent_getHistorySize(AttachCurrentThread(), event_.obj()); | 157 return cached_history_size_; |
120 } | 158 } |
121 | 159 |
122 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( | 160 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( |
123 size_t historical_index) const { | 161 size_t historical_index) const { |
124 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( | 162 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( |
125 AttachCurrentThread(), event_.obj(), historical_index)); | 163 AttachCurrentThread(), event_.obj(), historical_index)); |
126 } | 164 } |
127 | 165 |
128 float MotionEventAndroid::GetHistoricalTouchMajor(size_t pointer_index, | 166 float MotionEventAndroid::GetHistoricalTouchMajor(size_t pointer_index, |
129 size_t historical_index) | 167 size_t historical_index) |
(...skipping 13 matching lines...) Expand all Loading... |
143 return Java_MotionEvent_getHistoricalYF_I_I( | 181 return Java_MotionEvent_getHistoricalYF_I_I( |
144 AttachCurrentThread(), event_.obj(), pointer_index, historical_index); | 182 AttachCurrentThread(), event_.obj(), pointer_index, historical_index); |
145 } | 183 } |
146 | 184 |
147 // static | 185 // static |
148 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { | 186 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { |
149 return JNI_MotionEvent::RegisterNativesImpl(env); | 187 return JNI_MotionEvent::RegisterNativesImpl(env); |
150 } | 188 } |
151 | 189 |
152 // static | 190 // static |
153 scoped_ptr<MotionEventAndroid> MotionEventAndroid::Obtain( | 191 base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain( |
154 const MotionEventAndroid& event) { | 192 const MotionEventAndroid& event) { |
155 return make_scoped_ptr(new MotionEventAndroid( | 193 return Java_MotionEvent_obtainAVME_AVME(AttachCurrentThread(), |
156 Java_MotionEvent_obtainAVME_AVME(AttachCurrentThread(), | 194 event.event_.obj()); |
157 event.event_.obj()), | |
158 true)); | |
159 } | 195 } |
160 | 196 |
161 // static | 197 // static |
162 scoped_ptr<MotionEventAndroid> MotionEventAndroid::Obtain( | 198 base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain( |
163 base::TimeTicks down_time, | 199 base::TimeTicks down_time, |
164 base::TimeTicks event__time, | 200 base::TimeTicks event__time, |
165 Action action, | 201 Action action, |
166 float x, | 202 float x, |
167 float y) { | 203 float y) { |
168 return make_scoped_ptr(new MotionEventAndroid( | 204 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
169 Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 205 ToAndroidTime(down_time), |
170 ToAndroidTime(down_time), | 206 ToAndroidTime(down_time), |
171 ToAndroidTime(down_time), | 207 action, |
172 action, | 208 x, |
173 x, | 209 y, |
174 y, | 210 0); |
175 0), | |
176 true)); | |
177 } | 211 } |
178 | 212 |
179 } // namespace content | 213 } // namespace content |
OLD | NEW |