| Index: content/browser/renderer_host/input/motion_event_android.cc
|
| diff --git a/content/browser/renderer_host/input/motion_event_android.cc b/content/browser/renderer_host/input/motion_event_android.cc
|
| index d5c3861134ffec4dd273083dc37322c5f637799f..d5b589592c890adabe1f946715adb781b7423ac6 100644
|
| --- a/content/browser/renderer_host/input/motion_event_android.cc
|
| +++ b/content/browser/renderer_host/input/motion_event_android.cc
|
| @@ -13,15 +13,53 @@ using namespace JNI_MotionEvent;
|
| namespace content {
|
| namespace {
|
|
|
| -MotionEventAndroid::Action FromAndroidAction(int android_action) {
|
| - switch (android_action) {
|
| +// Note: These constants are taken directly from android.view.MotionEvent.
|
| +// Do NOT under any circumstances change these values.
|
| +enum AndroidMotionEventAction {
|
| + ANDROID_ACTION_DOWN = 0,
|
| + ANDROID_ACTION_UP = 1,
|
| + ANDROID_ACTION_MOVE = 2,
|
| + ANDROID_ACTION_CANCEL = 3,
|
| + // Purposefully removed, as there is no analogous action in Chromium.
|
| + // ANDROID_ACTION_OUTSIDE = 4,
|
| + ANDROID_ACTION_POINTER_DOWN = 5,
|
| + ANDROID_ACTION_POINTER_UP = 6
|
| +};
|
| +
|
| +AndroidMotionEventAction ToAndroidAction(MotionEventAndroid::Action action) {
|
| + switch (action) {
|
| case MotionEventAndroid::ACTION_DOWN:
|
| + return ANDROID_ACTION_DOWN;
|
| case MotionEventAndroid::ACTION_UP:
|
| + return ANDROID_ACTION_UP;
|
| case MotionEventAndroid::ACTION_MOVE:
|
| + return ANDROID_ACTION_MOVE;
|
| case MotionEventAndroid::ACTION_CANCEL:
|
| + return ANDROID_ACTION_CANCEL;
|
| case MotionEventAndroid::ACTION_POINTER_DOWN:
|
| + return ANDROID_ACTION_POINTER_DOWN;
|
| case MotionEventAndroid::ACTION_POINTER_UP:
|
| - return static_cast<MotionEventAndroid::Action>(android_action);
|
| + return ANDROID_ACTION_POINTER_UP;
|
| + };
|
| + NOTREACHED() << "Invalid Android MotionEvent type for gesture detection: "
|
| + << action;
|
| + return ANDROID_ACTION_CANCEL;
|
| +}
|
| +
|
| +MotionEventAndroid::Action FromAndroidAction(int android_action) {
|
| + switch (android_action) {
|
| + case ANDROID_ACTION_DOWN:
|
| + return MotionEventAndroid::ACTION_DOWN;
|
| + case ANDROID_ACTION_UP:
|
| + return MotionEventAndroid::ACTION_UP;
|
| + case ANDROID_ACTION_MOVE:
|
| + return MotionEventAndroid::ACTION_MOVE;
|
| + case ANDROID_ACTION_CANCEL:
|
| + return MotionEventAndroid::ACTION_CANCEL;
|
| + case ANDROID_ACTION_POINTER_DOWN:
|
| + return MotionEventAndroid::ACTION_POINTER_DOWN;
|
| + case ANDROID_ACTION_POINTER_UP:
|
| + return MotionEventAndroid::ACTION_POINTER_UP;
|
| default:
|
| NOTREACHED() << "Invalid Android MotionEvent type for gesture detection: "
|
| << android_action;
|
| @@ -39,18 +77,36 @@ base::TimeTicks FromAndroidTime(int64 time_ms) {
|
|
|
| } // namespace
|
|
|
| -MotionEventAndroid::MotionEventAndroid(jobject event)
|
| - : should_recycle_(false) {
|
| - event_.Reset(AttachCurrentThread(), event);
|
| +MotionEventAndroid::MotionEventAndroid(JNIEnv* env, jobject event, bool recycle)
|
| + : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))),
|
| + cached_action_(FromAndroidAction(
|
| + Java_MotionEvent_getActionMasked(env, event))),
|
| + cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)),
|
| + cached_history_size_(Java_MotionEvent_getHistorySize(env, event)),
|
| + cached_action_index_(Java_MotionEvent_getActionIndex(env, event)),
|
| + cached_x_(Java_MotionEvent_getXF(env, event)),
|
| + cached_y_(Java_MotionEvent_getYF(env, event)),
|
| + should_recycle_(recycle) {
|
| + event_.Reset(env, event);
|
| DCHECK(event_.obj());
|
| }
|
|
|
| -MotionEventAndroid::MotionEventAndroid(
|
| - const base::android::ScopedJavaLocalRef<jobject>& event,
|
| - bool should_recycle)
|
| - : event_(event),
|
| - should_recycle_(should_recycle) {
|
| - DCHECK(event_.obj());
|
| +MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other,
|
| + bool clone)
|
| + : cached_time_(other.cached_time_),
|
| + cached_action_(other.cached_action_),
|
| + cached_pointer_count_(other.cached_pointer_count_),
|
| + cached_history_size_(other.cached_history_size_),
|
| + cached_action_index_(other.cached_action_index_),
|
| + cached_x_(other.cached_x_),
|
| + cached_y_(other.cached_y_),
|
| + should_recycle_(clone) {
|
| + // An event with a pending recycle should never be copied (only cloned).
|
| + DCHECK(clone || !other.should_recycle_);
|
| + if (clone)
|
| + event_.Reset(Obtain(other));
|
| + else
|
| + event_.Reset(other.event_);
|
| }
|
|
|
| MotionEventAndroid::~MotionEventAndroid() {
|
| @@ -58,65 +114,52 @@ MotionEventAndroid::~MotionEventAndroid() {
|
| Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj());
|
| }
|
|
|
| -MotionEventAndroid::Action MotionEventAndroid::GetActionMasked() const {
|
| - return FromAndroidAction(
|
| - Java_MotionEvent_getActionMasked(AttachCurrentThread(), event_.obj()));
|
| +MotionEventAndroid::Action MotionEventAndroid::GetAction() const {
|
| + return cached_action_;
|
| }
|
|
|
| -size_t MotionEventAndroid::GetActionIndex() const {
|
| - return Java_MotionEvent_getActionIndex(AttachCurrentThread(), event_.obj());
|
| +int MotionEventAndroid::GetActionIndex() const {
|
| + return cached_action_index_;
|
| }
|
|
|
| size_t MotionEventAndroid::GetPointerCount() const {
|
| - return Java_MotionEvent_getPointerCount(AttachCurrentThread(), event_.obj());
|
| + return cached_pointer_count_;
|
| }
|
|
|
| int MotionEventAndroid::GetPointerId(size_t pointer_index) const {
|
| + DCHECK_LT(pointer_index, cached_pointer_count_);
|
| return Java_MotionEvent_getPointerId(
|
| AttachCurrentThread(), event_.obj(), pointer_index);
|
| }
|
|
|
| -float MotionEventAndroid::GetPressure(size_t pointer_index) const {
|
| - return Java_MotionEvent_getPressureF_I(
|
| - AttachCurrentThread(), event_.obj(), pointer_index);
|
| -}
|
| -
|
| float MotionEventAndroid::GetX(size_t pointer_index) const {
|
| + DCHECK_LT(pointer_index, cached_pointer_count_);
|
| + if (pointer_index == 0)
|
| + return cached_x_;
|
| return Java_MotionEvent_getXF_I(
|
| AttachCurrentThread(), event_.obj(), pointer_index);
|
| }
|
|
|
| float MotionEventAndroid::GetY(size_t pointer_index) const {
|
| + DCHECK_LT(pointer_index, cached_pointer_count_);
|
| + if (pointer_index == 0)
|
| + return cached_y_;
|
| return Java_MotionEvent_getYF_I(
|
| AttachCurrentThread(), event_.obj(), pointer_index);
|
| }
|
|
|
| float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const {
|
| + DCHECK_LT(pointer_index, cached_pointer_count_);
|
| return Java_MotionEvent_getTouchMajorF_I(
|
| AttachCurrentThread(), event_.obj(), pointer_index);
|
| }
|
|
|
| -float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const {
|
| - return Java_MotionEvent_getTouchMinorF_I(
|
| - AttachCurrentThread(), event_.obj(), pointer_index);
|
| -}
|
| -
|
| -float MotionEventAndroid::GetOrientation() const {
|
| - return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj());
|
| -}
|
| -
|
| base::TimeTicks MotionEventAndroid::GetEventTime() const {
|
| - return FromAndroidTime(
|
| - Java_MotionEvent_getEventTime(AttachCurrentThread(), event_.obj()));
|
| -}
|
| -
|
| -base::TimeTicks MotionEventAndroid::GetDownTime() const {
|
| - return FromAndroidTime(
|
| - Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj()));
|
| + return cached_time_;
|
| }
|
|
|
| size_t MotionEventAndroid::GetHistorySize() const {
|
| - return Java_MotionEvent_getHistorySize(AttachCurrentThread(), event_.obj());
|
| + return cached_history_size_;
|
| }
|
|
|
| base::TimeTicks MotionEventAndroid::GetHistoricalEventTime(
|
| @@ -144,36 +187,66 @@ float MotionEventAndroid::GetHistoricalY(size_t pointer_index,
|
| AttachCurrentThread(), event_.obj(), pointer_index, historical_index);
|
| }
|
|
|
| +scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const {
|
| + return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this, true));
|
| +}
|
| +
|
| +scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const {
|
| + return scoped_ptr<MotionEvent>(new MotionEventAndroid(
|
| + AttachCurrentThread(),
|
| + Obtain(GetDownTime(),
|
| + GetEventTime(),
|
| + ACTION_CANCEL,
|
| + GetX(0),
|
| + GetY(0)).obj(),
|
| + true));
|
| +}
|
| +
|
| +float MotionEventAndroid::GetPressure(size_t pointer_index) const {
|
| + return Java_MotionEvent_getPressureF_I(
|
| + AttachCurrentThread(), event_.obj(), pointer_index);
|
| +}
|
| +
|
| +float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const {
|
| + return Java_MotionEvent_getTouchMinorF_I(
|
| + AttachCurrentThread(), event_.obj(), pointer_index);
|
| +}
|
| +
|
| +float MotionEventAndroid::GetOrientation() const {
|
| + return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj());
|
| +}
|
| +
|
| +base::TimeTicks MotionEventAndroid::GetDownTime() const {
|
| + return FromAndroidTime(
|
| + Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj()));
|
| +}
|
| +
|
| // static
|
| bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) {
|
| return JNI_MotionEvent::RegisterNativesImpl(env);
|
| }
|
|
|
| // static
|
| -scoped_ptr<MotionEventAndroid> MotionEventAndroid::Obtain(
|
| +base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain(
|
| const MotionEventAndroid& event) {
|
| - return make_scoped_ptr(new MotionEventAndroid(
|
| - Java_MotionEvent_obtainAVME_AVME(AttachCurrentThread(),
|
| - event.event_.obj()),
|
| - true));
|
| + return Java_MotionEvent_obtainAVME_AVME(AttachCurrentThread(),
|
| + event.event_.obj());
|
| }
|
|
|
| // static
|
| -scoped_ptr<MotionEventAndroid> MotionEventAndroid::Obtain(
|
| +base::android::ScopedJavaLocalRef<jobject> MotionEventAndroid::Obtain(
|
| base::TimeTicks down_time,
|
| - base::TimeTicks event__time,
|
| + base::TimeTicks event_time,
|
| Action action,
|
| float x,
|
| float y) {
|
| - return make_scoped_ptr(new MotionEventAndroid(
|
| - Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(),
|
| - ToAndroidTime(down_time),
|
| - ToAndroidTime(down_time),
|
| - action,
|
| - x,
|
| - y,
|
| - 0),
|
| - true));
|
| + return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(),
|
| + ToAndroidTime(down_time),
|
| + ToAndroidTime(event_time),
|
| + ToAndroidAction(action),
|
| + x,
|
| + y,
|
| + 0);
|
| }
|
|
|
| } // namespace content
|
|
|