Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): fire touch events synchronously #12340

Merged
merged 4 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public boolean onFilterTouchEventForSecurity(MotionEvent event)
{
boolean isTouchAllowed = super.onFilterTouchEventForSecurity(event);
if (!isTouchAllowed) {
fireEvent(TiC.EVENT_TOUCH_FILTERED, dictFromEvent(event));
fireSyncEvent(TiC.EVENT_TOUCH_FILTERED, dictFromEvent(event));
}
return isTouchAllowed;
}
Expand Down
6 changes: 3 additions & 3 deletions android/runtime/common/src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Object.defineProperty(EventEmitter.prototype, 'callHandler', {

// Bubble the events to the parent view if needed.
if (data.bubbles && !cancelBubble) {
handled = this._fireEventToParent(type, data) || handled;
handled = this._fireSyncEventToParent(type, data) || handled;
}

return handled;
Expand Down Expand Up @@ -92,7 +92,7 @@ Object.defineProperty(EventEmitter.prototype, 'emit', {

if (!this._events || !this._events[type] || !this.callHandler) {
if (data.bubbles && !data.cancelBubble) {
handled = this._fireEventToParent(type, data);
handled = this._fireSyncEventToParent(type, data);
}
return handled;
}
Expand All @@ -108,7 +108,7 @@ Object.defineProperty(EventEmitter.prototype, 'emit', {
}

} else if (data.bubbles && !data.cancelBubble) {
handled = this._fireEventToParent(type, data);
handled = this._fireSyncEventToParent(type, data);
}

return handled;
Expand Down
19 changes: 19 additions & 0 deletions android/titanium/src/java/org/appcelerator/kroll/KrollProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,25 @@ public boolean fireSyncEvent(String event, Object data, long maxTimeout)
}
}

/**
* Fires an event synchronously to the view who is next to receive the event.
*
* @param eventName event to send to the next view
* @param data the data to include in the event
* @return true if the event was handled
*/
@Kroll.method(name = "_fireSyncEventToParent")
public boolean fireSyncEventToParent(String eventName, Object data)
{
if (bubbleParent) {
KrollProxy parentProxy = getParentForBubbling();
if (parentProxy != null) {
return parentProxy.fireSyncEvent(eventName, data);
}
}
return false;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean doFireEvent(String event, Object data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,13 +961,9 @@ public boolean fireEvent(String eventName, Object data, boolean bubbles)
if (data == null) {
data = new KrollDict();
}

// Set the "bubbles" property to indicate if the event needs to be bubbled.
if (data instanceof HashMap) {
((HashMap) data).put(TiC.PROPERTY_BUBBLES, bubbles);
}

// Dispatch the event to JavaScript which takes care of the bubbling.
return super.fireEvent(eventName, data);
}

Expand All @@ -977,12 +973,27 @@ public boolean fireEvent(String eventName, Object data, boolean bubbles)
@Override
public boolean fireEvent(String eventName, Object data)
{
// To remain compatible this override of fireEvent will always
// bubble the event to the parent view. It should eventually be deprecated
// in favor of using the fireEvent(String, Object, boolean) method.
return fireEvent(eventName, data, true);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean fireSyncEvent(String eventName, Object data, boolean bubbles)
{
if (data == null) {
data = new KrollDict();
}
if (data instanceof HashMap) {
((HashMap) data).put(TiC.PROPERTY_BUBBLES, bubbles);
}
return super.fireSyncEvent(eventName, data);
}

@Override
public boolean fireSyncEvent(String eventName, Object data)
{
return fireSyncEvent(eventName, data, true);
}

/**
* @return The parent view proxy of this view proxy.
* @module.api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ public boolean onScale(ScaleGestureDetector sgd)
data.put(TiC.EVENT_PROPERTY_VELOCITY, (sgd.getScaleFactor() - 1.0f) / timeDelta * 1000);
data.put(TiC.EVENT_PROPERTY_SOURCE, proxy);

return fireEvent(TiC.EVENT_PINCH, data);
return fireSyncEvent(TiC.EVENT_PINCH, data);
}
return false;
}
Expand Down Expand Up @@ -1808,7 +1808,7 @@ public boolean onTouch(View view, MotionEvent event)
String motionEvent = motionEvents.get(event.getAction());
if (motionEvent != null) {
if (proxy != null && proxy.hierarchyHasListener(motionEvent)) {
fireEvent(motionEvent, dictFromEvent(event));
fireSyncEvent(motionEvent, dictFromEvent(event));
}
}

Expand Down Expand Up @@ -2060,6 +2060,24 @@ public boolean fireEvent(String eventName, KrollDict data, boolean bubbles)
return proxy.fireEvent(eventName, data, bubbles);
}

public boolean fireSyncEvent(String eventName, KrollDict data)
{
return fireSyncEvent(eventName, data, true);
}

public boolean fireSyncEvent(String eventName, KrollDict data, boolean bubbles)
{
if (proxy == null) {
return false;
}
if (data == null && additionalEventData != null) {
data = new KrollDict(additionalEventData);
} else if (additionalEventData != null) {
data.putAll(additionalEventData);
}
return proxy.fireSyncEvent(eventName, data, bubbles);
}

protected void setOnLongClickListener(View view)
{
view.setOnLongClickListener(new OnLongClickListener() {
Expand Down