Skip to content
Open
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
35 changes: 27 additions & 8 deletions app/src/main/java/app/grapheneos/pdfviewer/GestureHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@
import android.view.ScaleGestureDetector;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/*
The GestureHelper present a simple gesture api for the PdfViewer
*/

class GestureHelper {
public interface GestureListener {
boolean onTapUp();
boolean onFling(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY);
void onZoom(float scaleFactor, float focusX, float focusY);
void onZoomEnd();
}

@SuppressLint("ClickableViewAccessibility")
static void attach(Context context, View gestureView, GestureListener listener) {

final GestureDetector detector = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return listener.onTapUp();
}
});

final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context,
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
Expand All @@ -45,6 +41,29 @@ public void onScaleEnd(ScaleGestureDetector detector) {
}
});

final GestureDetector detector = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(@NonNull MotionEvent motionEvent) {
return listener.onTapUp();
}

@Override
public boolean onFling(@Nullable MotionEvent e1, @NonNull MotionEvent e2,
float velocityX, float velocityY) {
if (scaleDetector.isInProgress()) {
return false;
}

// Ignore multi-touch
if (e1 != null && (e1.getPointerCount() > 1 || e2.getPointerCount() > 1)) {
return false;
}
Comment on lines +54 to +61
Copy link
Copy Markdown
Member

@inthewaves inthewaves Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android delivers touch events one at a time. When the user lifts their fingers from a pinch, this always happens as two separate events, one per finger (physically impossible to lift both fingers in the exact same instant)

The first finger to leave the screen produces a ACTION_POINTER_UP event. At this point, the ScaleGestureDetector sees that only one finger remains, so it ends the scale gesture and sets isInProgress() to false. however, GestureDetector does nothing special on ACTION_POINTER_UP; it never checks for flings on this event type. It only checks for flings when the last finger leaves the screen.

The second (last) finger to leave produces a different ACTION_UP event. Now GestureDetector wakes up: it checks the remaining finger's velocity, and if it's fast enough, fires onFling. But the scale already ended on the previous event. By the time onFling runs and checks isInProgress(), the answer is always false, as the scale ended one event ago.

This means the isInProgress() guard only works if both fingers are still touching the screen when onFling fires. But that never happens because onFling requires the last finger to lift (ACTION_UP), and the scale requires at least two fingers to stay active. The scale always ends first.

The same two-event split explains why the pointer count check also doesn't work as intended. The e1 event (the original ACTION_DOWN) was recorded when the first finger touched so that's one pointer. The e2 event (the ACTION_UP) is recorded when the last finger lifts so that's also one pointer, because the other finger already left on the earlier ACTION_POINTER_UP. GestureDetector never updates e1 when a second finger arrives, and e2 only ever sees the one finger that remains. So both e1.getPointerCount() and e2.getPointerCount() are 1, and the multi-touch guard sees a gesture that looks entirely single-finger.

Can add these logs to GestureHelper to confirm this:

@Override
public boolean onFling(@Nullable MotionEvent e1, @NonNull MotionEvent e2,
                        float velocityX, float velocityY) {
    Log.d(TAG, "onFling: isInProgress=" + scaleDetector.isInProgress()
            + " e1.pointers=" + (e1 != null ? e1.getPointerCount() : "null")
            + " e2.pointers=" + e2.getPointerCount()
            + " vX=" + velocityX + " vY=" + velocityY);
gestureView.setOnTouchListener((view, motionEvent) -> {
    final int action = motionEvent.getActionMasked();
    final boolean scaleInProgress = scaleDetector.isInProgress();

    if (action == MotionEvent.ACTION_DOWN) {
        Log.d(TAG, "ACTION_DOWN: pointers=" + motionEvent.getPointerCount());
    } else if (action == MotionEvent.ACTION_POINTER_DOWN) {
        Log.d(TAG, "ACTION_POINTER_DOWN: pointers=" + motionEvent.getPointerCount() + " isInProgress=" + scaleInProgress);
    } else if (action == MotionEvent.ACTION_POINTER_UP) {
        Log.d(TAG, "ACTION_POINTER_UP: pointers=" + motionEvent.getPointerCount() + " isInProgress=" + scaleInProgress);
    } else if (action == MotionEvent.ACTION_UP) {
        Log.d(TAG, "ACTION_UP: pointers=" + motionEvent.getPointerCount() + " isInProgress=" + scaleInProgress);
    }

10:51:17 is when I put both fingers down, 10:51:20 is when I released both fingers during a zoom gesture at the side. In terms of onFling, e1 happens at 10:51:17.887 (only 1 pointer), and e2 happens at 10:51:20.992.

2026-04-07 10:51:17.887  7592-7592  GestureHelper           app.grapheneos.pdfviewer.debug       D  ACTION_DOWN: pointers=1                            // This is e1 from onFling
2026-04-07 10:51:17.893  7592-7592  GestureHelper           app.grapheneos.pdfviewer.debug       D  ACTION_POINTER_DOWN: pointers=2 isInProgress=false
2026-04-07 10:51:20.991  7592-7592  GestureHelper           app.grapheneos.pdfviewer.debug       D  ACTION_POINTER_UP: pointers=2 isInProgress=true    // Scale ends after this event
2026-04-07 10:51:20.992  7592-7592  GestureHelper           app.grapheneos.pdfviewer.debug       D  ACTION_UP: pointers=1 isInProgress=false           // This is e2, i.e., what triggers the fling
2026-04-07 10:51:20.992  7592-7592  GestureHelper           app.grapheneos.pdfviewer.debug       D  onFling: isInProgress=false e1.pointers=1 e2.pointers=1 vX=145.1329 vY=67.527855

We could add a mWasScaling to GestureHelper inside of gestureView.setOnTouchListener and set that to true when scaleDetector.isInProgress(). This mWasScaling can replace both the scaleDetector and pointer count checks. Then only reset mWasScaling to false when user taps again, i.e., ACTION_DOWN is received


return listener.onFling(e1, e2, velocityX, velocityY);
}
});

gestureView.setOnTouchListener((view, motionEvent) -> {
detector.onTouchEvent(motionEvent);
scaleDetector.onTouchEvent(motionEvent);
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.RenderProcessGoneDetail;
Expand All @@ -28,6 +30,7 @@
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowCompat;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -125,6 +128,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private float mZoomRatio = 1f;
private float mZoomFocusX = 0f;
private float mZoomFocusY = 0f;
private int swipeThreshold;
private int swipeVelocityThreshold;
Comment on lines +131 to +132
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these should be prefixed with m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I saw the other PR, this is probably okay

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is based on the assumption that it will be on top of #616.

private int mDocumentOrientationDegrees;
private int mDocumentState;
private String mEncryptedDocumentPassword;
Expand Down Expand Up @@ -431,6 +436,8 @@ public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail)
}
});

