Repository navigation
fix: a swipe from the screen edge no longer opens the menu (#213) #216
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
android/app/src/main/java/com/openipc/decoder/TapGuard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright (c) OpenIPC https://openipc.org Prosperity Public License 3.0.0 | ||
| * | ||
| * TapGuard.java — tells a real tap on the picture from a swipe at the screen edge | ||
| * | ||
| */ | ||
|
|
||
| package com.openipc.decoder; | ||
|
|
||
| /** | ||
| * Follows one touch sequence on the screen (#213) and says whether it was a tap that may | ||
| * open the menu. Not a tap: a sequence that starts in the edge zone, where the system takes | ||
| * swipes to show its bars or go back; one whose finger moved more than the touch slop, also | ||
| * when the system kept the moves and only the up arrived (GestureDetector takes a down and a | ||
| * far-away up with no move between as a tap); one with a second finger (pinch); one that was | ||
| * cancelled. The answer is the one of the last finished sequence (up or cancel), so a tap | ||
| * confirmed after its up (GestureDetector waits for a double tap) or a click posted after it | ||
| * gets its own sequence's answer, not one of a new touch that has just begun. Coordinates are | ||
| * in pixels of the screen; no Android classes, for the unit tests. | ||
| */ | ||
| class TapGuard { | ||
| /** Edge zone where the system has no gesture insets (before Android 10, or 0 reported). */ | ||
| static final int EDGE_TOP_DP = 24; | ||
| static final int EDGE_BOTTOM_DP = 24; | ||
| static final int EDGE_SIDE_DP = 16; | ||
|
|
||
| private final float slop; | ||
| private boolean tap; | ||
| private boolean lastTap; | ||
| private float downX, downY; | ||
|
|
||
| TapGuard(float touchSlopPx) { | ||
| slop = touchSlopPx; | ||
| } | ||
|
|
||
| /** | ||
| * Whether (x, y) is in the edge zone of a w x h screen: within the given inset of | ||
| * each side (left, top, right, bottom). | ||
| */ | ||
| static boolean startsInEdgeZone(float x, float y, int w, int h, int[] insets) { | ||
| return x < insets[0] || y < insets[1] || x >= w - insets[2] || y >= h - insets[3]; | ||
| } | ||
|
|
||
| /** Per side the larger of the system's gesture inset and the fixed edge band. */ | ||
| static int[] edgeZone(int[] gestureInsets, float density) { | ||
| int[] fixed = { | ||
| Math.round(EDGE_SIDE_DP * density), Math.round(EDGE_TOP_DP * density), | ||
| Math.round(EDGE_SIDE_DP * density), Math.round(EDGE_BOTTOM_DP * density)}; | ||
| int[] zone = new int[4]; | ||
| for (int i = 0; i < 4; i++) { | ||
| zone[i] = Math.max(fixed[i], gestureInsets == null ? 0 : gestureInsets[i]); | ||
| } | ||
| return zone; | ||
| } | ||
|
|
||
| /** First finger down: a new sequence. */ | ||
| void down(float x, float y, int w, int h, int[] zone) { | ||
| downX = x; | ||
| downY = y; | ||
| tap = !startsInEdgeZone(x, y, w, h, zone); | ||
| } | ||
|
|
||
| /** A move of the first finger. */ | ||
| void moveTo(float x, float y) { | ||
| float dx = x - downX, dy = y - downY; | ||
| if (dx * dx + dy * dy > slop * slop) tap = false; | ||
| } | ||
|
|
||
| /** The up of the last finger: the sequence is finished. */ | ||
| void up(float x, float y) { | ||
| moveTo(x, y); | ||
| lastTap = tap; | ||
| } | ||
|
|
||
| /** A second finger: a pinch, not a tap. */ | ||
| void secondFinger() { | ||
| tap = false; | ||
| } | ||
|
|
||
| void cancel() { | ||
| tap = false; | ||
| lastTap = false; | ||
| } | ||
|
|
||
| /** Whether the last finished sequence was a tap that may open the menu. */ | ||
| boolean wasTap() { | ||
| return lastTap; | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
android/app/src/test/java/com/openipc/decoder/TapGuardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * Copyright (c) OpenIPC https://openipc.org Prosperity Public License 3.0.0 | ||
| * | ||
| * TapGuardTest.java — a swipe from the screen edge or a drag is no tap (#213) | ||
| * | ||
| */ | ||
|
|
||
| package com.openipc.decoder; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class TapGuardTest { | ||
| private static final int W = 1600, H = 720; | ||
| private static final int[] ZONE = {28, 47, 28, 47}; | ||
|
|
||
| @Test | ||
| public void edgeZoneOnEachSide() { | ||
| assertTrue(TapGuard.startsInEdgeZone(800, 0, W, H, ZONE)); | ||
| assertTrue(TapGuard.startsInEdgeZone(800, 46, W, H, ZONE)); | ||
| assertFalse(TapGuard.startsInEdgeZone(800, 47, W, H, ZONE)); | ||
| assertTrue(TapGuard.startsInEdgeZone(800, 719, W, H, ZONE)); | ||
| assertTrue(TapGuard.startsInEdgeZone(800, H - 47, W, H, ZONE)); | ||
| assertFalse(TapGuard.startsInEdgeZone(800, H - 48, W, H, ZONE)); | ||
| assertTrue(TapGuard.startsInEdgeZone(0, 360, W, H, ZONE)); | ||
| assertTrue(TapGuard.startsInEdgeZone(1599, 360, W, H, ZONE)); | ||
| assertFalse(TapGuard.startsInEdgeZone(800, 360, W, H, ZONE)); | ||
| } | ||
|
|
||
| @Test | ||
| public void zoneIsTheLargerOfGestureInsetsAndFixedBand() { | ||
| // 280 dpi: density 1.75; 16 dp = 28 px, 24 dp = 42 px | ||
| assertArrayEquals(new int[] {28, 42, 28, 42}, TapGuard.edgeZone(null, 1.75f)); | ||
| assertArrayEquals(new int[] {28, 42, 28, 42}, TapGuard.edgeZone(new int[4], 1.75f)); | ||
| // Gesture navigation: wide back zones left and right, a taller one at the bottom | ||
| assertArrayEquals(new int[] {53, 42, 53, 84}, | ||
| TapGuard.edgeZone(new int[] {53, 0, 53, 84}, 1.75f)); | ||
| } | ||
|
|
||
| @Test | ||
| public void tapInTheMiddleOpensTheMenu() { | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 400, W, H, ZONE); | ||
| g.moveTo(805, 403); | ||
| g.up(805, 403); | ||
| assertTrue(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void swipeFromTheTopEdgeIsNoTap() { | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 0, W, H, ZONE); | ||
| g.up(800, 2); | ||
| assertFalse(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void upFarAwayWithoutMovesIsNoTap() { | ||
| // GestureDetector takes this as a tap: the system kept the moves of its swipe | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 100, W, H, ZONE); | ||
| g.up(800, 400); | ||
| assertFalse(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void dragBackWithinSlopIsStillNoTap() { | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 400, W, H, ZONE); | ||
| g.moveTo(900, 400); | ||
| g.up(800, 400); | ||
| assertFalse(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void pinchAndCancelAreNoTap() { | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 400, W, H, ZONE); | ||
| g.secondFinger(); | ||
| g.up(800, 400); | ||
| assertFalse(g.wasTap()); | ||
| g.down(800, 400, W, H, ZONE); | ||
| g.cancel(); | ||
| assertFalse(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void answerIsTheLastFinishedSequence() { | ||
| // Qodo on #216: a new touch begun before a late tap confirmation does not change it | ||
| TapGuard g = new TapGuard(14); | ||
| g.down(800, 0, W, H, ZONE); | ||
| g.up(800, 300); | ||
| g.down(800, 400, W, H, ZONE); | ||
| assertFalse(g.wasTap()); | ||
| g.up(800, 400); | ||
| assertTrue(g.wasTap()); | ||
| g.down(800, 0, W, H, ZONE); | ||
| assertTrue(g.wasTap()); | ||
| g.up(800, 300); | ||
| assertFalse(g.wasTap()); | ||
| } | ||
|
|
||
| @Test | ||
| public void nothingTouchedIsNoTap() { | ||
| assertFalse(new TapGuard(14).wasTap()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.