From 3f07f457020355afb23e8c5bf1d4bbbd816a0f32 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 14 Oct 2013 13:50:50 +0100 Subject: [PATCH] Improved Android event callback mechanism. --- .../native/java/JuceAppActivity.java | 56 +++++++++++-------- .../native/juce_android_JNIHelpers.h | 2 +- .../native/juce_android_Windowing.cpp | 34 ++++------- 3 files changed, 44 insertions(+), 48 deletions(-) diff --git a/modules/juce_core/native/java/JuceAppActivity.java b/modules/juce_core/native/java/JuceAppActivity.java index 8c3aae4712..c7ba17891f 100644 --- a/modules/juce_core/native/java/JuceAppActivity.java +++ b/modules/juce_core/native/java/JuceAppActivity.java @@ -142,9 +142,9 @@ public final class JuceAppActivity extends Activity //============================================================================== private ViewHolder viewHolder; - public final ComponentPeerView createNewView (boolean opaque) + public final ComponentPeerView createNewView (boolean opaque, long host) { - ComponentPeerView v = new ComponentPeerView (this, opaque); + ComponentPeerView v = new ComponentPeerView (this, opaque, host); viewHolder.addView (v); return v; } @@ -312,9 +312,10 @@ public final class JuceAppActivity extends Activity public final class ComponentPeerView extends ViewGroup implements View.OnFocusChangeListener { - public ComponentPeerView (Context context, boolean opaque_) + public ComponentPeerView (Context context, boolean opaque_, long host) { super (context); + this.host = host; setWillNotDraw (false); opaque = opaque_; @@ -325,13 +326,12 @@ public final class JuceAppActivity extends Activity } //============================================================================== - private native void handlePaint (Canvas canvas); + private native void handlePaint (long host, Canvas canvas); @Override - public void draw (Canvas canvas) + public void onDraw (Canvas canvas) { - super.draw (canvas); - handlePaint (canvas); + handlePaint (host, canvas); } @Override @@ -341,11 +341,12 @@ public final class JuceAppActivity extends Activity } private boolean opaque; + private long host; //============================================================================== - private native void handleMouseDown (int index, float x, float y, long time); - private native void handleMouseDrag (int index, float x, float y, long time); - private native void handleMouseUp (int index, float x, float y, long time); + private native void handleMouseDown (long host, int index, float x, float y, long time); + private native void handleMouseDrag (long host, int index, float x, float y, long time); + private native void handleMouseUp (long host, int index, float x, float y, long time); @Override public boolean onTouchEvent (MotionEvent event) @@ -356,19 +357,19 @@ public final class JuceAppActivity extends Activity switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: - handleMouseDown (event.getPointerId(0), event.getX(), event.getY(), time); + handleMouseDown (host, event.getPointerId(0), event.getX(), event.getY(), time); return true; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: - handleMouseUp (event.getPointerId(0), event.getX(), event.getY(), time); + handleMouseUp (host, event.getPointerId(0), event.getX(), event.getY(), time); return true; case MotionEvent.ACTION_MOVE: { int n = event.getPointerCount(); for (int i = 0; i < n; ++i) - handleMouseDrag (event.getPointerId(i), event.getX(i), event.getY(i), time); + handleMouseDrag (host, event.getPointerId(i), event.getX(i), event.getY(i), time); return true; } @@ -376,14 +377,14 @@ public final class JuceAppActivity extends Activity case MotionEvent.ACTION_POINTER_UP: { int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; - handleMouseUp (event.getPointerId(i), event.getX(i), event.getY(i), time); + handleMouseUp (host, event.getPointerId(i), event.getX(i), event.getY(i), time); return true; } case MotionEvent.ACTION_POINTER_DOWN: { int i = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; - handleMouseDown (event.getPointerId(i), event.getX(i), event.getY(i), time); + handleMouseDown (host, event.getPointerId(i), event.getX(i), event.getY(i), time); return true; } @@ -395,8 +396,8 @@ public final class JuceAppActivity extends Activity } //============================================================================== - private native void handleKeyDown (int keycode, int textchar); - private native void handleKeyUp (int keycode, int textchar); + private native void handleKeyDown (long host, int keycode, int textchar); + private native void handleKeyUp (long host, int keycode, int textchar); public void showKeyboard (boolean shouldShow) { @@ -414,14 +415,23 @@ public final class JuceAppActivity extends Activity @Override public boolean onKeyDown (int keyCode, KeyEvent event) { - handleKeyDown (keyCode, event.getUnicodeChar()); + switch (keyCode) + { + case KeyEvent.KEYCODE_VOLUME_UP: + case KeyEvent.KEYCODE_VOLUME_DOWN: + return super.onKeyDown (keyCode, event); + + default: break; + } + + handleKeyDown (host, keyCode, event.getUnicodeChar()); return true; } @Override public boolean onKeyUp (int keyCode, KeyEvent event) { - handleKeyUp (keyCode, event.getUnicodeChar()); + handleKeyUp (host, keyCode, event.getUnicodeChar()); return true; } @@ -445,7 +455,7 @@ public final class JuceAppActivity extends Activity protected void onSizeChanged (int w, int h, int oldw, int oldh) { super.onSizeChanged (w, h, oldw, oldh); - viewSizeChanged(); + viewSizeChanged (host); } @Override @@ -455,16 +465,16 @@ public final class JuceAppActivity extends Activity requestTransparentRegion (getChildAt (i)); } - private native void viewSizeChanged(); + private native void viewSizeChanged (long host); @Override public void onFocusChange (View v, boolean hasFocus) { if (v == this) - focusChanged (hasFocus); + focusChanged (host, hasFocus); } - private native void focusChanged (boolean hasFocus); + private native void focusChanged (long host, boolean hasFocus); public void setViewName (String newName) {} diff --git a/modules/juce_core/native/juce_android_JNIHelpers.h b/modules/juce_core/native/juce_android_JNIHelpers.h index 5c3ee0eea4..92f2bbd42b 100644 --- a/modules/juce_core/native/juce_android_JNIHelpers.h +++ b/modules/juce_core/native/juce_android_JNIHelpers.h @@ -349,7 +349,7 @@ extern ThreadLocalJNIEnvHolder threadLocalJNIEnvHolder; //============================================================================== #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \ - METHOD (createNewView, "createNewView", "(Z)L" JUCE_ANDROID_ACTIVITY_CLASSPATH "$ComponentPeerView;") \ + METHOD (createNewView, "createNewView", "(ZJ)L" JUCE_ANDROID_ACTIVITY_CLASSPATH "$ComponentPeerView;") \ METHOD (deleteView, "deleteView", "(L" JUCE_ANDROID_ACTIVITY_CLASSPATH "$ComponentPeerView;)V") \ METHOD (postMessage, "postMessage", "(J)V") \ METHOD (finish, "finish", "()V") \ diff --git a/modules/juce_gui_basics/native/juce_android_Windowing.cpp b/modules/juce_gui_basics/native/juce_android_Windowing.cpp index 369a991bd6..fed413e8d8 100644 --- a/modules/juce_gui_basics/native/juce_android_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_android_Windowing.cpp @@ -111,7 +111,7 @@ public: // NB: must not put this in the initialiser list, as it invokes a callback, // which will fail if the peer is only half-constructed. view = GlobalRef (android.activity.callObjectMethod (JuceAppActivity.createNewView, - component.isOpaque())); + component.isOpaque(), (jlong) this)); if (isFocused()) handleFocusGain(); @@ -460,20 +460,6 @@ public: } //============================================================================== - static AndroidComponentPeer* findPeerForJavaView (JNIEnv* env, jobject viewToFind) - { - for (int i = getNumPeers(); --i >= 0;) - { - AndroidComponentPeer* const ap = static_cast (getPeer(i)); - jassert (dynamic_cast (getPeer(i)) != nullptr); - - if (env->IsSameObject (ap->view.get(), viewToFind)) - return ap; - } - - return nullptr; - } - static ModifierKeys currentModifiers; static Point lastMousePos; @@ -546,18 +532,18 @@ Point AndroidComponentPeer::lastMousePos; #define JUCE_VIEW_CALLBACK(returnType, javaMethodName, params, juceMethodInvocation) \ JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024ComponentPeerView), javaMethodName, returnType, params) \ { \ - if (AndroidComponentPeer* const peer = AndroidComponentPeer::findPeerForJavaView (env, view)) \ + if (AndroidComponentPeer* peer = (AndroidComponentPeer*) (pointer_sized_uint) host) \ peer->juceMethodInvocation; \ } -JUCE_VIEW_CALLBACK (void, handlePaint, (JNIEnv* env, jobject view, jobject canvas), handlePaintCallback (env, canvas)) -JUCE_VIEW_CALLBACK (void, handleMouseDown, (JNIEnv* env, jobject view, jint i, jfloat x, jfloat y, jlong time), handleMouseDownCallback (i, (float) x, (float) y, (int64) time)) -JUCE_VIEW_CALLBACK (void, handleMouseDrag, (JNIEnv* env, jobject view, jint i, jfloat x, jfloat y, jlong time), handleMouseDragCallback (i, (float) x, (float) y, (int64) time)) -JUCE_VIEW_CALLBACK (void, handleMouseUp, (JNIEnv* env, jobject view, jint i, jfloat x, jfloat y, jlong time), handleMouseUpCallback (i, (float) x, (float) y, (int64) time)) -JUCE_VIEW_CALLBACK (void, viewSizeChanged, (JNIEnv* env, jobject view), handleMovedOrResized()) -JUCE_VIEW_CALLBACK (void, focusChanged, (JNIEnv* env, jobject view, jboolean hasFocus), handleFocusChangeCallback (hasFocus)) -JUCE_VIEW_CALLBACK (void, handleKeyDown, (JNIEnv* env, jobject view, jint k, jint kc), handleKeyDownCallback ((int) k, (int) kc)) -JUCE_VIEW_CALLBACK (void, handleKeyUp, (JNIEnv* env, jobject view, jint k, jint kc), handleKeyUpCallback ((int) k, (int) kc)) +JUCE_VIEW_CALLBACK (void, handlePaint, (JNIEnv* env, jobject view, jlong host, jobject canvas), handlePaintCallback (env, canvas)) +JUCE_VIEW_CALLBACK (void, handleMouseDown, (JNIEnv* env, jobject view, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDownCallback (i, (float) x, (float) y, (int64) time)) +JUCE_VIEW_CALLBACK (void, handleMouseDrag, (JNIEnv* env, jobject view, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDragCallback (i, (float) x, (float) y, (int64) time)) +JUCE_VIEW_CALLBACK (void, handleMouseUp, (JNIEnv* env, jobject view, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseUpCallback (i, (float) x, (float) y, (int64) time)) +JUCE_VIEW_CALLBACK (void, viewSizeChanged, (JNIEnv* env, jobject view, jlong host), handleMovedOrResized()) +JUCE_VIEW_CALLBACK (void, focusChanged, (JNIEnv* env, jobject view, jlong host, jboolean hasFocus), handleFocusChangeCallback (hasFocus)) +JUCE_VIEW_CALLBACK (void, handleKeyDown, (JNIEnv* env, jobject view, jlong host, jint k, jint kc), handleKeyDownCallback ((int) k, (int) kc)) +JUCE_VIEW_CALLBACK (void, handleKeyUp, (JNIEnv* env, jobject view, jlong host, jint k, jint kc), handleKeyUpCallback ((int) k, (int) kc)) //============================================================================== ComponentPeer* Component::createNewPeer (int styleFlags, void*)