OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/platform_window/android/platform_window_android.h" |
| 6 |
| 7 #include <android/input.h> |
| 8 #include <android/native_window_jni.h> |
| 9 |
| 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" |
| 16 |
| 17 namespace ui { |
| 18 |
| 19 namespace { |
| 20 |
| 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; |
| 40 } |
| 41 return ui::ET_UNKNOWN; |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 //////////////////////////////////////////////////////////////////////////////// |
| 47 // PlatformWindowAndroid, public: |
| 48 |
| 49 // static |
| 50 bool PlatformWindowAndroid::Register(JNIEnv* env) { |
| 51 return RegisterNativesImpl(env); |
| 52 } |
| 53 |
| 54 PlatformWindowAndroid::PlatformWindowAndroid(PlatformWindowDelegate* delegate) |
| 55 : delegate_(delegate), |
| 56 window_(NULL), |
| 57 id_generator_(0), |
| 58 weak_factory_(this) { |
| 59 } |
| 60 |
| 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()); |
| 68 } |
| 69 } |
| 70 |
| 71 void PlatformWindowAndroid::Destroy(JNIEnv* env, jobject obj) { |
| 72 delegate_->OnClosed(); |
| 73 } |
| 74 |
| 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 |
| 83 { |
| 84 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
| 85 window_ = ANativeWindow_fromSurface(env, jsurface); |
| 86 } |
| 87 delegate_->OnAcceleratedWidgetAvailable(window_); |
| 88 } |
| 89 |
| 90 void PlatformWindowAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { |
| 91 DCHECK(window_); |
| 92 ReleaseWindow(); |
| 93 // TODO(sad): Call OnClosed() here? |
| 94 } |
| 95 |
| 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_)); |
| 103 } |
| 104 |
| 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; |
| 126 } |
| 127 |
| 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 key_event.set_platform_keycode(key_code); |
| 136 delegate_->DispatchEvent(&key_event); |
| 137 if (pressed && unicode_character) { |
| 138 ui::KeyEvent char_event(unicode_character, |
| 139 ui::KeyboardCodeFromAndroidKeyCode(key_code), 0); |
| 140 char_event.set_platform_keycode(key_code); |
| 141 delegate_->DispatchEvent(&char_event); |
| 142 } |
| 143 return true; |
| 144 } |
| 145 |
| 146 void PlatformWindowAndroid::ReleaseWindow() { |
| 147 ANativeWindow_release(window_); |
| 148 window_ = NULL; |
| 149 } |
| 150 |
| 151 //////////////////////////////////////////////////////////////////////////////// |
| 152 // PlatformWindowAndroid, PlatformWindow implementation: |
| 153 |
| 154 void PlatformWindowAndroid::Show() { |
| 155 if (!java_platform_window_android_.is_empty()) |
| 156 return; |
| 157 JNIEnv* env = base::android::AttachCurrentThread(); |
| 158 java_platform_window_android_ = JavaObjectWeakGlobalRef( |
| 159 env, Java_PlatformWindowAndroid_createForActivity( |
| 160 env, base::android::GetApplicationContext(), |
| 161 reinterpret_cast<jlong>(this)).obj()); |
| 162 } |
| 163 |
| 164 void PlatformWindowAndroid::Hide() { |
| 165 // Nothing to do. View is always visible. |
| 166 } |
| 167 |
| 168 void PlatformWindowAndroid::Close() { |
| 169 delegate_->OnCloseRequest(); |
| 170 } |
| 171 |
| 172 void PlatformWindowAndroid::SetBounds(const gfx::Rect& bounds) { |
| 173 NOTIMPLEMENTED(); |
| 174 } |
| 175 |
| 176 gfx::Rect PlatformWindowAndroid::GetBounds() { |
| 177 return gfx::Rect(size_); |
| 178 } |
| 179 |
| 180 void PlatformWindowAndroid::SetCapture() { |
| 181 NOTIMPLEMENTED(); |
| 182 } |
| 183 |
| 184 void PlatformWindowAndroid::ReleaseCapture() { |
| 185 NOTIMPLEMENTED(); |
| 186 } |
| 187 |
| 188 void PlatformWindowAndroid::ToggleFullscreen() { |
| 189 NOTIMPLEMENTED(); |
| 190 } |
| 191 |
| 192 void PlatformWindowAndroid::Maximize() { |
| 193 NOTIMPLEMENTED(); |
| 194 } |
| 195 |
| 196 void PlatformWindowAndroid::Minimize() { |
| 197 NOTIMPLEMENTED(); |
| 198 } |
| 199 |
| 200 void PlatformWindowAndroid::Restore() { |
| 201 NOTIMPLEMENTED(); |
| 202 } |
| 203 |
| 204 void PlatformWindowAndroid::SetCursor(PlatformCursor cursor) { |
| 205 NOTIMPLEMENTED(); |
| 206 } |
| 207 |
| 208 void PlatformWindowAndroid::MoveCursorTo(const gfx::Point& location) { |
| 209 NOTIMPLEMENTED(); |
| 210 } |
| 211 |
| 212 void PlatformWindowAndroid::ConfineCursorToBounds(const gfx::Rect& bounds) { |
| 213 NOTIMPLEMENTED(); |
| 214 } |
| 215 |
| 216 } // namespace ui |
OLD | NEW |