Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / native_viewport_android.cc
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/native_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/NativeViewportAndroid_jni.h"
12 #include "mojo/shell/context.h"
13 #include "ui/events/event.h"
14 #include "ui/gfx/point.h"
15
16 namespace mojo {
17 namespace services {
18
19 ui::EventType MotionEventActionToEventType(jint action) {
20   switch (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;
27     default:
28       NOTREACHED();
29   }
30   return ui::ET_UNKNOWN;
31 }
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // NativeViewportAndroid, public:
35
36 // static
37 bool NativeViewportAndroid::Register(JNIEnv* env) {
38   return RegisterNativesImpl(env);
39 }
40
41 NativeViewportAndroid::NativeViewportAndroid(shell::Context* context,
42                                              NativeViewportDelegate* delegate)
43     : delegate_(delegate),
44       context_(context),
45       window_(NULL),
46       id_generator_(0),
47       weak_factory_(this) {
48 }
49
50 NativeViewportAndroid::~NativeViewportAndroid() {
51   if (window_)
52     ReleaseWindow();
53 }
54
55 void NativeViewportAndroid::Destroy(JNIEnv* env, jobject obj) {
56   delegate_->OnDestroyed();
57 }
58
59 void NativeViewportAndroid::SurfaceCreated(JNIEnv* env,
60                                            jobject obj,
61                                            jobject jsurface) {
62   base::android::ScopedJavaLocalRef<jobject> protector(env, jsurface);
63   window_ = ANativeWindow_fromSurface(env, jsurface);
64   delegate_->OnAcceleratedWidgetAvailable(window_);
65 }
66
67 void NativeViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
68   DCHECK(window_);
69   ReleaseWindow();
70 }
71
72 void NativeViewportAndroid::SurfaceSetSize(JNIEnv* env, jobject obj,
73                                            jint width, jint height) {
74   bounds_ = gfx::Rect(width, height);
75   delegate_->OnBoundsChanged(bounds_);
76 }
77
78 bool NativeViewportAndroid::TouchEvent(JNIEnv* env, jobject obj,
79                                        jint pointer_id,
80                                        jint action,
81                                        jfloat x, jfloat y,
82                                        jlong time_ms) {
83   gfx::Point location(static_cast<int>(x), static_cast<int>(y));
84   ui::TouchEvent event(MotionEventActionToEventType(action), location,
85                        id_generator_.GetGeneratedID(pointer_id),
86                        base::TimeDelta::FromMilliseconds(time_ms));
87   // TODO(beng): handle multiple touch-points.
88   delegate_->OnEvent(&event);
89   if (action == ui::ET_TOUCH_RELEASED)
90     id_generator_.ReleaseNumber(pointer_id);
91
92   return true;
93 }
94
95 ////////////////////////////////////////////////////////////////////////////////
96 // NativeViewportAndroid, NativeViewport implementation:
97
98 void NativeViewportAndroid::Init(const gfx::Rect& bounds) {
99   JNIEnv* env = base::android::AttachCurrentThread();
100   Java_NativeViewportAndroid_createForActivity(env, context_->activity(),
101                                                reinterpret_cast<jint>(this));
102 }
103
104 void NativeViewportAndroid::Show() {
105   // Nothing to do. View is created visible.
106 }
107
108 void NativeViewportAndroid::Hide() {
109   // Nothing to do. View is always visible.
110 }
111
112 void NativeViewportAndroid::Close() {
113   // TODO(beng): close activity containing MojoView?
114
115   // TODO(beng): perform this in response to view destruction.
116   delegate_->OnDestroyed();
117 }
118
119 gfx::Size NativeViewportAndroid::GetSize() {
120   return bounds_.size();
121 }
122
123 void NativeViewportAndroid::SetBounds(const gfx::Rect& bounds) {
124   NOTIMPLEMENTED();
125 }
126
127 void NativeViewportAndroid::SetCapture() {
128   NOTIMPLEMENTED();
129 }
130
131 void NativeViewportAndroid::ReleaseCapture() {
132   NOTIMPLEMENTED();
133 }
134
135 ////////////////////////////////////////////////////////////////////////////////
136 // NativeViewportAndroid, private:
137
138 void NativeViewportAndroid::ReleaseWindow() {
139   ANativeWindow_release(window_);
140   window_ = NULL;
141 }
142
143 ////////////////////////////////////////////////////////////////////////////////
144 // NativeViewport, public:
145
146 // static
147 scoped_ptr<NativeViewport> NativeViewport::Create(
148     shell::Context* context,
149     NativeViewportDelegate* delegate) {
150   return scoped_ptr<NativeViewport>(
151       new NativeViewportAndroid(context, delegate)).Pass();
152 }
153
154 }  // namespace services
155 }  // namespace mojo