Skip to content

Commit

Permalink
Fix taps on Android (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
grabbou authored Dec 11, 2016
1 parent 2aaf2ec commit d2d71cb
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package io.callstack.react.fbads;

import android.view.View;
import android.view.MotionEvent;

import com.facebook.ads.NativeAd;
import com.facebook.react.bridge.Arguments;
Expand All @@ -23,6 +23,12 @@ public class NativeAdView extends ReactViewGroup {
/** @{RCTEventEmitter} instance used for sending events back to JS **/
private RCTEventEmitter mEventEmitter;

/** @{float} x coordinate where the touch event started **/
private float startX;

/** @{float} y coordinate where the touche event started **/
private float startY;

/**
* Creates new NativeAdView instance and retrieves event emitter
*
Expand Down Expand Up @@ -73,4 +79,30 @@ public void setNativeAd(NativeAd nativeAd) {

mNativeAd.registerViewForInteraction(this);
}

/**
* If touch event is a click, simulate native event so that `FBAds` can
* trigger its listener
*
* @param {MotionEvent} ev
*
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
startX = ev.getX();
startY = ev.getY();
break;
case MotionEvent.ACTION_UP:
float deltaX = Math.abs(startX - ev.getX());
float deltaY = Math.abs(startY - ev.getY());
if (deltaX < 200 & deltaY < 200) {
performClick();
}
break;
}
return true;
}
}

0 comments on commit d2d71cb

Please sign in to comment.