initializeGestures();

GestureHelper.attach(PdfViewer.this, binding.webview,
new GestureHelper.GestureListener() {
@Override
Expand All @@ -450,6 +457,39 @@ public boolean onTapUp() {
return false;
}

@Override
public boolean onFling(@Nullable MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {
if (e1 == null) return false;

float deltaX = e2.getX() - e1.getX();
float deltaY = e2.getY() - e1.getY();
float absDeltaX = Math.abs(deltaX);
float absDeltaY = Math.abs(deltaY);

// Check primarily horizontal
if (absDeltaX > absDeltaY &&
absDeltaX > swipeThreshold &&
Math.abs(velocityX) > swipeVelocityThreshold) {

boolean swipeLeft = deltaX < 0;
boolean swipeRight = deltaX > 0;

// Edge detection
boolean atLeftEdge = !binding.webview.canScrollHorizontally(-1);
boolean atRightEdge = !binding.webview.canScrollHorizontally(1);

if (swipeLeft && atRightEdge) {
onJumpToPageInDocument(mPage + 1);
return true;
} else if (swipeRight && atLeftEdge) {
onJumpToPageInDocument(mPage - 1);
return true;
}
}

return false;
}

@Override
public void onZoom(float scaleFactor, float focusX, float focusY) {
zoom(scaleFactor, focusX, focusY, false);
Expand Down Expand Up @@ -520,6 +560,12 @@ public void onZoomEnd() {
}
}

private void initializeGestures() {
ViewConfiguration vc = ViewConfiguration.get(this);
swipeThreshold = vc.getScaledTouchSlop() * 4;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit too sensitive, maybe 6 would work better

swipeVelocityThreshold = vc.getScaledMinimumFlingVelocity();
}

private void purgeWebView() {
binding.webview.removeJavascriptInterface("channel");
binding.getRoot().removeView(binding.webview);
Expand Down