OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "mojo/services/native_viewport/platform_viewport_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/PlatformViewportAndroid_jni.h" |
| 12 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 13 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 14 #include "ui/events/event.h" |
| 15 #include "ui/events/keycodes/keyboard_code_conversion_android.h" |
| 16 #include "ui/gfx/geometry/point.h" |
| 17 |
| 18 namespace native_viewport { |
| 19 namespace { |
| 20 |
| 21 mojo::EventType MotionEventActionToEventType(jint action) { |
| 22 switch (action) { |
| 23 case AMOTION_EVENT_ACTION_DOWN: |
| 24 case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 25 return mojo::EVENT_TYPE_POINTER_DOWN; |
| 26 case AMOTION_EVENT_ACTION_UP: |
| 27 case AMOTION_EVENT_ACTION_POINTER_UP: |
| 28 return mojo::EVENT_TYPE_POINTER_UP; |
| 29 case AMOTION_EVENT_ACTION_MOVE: |
| 30 return mojo::EVENT_TYPE_POINTER_MOVE; |
| 31 case AMOTION_EVENT_ACTION_CANCEL: |
| 32 return mojo::EVENT_TYPE_POINTER_CANCEL; |
| 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 mojo::EVENT_TYPE_UNKNOWN; |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 //////////////////////////////////////////////////////////////////////////////// |
| 47 // PlatformViewportAndroid, public: |
| 48 |
| 49 // static |
| 50 bool PlatformViewportAndroid::Register(JNIEnv* env) { |
| 51 return RegisterNativesImpl(env); |
| 52 } |
| 53 |
| 54 PlatformViewportAndroid::PlatformViewportAndroid(Delegate* delegate) |
| 55 : delegate_(delegate), |
| 56 window_(NULL), |
| 57 id_generator_(0), |
| 58 weak_factory_(this) { |
| 59 } |
| 60 |
| 61 PlatformViewportAndroid::~PlatformViewportAndroid() { |
| 62 if (window_) |
| 63 ReleaseWindow(); |
| 64 } |
| 65 |
| 66 void PlatformViewportAndroid::Destroy(JNIEnv* env, jobject obj) { |
| 67 delegate_->OnDestroyed(); |
| 68 } |
| 69 |
| 70 void PlatformViewportAndroid::SurfaceCreated(JNIEnv* env, |
| 71 jobject obj, |
| 72 jobject jsurface) { |
| 73 base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface); |
| 74 // Note: This ensures that any local references used by |
| 75 // ANativeWindow_fromSurface are released immediately. This is needed as a |
| 76 // workaround for https://code.google.com/p/android/issues/detail?id=68174 |
| 77 { |
| 78 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env); |
| 79 window_ = ANativeWindow_fromSurface(env, jsurface); |
| 80 } |
| 81 delegate_->OnAcceleratedWidgetAvailable(window_); |
| 82 } |
| 83 |
| 84 void PlatformViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { |
| 85 DCHECK(window_); |
| 86 delegate_->OnAcceleratedWidgetDestroyed(); |
| 87 ReleaseWindow(); |
| 88 } |
| 89 |
| 90 void PlatformViewportAndroid::SurfaceSetSize(JNIEnv* env, |
| 91 jobject obj, |
| 92 jint width, |
| 93 jint height, |
| 94 jfloat density) { |
| 95 metrics_ = mojo::ViewportMetrics::New(); |
| 96 metrics_->size = mojo::Size::New(); |
| 97 metrics_->size->width = static_cast<int>(width); |
| 98 metrics_->size->height = static_cast<int>(height); |
| 99 metrics_->device_pixel_ratio = density; |
| 100 delegate_->OnMetricsChanged(metrics_.Clone()); |
| 101 } |
| 102 |
| 103 bool PlatformViewportAndroid::TouchEvent(JNIEnv* env, |
| 104 jobject obj, |
| 105 jlong time_ms, |
| 106 jint masked_action, |
| 107 jint pointer_id, |
| 108 jfloat x, |
| 109 jfloat y, |
| 110 jfloat pressure, |
| 111 jfloat touch_major, |
| 112 jfloat touch_minor, |
| 113 jfloat orientation, |
| 114 jfloat h_wheel, |
| 115 jfloat v_wheel) { |
| 116 mojo::EventPtr event(mojo::Event::New()); |
| 117 event->time_stamp = time_ms; |
| 118 event->action = MotionEventActionToEventType(masked_action); |
| 119 if (event->action == mojo::EVENT_TYPE_UNKNOWN) |
| 120 return false; |
| 121 |
| 122 event->pointer_data = mojo::PointerData::New(); |
| 123 event->pointer_data->pointer_id = pointer_id; |
| 124 event->pointer_data->x = x; |
| 125 event->pointer_data->y = y; |
| 126 event->pointer_data->screen_x = x; |
| 127 event->pointer_data->screen_y = y; |
| 128 event->pointer_data->pressure = pressure; |
| 129 event->pointer_data->radius_major = touch_major; |
| 130 event->pointer_data->radius_minor = touch_minor; |
| 131 event->pointer_data->orientation = orientation; |
| 132 event->pointer_data->horizontal_wheel = h_wheel; |
| 133 event->pointer_data->vertical_wheel = v_wheel; |
| 134 delegate_->OnEvent(event.Pass()); |
| 135 |
| 136 return true; |
| 137 } |
| 138 |
| 139 bool PlatformViewportAndroid::KeyEvent(JNIEnv* env, |
| 140 jobject obj, |
| 141 bool pressed, |
| 142 jint key_code, |
| 143 jint unicode_character) { |
| 144 ui::KeyEvent event(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, |
| 145 ui::KeyboardCodeFromAndroidKeyCode(key_code), 0); |
| 146 event.set_platform_keycode(key_code); |
| 147 delegate_->OnEvent(mojo::Event::From(event)); |
| 148 if (pressed && unicode_character) { |
| 149 ui::KeyEvent char_event(unicode_character, |
| 150 ui::KeyboardCodeFromAndroidKeyCode(key_code), 0); |
| 151 char_event.set_platform_keycode(key_code); |
| 152 delegate_->OnEvent(mojo::Event::From(char_event)); |
| 153 } |
| 154 return true; |
| 155 } |
| 156 |
| 157 //////////////////////////////////////////////////////////////////////////////// |
| 158 // PlatformViewportAndroid, PlatformViewport implementation: |
| 159 |
| 160 void PlatformViewportAndroid::Init(const gfx::Rect& bounds) { |
| 161 JNIEnv* env = base::android::AttachCurrentThread(); |
| 162 Java_PlatformViewportAndroid_createForActivity( |
| 163 env, |
| 164 base::android::GetApplicationContext(), |
| 165 reinterpret_cast<jlong>(this)); |
| 166 } |
| 167 |
| 168 void PlatformViewportAndroid::Show() { |
| 169 // Nothing to do. View is created visible. |
| 170 } |
| 171 |
| 172 void PlatformViewportAndroid::Hide() { |
| 173 // Nothing to do. View is always visible. |
| 174 } |
| 175 |
| 176 void PlatformViewportAndroid::Close() { |
| 177 // TODO(beng): close activity containing MojoView? |
| 178 |
| 179 // TODO(beng): perform this in response to view destruction. |
| 180 delegate_->OnDestroyed(); |
| 181 } |
| 182 |
| 183 gfx::Size PlatformViewportAndroid::GetSize() { |
| 184 return metrics_->size.To<gfx::Size>(); |
| 185 } |
| 186 |
| 187 void PlatformViewportAndroid::SetBounds(const gfx::Rect& bounds) { |
| 188 NOTIMPLEMENTED(); |
| 189 } |
| 190 |
| 191 //////////////////////////////////////////////////////////////////////////////// |
| 192 // PlatformViewportAndroid, private: |
| 193 |
| 194 void PlatformViewportAndroid::ReleaseWindow() { |
| 195 ANativeWindow_release(window_); |
| 196 window_ = NULL; |
| 197 } |
| 198 |
| 199 //////////////////////////////////////////////////////////////////////////////// |
| 200 // PlatformViewport, public: |
| 201 |
| 202 // static |
| 203 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { |
| 204 return scoped_ptr<PlatformViewport>( |
| 205 new PlatformViewportAndroid(delegate)).Pass(); |
| 206 } |
| 207 |
| 208 } // namespace native_viewport |
OLD | NEW |