| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2012 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 #include "content/browser/android/touch_point.h" | 
|  | 6 | 
|  | 7 #include "base/debug/debugger.h" | 
|  | 8 #include "base/time.h" | 
|  | 9 #include "base/logging.h" | 
|  | 10 | 
|  | 11 #include "jni/touch_point_jni.h" | 
|  | 12 | 
|  | 13 using WebKit::WebTouchEvent; | 
|  | 14 using WebKit::WebTouchPoint; | 
|  | 15 | 
|  | 16 namespace { | 
|  | 17 | 
|  | 18 void MaybeAddTouchPoint(JNIEnv* env, jobject pt, WebKit::WebTouchEvent& event) { | 
|  | 19   WebTouchPoint::State state = static_cast<WebTouchPoint::State>( | 
|  | 20       Java_TouchPoint_getState(env, pt)); | 
|  | 21   if (state == WebTouchPoint::StateUndefined) | 
|  | 22     return; | 
|  | 23 | 
|  | 24   // When generating a cancel event from an event of a different type, the | 
|  | 25   // touch points are out of sync, so we ensure the points are marked as | 
|  | 26   // canceled as well. | 
|  | 27   if (event.type == WebTouchEvent::TouchCancel) | 
|  | 28     state = WebTouchPoint::StateCancelled; | 
|  | 29 | 
|  | 30   // Record the current number of points in the WebTouchEvent | 
|  | 31   const int idx = event.touchesLength; | 
|  | 32   DCHECK_LT(idx, WebKit::WebTouchEvent::touchesLengthCap); | 
|  | 33 | 
|  | 34   WebTouchPoint wtp; | 
|  | 35   wtp.id = Java_TouchPoint_getId(env, pt); | 
|  | 36   wtp.state = state; | 
|  | 37   wtp.position.x = Java_TouchPoint_getX(env, pt); | 
|  | 38   wtp.position.y = Java_TouchPoint_getY(env, pt); | 
|  | 39   // TODO(joth): Raw event co-ordinates. | 
|  | 40   wtp.screenPosition = wtp.position; | 
|  | 41   wtp.force = Java_TouchPoint_getPressure(env, pt); | 
|  | 42 | 
|  | 43   // TODO(djsollen): WebKit stores touch point size as a pair of radii, which | 
|  | 44   // are integers.  We receive touch point size from Android as a float | 
|  | 45   // between 0 and 1 and interpret 'size' as an elliptical area.  We convert | 
|  | 46   // size to a radius and then scale up to avoid truncating away all of the | 
|  | 47   // data. W3C spec is for the radii to be in units of screen pixels. Need to | 
|  | 48   // change. | 
|  | 49   const static double PI = 3.1415926; | 
|  | 50   const static double SCALE_FACTOR = 1024.0; | 
|  | 51   const int radius = static_cast<int>( | 
|  | 52       (sqrt(Java_TouchPoint_getSize(env, pt)) / PI) * SCALE_FACTOR); | 
|  | 53   wtp.radiusX = radius; | 
|  | 54   wtp.radiusY = radius; | 
|  | 55   // Since our radii are equal, a rotation angle doesn't mean anything. | 
|  | 56   wtp.rotationAngle = 0.0; | 
|  | 57 | 
|  | 58   // Add the newly created WebTouchPoint to the event | 
|  | 59   event.touches[idx] = wtp; | 
|  | 60   ++(event.touchesLength); | 
|  | 61 } | 
|  | 62 | 
|  | 63 }  // namespace | 
|  | 64 | 
|  | 65 namespace content { | 
|  | 66 | 
|  | 67 void TouchPoint::BuildWebTouchEvent(JNIEnv* env, jint type, jlong time_ms, | 
|  | 68     jobjectArray pts, WebKit::WebTouchEvent& event) { | 
|  | 69   event.type = static_cast<WebTouchEvent::Type>(type); | 
|  | 70   event.timeStampSeconds = | 
|  | 71       static_cast<double>(time_ms) / base::Time::kMillisecondsPerSecond; | 
|  | 72   int arrayLength = env->GetArrayLength(pts); | 
|  | 73   // Loop until either all of the input points have been consumed or the output | 
|  | 74   // array has been filled | 
|  | 75   for (int i = 0; i < arrayLength; i++) { | 
|  | 76     jobject pt = env->GetObjectArrayElement(pts, i); | 
|  | 77     MaybeAddTouchPoint(env, pt, event); | 
|  | 78     if (event.touchesLength >= event.touchesLengthCap) | 
|  | 79       break; | 
|  | 80   } | 
|  | 81   DCHECK_GT(event.touchesLength, 0U); | 
|  | 82 } | 
|  | 83 | 
|  | 84 static void RegisterConstants(JNIEnv* env) { | 
|  | 85    Java_TouchPoint_initializeConstants( | 
|  | 86        env, | 
|  | 87        WebKit::WebTouchEvent::TouchStart, | 
|  | 88        WebKit::WebTouchEvent::TouchMove, | 
|  | 89        WebKit::WebTouchEvent::TouchEnd, | 
|  | 90        WebKit::WebTouchEvent::TouchCancel, | 
|  | 91        WebKit::WebTouchPoint::StateUndefined, | 
|  | 92        WebKit::WebTouchPoint::StateReleased, | 
|  | 93        WebKit::WebTouchPoint::StatePressed, | 
|  | 94        WebKit::WebTouchPoint::StateMoved, | 
|  | 95        WebKit::WebTouchPoint::StateStationary, | 
|  | 96        WebKit::WebTouchPoint::StateCancelled); | 
|  | 97 } | 
|  | 98 | 
|  | 99 bool RegisterTouchPoint(JNIEnv* env) { | 
|  | 100   if (!RegisterNativesImpl(env)) | 
|  | 101     return false; | 
|  | 102 | 
|  | 103   RegisterConstants(env); | 
|  | 104 | 
|  | 105   return true; | 
|  | 106 } | 
|  | 107 | 
|  | 108 }  // namespace content | 
| OLD | NEW | 
|---|