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.
5 #include "mojo/services/native_viewport/native_viewport_android.h"
7 #include <android/input.h>
8 #include <android/native_window_jni.h>
10 #include "base/android/jni_android.h"
11 #include "jni/NativeViewportAndroid_jni.h"
12 #include "mojo/shell/context.h"
13 #include "ui/events/event.h"
14 #include "ui/gfx/point.h"
19 ui::EventType MotionEventActionToEventType(jint action) {
21 case AMOTION_EVENT_ACTION_DOWN:
22 return ui::ET_TOUCH_PRESSED;
23 case AMOTION_EVENT_ACTION_MOVE:
24 return ui::ET_TOUCH_MOVED;
25 case AMOTION_EVENT_ACTION_UP:
26 return ui::ET_TOUCH_RELEASED;
30 return ui::ET_UNKNOWN;
33 ////////////////////////////////////////////////////////////////////////////////
34 // NativeViewportAndroid, public:
37 bool NativeViewportAndroid::Register(JNIEnv* env) {
38 return RegisterNativesImpl(env);
41 NativeViewportAndroid::NativeViewportAndroid(shell::Context* context,
42 NativeViewportDelegate* delegate)
43 : delegate_(delegate),
50 NativeViewportAndroid::~NativeViewportAndroid() {
55 void NativeViewportAndroid::Destroy(JNIEnv* env, jobject obj) {
56 delegate_->OnDestroyed();
59 void NativeViewportAndroid::SurfaceCreated(JNIEnv* env,
62 base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface);
63 // Note: This ensures that any local references used by
64 // ANativeWindow_fromSurface are released immediately. This is needed as a
65 // workaround for https://code.google.com/p/android/issues/detail?id=68174
67 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
68 window_ = ANativeWindow_fromSurface(env, jsurface);
70 delegate_->OnAcceleratedWidgetAvailable(window_);
73 void NativeViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
78 void NativeViewportAndroid::SurfaceSetSize(JNIEnv* env, jobject obj,
79 jint width, jint height) {
80 bounds_ = gfx::Rect(width, height);
81 delegate_->OnBoundsChanged(bounds_);
84 bool NativeViewportAndroid::TouchEvent(JNIEnv* env, jobject obj,
89 gfx::Point location(static_cast<int>(x), static_cast<int>(y));
90 ui::TouchEvent event(MotionEventActionToEventType(action), location,
91 id_generator_.GetGeneratedID(pointer_id),
92 base::TimeDelta::FromMilliseconds(time_ms));
93 // TODO(beng): handle multiple touch-points.
94 delegate_->OnEvent(&event);
95 if (action == ui::ET_TOUCH_RELEASED)
96 id_generator_.ReleaseNumber(pointer_id);
101 ////////////////////////////////////////////////////////////////////////////////
102 // NativeViewportAndroid, NativeViewport implementation:
104 void NativeViewportAndroid::Init(const gfx::Rect& bounds) {
105 JNIEnv* env = base::android::AttachCurrentThread();
106 Java_NativeViewportAndroid_createForActivity(env, context_->activity(),
107 reinterpret_cast<jlong>(this));
110 void NativeViewportAndroid::Show() {
111 // Nothing to do. View is created visible.
114 void NativeViewportAndroid::Hide() {
115 // Nothing to do. View is always visible.
118 void NativeViewportAndroid::Close() {
119 // TODO(beng): close activity containing MojoView?
121 // TODO(beng): perform this in response to view destruction.
122 delegate_->OnDestroyed();
125 gfx::Size NativeViewportAndroid::GetSize() {
126 return bounds_.size();
129 void NativeViewportAndroid::SetBounds(const gfx::Rect& bounds) {
133 void NativeViewportAndroid::SetCapture() {
137 void NativeViewportAndroid::ReleaseCapture() {
141 ////////////////////////////////////////////////////////////////////////////////
142 // NativeViewportAndroid, private:
144 void NativeViewportAndroid::ReleaseWindow() {
145 ANativeWindow_release(window_);
149 ////////////////////////////////////////////////////////////////////////////////
150 // NativeViewport, public:
153 scoped_ptr<NativeViewport> NativeViewport::Create(
154 shell::Context* context,
155 NativeViewportDelegate* delegate) {
156 return scoped_ptr<NativeViewport>(
157 new NativeViewportAndroid(context, delegate)).Pass();
160 } // namespace services