| 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 "components/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 if (!java_platform_viewport_android_.is_empty()) { | |
| 65 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 66 Java_PlatformViewportAndroid_detach( | |
| 67 env, java_platform_viewport_android_.get(env).obj()); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void PlatformViewportAndroid::Destroy(JNIEnv* env, jobject obj) { | |
| 72 delegate_->OnDestroyed(); | |
| 73 } | |
| 74 | |
| 75 void PlatformViewportAndroid::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_, device_pixel_ratio); | |
| 88 } | |
| 89 | |
| 90 void PlatformViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) { | |
| 91 DCHECK(window_); | |
| 92 delegate_->OnAcceleratedWidgetDestroyed(); | |
| 93 ReleaseWindow(); | |
| 94 } | |
| 95 | |
| 96 void PlatformViewportAndroid::SurfaceSetSize(JNIEnv* env, | |
| 97 jobject obj, | |
| 98 jint width, | |
| 99 jint height, | |
| 100 jfloat density) { | |
| 101 metrics_ = mojo::ViewportMetrics::New(); | |
| 102 metrics_->size = mojo::Size::New(); | |
| 103 metrics_->size->width = static_cast<int>(width); | |
| 104 metrics_->size->height = static_cast<int>(height); | |
| 105 metrics_->device_pixel_ratio = density; | |
| 106 delegate_->OnMetricsChanged(metrics_.Clone()); | |
| 107 } | |
| 108 | |
| 109 bool PlatformViewportAndroid::TouchEvent(JNIEnv* env, | |
| 110 jobject obj, | |
| 111 jlong time_ms, | |
| 112 jint masked_action, | |
| 113 jint pointer_id, | |
| 114 jfloat x, | |
| 115 jfloat y, | |
| 116 jfloat pressure, | |
| 117 jfloat touch_major, | |
| 118 jfloat touch_minor, | |
| 119 jfloat orientation, | |
| 120 jfloat h_wheel, | |
| 121 jfloat v_wheel) { | |
| 122 mojo::EventPtr event(mojo::Event::New()); | |
| 123 event->time_stamp = | |
| 124 (base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms)) | |
| 125 .ToInternalValue(); | |
| 126 event->action = MotionEventActionToEventType(masked_action); | |
| 127 if (event->action == mojo::EVENT_TYPE_UNKNOWN) | |
| 128 return false; | |
| 129 | |
| 130 event->pointer_data = mojo::PointerData::New(); | |
| 131 event->pointer_data->pointer_id = pointer_id; | |
| 132 event->pointer_data->x = x; | |
| 133 event->pointer_data->y = y; | |
| 134 event->pointer_data->screen_x = x; | |
| 135 event->pointer_data->screen_y = y; | |
| 136 event->pointer_data->pressure = pressure; | |
| 137 event->pointer_data->radius_major = touch_major; | |
| 138 event->pointer_data->radius_minor = touch_minor; | |
| 139 event->pointer_data->orientation = orientation; | |
| 140 event->pointer_data->horizontal_wheel = h_wheel; | |
| 141 event->pointer_data->vertical_wheel = v_wheel; | |
| 142 delegate_->OnEvent(event.Pass()); | |
| 143 | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 bool PlatformViewportAndroid::KeyEvent(JNIEnv* env, | |
| 148 jobject obj, | |
| 149 bool pressed, | |
| 150 jint key_code, | |
| 151 jint unicode_character) { | |
| 152 ui::KeyEvent event(pressed ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, | |
| 153 ui::KeyboardCodeFromAndroidKeyCode(key_code), 0); | |
| 154 event.set_platform_keycode(key_code); | |
| 155 delegate_->OnEvent(mojo::Event::From(event)); | |
| 156 if (pressed && unicode_character) { | |
| 157 ui::KeyEvent char_event(unicode_character, | |
| 158 ui::KeyboardCodeFromAndroidKeyCode(key_code), 0); | |
| 159 char_event.set_platform_keycode(key_code); | |
| 160 delegate_->OnEvent(mojo::Event::From(char_event)); | |
| 161 } | |
| 162 return true; | |
| 163 } | |
| 164 | |
| 165 //////////////////////////////////////////////////////////////////////////////// | |
| 166 // PlatformViewportAndroid, PlatformViewport implementation: | |
| 167 | |
| 168 void PlatformViewportAndroid::Init(const gfx::Rect& bounds) { | |
| 169 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 170 java_platform_viewport_android_ = JavaObjectWeakGlobalRef( | |
| 171 env, Java_PlatformViewportAndroid_createForActivity( | |
| 172 env, base::android::GetApplicationContext(), | |
| 173 reinterpret_cast<jlong>(this)).obj()); | |
| 174 } | |
| 175 | |
| 176 void PlatformViewportAndroid::Show() { | |
| 177 // Nothing to do. View is created visible. | |
| 178 } | |
| 179 | |
| 180 void PlatformViewportAndroid::Hide() { | |
| 181 // Nothing to do. View is always visible. | |
| 182 } | |
| 183 | |
| 184 void PlatformViewportAndroid::Close() { | |
| 185 // TODO(beng): close activity containing MojoView? | |
| 186 | |
| 187 // TODO(beng): perform this in response to view destruction. | |
| 188 delegate_->OnDestroyed(); | |
| 189 } | |
| 190 | |
| 191 gfx::Size PlatformViewportAndroid::GetSize() { | |
| 192 return metrics_->size.To<gfx::Size>(); | |
| 193 } | |
| 194 | |
| 195 void PlatformViewportAndroid::SetBounds(const gfx::Rect& bounds) { | |
| 196 NOTIMPLEMENTED(); | |
| 197 } | |
| 198 | |
| 199 //////////////////////////////////////////////////////////////////////////////// | |
| 200 // PlatformViewportAndroid, private: | |
| 201 | |
| 202 void PlatformViewportAndroid::ReleaseWindow() { | |
| 203 ANativeWindow_release(window_); | |
| 204 window_ = NULL; | |
| 205 } | |
| 206 | |
| 207 //////////////////////////////////////////////////////////////////////////////// | |
| 208 // PlatformViewport, public: | |
| 209 | |
| 210 // static | |
| 211 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { | |
| 212 return scoped_ptr<PlatformViewport>( | |
| 213 new PlatformViewportAndroid(delegate)).Pass(); | |
| 214 } | |
| 215 | |
| 216 } // namespace native_viewport | |
| OLD | NEW |