15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file. 45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "content/browser/android/popup_touch_handle_drawable.h" 65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "jni/PopupTouchHandleDrawable_jni.h" 85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace content { 105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)PopupTouchHandleDrawable::PopupTouchHandleDrawable( 125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) base::android::ScopedJavaLocalRef<jobject> drawable, 135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) float dpi_scale) 145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) : dpi_scale_(dpi_scale), drawable_(drawable) { 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) DCHECK(drawable.obj()); 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)PopupTouchHandleDrawable::~PopupTouchHandleDrawable() { 19c016285e1e5f5f035c0063c966ef3d7f29ca550fBen Murdoch // Explicitly disabling ensures that any external references to the Java 20c016285e1e5f5f035c0063c966ef3d7f29ca550fBen Murdoch // object are cleared, allowing it to be GC'ed in a timely fashion. 21c016285e1e5f5f035c0063c966ef3d7f29ca550fBen Murdoch SetEnabled(false); 225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PopupTouchHandleDrawable::SetEnabled(bool enabled) { 255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (enabled) 275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_show(env, drawable_.obj()); 285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) else 295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_hide(env, drawable_.obj()); 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PopupTouchHandleDrawable::SetOrientation( 335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) TouchHandleOrientation orientation) { 345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) jobject obj = drawable_.obj(); 365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) switch (orientation) { 375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) case TOUCH_HANDLE_LEFT: 385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setLeftOrientation(env, obj); 395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) break; 405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) case TOUCH_HANDLE_RIGHT: 425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setRightOrientation(env, obj); 435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) break; 445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) case TOUCH_HANDLE_CENTER: 465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setCenterOrientation(env, obj); 475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) break; 485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) case TOUCH_HANDLE_ORIENTATION_UNDEFINED: 505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) NOTREACHED() << "Invalid touch handle orientation."; 515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) }; 525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PopupTouchHandleDrawable::SetAlpha(float alpha) { 555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setOpacity(env, drawable_.obj(), alpha); 575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PopupTouchHandleDrawable::SetFocus(const gfx::PointF& position) { 605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const gfx::PointF position_pix = gfx::ScalePoint(position, dpi_scale_); 615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setFocus( 635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) env, drawable_.obj(), position_pix.x(), position_pix.y()); 645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void PopupTouchHandleDrawable::SetVisible(bool visible) { 675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) Java_PopupTouchHandleDrawable_setVisible(env, drawable_.obj(), visible); 695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool PopupTouchHandleDrawable::IntersectsWith(const gfx::RectF& rect) const { 725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const gfx::RectF rect_pix = gfx::ScaleRect(rect, dpi_scale_); 735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) JNIEnv* env = base::android::AttachCurrentThread(); 745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return Java_PopupTouchHandleDrawable_intersectsWith(env, 755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) drawable_.obj(), 765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) rect_pix.x(), 775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) rect_pix.y(), 785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) rect_pix.width(), 795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) rect_pix.height()); 805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// static 835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool PopupTouchHandleDrawable::RegisterPopupTouchHandleDrawable(JNIEnv* env) { 845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return RegisterNativesImpl(env); 855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} 865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} // namespace content 88