Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(700)

Side by Side Diff: content/browser/android/touch_point.cc

Issue 10532148: Adding java TouchPoint class for android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding native side and JNI binding code Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 void MaybeAddTouchPoint(JNIEnv* env, jobject pt, WebKit::WebTouchEvent& event) {
Yaron 2012/06/14 21:56:27 Put in anon namespace
qinmin 2012/06/14 22:20:58 Done.
17 WebTouchPoint::State state = static_cast<WebTouchPoint::State>(
18 Java_TouchPoint_getState(env, pt));
19 if (state == WebTouchPoint::StateUndefined)
20 return;
21
22 // When generating a cancel event from an event of a different type, the
23 // touch points are out of sync, so we ensure the points are marked as
24 // canceled as well.
25 if (event.type == WebTouchEvent::TouchCancel)
26 state = WebTouchPoint::StateCancelled;
27
28 // Record the current number of points in the WebTouchEvent
29 const int idx = event.touchesLength;
30 DCHECK_LT(idx, WebKit::WebTouchEvent::touchesLengthCap);
31
32 WebTouchPoint wtp;
33 wtp.id = Java_TouchPoint_getId(env, pt);
34 wtp.state = state;
35 wtp.position.x = Java_TouchPoint_getX(env, pt);
36 wtp.position.y = Java_TouchPoint_getY(env, pt);
37 // TODO(joth): Raw event co-ordinates.
38 wtp.screenPosition = wtp.position;
39 wtp.force = Java_TouchPoint_getPressure(env, pt);
40
41 // TODO(djsollen): WebKit stores touch point size as a pair of radii, which
42 // are integers. We receive touch point size from Android as a float
43 // between 0 and 1 and interpret 'size' as an elliptical area. We convert
44 // size to a radius and then scale up to avoid truncating away all of the
45 // data. W3C spec is for the radii to be in units of screen pixels. Need to
46 // change.
47 const static double PI = 3.1415926;
48 const static double SCALE_FACTOR = 1024.0;
49 const int radius = static_cast<int>(
50 (sqrt(Java_TouchPoint_getSize(env, pt)) / PI) * SCALE_FACTOR);
51 wtp.radiusX = radius;
52 wtp.radiusY = radius;
53 // Since our radii are equal, a rotation angle doesn't mean anything.
54 wtp.rotationAngle = 0.0;
55
56 // Add the newly created WebTouchPoint to the event
57 event.touches[idx] = wtp;
58 ++(event.touchesLength);
59 }
60
61 void TouchPoint::BuildWebTouchEvent(JNIEnv* env, jint type, jlong time_ms,
62 jobjectArray pts, WebKit::WebTouchEvent& event) {
63 event.type = static_cast<WebTouchEvent::Type>(type);
64 event.timeStampSeconds =
65 static_cast<double>(time_ms) / base::Time::kMillisecondsPerSecond;
66 int arrayLength = env->GetArrayLength(pts);
67 // Loop until either all of the input points have been consumed or the output
68 // array has been filled
69 for (int i = 0; i < arrayLength; i++) {
70 jobject pt = env->GetObjectArrayElement(pts, i);
71 MaybeAddTouchPoint(env, pt, event);
72 if (event.touchesLength >= event.touchesLengthCap)
73 break;
74 }
75 DCHECK_GT(event.touchesLength, 0U);
76 }
77
78 static void RegisterConstants(JNIEnv* env) {
79 Java_TouchPoint_initializeConstants(
80 env,
81 WebKit::WebTouchEvent::TouchStart,
82 WebKit::WebTouchEvent::TouchMove,
83 WebKit::WebTouchEvent::TouchEnd,
84 WebKit::WebTouchEvent::TouchCancel,
85 WebKit::WebTouchPoint::StateUndefined,
86 WebKit::WebTouchPoint::StateReleased,
87 WebKit::WebTouchPoint::StatePressed,
88 WebKit::WebTouchPoint::StateMoved,
89 WebKit::WebTouchPoint::StateStationary,
90 WebKit::WebTouchPoint::StateCancelled);
91 }
92
93 bool RegisterTouchPoint(JNIEnv* env) {
94 if (!RegisterNativesImpl(env))
95 return false;
96
97 RegisterConstants(env);
98
99 return true;
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698