Skip to content

Commit c6321bb

Browse files
committed
fix: android fixes for native gestures
1 parent 3b2d2ec commit c6321bb

File tree

7 files changed

+78
-25
lines changed

7 files changed

+78
-25
lines changed

plugin/platforms/android/java/com/nativescript/gesturehandler/GestureHandlerInteractionController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import android.view.KeyEvent;
88
import android.util.AttributeSet;
99
import android.view.inputmethod.InputMethodManager;
10+
import android.util.Log;
1011

1112
import com.swmansion.gesturehandler.GestureHandler;
1213

1314
import java.util.HashMap;
1415

15-
public class GestureHandlerInteractionController
16-
implements com.swmansion.gesturehandler.GestureHandlerInteractionController {
16+
public class GestureHandlerInteractionController implements com.swmansion.gesturehandler.GestureHandlerInteractionController {
1717
private HashMap<Number, int[]> mWaitForRelations = new HashMap();
1818
private HashMap<Number, int[]> mSimultaneousRelations = new HashMap();
1919

@@ -59,6 +59,9 @@ public boolean shouldHandlerBeCancelledBy(GestureHandler handler, GestureHandler
5959

6060
public boolean shouldRecognizeSimultaneously(GestureHandler handler, GestureHandler otherHandler) {
6161
int[] simultHandlerTags = mSimultaneousRelations.get(handler.getTag());
62+
if (GestureHandler.debug) {
63+
Log.d("JS", "GestureHandlerInteractionController shouldRecognizeSimultaneously " + handler + " " + otherHandler + " " + simultHandlerTags);
64+
}
6265
if (simultHandlerTags != null) {
6366
for (int i = 0; i < simultHandlerTags.length; i++) {
6467
if (simultHandlerTags[i] == otherHandler.getTag()) {

plugin/platforms/android/java/com/nativescript/gesturehandler/PageLayout.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import android.view.MotionEvent;
55
import android.view.View;
66
import android.view.ViewGroup;
7+
import android.util.Log;
78

89
import org.nativescript.widgets.ItemSpec;
910
import org.nativescript.widgets.GridUnitType;
1011

1112
import com.swmansion.gesturehandler.GestureHandlerOrchestrator;
1213
import com.swmansion.gesturehandler.GestureHandlerRegistryImpl;
1314
import com.swmansion.gesturehandler.PointerEventsConfig;
15+
import com.swmansion.gesturehandler.GestureHandler;
1416
import com.swmansion.gesturehandler.ViewConfigurationHelper;
1517

1618
public class PageLayout extends org.nativescript.widgets.GridLayout {
@@ -30,6 +32,9 @@ public PageLayout(Context context) {
3032
private boolean mPassingTouch = false;
3133

3234
public void setShouldIntercept(boolean value) {
35+
if (GestureHandler.debug) {
36+
Log.d("JS", "PageLayout setShouldIntercept " + value);
37+
}
3338
this.mShouldIntercept = value;
3439
}
3540

@@ -55,6 +60,9 @@ public GestureHandlerRegistryImpl registry() {
5560
// return super.dispatchTouchEvent(ev);
5661
// }
5762
public void tryCancelAllHandlers() {
63+
if (GestureHandler.debug) {
64+
Log.d("JS", "PageLayout tryCancelAllHandlers ");
65+
}
5866
// In order to cancel handlers we activate handler that is hooked to the root view
5967
if (this.rootGestureHandler != null && this.rootGestureHandler.getState() == com.swmansion.gesturehandler.GestureHandler.STATE_BEGAN) {
6068
// Try activate main JS handler
@@ -64,6 +72,9 @@ public void tryCancelAllHandlers() {
6472
}
6573

6674
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
75+
if (GestureHandler.debug) {
76+
Log.d("JS", "PageLayout requestDisallowInterceptTouchEvent " + disallowIntercept + " " + this.mPassingTouch);
77+
}
6778
// If this method gets called it means that some native view is attempting to grab lock for
6879
// touch event delivery. In that case we cancel all gesture recognizers
6980
if (this.mOrchestrator != null && !this.mPassingTouch) {
@@ -78,7 +89,9 @@ public boolean dispatchTouchEventToOrchestrator(MotionEvent ev) {
7889
this.mPassingTouch = true;
7990
this.mOrchestrator.onTouchEvent(ev);
8091
this.mPassingTouch = false;
81-
92+
// if (GestureHandler.debug) {
93+
// Log.d("JS", "PageLayout dispatchTouchEventToOrchestrator " + this.mShouldIntercept);
94+
// }
8295
return this.mShouldIntercept;
8396
}
8497

@@ -106,7 +119,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
106119
* handler library logic. Unless this method is called (which happens as a result of instantiating
107120
* new gesture handler from JS) the root view component will just proxy all touch related methods
108121
* to its superclass. Thus in the "disabled" state all touch related events will fallback to
109-
* default RN behavior.
122+
* default behavior.
110123
*/
111124
public void initialize() {
112125
this.mRegistry = new com.swmansion.gesturehandler.GestureHandlerRegistryImpl();
@@ -124,22 +137,17 @@ public View getChildInDrawingOrderAtIndex(ViewGroup parent, int index) {
124137
}
125138
};
126139
this.mOrchestrator = new com.swmansion.gesturehandler.GestureHandlerOrchestrator(this, this.mRegistry, this.configurationHelper);
127-
// console.log(this.constructor.name, 'initialize', this.mOrchestrator, this.mRegistry);
128140
this.mOrchestrator.setMinimumAlphaForTraversal(0.01f);
129141

130142
this.rootGestureHandler = new RootViewGestureHandler();
131143
this.rootGestureHandler.setTag(GESTURE_HANDLER_TAG);
132144
this.mRegistry.registerHandler(this.rootGestureHandler);
133-
// registry.attachHandlerToView(this.rootGestureHandler.getTag(), this);
145+
this.mRegistry.attachHandlerToView(GESTURE_HANDLER_TAG, this);
134146
}
135147

136148
public void tearDown() {
137149
this.configurationHelper = null;
138150
this.mOrchestrator = null;
139151
this.mRegistry = null;
140-
// if (this.mGestureRootHelper != null) {
141-
// this.mGestureRootHelper.tearDown();
142-
// this.mGestureRootHelper = null;
143-
// }
144152
}
145153
}

plugin/platforms/android/java/com/nativescript/gesturehandler/RootViewGestureHandler.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import com.swmansion.gesturehandler.GestureHandler;
66
import android.os.SystemClock;
77
import android.view.View;
8-
8+
import android.util.Log;
99

1010
public class RootViewGestureHandler extends GestureHandler {
11+
private static final String TAG = "RootViewGestureHandler";
1112
public RootViewGestureHandler() {
1213
super();
1314
}
@@ -19,17 +20,24 @@ protected void onHandle(MotionEvent event) {
1920
final int currentState = getState();
2021
if (currentState == GestureHandler.STATE_UNDETERMINED) {
2122
begin();
22-
getView().setShouldIntercept(true);
23+
getView().setShouldIntercept(false);
2324
}
2425
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
2526
end();
2627
}
2728
}
2829

2930
protected void onCancel() {
30-
getView().setShouldIntercept(false);
31+
if (GestureHandler.debug) {
32+
Log.d("JS", "RootViewGestureHandler onCancel");
33+
}
34+
getView().setShouldIntercept(true);
3135
final long time = SystemClock.uptimeMillis();
3236
final MotionEvent event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0, 0, 0);
3337
event.setAction(MotionEvent.ACTION_CANCEL);
38+
getView().onTouchEvent(event);
3439
}
40+
// public boolean shouldRecognizeSimultaneously(GestureHandler handler) {
41+
// return true;
42+
// }
3543
}

plugin/platforms/android/java/com/swmansion/gesturehandler/FlingGestureHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import android.os.Handler;
44
import android.view.MotionEvent;
5+
import android.util.Log;
56

67
public class FlingGestureHandler extends GestureHandler<FlingGestureHandler> {
8+
private static final String TAG = "FlingGestureHandler";
79
private static final long DEFAULT_MAX_DURATION_MS = 800;
810
private static final long DEFAULT_MIN_ACCEPTABLE_DELTA = 160;
911
private static final int DEFAULT_DIRECTION = DIRECTION_RIGHT | DIRECTION_LEFT | DIRECTION_UP | DIRECTION_DOWN;
@@ -74,6 +76,9 @@ private boolean tryEndFling(MotionEvent event) {
7476
}
7577

7678
private void endFling(MotionEvent event) {
79+
if (GestureHandler.debug) {
80+
Log.d("JS", "FlingGestureHandler endFling");
81+
}
7782
if (!tryEndFling(event)) {
7883
fail();
7984
}
@@ -103,6 +108,9 @@ protected void onHandle(MotionEvent event) {
103108

104109
@Override
105110
protected void onCancel() {
111+
if (GestureHandler.debug) {
112+
Log.d("JS", "FlingGestureHandler onCancel");
113+
}
106114
if (mHandler != null) {
107115
mHandler.removeCallbacksAndMessages(null);
108116
}

plugin/platforms/android/java/com/swmansion/gesturehandler/GestureHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
public class GestureHandler<T extends GestureHandler> {
1111
private final String TAG = "GestureHandler";
12+
public static boolean debug = false;
1213

1314
public static final int STATE_UNDETERMINED = 0;
1415
public static final int STATE_FAILED = 1;
@@ -452,6 +453,9 @@ public final void cancel() {
452453
}
453454

454455
public final void fail() {
456+
if (GestureHandler.debug) {
457+
Log.d("JS", "GestureHandler fail " + this);
458+
}
455459
if (mState == STATE_ACTIVE || mState == STATE_UNDETERMINED || mState == STATE_BEGAN) {
456460
moveToState(STATE_FAILED);
457461
}

plugin/platforms/android/java/com/swmansion/gesturehandler/GestureHandlerOrchestrator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ private void cleanupAwaitingHandlers() {
174174

175175
void onHandlerStateChange(GestureHandler handler, int newState, int prevState) {
176176
mHandlingChangeSemaphore += 1;
177+
if (GestureHandler.debug) {
178+
Log.d("JS", "GestureHandlerOrchestrator onHandlerStateChange " + handler + " " + prevState + " " + newState);
179+
}
177180
if (isFinished(newState)) {
181+
178182
// if there were handlers awaiting completion of this handler, we can trigger
179183
// active state
180184
for (int i = 0; i < mAwaitingHandlersCount; i++) {
@@ -212,13 +216,6 @@ private void makeActive(GestureHandler handler) {
212216
handler.mIsActive = true;
213217
handler.mActivationIndex = mActivationIndex++;
214218

215-
// TODO: for now we do it the same as android and disable
216-
// native gestures once a gesture is recognized
217-
long time = SystemClock.uptimeMillis();
218-
MotionEvent event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0, 0, 0);
219-
event.setAction(MotionEvent.ACTION_CANCEL);
220-
handler.getView().dispatchTouchEvent(event);
221-
222219
int toCancelCount = 0;
223220
// Cancel all handlers that are required to be cancel upon current handler's
224221
// activation
@@ -541,6 +538,9 @@ private static boolean canRunSimultaneously(GestureHandler a, GestureHandler b)
541538

542539
private static boolean shouldHandlerBeCancelledBy(GestureHandler handler, GestureHandler other) {
543540

541+
if (GestureHandler.debug) {
542+
Log.d("JS", "GestureHandlerOrchestrator shouldHandlerBeCancelledBy " + handler + " " + other + " " + handler.hasCommonPointers(other) + " " + canRunSimultaneously(handler, other) + " " + handler.shouldBeCancelledBy(other));
543+
}
544544
if (!handler.hasCommonPointers(other)) {
545545
// if two handlers share no common pointer one can never trigger cancel for the
546546
// other

plugin/platforms/android/java/com/swmansion/gesturehandler/NativeViewGestureHandler.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public boolean shouldRecognizeSimultaneously(GestureHandler handler) {
5656
}
5757

5858
boolean canBeInterrupted = !mDisallowInterruption;
59+
60+
if (GestureHandler.debug) {
61+
Log.d("JS", "NativeViewGestureHandler shouldRecognizeSimultaneously " + canBeInterrupted);
62+
}
5963
int state = getState();
6064
int otherState = handler.getState();
6165

@@ -73,47 +77,65 @@ public boolean shouldRecognizeSimultaneously(GestureHandler handler) {
7377

7478
@Override
7579
public boolean shouldBeCancelledBy(GestureHandler handler) {
80+
81+
// if (GestureHandler.debug) {
82+
// Log.d("JS", "NativeViewGestureHandler shouldBeCancelledBy " + mDisallowInterruption);
83+
// }
7684
return !mDisallowInterruption;
7785
}
7886

7987
@Override
8088
protected void onHandle(MotionEvent event) {
8189
View view = getView();
8290
int state = getState();
91+
if (GestureHandler.debug) {
92+
Log.d("JS", "NativeViewGestureHandler onHandle " + state + " " + view +" " + event.getActionMasked() + " " + " " + mShouldActivateOnStart);
93+
}
8394
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
84-
view.onTouchEvent(event);
95+
view.dispatchTouchEvent(event);
8596
if ((state == STATE_UNDETERMINED || state == STATE_BEGAN) && view.isPressed()) {
8697
activate();
8798
}
8899
end();
89100
} else if (state == STATE_UNDETERMINED || state == STATE_BEGAN) {
101+
90102
if (mShouldActivateOnStart) {
91103
tryIntercept(view, event);
92-
view.onTouchEvent(event);
104+
view.dispatchTouchEvent(event);
93105
activate();
94106
} else if (tryIntercept(view, event)) {
95-
view.onTouchEvent(event);
107+
view.dispatchTouchEvent(event);
96108
activate();
97109
} else if (state != STATE_BEGAN) {
98110
begin();
99111
}
100112
} else if (state == STATE_ACTIVE) {
101-
view.onTouchEvent(event);
113+
view.dispatchTouchEvent(event);
102114
}
103115
}
104116

105117
private static boolean tryIntercept(View view, MotionEvent event) {
106118
if (view instanceof ViewGroup && ((ViewGroup) view).onInterceptTouchEvent(event)) {
119+
// if (GestureHandler.debug) {
120+
// Log.d("JS", "NativeViewGestureHandler tryIntercept true");
121+
// }
107122
return true;
108123
}
124+
// if (GestureHandler.debug) {
125+
// Log.d("JS", "NativeViewGestureHandler tryIntercept false");
126+
// }
109127
return false;
110128
}
111129

112130
@Override
113131
protected void onCancel() {
132+
133+
if (GestureHandler.debug) {
134+
Log.d("JS", "NativeViewGestureHandler onCancel " + getView());
135+
}
114136
long time = SystemClock.uptimeMillis();
115137
MotionEvent event = MotionEvent.obtain(time, time, MotionEvent.ACTION_CANCEL, 0, 0, 0);
116138
event.setAction(MotionEvent.ACTION_CANCEL);
117-
getView().dispatchTouchEvent(event);
139+
getView().onTouchEvent(event);
118140
}
119141
}

0 commit comments

Comments
 (0)