Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: components/view_manager/native_viewport/platform_viewport_android.cc

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

Powered by Google App Engine
This is Rietveld 408576698