1 // Copyright 2015 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.
5 #include "ui/platform_window/android/platform_window_android.h"
7 #include <android/input.h>
8 #include <android/native_window_jni.h>
10 #include "base/android/jni_android.h"
11 #include "jni/PlatformWindowAndroid_jni.h"
12 #include "ui/events/event.h"
13 #include "ui/events/keycodes/keyboard_code_conversion_android.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/platform_window/platform_window_delegate.h"
21 ui::EventType
MotionEventActionToEventType(jint action
) {
23 case AMOTION_EVENT_ACTION_DOWN
:
24 case AMOTION_EVENT_ACTION_POINTER_DOWN
:
25 return ui::ET_TOUCH_PRESSED
;
26 case AMOTION_EVENT_ACTION_UP
:
27 case AMOTION_EVENT_ACTION_POINTER_UP
:
28 return ui::ET_TOUCH_RELEASED
;
29 case AMOTION_EVENT_ACTION_MOVE
:
30 return ui::ET_TOUCH_MOVED
;
31 case AMOTION_EVENT_ACTION_CANCEL
:
32 return ui::ET_TOUCH_CANCELLED
;
33 case AMOTION_EVENT_ACTION_OUTSIDE
:
34 case AMOTION_EVENT_ACTION_HOVER_MOVE
:
35 case AMOTION_EVENT_ACTION_SCROLL
:
36 case AMOTION_EVENT_ACTION_HOVER_ENTER
:
37 case AMOTION_EVENT_ACTION_HOVER_EXIT
:
39 NOTIMPLEMENTED() << "Unimplemented motion action: " << action
;
41 return ui::ET_UNKNOWN
;
46 ////////////////////////////////////////////////////////////////////////////////
47 // PlatformWindowAndroid, public:
50 bool PlatformWindowAndroid::Register(JNIEnv
* env
) {
51 return RegisterNativesImpl(env
);
54 PlatformWindowAndroid::PlatformWindowAndroid(PlatformWindowDelegate
* delegate
)
55 : delegate_(delegate
),
61 PlatformWindowAndroid::~PlatformWindowAndroid() {
64 if (!java_platform_window_android_
.is_empty()) {
65 JNIEnv
* env
= base::android::AttachCurrentThread();
66 Java_PlatformWindowAndroid_detach(
67 env
, java_platform_window_android_
.get(env
).obj());
71 void PlatformWindowAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
72 delegate_
->OnClosed();
75 void PlatformWindowAndroid::SurfaceCreated(JNIEnv
* env
,
78 float device_pixel_ratio
) {
79 base::android::ScopedJavaLocalRef
<jobject
> protector(env
, jsurface
);
80 // Note: This ensures that any local references used by
81 // ANativeWindow_fromSurface are released immediately. This is needed as a
82 // workaround for https://code.google.com/p/android/issues/detail?id=68174
84 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
85 window_
= ANativeWindow_fromSurface(env
, jsurface
);
87 delegate_
->OnAcceleratedWidgetAvailable(window_
, device_pixel_ratio
);
90 void PlatformWindowAndroid::SurfaceDestroyed(JNIEnv
* env
, jobject obj
) {
93 delegate_
->OnAcceleratedWidgetAvailable(gfx::kNullAcceleratedWidget
, 0.f
);
96 void PlatformWindowAndroid::SurfaceSetSize(JNIEnv
* env
,
101 size_
= gfx::Size(static_cast<int>(width
), static_cast<int>(height
));
102 delegate_
->OnBoundsChanged(gfx::Rect(size_
));
105 bool PlatformWindowAndroid::TouchEvent(JNIEnv
* env
,
118 ui::EventType event_type
= MotionEventActionToEventType(masked_action
);
119 if (event_type
== ui::ET_UNKNOWN
)
121 ui::TouchEvent
touch(event_type
, gfx::PointF(x
, y
), ui::EF_NONE
, pointer_id
,
122 base::TimeDelta::FromMilliseconds(time_ms
),
123 touch_major
, touch_minor
, orientation
, pressure
);
124 delegate_
->DispatchEvent(&touch
);
128 bool PlatformWindowAndroid::KeyEvent(JNIEnv
* env
,
132 jint unicode_character
) {
133 ui::KeyEvent
key_event(pressed
? ui::ET_KEY_PRESSED
: ui::ET_KEY_RELEASED
,
134 ui::KeyboardCodeFromAndroidKeyCode(key_code
), 0);
135 delegate_
->DispatchEvent(&key_event
);
136 if (pressed
&& unicode_character
) {
137 ui::KeyEvent
char_event(unicode_character
,
138 ui::KeyboardCodeFromAndroidKeyCode(key_code
), 0);
139 delegate_
->DispatchEvent(&char_event
);
144 void PlatformWindowAndroid::ReleaseWindow() {
145 ANativeWindow_release(window_
);
149 ////////////////////////////////////////////////////////////////////////////////
150 // PlatformWindowAndroid, PlatformWindow implementation:
152 void PlatformWindowAndroid::Show() {
153 if (!java_platform_window_android_
.is_empty())
155 JNIEnv
* env
= base::android::AttachCurrentThread();
156 java_platform_window_android_
= JavaObjectWeakGlobalRef(
157 env
, Java_PlatformWindowAndroid_createForActivity(
158 env
, base::android::GetApplicationContext(),
159 reinterpret_cast<jlong
>(this),
160 reinterpret_cast<jlong
>(&platform_ime_controller_
)).obj());
163 void PlatformWindowAndroid::Hide() {
164 // Nothing to do. View is always visible.
167 void PlatformWindowAndroid::Close() {
168 delegate_
->OnCloseRequest();
171 void PlatformWindowAndroid::SetBounds(const gfx::Rect
& bounds
) {
175 gfx::Rect
PlatformWindowAndroid::GetBounds() {
176 return gfx::Rect(size_
);
179 void PlatformWindowAndroid::SetTitle(const base::string16
& title
) {
183 void PlatformWindowAndroid::SetCapture() {
187 void PlatformWindowAndroid::ReleaseCapture() {
191 void PlatformWindowAndroid::ToggleFullscreen() {
195 void PlatformWindowAndroid::Maximize() {
199 void PlatformWindowAndroid::Minimize() {
203 void PlatformWindowAndroid::Restore() {
207 void PlatformWindowAndroid::SetCursor(PlatformCursor cursor
) {
211 void PlatformWindowAndroid::MoveCursorTo(const gfx::Point
& location
) {
215 void PlatformWindowAndroid::ConfineCursorToBounds(const gfx::Rect
& bounds
) {
219 PlatformImeController
* PlatformWindowAndroid::GetPlatformImeController() {
220 return &platform_ime_controller_
;