diff options
author | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-08 00:15:40 +0000 |
---|---|---|
committer | ben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-08 00:15:40 +0000 |
commit | f880cb6a95cc4c3810de87490b7434811f210897 (patch) | |
tree | c5fa221b8957052cb87bc1605415e0278c3d7180 | |
parent | 95ed1f93634d2552c4694a3261abc10f41b536a4 (diff) | |
download | chromium_src-f880cb6a95cc4c3810de87490b7434811f210897.zip chromium_src-f880cb6a95cc4c3810de87490b7434811f210897.tar.gz chromium_src-f880cb6a95cc4c3810de87490b7434811f210897.tar.bz2 |
Wires up MotionEvents for mojo_shell_apk on Android.
Receive Touch and Trackball events from our MojoViewport View and send them via NativeViewport to sample_app.
Note that I do not support multiple touchpoints right now. This is something I can easily add later but decided to start here.
BUG=
R=abarth@chromium.org
Review URL: https://codereview.chromium.org/63493002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233716 0039d316-1c4b-4281-b951-d872f2087c98
5 files changed, 72 insertions, 0 deletions
diff --git a/mojo/services/native_viewport/android/mojo_viewport.cc b/mojo/services/native_viewport/android/mojo_viewport.cc index 8530b7c..5f1adc8 100644 --- a/mojo/services/native_viewport/android/mojo_viewport.cc +++ b/mojo/services/native_viewport/android/mojo_viewport.cc @@ -4,7 +4,9 @@ #include "mojo/services/native_viewport/android/mojo_viewport.h" +#include <android/input.h> #include <android/native_window_jni.h> + #include "base/android/jni_android.h" #include "base/bind.h" #include "base/location.h" @@ -14,6 +16,20 @@ namespace mojo { namespace services { +ui::EventType MotionEventActionToEventType(jint action) { + switch (action) { + case AMOTION_EVENT_ACTION_DOWN: + return ui::ET_TOUCH_PRESSED; + case AMOTION_EVENT_ACTION_MOVE: + return ui::ET_TOUCH_MOVED; + case AMOTION_EVENT_ACTION_UP: + return ui::ET_TOUCH_RELEASED; + default: + NOTREACHED(); + } + return ui::ET_UNKNOWN; +} + MojoViewportInit::MojoViewportInit() { } @@ -70,6 +86,23 @@ void MojoViewport::SurfaceSetSize( gfx::Size(width, height))); } +bool MojoViewport::TouchEvent(JNIEnv* env, jobject obj, + jint pointer_id, + jint action, + jfloat x, jfloat y, + jlong time_ms) { + ui_runner_->PostTask(FROM_HERE, base::Bind( + &NativeViewportAndroid::OnTouchEvent, + native_viewport_, + pointer_id, + MotionEventActionToEventType(action), + x, y, + time_ms)); + // TODO(beng): This type needs to live on the main thread so we can respond to + // this question truthfully. + return true; +} + bool MojoViewport::Register(JNIEnv* env) { return RegisterNativesImpl(env); } diff --git a/mojo/services/native_viewport/android/mojo_viewport.h b/mojo/services/native_viewport/android/mojo_viewport.h index 6c60285..1ee9bdc 100644 --- a/mojo/services/native_viewport/android/mojo_viewport.h +++ b/mojo/services/native_viewport/android/mojo_viewport.h @@ -39,6 +39,8 @@ class MojoViewport { void SurfaceCreated(JNIEnv* env, jobject obj, jobject jsurface); void SurfaceDestroyed(JNIEnv* env, jobject obj); void SurfaceSetSize(JNIEnv* env, jobject obj, jint width, jint height); + bool TouchEvent(JNIEnv* env, jobject obj, jint pointer_id, jint action, + jfloat x, jfloat y, jlong time_ms); private: ~MojoViewport(); diff --git a/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java b/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java index 36d2440..f76ee9c 100644 --- a/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java +++ b/mojo/services/native_viewport/android/src/org/chromium/mojo/MojoViewport.java @@ -7,6 +7,7 @@ package org.chromium.mojo; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; +import android.view.MotionEvent; import android.view.Surface; import android.view.SurfaceHolder; import android.view.SurfaceView; @@ -62,9 +63,23 @@ public class MojoViewport extends SurfaceView { mNativeMojoViewport = 0; } + @Override + public boolean onTouchEvent(MotionEvent event) { + return nativeTouchEvent(mNativeMojoViewport, + event.getPointerId(0), + event.getAction(), + event.getX(), event.getY(), + event.getEventTime()); + } + private static native int nativeInit(int init); private static native void nativeDestroy(int nativeMojoViewport); private static native void nativeSurfaceCreated(int nativeMojoViewport, Surface surface); private static native void nativeSurfaceDestroyed(int nativeMojoViewport); private static native void nativeSurfaceSetSize(int nativeMojoViewport, int width, int height); + private static native boolean nativeTouchEvent(int nativeMojoViewport, + int pointerId, + int action, + float x, float y, + long timeMs); }; diff --git a/mojo/services/native_viewport/native_viewport_android.cc b/mojo/services/native_viewport/native_viewport_android.cc index fbb02f5..4340c4e 100644 --- a/mojo/services/native_viewport/native_viewport_android.cc +++ b/mojo/services/native_viewport/native_viewport_android.cc @@ -7,6 +7,8 @@ #include <android/native_window_jni.h> #include "mojo/services/native_viewport/android/mojo_viewport.h" #include "mojo/shell/context.h" +#include "ui/events/event.h" +#include "ui/gfx/point.h" namespace mojo { namespace services { @@ -14,6 +16,7 @@ namespace services { NativeViewportAndroid::NativeViewportAndroid(NativeViewportDelegate* delegate) : delegate_(delegate), window_(NULL), + id_generator_(0), weak_factory_(this) { } @@ -38,6 +41,20 @@ void NativeViewportAndroid::OnResized(const gfx::Size& size) { delegate_->OnResized(size); } +void NativeViewportAndroid::OnTouchEvent(int pointer_id, + ui::EventType action, + float x, float y, + int64 time_ms) { + gfx::Point location(static_cast<int>(x), static_cast<int>(y)); + ui::TouchEvent event(action, location, + id_generator_.GetGeneratedID(pointer_id), + base::TimeDelta::FromMilliseconds(time_ms)); + // TODO(beng): handle multiple touch-points. + delegate_->OnEvent(&event); + if (action == ui::ET_TOUCH_RELEASED) + id_generator_.ReleaseNumber(pointer_id); +} + void NativeViewportAndroid::ReleaseWindow() { ANativeWindow_release(window_); window_ = NULL; diff --git a/mojo/services/native_viewport/native_viewport_android.h b/mojo/services/native_viewport/native_viewport_android.h index ba77f83..1b366ab 100644 --- a/mojo/services/native_viewport/native_viewport_android.h +++ b/mojo/services/native_viewport/native_viewport_android.h @@ -7,6 +7,8 @@ #include "base/memory/weak_ptr.h" #include "mojo/services/native_viewport/native_viewport.h" +#include "ui/events/event_constants.h" +#include "ui/gfx/sequential_id_generator.h" #include "ui/gfx/size.h" namespace gpu { @@ -30,6 +32,8 @@ class NativeViewportAndroid : public NativeViewport { void OnNativeWindowCreated(ANativeWindow* window); void OnNativeWindowDestroyed(); void OnResized(const gfx::Size& size); + void OnTouchEvent(int pointer_id, ui::EventType action, float x, float y, + int64 time_ms); private: // Overridden from NativeViewport: @@ -41,6 +45,7 @@ class NativeViewportAndroid : public NativeViewport { NativeViewportDelegate* delegate_; ANativeWindow* window_; gfx::Size size_; + ui::SequentialIDGenerator id_generator_; base::WeakPtrFactory<NativeViewportAndroid> weak_factory_; |