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
3 // found in the LICENSE file.
4
5 #include "content/browser/android/popup_touch_handle_drawable.h"
6
7 #include "jni/PopupTouchHandleDrawable_jni.h"
8
9 namespace content {
10
PopupTouchHandleDrawable(base::android::ScopedJavaLocalRef<jobject> drawable,float dpi_scale)11 PopupTouchHandleDrawable::PopupTouchHandleDrawable(
12 base::android::ScopedJavaLocalRef<jobject> drawable,
13 float dpi_scale)
14 : dpi_scale_(dpi_scale), drawable_(drawable) {
15 DCHECK(drawable.obj());
16 }
17
~PopupTouchHandleDrawable()18 PopupTouchHandleDrawable::~PopupTouchHandleDrawable() {
19 // Explicitly disabling ensures that any external references to the Java
20 // object are cleared, allowing it to be GC'ed in a timely fashion.
21 SetEnabled(false);
22 }
23
SetEnabled(bool enabled)24 void PopupTouchHandleDrawable::SetEnabled(bool enabled) {
25 JNIEnv* env = base::android::AttachCurrentThread();
26 if (enabled)
27 Java_PopupTouchHandleDrawable_show(env, drawable_.obj());
28 else
29 Java_PopupTouchHandleDrawable_hide(env, drawable_.obj());
30 }
31
SetOrientation(TouchHandleOrientation orientation)32 void PopupTouchHandleDrawable::SetOrientation(
33 TouchHandleOrientation orientation) {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 jobject obj = drawable_.obj();
36 switch (orientation) {
37 case TOUCH_HANDLE_LEFT:
38 Java_PopupTouchHandleDrawable_setLeftOrientation(env, obj);
39 break;
40
41 case TOUCH_HANDLE_RIGHT:
42 Java_PopupTouchHandleDrawable_setRightOrientation(env, obj);
43 break;
44
45 case TOUCH_HANDLE_CENTER:
46 Java_PopupTouchHandleDrawable_setCenterOrientation(env, obj);
47 break;
48
49 case TOUCH_HANDLE_ORIENTATION_UNDEFINED:
50 NOTREACHED() << "Invalid touch handle orientation.";
51 };
52 }
53
SetAlpha(float alpha)54 void PopupTouchHandleDrawable::SetAlpha(float alpha) {
55 JNIEnv* env = base::android::AttachCurrentThread();
56 Java_PopupTouchHandleDrawable_setOpacity(env, drawable_.obj(), alpha);
57 }
58
SetFocus(const gfx::PointF & position)59 void PopupTouchHandleDrawable::SetFocus(const gfx::PointF& position) {
60 const gfx::PointF position_pix = gfx::ScalePoint(position, dpi_scale_);
61 JNIEnv* env = base::android::AttachCurrentThread();
62 Java_PopupTouchHandleDrawable_setFocus(
63 env, drawable_.obj(), position_pix.x(), position_pix.y());
64 }
65
SetVisible(bool visible)66 void PopupTouchHandleDrawable::SetVisible(bool visible) {
67 JNIEnv* env = base::android::AttachCurrentThread();
68 Java_PopupTouchHandleDrawable_setVisible(env, drawable_.obj(), visible);
69 }
70
IntersectsWith(const gfx::RectF & rect) const71 bool PopupTouchHandleDrawable::IntersectsWith(const gfx::RectF& rect) const {
72 const gfx::RectF rect_pix = gfx::ScaleRect(rect, dpi_scale_);
73 JNIEnv* env = base::android::AttachCurrentThread();
74 return Java_PopupTouchHandleDrawable_intersectsWith(env,
75 drawable_.obj(),
76 rect_pix.x(),
77 rect_pix.y(),
78 rect_pix.width(),
79 rect_pix.height());
80 }
81
82 // static
RegisterPopupTouchHandleDrawable(JNIEnv * env)83 bool PopupTouchHandleDrawable::RegisterPopupTouchHandleDrawable(JNIEnv* env) {
84 return RegisterNativesImpl(env);
85 }
86
87 } // namespace content
88