Move gesture detectors and listeners in the GestureHelper class/file

This commit is contained in:
Antoine d'Aligny 2019-10-05 16:00:25 +02:00 committed by Daniel Micay
parent 0ac0102a8a
commit f7289be8a5
2 changed files with 84 additions and 43 deletions

View File

@ -0,0 +1,71 @@
package org.grapheneos.pdfviewer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
/*
The GestureHelper present a simple gesture api for the PdfViewer
*/
class GestureHelper {
public interface GestureListener {
boolean onTapUp();
// Can be replaced with ratio when supported
void onZoomIn(int steps);
void onZoomOut(int steps);
}
@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() {
// As the zoom value is discrete we listen to scaling step and not scaling ratio
float SPAN_STEP = 150;
float initialSpan;
int prevNbStep;
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
initialSpan = detector.getCurrentSpan();
prevNbStep = 0;
return super.onScaleBegin(detector);
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float spanDiff = initialSpan - detector.getCurrentSpan();
int curNbStep = (int) (spanDiff/SPAN_STEP);
if (curNbStep != prevNbStep) {
int stepDiff = curNbStep - prevNbStep;
if (stepDiff > 0) {
listener.onZoomOut(stepDiff);
} else {
listener.onZoomIn(Math.abs(stepDiff));
}
prevNbStep = curNbStep;
}
return true;
}
});
gestureView.setOnTouchListener((view, motionEvent) -> {
detector.onTouchEvent(motionEvent);
scaleDetector.onTouchEvent(motionEvent);
return false;
});
}
}

View File

@ -8,13 +8,10 @@ import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity; import android.view.Gravity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View; import android.view.View;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
@ -235,10 +232,10 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
showSystemUi(); showSystemUi();
final GestureDetector detector = new GestureDetector(PdfViewer.this, GestureHelper.attach(PdfViewer.this, mWebView,
new GestureDetector.SimpleOnGestureListener() { new GestureHelper.GestureListener() {
@Override @Override
public boolean onSingleTapUp(MotionEvent motionEvent) { public boolean onTapUp() {
if (mUri != null) { if (mUri != null) {
mWebView.evaluateJavascript("isTextSelected()", selection -> { mWebView.evaluateJavascript("isTextSelected()", selection -> {
if (!Boolean.valueOf(selection)) { if (!Boolean.valueOf(selection)) {
@ -254,49 +251,22 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
} }
return false; return false;
} }
});
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(PdfViewer.this,
new ScaleGestureDetector.SimpleOnScaleGestureListener() {
// As the zoom value is discrete we listen to scaling step and not scaling ratio
float SPAN_STEP = 150;
float initialSpan;
int prevNbStep;
@Override @Override
public boolean onScaleBegin(ScaleGestureDetector detector) { public void onZoomIn(int steps) {
initialSpan = detector.getCurrentSpan(); for (int i = 0; i < steps; i++) {
prevNbStep = 0; zoomIn();
return super.onScaleBegin(detector); }
} }
@Override @Override
public boolean onScale(ScaleGestureDetector detector) { public void onZoomOut(int steps) {
float spanDiff = initialSpan - detector.getCurrentSpan(); for (int i = 0; i < steps; i++) {
int curNbStep = (int) (spanDiff/SPAN_STEP); zoomOut();
if (curNbStep != prevNbStep) {
int stepDiff = curNbStep - prevNbStep;
if (stepDiff > 0) {
for (int i = prevNbStep; i < curNbStep; i++) {
zoomOut();
}
} else {
for (int i = prevNbStep; i > curNbStep; i--) {
zoomIn();
}
}
prevNbStep = curNbStep;
} }
return true;
} }
}); });
mWebView.setOnTouchListener((view, motionEvent) -> {
detector.onTouchEvent(motionEvent);
scaleDetector.onTouchEvent(motionEvent);
return false;
});
mTextView = new TextView(this); mTextView = new TextView(this);
mTextView.setBackgroundColor(Color.DKGRAY); mTextView.setBackgroundColor(Color.DKGRAY);
mTextView.setTextColor(ColorStateList.valueOf(Color.WHITE)); mTextView.setTextColor(ColorStateList.valueOf(Color.WHITE));