Upstream version 7.36.149.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   // 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
66   {
67     base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
68     window_ = ANativeWindow_fromSurface(env, jsurface);
69   }
70   delegate_->OnAcceleratedWidgetAvailable(window_);
71 }
72
73 void NativeViewportAndroid::SurfaceDestroyed(JNIEnv* env, jobject obj) {
74   DCHECK(window_);
75   ReleaseWindow();
76 }
77
78 void NativeViewportAndroid::SurfaceSetSize(JNIEnv* env, jobject obj,
79                                            jint width, jint height) {
80   bounds_ = gfx::Rect(width, height);
81   delegate_->OnBoundsChanged(bounds_);
82 }
83
84 bool NativeViewportAndroid::TouchEvent(JNIEnv* env, jobject obj,
85                                        jint pointer_id,
86                                        jint action,
87                                        jfloat x, jfloat y,
88                                        jlong time_ms) {
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);
97
98   return true;
99 }
100
101 ////////////////////////////////////////////////////////////////////////////////
102 // NativeViewportAndroid, NativeViewport implementation:
103
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));
108 }
109
110 void NativeViewportAndroid::Show() {
111   // Nothing to do. View is created visible.
112 }
113
114 void NativeViewportAndroid::Hide() {
115   // Nothing to do. View is always visible.
116 }
117
118 void NativeViewportAndroid::Close() {
119   // TODO(beng): close activity containing MojoView?
120
121   // TODO(beng): perform this in response to view destruction.
122   delegate_->OnDestroyed();
123 }
124
125 gfx::Size NativeViewportAndroid::GetSize() {
126   return bounds_.size();
127 }
128
129 void NativeViewportAndroid::SetBounds(const gfx::Rect& bounds) {
130   NOTIMPLEMENTED();
131 }
132
133 void NativeViewportAndroid::SetCapture() {
134   NOTIMPLEMENTED();
135 }
136
137 void NativeViewportAndroid::ReleaseCapture() {
138   NOTIMPLEMENTED();
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////
142 // NativeViewportAndroid, private:
143
144 void NativeViewportAndroid::ReleaseWindow() {
145   ANativeWindow_release(window_);
146   window_ = NULL;
147 }
148
149 ////////////////////////////////////////////////////////////////////////////////
150 // NativeViewport, public:
151
152 // static
153 scoped_ptr<NativeViewport> NativeViewport::Create(
154     shell::Context* context,
155     NativeViewportDelegate* delegate) {
156   return scoped_ptr<NativeViewport>(
157       new NativeViewportAndroid(context, delegate)).Pass();
158 }
159
160 }  // namespace services
161 }  // namespace mojo