Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / platform_window / android / platform_window_android.cc
blob94500a25ebf30a0da68b9889b1ed599345df049c
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"
17 namespace ui {
19 namespace {
21 ui::EventType MotionEventActionToEventType(jint action) {
22 switch (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:
38 default:
39 NOTIMPLEMENTED() << "Unimplemented motion action: " << action;
41 return ui::ET_UNKNOWN;
44 } // namespace
46 ////////////////////////////////////////////////////////////////////////////////
47 // PlatformWindowAndroid, public:
49 // static
50 bool PlatformWindowAndroid::Register(JNIEnv* env) {
51 return RegisterNativesImpl(env);
54 PlatformWindowAndroid::PlatformWindowAndroid(PlatformWindowDelegate* delegate)
55 : delegate_(delegate),
56 window_(NULL),
57 id_generator_(0),
58 weak_factory_(this) {
61 PlatformWindowAndroid::~PlatformWindowAndroid() {
62 if (window_)
63 ReleaseWindow();
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,
76 jobject obj,
77 jobject jsurface,
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) {
91 DCHECK(window_);
92 ReleaseWindow();
93 delegate_->OnAcceleratedWidgetAvailable(gfx::kNullAcceleratedWidget, 0.f);
96 void PlatformWindowAndroid::SurfaceSetSize(JNIEnv* env,
97 jobject obj,
98 jint width,
99 jint height,
100 jfloat density) {
101 size_ = gfx::Size(static_cast<int>(width), static_cast<int>(height));
102 delegate_->OnBoundsChanged(gfx::Rect(size_));
105 bool PlatformWindowAndroid::TouchEvent(JNIEnv* env,
106 jobject obj,
107 jlong time_ms,
108 jint masked_action,
109 jint pointer_id,
110 jfloat x,
111 jfloat y,
112 jfloat pressure,
113 jfloat touch_major,
114 jfloat touch_minor,
115 jfloat orientation,
116 jfloat h_wheel,
117 jfloat v_wheel) {
118 ui::EventType event_type = MotionEventActionToEventType(masked_action);
119 if (event_type == ui::ET_UNKNOWN)
120 return false;
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);
125 return true;
128 bool PlatformWindowAndroid::KeyEvent(JNIEnv* env,
129 jobject obj,
130 bool pressed,
131 jint key_code,
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);
141 return true;
144 void PlatformWindowAndroid::ReleaseWindow() {
145 ANativeWindow_release(window_);
146 window_ = NULL;
149 ////////////////////////////////////////////////////////////////////////////////
150 // PlatformWindowAndroid, PlatformWindow implementation:
152 void PlatformWindowAndroid::Show() {
153 if (!java_platform_window_android_.is_empty())
154 return;
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) {
172 NOTIMPLEMENTED();
175 gfx::Rect PlatformWindowAndroid::GetBounds() {
176 return gfx::Rect(size_);
179 void PlatformWindowAndroid::SetTitle(const base::string16& title) {
180 NOTIMPLEMENTED();
183 void PlatformWindowAndroid::SetCapture() {
184 NOTIMPLEMENTED();
187 void PlatformWindowAndroid::ReleaseCapture() {
188 NOTIMPLEMENTED();
191 void PlatformWindowAndroid::ToggleFullscreen() {
192 NOTIMPLEMENTED();
195 void PlatformWindowAndroid::Maximize() {
196 NOTIMPLEMENTED();
199 void PlatformWindowAndroid::Minimize() {
200 NOTIMPLEMENTED();
203 void PlatformWindowAndroid::Restore() {
204 NOTIMPLEMENTED();
207 void PlatformWindowAndroid::SetCursor(PlatformCursor cursor) {
208 NOTIMPLEMENTED();
211 void PlatformWindowAndroid::MoveCursorTo(const gfx::Point& location) {
212 NOTIMPLEMENTED();
215 void PlatformWindowAndroid::ConfineCursorToBounds(const gfx::Rect& bounds) {
216 NOTIMPLEMENTED();
219 PlatformImeController* PlatformWindowAndroid::GetPlatformImeController() {
220 return &platform_ime_controller_;
223 } // namespace ui