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 "webkit/glue/fling_animator_impl_android.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/logging.h" | |
10 | |
11 using namespace base::android; | |
12 | |
13 namespace webkit_glue { | |
14 | |
15 FlingAnimatorImpl::FlingAnimatorImpl() | |
16 : is_active_(false) { | |
17 // hold the global reference of the Java objects. | |
18 JNIEnv* env = AttachCurrentThread(); | |
19 DCHECK(env); | |
20 ScopedJavaLocalRef<jclass> cls(GetClass(env, "android/widget/OverScroller")); | |
21 jmethodID constructor = GetMethodID(env, cls, "<init>", | |
22 "(Landroid/content/Context;)V"); | |
23 ScopedJavaLocalRef<jobject> tmp(env, env->NewObject(cls.obj(), constructor, | |
24 GetApplicationContext())); | |
25 DCHECK(tmp.obj()); | |
26 java_scroller_.Reset(tmp); | |
27 | |
28 fling_method_id_ = GetMethodID(env, cls, "fling", "(IIIIIIIIII)V"); | |
29 abort_method_id_ = GetMethodID(env, cls, "abortAnimation", "()V"); | |
30 compute_method_id_ = GetMethodID(env, cls, "computeScrollOffset", "()Z"); | |
31 getX_method_id_ = GetMethodID(env, cls, "getCurrX", "()I"); | |
32 getY_method_id_ = GetMethodID(env, cls, "getCurrY", "()I"); | |
33 } | |
34 | |
35 FlingAnimatorImpl::~FlingAnimatorImpl() | |
36 { | |
37 } | |
38 | |
39 void FlingAnimatorImpl::startFling(const WebKit::WebFloatPoint& velocity, | |
40 const WebKit::WebRect& range) | |
41 { | |
42 DCHECK(velocity.x || velocity.y); | |
43 if (is_active_) | |
44 cancelFling(); | |
45 | |
46 is_active_ = true; | |
47 | |
48 JNIEnv* env = AttachCurrentThread(); | |
49 env->CallVoidMethod(java_scroller_.obj(), fling_method_id_, 0, 0, | |
50 (int) -velocity.x, (int) -velocity.y, | |
51 range.x, range.x + range.width, | |
52 range.y, range.y + range.height, 0, 0); | |
53 CheckException(env); | |
54 } | |
55 | |
56 void FlingAnimatorImpl::cancelFling() | |
57 { | |
58 if (!is_active_) | |
59 return; | |
60 | |
61 is_active_ = false; | |
62 JNIEnv* env = AttachCurrentThread(); | |
63 env->CallVoidMethod(java_scroller_.obj(), abort_method_id_); | |
64 CheckException(env); | |
65 } | |
66 | |
67 bool FlingAnimatorImpl::updatePosition() | |
68 { | |
69 JNIEnv* env = AttachCurrentThread(); | |
70 bool result = env->CallBooleanMethod(java_scroller_.obj(), | |
71 compute_method_id_); | |
72 CheckException(env); | |
73 return is_active_ = result; | |
74 } | |
75 | |
76 WebKit::WebPoint FlingAnimatorImpl::getCurrentPosition() | |
77 { | |
78 JNIEnv* env = AttachCurrentThread(); | |
79 WebKit::WebPoint position(env->CallIntMethod(java_scroller_.obj(), | |
80 getX_method_id_), | |
81 env->CallIntMethod(java_scroller_.obj(), getY_method_id_)); | |
jamesr
2012/08/09 00:03:41
indentation here seems wrong somehow - maybe a new
| |
82 CheckException(env); | |
83 return position; | |
84 } | |
85 | |
86 } // namespace webkit_glue | |
OLD | NEW |