| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import android.graphics.PointF; |
| 9 |
import android.graphics.RectF; |
| 10 |
import android.util.Log; |
| 11 |
import android.view.GestureDetector; |
| 12 |
import android.view.InputDevice; |
| 13 |
import android.view.MotionEvent; |
| 14 |
import android.view.View; |
| 15 |
|
| 16 |
import org.libreoffice.LOKitShell; |
| 17 |
import org.libreoffice.LibreOfficeMainActivity; |
| 18 |
import org.mozilla.gecko.ZoomConstraints; |
| 19 |
import org.mozilla.gecko.util.FloatUtils; |
| 20 |
|
| 21 |
import java.util.Timer; |
| 22 |
import java.util.TimerTask; |
| 23 |
import java.lang.StrictMath; |
| 24 |
|
| 25 |
/* |
| 26 |
* Handles the kinetic scrolling and zooming physics for a layer controller. |
| 27 |
* |
| 28 |
* Many ideas are from Joe Hewitt's Scrollability: |
| 29 |
* https://github.com/joehewitt/scrollability/ |
| 30 |
*/ |
| 31 |
class JavaPanZoomController |
| 32 |
extends GestureDetector.SimpleOnGestureListener |
| 33 |
implements SimpleScaleGestureDetector.SimpleScaleGestureListener |
| 34 |
{ |
| 35 |
private static final String LOGTAG = "GeckoPanZoomController"; |
| 36 |
|
| 37 |
// Animation stops if the velocity is below this value when overscrolled or panning. |
| 38 |
private static final float STOPPED_THRESHOLD = 4.0f; |
| 39 |
|
| 40 |
// Animation stops is the velocity is below this threshold when flinging. |
| 41 |
private static final float FLING_STOPPED_THRESHOLD = 0.1f; |
| 42 |
|
| 43 |
// The distance the user has to pan before we recognize it as such (e.g. to avoid 1-pixel pans |
| 44 |
// between the touch-down and touch-up of a click). In units of density-independent pixels. |
| 45 |
private final float PAN_THRESHOLD; |
| 46 |
|
| 47 |
// Angle from axis within which we stay axis-locked |
| 48 |
private static final double AXIS_LOCK_ANGLE = Math.PI / 6.0; // 30 degrees |
| 49 |
|
| 50 |
// The maximum amount we allow you to zoom into a page |
| 51 |
private static final float MAX_ZOOM = 4.0f; |
| 52 |
|
| 53 |
// The threshold zoom factor of whether a double tap triggers zoom-in or zoom-out |
| 54 |
private static final float DOUBLE_TAP_THRESHOLD = 1.0f; |
| 55 |
|
| 56 |
// The maximum amount we would like to scroll with the mouse |
| 57 |
private final float MAX_SCROLL; |
| 58 |
|
| 59 |
private enum PanZoomState { |
| 60 |
NOTHING, /* no touch-start events received */ |
| 61 |
FLING, /* all touches removed, but we're still scrolling page */ |
| 62 |
TOUCHING, /* one touch-start event received */ |
| 63 |
PANNING_LOCKED, /* touch-start followed by move (i.e. panning with axis lock) */ |
| 64 |
PANNING, /* panning without axis lock */ |
| 65 |
PANNING_HOLD, /* in panning, but not moving. |
| 66 |
* similar to TOUCHING but after starting a pan */ |
| 67 |
PANNING_HOLD_LOCKED, /* like PANNING_HOLD, but axis lock still in effect */ |
| 68 |
PINCHING, /* nth touch-start, where n > 1. this mode allows pan and zoom */ |
| 69 |
ANIMATED_ZOOM, /* animated zoom to a new rect */ |
| 70 |
BOUNCE, /* in a bounce animation */ |
| 71 |
} |
| 72 |
|
| 73 |
private final PanZoomTarget mTarget; |
| 74 |
private final Axis mX; |
| 75 |
private final Axis mY; |
| 76 |
private final TouchEventHandler mTouchEventHandler; |
| 77 |
private final LibreOfficeMainActivity mContext; |
| 78 |
|
| 79 |
/* The timer that handles flings or bounces. */ |
| 80 |
private Timer mAnimationTimer; |
| 81 |
/* The runnable being scheduled by the animation timer. */ |
| 82 |
private AnimationRunnable mAnimationRunnable; |
| 83 |
/* The zoom focus at the first zoom event (in page coordinates). */ |
| 84 |
private PointF mLastZoomFocus; |
| 85 |
/* The time the last motion event took place. */ |
| 86 |
private long mLastEventTime; |
| 87 |
/* Current state the pan/zoom UI is in. */ |
| 88 |
private PanZoomState mState; |
| 89 |
/* Whether or not to wait for a double-tap before dispatching a single-tap */ |
| 90 |
private boolean mWaitForDoubleTap; |
| 91 |
|
| 92 |
JavaPanZoomController(LibreOfficeMainActivity context, PanZoomTarget target, View view) { |
| 93 |
mContext = context; |
| 94 |
PAN_THRESHOLD = 1/16f * LOKitShell.getDpi(view.getContext()); |
| 95 |
MAX_SCROLL = 0.075f * LOKitShell.getDpi(view.getContext()); |
| 96 |
mTarget = target; |
| 97 |
mX = new AxisX(); |
| 98 |
mY = new AxisY(); |
| 99 |
mTouchEventHandler = new TouchEventHandler(view.getContext(), this); |
| 100 |
|
| 101 |
checkMainThread(); |
| 102 |
|
| 103 |
setState(PanZoomState.NOTHING); |
| 104 |
} |
| 105 |
|
| 106 |
private static float easeOut(float t) { |
| 107 |
// ease-out approx. |
| 108 |
// -(t-1)^2+1 |
| 109 |
t = t-1; |
| 110 |
return -t*t+1; |
| 111 |
} |
| 112 |
|
| 113 |
private void setState(PanZoomState state) { |
| 114 |
if (state != mState) { |
| 115 |
mState = state; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private ImmutableViewportMetrics getMetrics() { |
| 120 |
return mTarget.getViewportMetrics(); |
| 121 |
} |
| 122 |
|
| 123 |
// for debugging bug 713011; it can be taken out once that is resolved. |
| 124 |
private void checkMainThread() { |
| 125 |
if (mContext.getMainLooper().getThread() != Thread.currentThread()) { |
| 126 |
// log with full stack trace |
| 127 |
Log.e(LOGTAG, "Uh-oh, we're running on the wrong thread!", new Exception()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** This function MUST be called on the UI thread */ |
| 132 |
public boolean onMotionEvent(MotionEvent event) { |
| 133 |
if ((event.getSource() & InputDevice.SOURCE_CLASS_MASK) == InputDevice.SOURCE_CLASS_POINTER |
| 134 |
&& (event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_SCROLL) { |
| 135 |
return handlePointerScroll(event); |
| 136 |
} |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/** This function MUST be called on the UI thread */ |
| 141 |
public boolean onTouchEvent(MotionEvent event) { |
| 142 |
return mTouchEventHandler.handleEvent(event); |
| 143 |
} |
| 144 |
|
| 145 |
boolean handleEvent(MotionEvent event) { |
| 146 |
switch (event.getAction() & MotionEvent.ACTION_MASK) { |
| 147 |
case MotionEvent.ACTION_DOWN: return handleTouchStart(event); |
| 148 |
case MotionEvent.ACTION_MOVE: return handleTouchMove(event); |
| 149 |
case MotionEvent.ACTION_UP: return handleTouchEnd(event); |
| 150 |
case MotionEvent.ACTION_CANCEL: return handleTouchCancel(event); |
| 151 |
} |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
/** This must be called on the UI thread. */ |
| 156 |
public void pageRectUpdated() { |
| 157 |
if (mState == PanZoomState.NOTHING) { |
| 158 |
synchronized (mTarget.getLock()) { |
| 159 |
ImmutableViewportMetrics validated = getValidViewportMetrics(); |
| 160 |
if (!getMetrics().fuzzyEquals(validated)) { |
| 161 |
// page size changed such that we are now in overscroll. snap to |
| 162 |
// the nearest valid viewport |
| 163 |
mTarget.setViewportMetrics(validated); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* Panning/scrolling |
| 171 |
*/ |
| 172 |
|
| 173 |
private boolean handleTouchStart(MotionEvent event) { |
| 174 |
// user is taking control of movement, so stop |
| 175 |
// any auto-movement we have going |
| 176 |
stopAnimationTimer(); |
| 177 |
|
| 178 |
switch (mState) { |
| 179 |
case ANIMATED_ZOOM: |
| 180 |
// We just interrupted a double-tap animation, so force a redraw in |
| 181 |
// case this touchstart is just a tap that doesn't end up triggering |
| 182 |
// a redraw |
| 183 |
mTarget.forceRedraw(); |
| 184 |
// fall through |
| 185 |
case FLING: |
| 186 |
case BOUNCE: |
| 187 |
case NOTHING: |
| 188 |
startTouch(event.getX(0), event.getY(0), event.getEventTime()); |
| 189 |
return false; |
| 190 |
case TOUCHING: |
| 191 |
case PANNING: |
| 192 |
case PANNING_LOCKED: |
| 193 |
case PANNING_HOLD: |
| 194 |
case PANNING_HOLD_LOCKED: |
| 195 |
case PINCHING: |
| 196 |
Log.e(LOGTAG, "Received impossible touch down while in " + mState); |
| 197 |
return false; |
| 198 |
} |
| 199 |
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchStart"); |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
private boolean handleTouchMove(MotionEvent event) { |
| 204 |
if (mState == PanZoomState.PANNING_LOCKED || mState == PanZoomState.PANNING) { |
| 205 |
if (getVelocity() > 18.0f) { |
| 206 |
mContext.hideSoftKeyboard(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
switch (mState) { |
| 211 |
case FLING: |
| 212 |
case BOUNCE: |
| 213 |
// should never happen |
| 214 |
Log.e(LOGTAG, "Received impossible touch move while in " + mState); |
| 215 |
// fall through |
| 216 |
case ANIMATED_ZOOM: |
| 217 |
case NOTHING: |
| 218 |
// may happen if user double-taps and drags without lifting after the |
| 219 |
// second tap. ignore the move if this happens. |
| 220 |
return false; |
| 221 |
|
| 222 |
case TOUCHING: |
| 223 |
// Don't allow panning if there is an element in full-screen mode. See bug 775511. |
| 224 |
if (mTarget.isFullScreen() || panDistance(event) < PAN_THRESHOLD) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
cancelTouch(); |
| 228 |
startPanning(event.getX(0), event.getY(0), event.getEventTime()); |
| 229 |
track(event); |
| 230 |
return true; |
| 231 |
|
| 232 |
case PANNING_HOLD_LOCKED: |
| 233 |
setState(PanZoomState.PANNING_LOCKED); |
| 234 |
// fall through |
| 235 |
case PANNING_LOCKED: |
| 236 |
track(event); |
| 237 |
return true; |
| 238 |
|
| 239 |
case PANNING_HOLD: |
| 240 |
setState(PanZoomState.PANNING); |
| 241 |
// fall through |
| 242 |
case PANNING: |
| 243 |
track(event); |
| 244 |
return true; |
| 245 |
|
| 246 |
case PINCHING: |
| 247 |
// scale gesture listener will handle this |
| 248 |
return false; |
| 249 |
} |
| 250 |
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchMove"); |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
private boolean handleTouchEnd(MotionEvent event) { |
| 255 |
|
| 256 |
switch (mState) { |
| 257 |
case FLING: |
| 258 |
case BOUNCE: |
| 259 |
// should never happen |
| 260 |
Log.e(LOGTAG, "Received impossible touch end while in " + mState); |
| 261 |
// fall through |
| 262 |
case ANIMATED_ZOOM: |
| 263 |
case NOTHING: |
| 264 |
// may happen if user double-taps and drags without lifting after the |
| 265 |
// second tap. ignore if this happens. |
| 266 |
return false; |
| 267 |
|
| 268 |
case TOUCHING: |
| 269 |
// the switch into TOUCHING might have happened while the page was |
| 270 |
// snapping back after overscroll. we need to finish the snap if that |
| 271 |
// was the case |
| 272 |
bounce(); |
| 273 |
return false; |
| 274 |
|
| 275 |
case PANNING: |
| 276 |
case PANNING_LOCKED: |
| 277 |
case PANNING_HOLD: |
| 278 |
case PANNING_HOLD_LOCKED: |
| 279 |
setState(PanZoomState.FLING); |
| 280 |
fling(); |
| 281 |
return true; |
| 282 |
|
| 283 |
case PINCHING: |
| 284 |
setState(PanZoomState.NOTHING); |
| 285 |
return true; |
| 286 |
} |
| 287 |
Log.e(LOGTAG, "Unhandled case " + mState + " in handleTouchEnd"); |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
private boolean handleTouchCancel(MotionEvent event) { |
| 292 |
cancelTouch(); |
| 293 |
|
| 294 |
// ensure we snap back if we're overscrolled |
| 295 |
bounce(); |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
private boolean handlePointerScroll(MotionEvent event) { |
| 300 |
if (mState == PanZoomState.NOTHING || mState == PanZoomState.FLING) { |
| 301 |
float scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 302 |
float scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 303 |
|
| 304 |
scrollBy(scrollX * MAX_SCROLL, scrollY * MAX_SCROLL); |
| 305 |
bounce(); |
| 306 |
return true; |
| 307 |
} |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
private void startTouch(float x, float y, long time) { |
| 312 |
mX.startTouch(x); |
| 313 |
mY.startTouch(y); |
| 314 |
setState(PanZoomState.TOUCHING); |
| 315 |
mLastEventTime = time; |
| 316 |
} |
| 317 |
|
| 318 |
private void startPanning(float x, float y, long time) { |
| 319 |
float dx = mX.panDistance(x); |
| 320 |
float dy = mY.panDistance(y); |
| 321 |
double angle = Math.atan2(dy, dx); // range [-pi, pi] |
| 322 |
angle = Math.abs(angle); // range [0, pi] |
| 323 |
|
| 324 |
// When the touch move breaks through the pan threshold, reposition the touch down origin |
| 325 |
// so the page won't jump when we start panning. |
| 326 |
mX.startTouch(x); |
| 327 |
mY.startTouch(y); |
| 328 |
mLastEventTime = time; |
| 329 |
|
| 330 |
if (!mX.scrollable() || !mY.scrollable()) { |
| 331 |
setState(PanZoomState.PANNING); |
| 332 |
} else if (angle < AXIS_LOCK_ANGLE || angle > (Math.PI - AXIS_LOCK_ANGLE)) { |
| 333 |
mY.setScrollingDisabled(true); |
| 334 |
setState(PanZoomState.PANNING_LOCKED); |
| 335 |
} else if (Math.abs(angle - (Math.PI / 2)) < AXIS_LOCK_ANGLE) { |
| 336 |
mX.setScrollingDisabled(true); |
| 337 |
setState(PanZoomState.PANNING_LOCKED); |
| 338 |
} else { |
| 339 |
setState(PanZoomState.PANNING); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
private float panDistance(MotionEvent move) { |
| 344 |
float dx = mX.panDistance(move.getX(0)); |
| 345 |
float dy = mY.panDistance(move.getY(0)); |
| 346 |
return (float) Math.hypot(dx , dy); |
| 347 |
} |
| 348 |
|
| 349 |
private void track(float x, float y, long time) { |
| 350 |
float timeDelta = (float)(time - mLastEventTime); |
| 351 |
if (FloatUtils.fuzzyEquals(timeDelta, 0)) { |
| 352 |
// probably a duplicate event, ignore it. using a zero timeDelta will mess |
| 353 |
// up our velocity |
| 354 |
return; |
| 355 |
} |
| 356 |
mLastEventTime = time; |
| 357 |
|
| 358 |
mX.updateWithTouchAt(x, timeDelta); |
| 359 |
mY.updateWithTouchAt(y, timeDelta); |
| 360 |
} |
| 361 |
|
| 362 |
private void track(MotionEvent event) { |
| 363 |
mX.saveTouchPos(); |
| 364 |
mY.saveTouchPos(); |
| 365 |
|
| 366 |
for (int i = 0; i < event.getHistorySize(); i++) { |
| 367 |
track(event.getHistoricalX(0, i), |
| 368 |
event.getHistoricalY(0, i), |
| 369 |
event.getHistoricalEventTime(i)); |
| 370 |
} |
| 371 |
track(event.getX(0), event.getY(0), event.getEventTime()); |
| 372 |
|
| 373 |
if (stopped()) { |
| 374 |
if (mState == PanZoomState.PANNING) { |
| 375 |
setState(PanZoomState.PANNING_HOLD); |
| 376 |
} else if (mState == PanZoomState.PANNING_LOCKED) { |
| 377 |
setState(PanZoomState.PANNING_HOLD_LOCKED); |
| 378 |
} else { |
| 379 |
// should never happen, but handle anyway for robustness |
| 380 |
Log.e(LOGTAG, "Impossible case " + mState + " when stopped in track"); |
| 381 |
setState(PanZoomState.PANNING_HOLD_LOCKED); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
mX.startPan(); |
| 386 |
mY.startPan(); |
| 387 |
updatePosition(); |
| 388 |
} |
| 389 |
|
| 390 |
private void scrollBy(float dx, float dy) { |
| 391 |
ImmutableViewportMetrics scrolled = getMetrics().offsetViewportBy(dx, dy); |
| 392 |
mTarget.setViewportMetrics(scrolled); |
| 393 |
} |
| 394 |
|
| 395 |
private void fling() { |
| 396 |
updatePosition(); |
| 397 |
|
| 398 |
stopAnimationTimer(); |
| 399 |
|
| 400 |
boolean stopped = stopped(); |
| 401 |
mX.startFling(stopped); |
| 402 |
mY.startFling(stopped); |
| 403 |
|
| 404 |
startAnimationTimer(new FlingRunnable()); |
| 405 |
} |
| 406 |
|
| 407 |
/* Performs a bounce-back animation to the given viewport metrics. */ |
| 408 |
private void bounce(ImmutableViewportMetrics metrics, PanZoomState state) { |
| 409 |
stopAnimationTimer(); |
| 410 |
|
| 411 |
ImmutableViewportMetrics bounceStartMetrics = getMetrics(); |
| 412 |
if (bounceStartMetrics.fuzzyEquals(metrics)) { |
| 413 |
setState(PanZoomState.NOTHING); |
| 414 |
finishAnimation(); |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
setState(state); |
| 419 |
|
| 420 |
// At this point we have already set mState to BOUNCE or ANIMATED_ZOOM, so |
| 421 |
// getRedrawHint() is returning false. This means we can safely call |
| 422 |
// setAnimationTarget to set the new final display port and not have it get |
| 423 |
// clobbered by display ports from intermediate animation frames. |
| 424 |
mTarget.setAnimationTarget(metrics); |
| 425 |
startAnimationTimer(new BounceRunnable(bounceStartMetrics, metrics)); |
| 426 |
} |
| 427 |
|
| 428 |
/* Performs a bounce-back animation to the nearest valid viewport metrics. */ |
| 429 |
private void bounce() { |
| 430 |
bounce(getValidViewportMetrics(), PanZoomState.BOUNCE); |
| 431 |
} |
| 432 |
|
| 433 |
/* Starts the fling or bounce animation. */ |
| 434 |
private void startAnimationTimer(final AnimationRunnable runnable) { |
| 435 |
if (mAnimationTimer != null) { |
| 436 |
Log.e(LOGTAG, "Attempted to start a new fling without canceling the old one!"); |
| 437 |
stopAnimationTimer(); |
| 438 |
} |
| 439 |
|
| 440 |
mAnimationTimer = new Timer("Animation Timer"); |
| 441 |
mAnimationRunnable = runnable; |
| 442 |
mAnimationTimer.scheduleAtFixedRate(new TimerTask() { |
| 443 |
@Override |
| 444 |
public void run() { mTarget.post(runnable); } |
| 445 |
}, 0, (int)Axis.MS_PER_FRAME); |
| 446 |
} |
| 447 |
|
| 448 |
/* Stops the fling or bounce animation. */ |
| 449 |
private void stopAnimationTimer() { |
| 450 |
if (mAnimationTimer != null) { |
| 451 |
mAnimationTimer.cancel(); |
| 452 |
mAnimationTimer = null; |
| 453 |
} |
| 454 |
if (mAnimationRunnable != null) { |
| 455 |
mAnimationRunnable.terminate(); |
| 456 |
mAnimationRunnable = null; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
private float getVelocity() { |
| 461 |
float xvel = mX.getRealVelocity(); |
| 462 |
float yvel = mY.getRealVelocity(); |
| 463 |
return (float) StrictMath.hypot(xvel, yvel); |
| 464 |
} |
| 465 |
|
| 466 |
public PointF getVelocityVector() { |
| 467 |
return new PointF(mX.getRealVelocity(), mY.getRealVelocity()); |
| 468 |
} |
| 469 |
|
| 470 |
private boolean stopped() { |
| 471 |
return getVelocity() < STOPPED_THRESHOLD; |
| 472 |
} |
| 473 |
|
| 474 |
private PointF resetDisplacement() { |
| 475 |
return new PointF(mX.resetDisplacement(), mY.resetDisplacement()); |
| 476 |
} |
| 477 |
|
| 478 |
private void updatePosition() { |
| 479 |
mX.displace(); |
| 480 |
mY.displace(); |
| 481 |
PointF displacement = resetDisplacement(); |
| 482 |
if (FloatUtils.fuzzyEquals(displacement.x, 0.0f) && FloatUtils.fuzzyEquals(displacement.y, 0.0f)) { |
| 483 |
return; |
| 484 |
} |
| 485 |
synchronized (mTarget.getLock()) { |
| 486 |
scrollBy(displacement.x, displacement.y); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
private abstract class AnimationRunnable implements Runnable { |
| 491 |
private boolean mAnimationTerminated; |
| 492 |
|
| 493 |
/* This should always run on the UI thread */ |
| 494 |
public final void run() { |
| 495 |
/* |
| 496 |
* Since the animation timer queues this runnable on the UI thread, it |
| 497 |
* is possible that even when the animation timer is cancelled, there |
| 498 |
* are multiple instances of this queued, so we need to have another |
| 499 |
* mechanism to abort. This is done by using the mAnimationTerminated flag. |
| 500 |
*/ |
| 501 |
if (mAnimationTerminated) { |
| 502 |
return; |
| 503 |
} |
| 504 |
animateFrame(); |
| 505 |
} |
| 506 |
|
| 507 |
protected abstract void animateFrame(); |
| 508 |
|
| 509 |
/* This should always run on the UI thread */ |
| 510 |
final void terminate() { |
| 511 |
mAnimationTerminated = true; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/* The callback that performs the bounce animation. */ |
| 516 |
private class BounceRunnable extends AnimationRunnable { |
| 517 |
/* The current frame of the bounce-back animation */ |
| 518 |
private int mBounceFrame; |
| 519 |
/* |
| 520 |
* The viewport metrics that represent the start and end of the bounce-back animation, |
| 521 |
* respectively. |
| 522 |
*/ |
| 523 |
private final ImmutableViewportMetrics mBounceStartMetrics; |
| 524 |
private final ImmutableViewportMetrics mBounceEndMetrics; |
| 525 |
|
| 526 |
BounceRunnable(ImmutableViewportMetrics startMetrics, ImmutableViewportMetrics endMetrics) { |
| 527 |
mBounceStartMetrics = startMetrics; |
| 528 |
mBounceEndMetrics = endMetrics; |
| 529 |
} |
| 530 |
|
| 531 |
protected void animateFrame() { |
| 532 |
/* |
| 533 |
* The pan/zoom controller might have signaled to us that it wants to abort the |
| 534 |
* animation by setting the state to PanZoomState.NOTHING. Handle this case and bail |
| 535 |
* out. |
| 536 |
*/ |
| 537 |
if (!(mState == PanZoomState.BOUNCE || mState == PanZoomState.ANIMATED_ZOOM)) { |
| 538 |
finishAnimation(); |
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
/* Perform the next frame of the bounce-back animation. */ |
| 543 |
if (mBounceFrame < (int)(256f/Axis.MS_PER_FRAME)) { |
| 544 |
advanceBounce(); |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
/* Finally, if there's nothing else to do, complete the animation and go to sleep. */ |
| 549 |
finishBounce(); |
| 550 |
finishAnimation(); |
| 551 |
setState(PanZoomState.NOTHING); |
| 552 |
} |
| 553 |
|
| 554 |
/* Performs one frame of a bounce animation. */ |
| 555 |
private void advanceBounce() { |
| 556 |
synchronized (mTarget.getLock()) { |
| 557 |
float t = easeOut(mBounceFrame * Axis.MS_PER_FRAME / 256f); |
| 558 |
ImmutableViewportMetrics newMetrics = mBounceStartMetrics.interpolate(mBounceEndMetrics, t); |
| 559 |
mTarget.setViewportMetrics(newMetrics); |
| 560 |
mBounceFrame++; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/* Concludes a bounce animation and snaps the viewport into place. */ |
| 565 |
private void finishBounce() { |
| 566 |
synchronized (mTarget.getLock()) { |
| 567 |
mTarget.setViewportMetrics(mBounceEndMetrics); |
| 568 |
mBounceFrame = -1; |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
// The callback that performs the fling animation. |
| 574 |
private class FlingRunnable extends AnimationRunnable { |
| 575 |
protected void animateFrame() { |
| 576 |
/* |
| 577 |
* The pan/zoom controller might have signaled to us that it wants to abort the |
| 578 |
* animation by setting the state to PanZoomState.NOTHING. Handle this case and bail |
| 579 |
* out. |
| 580 |
*/ |
| 581 |
if (mState != PanZoomState.FLING) { |
| 582 |
finishAnimation(); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
/* Advance flings, if necessary. */ |
| 587 |
boolean flingingX = mX.advanceFling(); |
| 588 |
boolean flingingY = mY.advanceFling(); |
| 589 |
|
| 590 |
boolean overscrolled = (mX.overscrolled() || mY.overscrolled()); |
| 591 |
|
| 592 |
/* If we're still flinging in any direction, update the origin. */ |
| 593 |
if (flingingX || flingingY) { |
| 594 |
updatePosition(); |
| 595 |
|
| 596 |
/* |
| 597 |
* Check to see if we're still flinging with an appreciable velocity. The threshold is |
| 598 |
* higher in the case of overscroll, so we bounce back eagerly when overscrolling but |
| 599 |
* coast smoothly to a stop when not. In other words, require a greater velocity to |
| 600 |
* maintain the fling once we enter overscroll. |
| 601 |
*/ |
| 602 |
float threshold = (overscrolled ? STOPPED_THRESHOLD : FLING_STOPPED_THRESHOLD); |
| 603 |
if (getVelocity() >= threshold) { |
| 604 |
mContext.getDocumentOverlay().showPageNumberRect(); |
| 605 |
// we're still flinging |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
mX.stopFling(); |
| 610 |
mY.stopFling(); |
| 611 |
} |
| 612 |
|
| 613 |
/* Perform a bounce-back animation if overscrolled. */ |
| 614 |
if (overscrolled) { |
| 615 |
bounce(); |
| 616 |
} else { |
| 617 |
finishAnimation(); |
| 618 |
setState(PanZoomState.NOTHING); |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
private void finishAnimation() { |
| 624 |
checkMainThread(); |
| 625 |
|
| 626 |
stopAnimationTimer(); |
| 627 |
|
| 628 |
mContext.getDocumentOverlay().hidePageNumberRect(); |
| 629 |
|
| 630 |
// Force a viewport synchronisation |
| 631 |
mTarget.forceRedraw(); |
| 632 |
} |
| 633 |
|
| 634 |
/* Returns the nearest viewport metrics with no overscroll visible. */ |
| 635 |
private ImmutableViewportMetrics getValidViewportMetrics() { |
| 636 |
return getValidViewportMetrics(getMetrics()); |
| 637 |
} |
| 638 |
|
| 639 |
private ImmutableViewportMetrics getValidViewportMetrics(ImmutableViewportMetrics viewportMetrics) { |
| 640 |
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */ |
| 641 |
float zoomFactor = viewportMetrics.zoomFactor; |
| 642 |
RectF viewport = viewportMetrics.getViewport(); |
| 643 |
|
| 644 |
float focusX = viewport.width() / 2.0f; |
| 645 |
float focusY = viewport.height() / 2.0f; |
| 646 |
|
| 647 |
float minZoomFactor = 0.0f; |
| 648 |
float maxZoomFactor = MAX_ZOOM; |
| 649 |
|
| 650 |
ZoomConstraints constraints = mTarget.getZoomConstraints(); |
| 651 |
if (null == constraints) { |
| 652 |
Log.e(LOGTAG, "ZoomConstraints not available - too impatient?"); |
| 653 |
return viewportMetrics; |
| 654 |
|
| 655 |
} |
| 656 |
if (constraints.getMinZoom() > 0) |
| 657 |
minZoomFactor = constraints.getMinZoom(); |
| 658 |
if (constraints.getMaxZoom() > 0) |
| 659 |
maxZoomFactor = constraints.getMaxZoom(); |
| 660 |
|
| 661 |
maxZoomFactor = Math.max(maxZoomFactor, minZoomFactor); |
| 662 |
|
| 663 |
if (zoomFactor < minZoomFactor) { |
| 664 |
// if one (or both) of the page dimensions is smaller than the viewport, |
| 665 |
// zoom using the top/left as the focus on that axis. this prevents the |
| 666 |
// scenario where, if both dimensions are smaller than the viewport, but |
| 667 |
// by different scale factors, we end up scrolled to the end on one axis |
| 668 |
// after applying the scale |
| 669 |
PointF center = new PointF(focusX, focusY); |
| 670 |
viewportMetrics = viewportMetrics.scaleTo(minZoomFactor, center); |
| 671 |
} else if (zoomFactor > maxZoomFactor) { |
| 672 |
PointF center = new PointF(viewport.width() / 2.0f, viewport.height() / 2.0f); |
| 673 |
viewportMetrics = viewportMetrics.scaleTo(maxZoomFactor, center); |
| 674 |
} |
| 675 |
|
| 676 |
/* Now we pan to the right origin. */ |
| 677 |
viewportMetrics = viewportMetrics.clamp(); |
| 678 |
|
| 679 |
viewportMetrics = pushPageToCenterOfViewport(viewportMetrics); |
| 680 |
|
| 681 |
return viewportMetrics; |
| 682 |
} |
| 683 |
|
| 684 |
private ImmutableViewportMetrics pushPageToCenterOfViewport(ImmutableViewportMetrics viewportMetrics) { |
| 685 |
RectF pageRect = viewportMetrics.getPageRect(); |
| 686 |
RectF viewportRect = viewportMetrics.getViewport(); |
| 687 |
|
| 688 |
if (pageRect.width() < viewportRect.width()) { |
| 689 |
float originX = (viewportRect.width() - pageRect.width()) / 2.0f; |
| 690 |
viewportMetrics = viewportMetrics.setViewportOrigin(-originX, viewportMetrics.getOrigin().y); |
| 691 |
} |
| 692 |
|
| 693 |
if (pageRect.height() < viewportRect.height()) { |
| 694 |
float originY = (viewportRect.height() - pageRect.height()) / 2.0f; |
| 695 |
viewportMetrics = viewportMetrics.setViewportOrigin(viewportMetrics.getOrigin().x, -originY); |
| 696 |
} |
| 697 |
|
| 698 |
return viewportMetrics; |
| 699 |
} |
| 700 |
|
| 701 |
private class AxisX extends Axis { |
| 702 |
@Override |
| 703 |
public float getOrigin() { return getMetrics().viewportRectLeft; } |
| 704 |
@Override |
| 705 |
protected float getViewportLength() { return getMetrics().getWidth(); } |
| 706 |
@Override |
| 707 |
protected float getPageStart() { return getMetrics().pageRectLeft; } |
| 708 |
@Override |
| 709 |
protected float getPageLength() { return getMetrics().getPageWidth(); } |
| 710 |
} |
| 711 |
|
| 712 |
private class AxisY extends Axis { |
| 713 |
@Override |
| 714 |
public float getOrigin() { return getMetrics().viewportRectTop; } |
| 715 |
@Override |
| 716 |
protected float getViewportLength() { return getMetrics().getHeight(); } |
| 717 |
@Override |
| 718 |
protected float getPageStart() { return getMetrics().pageRectTop; } |
| 719 |
@Override |
| 720 |
protected float getPageLength() { return getMetrics().getPageHeight(); } |
| 721 |
} |
| 722 |
|
| 723 |
/* |
| 724 |
* Zooming |
| 725 |
*/ |
| 726 |
@Override |
| 727 |
public boolean onScaleBegin(SimpleScaleGestureDetector detector) { |
| 728 |
if (mState == PanZoomState.ANIMATED_ZOOM) |
| 729 |
return false; |
| 730 |
|
| 731 |
if (null == mTarget.getZoomConstraints()) |
| 732 |
return false; |
| 733 |
|
| 734 |
setState(PanZoomState.PINCHING); |
| 735 |
mLastZoomFocus = new PointF(detector.getFocusX(), detector.getFocusY()); |
| 736 |
cancelTouch(); |
| 737 |
|
| 738 |
return true; |
| 739 |
} |
| 740 |
|
| 741 |
@Override |
| 742 |
public boolean onScale(SimpleScaleGestureDetector detector) { |
| 743 |
if (mTarget.isFullScreen()) |
| 744 |
return false; |
| 745 |
|
| 746 |
if (mState != PanZoomState.PINCHING) |
| 747 |
return false; |
| 748 |
|
| 749 |
float prevSpan = detector.getPreviousSpan(); |
| 750 |
if (FloatUtils.fuzzyEquals(prevSpan, 0.0f)) { |
| 751 |
// let's eat this one to avoid setting the new zoom to infinity (bug 711453) |
| 752 |
return true; |
| 753 |
} |
| 754 |
|
| 755 |
float spanRatio = detector.getCurrentSpan() / prevSpan; |
| 756 |
|
| 757 |
synchronized (mTarget.getLock()) { |
| 758 |
float newZoomFactor = getMetrics().zoomFactor * spanRatio; |
| 759 |
float minZoomFactor = 0.0f; // deliberately set to zero to allow big zoom out effect |
| 760 |
float maxZoomFactor = MAX_ZOOM; |
| 761 |
|
| 762 |
ZoomConstraints constraints = mTarget.getZoomConstraints(); |
| 763 |
|
| 764 |
if (constraints.getMaxZoom() > 0) |
| 765 |
maxZoomFactor = constraints.getMaxZoom(); |
| 766 |
|
| 767 |
if (newZoomFactor < minZoomFactor) { |
| 768 |
// apply resistance when zooming past minZoomFactor, |
| 769 |
// such that it asymptotically reaches minZoomFactor / 2.0 |
| 770 |
// but never exceeds that |
| 771 |
final float rate = 0.5f; // controls how quickly we approach the limit |
| 772 |
float excessZoom = minZoomFactor - newZoomFactor; |
| 773 |
excessZoom = 1.0f - (float)Math.exp(-excessZoom * rate); |
| 774 |
newZoomFactor = minZoomFactor * (1.0f - excessZoom / 2.0f); |
| 775 |
} |
| 776 |
|
| 777 |
if (newZoomFactor > maxZoomFactor) { |
| 778 |
// apply resistance when zooming past maxZoomFactor, |
| 779 |
// such that it asymptotically reaches maxZoomFactor + 1.0 |
| 780 |
// but never exceeds that |
| 781 |
float excessZoom = newZoomFactor - maxZoomFactor; |
| 782 |
excessZoom = 1.0f - (float)Math.exp(-excessZoom); |
| 783 |
newZoomFactor = maxZoomFactor + excessZoom; |
| 784 |
} |
| 785 |
|
| 786 |
scrollBy(mLastZoomFocus.x - detector.getFocusX(), |
| 787 |
mLastZoomFocus.y - detector.getFocusY()); |
| 788 |
PointF focus = new PointF(detector.getFocusX(), detector.getFocusY()); |
| 789 |
scaleWithFocus(newZoomFactor, focus); |
| 790 |
} |
| 791 |
|
| 792 |
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY()); |
| 793 |
|
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
@Override |
| 798 |
public void onScaleEnd(SimpleScaleGestureDetector detector) { |
| 799 |
if (mState == PanZoomState.ANIMATED_ZOOM) |
| 800 |
return; |
| 801 |
|
| 802 |
// switch back to the touching state |
| 803 |
startTouch(detector.getFocusX(), detector.getFocusY(), detector.getEventTime()); |
| 804 |
|
| 805 |
// Force a viewport synchronisation |
| 806 |
mTarget.forceRedraw(); |
| 807 |
|
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
* Scales the viewport, keeping the given focus point in the same place before and after the |
| 812 |
* scale operation. You must hold the monitor while calling this. |
| 813 |
*/ |
| 814 |
private void scaleWithFocus(float zoomFactor, PointF focus) { |
| 815 |
ImmutableViewportMetrics viewportMetrics = getMetrics(); |
| 816 |
viewportMetrics = viewportMetrics.scaleTo(zoomFactor, focus); |
| 817 |
mTarget.setViewportMetrics(viewportMetrics); |
| 818 |
} |
| 819 |
|
| 820 |
public boolean getRedrawHint() { |
| 821 |
switch (mState) { |
| 822 |
case PINCHING: |
| 823 |
case ANIMATED_ZOOM: |
| 824 |
case BOUNCE: |
| 825 |
// don't redraw during these because the zoom is (or might be, in the case |
| 826 |
// of BOUNCE) be changing rapidly and gecko will have to redraw the entire |
| 827 |
// display port area. we trigger a force-redraw upon exiting these states. |
| 828 |
return false; |
| 829 |
default: |
| 830 |
// allow redrawing in other states |
| 831 |
return true; |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
@Override |
| 836 |
public boolean onDown(MotionEvent motionEvent) { |
| 837 |
mWaitForDoubleTap = mTarget.getZoomConstraints() != null; |
| 838 |
return false; |
| 839 |
} |
| 840 |
|
| 841 |
@Override |
| 842 |
public void onShowPress(MotionEvent motionEvent) { |
| 843 |
// If we get this, it will be followed either by a call to |
| 844 |
// onSingleTapUp (if the user lifts their finger before the |
| 845 |
// long-press timeout) or a call to onLongPress (if the user |
| 846 |
// does not). In the former case, we want to make sure it is |
| 847 |
// treated as a click. (Note that if this is called, we will |
| 848 |
// not get a call to onDoubleTap). |
| 849 |
mWaitForDoubleTap = false; |
| 850 |
} |
| 851 |
|
| 852 |
private PointF getMotionInDocumentCoordinates(MotionEvent motionEvent) { |
| 853 |
RectF viewport = getValidViewportMetrics().getViewport(); |
| 854 |
PointF viewPoint = new PointF(motionEvent.getX(0), motionEvent.getY(0)); |
| 855 |
return mTarget.convertViewPointToLayerPoint(viewPoint); |
| 856 |
} |
| 857 |
|
| 858 |
@Override |
| 859 |
public void onLongPress(MotionEvent motionEvent) { |
| 860 |
LOKitShell.sendTouchEvent("LongPress", getMotionInDocumentCoordinates(motionEvent)); |
| 861 |
} |
| 862 |
|
| 863 |
@Override |
| 864 |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { |
| 865 |
mContext.getDocumentOverlay().showPageNumberRect(); |
| 866 |
return super.onScroll(e1, e2, distanceX, distanceY); |
| 867 |
} |
| 868 |
|
| 869 |
@Override |
| 870 |
public boolean onSingleTapUp(MotionEvent motionEvent) { |
| 871 |
// When double-tapping is allowed, we have to wait to see if this is |
| 872 |
// going to be a double-tap. |
| 873 |
if (!mWaitForDoubleTap) { |
| 874 |
LOKitShell.sendTouchEvent("SingleTap", getMotionInDocumentCoordinates(motionEvent)); |
| 875 |
} |
| 876 |
// return false because we still want to get the ACTION_UP event that triggers this |
| 877 |
return false; |
| 878 |
} |
| 879 |
|
| 880 |
@Override |
| 881 |
public boolean onSingleTapConfirmed(MotionEvent motionEvent) { |
| 882 |
// In cases where we don't wait for double-tap, we handle this in onSingleTapUp. |
| 883 |
if (mWaitForDoubleTap) { |
| 884 |
LOKitShell.sendTouchEvent("SingleTap", getMotionInDocumentCoordinates(motionEvent)); |
| 885 |
} |
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
@Override |
| 890 |
public boolean onDoubleTap(MotionEvent motionEvent) { |
| 891 |
if (null == mTarget.getZoomConstraints()) { |
| 892 |
return true; |
| 893 |
} |
| 894 |
// Double tap zooms in or out depending on the current zoom factor |
| 895 |
PointF pointOfTap = getMotionInDocumentCoordinates(motionEvent); |
| 896 |
ImmutableViewportMetrics metrics = getMetrics(); |
| 897 |
float newZoom = metrics.getZoomFactor() >= |
| 898 |
DOUBLE_TAP_THRESHOLD ? mTarget.getZoomConstraints().getDefaultZoom() : DOUBLE_TAP_THRESHOLD; |
| 899 |
// calculate new top_left point from the point of tap |
| 900 |
float ratio = newZoom/metrics.getZoomFactor(); |
| 901 |
float newLeft = pointOfTap.x - 1/ratio * (pointOfTap.x - metrics.getOrigin().x / metrics.getZoomFactor()); |
| 902 |
float newTop = pointOfTap.y - 1/ratio * (pointOfTap.y - metrics.getOrigin().y / metrics.getZoomFactor()); |
| 903 |
// animate move to the new view |
| 904 |
animatedMove(new PointF(newLeft, newTop), newZoom); |
| 905 |
|
| 906 |
LOKitShell.sendTouchEvent("DoubleTap", pointOfTap); |
| 907 |
return true; |
| 908 |
} |
| 909 |
|
| 910 |
private void cancelTouch() { |
| 911 |
//GeckoEvent e = GeckoEvent.createBroadcastEvent("Gesture:CancelTouch", ""); |
| 912 |
//GeckoAppShell.sendEventToGecko(e); |
| 913 |
} |
| 914 |
|
| 915 |
/** |
| 916 |
* Zoom to a specified rect IN CSS PIXELS. |
| 917 |
* |
| 918 |
* While we usually use device pixels, zoomToRect must be specified in CSS |
| 919 |
* pixels. |
| 920 |
*/ |
| 921 |
boolean animatedZoomTo(RectF zoomToRect) { |
| 922 |
RectF viewport = getMetrics().getViewport(); |
| 923 |
// 1. adjust the aspect ratio of zoomToRect to match that of the current viewport, |
| 924 |
// enlarging as necessary (if it gets too big, it will get shrunk in the next step). |
| 925 |
// while enlarging make sure we enlarge equally on both sides to keep the target rect |
| 926 |
// centered. |
| 927 |
float targetRatio = viewport.width() / viewport.height(); |
| 928 |
float rectRatio = zoomToRect.width() / zoomToRect.height(); |
| 929 |
if (FloatUtils.fuzzyEquals(targetRatio, rectRatio)) { |
| 930 |
// all good, do nothing |
| 931 |
} else if (targetRatio < rectRatio) { |
| 932 |
// need to increase zoomToRect height |
| 933 |
float newHeight = zoomToRect.width() / targetRatio; |
| 934 |
zoomToRect.top -= (newHeight - zoomToRect.height()) / 2; |
| 935 |
zoomToRect.bottom = zoomToRect.top + newHeight; |
| 936 |
} else { // targetRatio > rectRatio) { |
| 937 |
// need to increase zoomToRect width |
| 938 |
float newWidth = targetRatio * zoomToRect.height(); |
| 939 |
zoomToRect.left -= (newWidth - zoomToRect.width()) / 2; |
| 940 |
zoomToRect.right = zoomToRect.left + newWidth; |
| 941 |
} |
| 942 |
|
| 943 |
float finalZoom = viewport.width() / zoomToRect.width(); |
| 944 |
|
| 945 |
ImmutableViewportMetrics finalMetrics = getMetrics(); |
| 946 |
finalMetrics = finalMetrics.setViewportOrigin( |
| 947 |
zoomToRect.left * finalMetrics.zoomFactor, |
| 948 |
zoomToRect.top * finalMetrics.zoomFactor); |
| 949 |
finalMetrics = finalMetrics.scaleTo(finalZoom, new PointF(0.0f, 0.0f)); |
| 950 |
|
| 951 |
// 2. now run getValidViewportMetrics on it, so that the target viewport is |
| 952 |
// clamped down to prevent overscroll, over-zoom, and other bad conditions. |
| 953 |
finalMetrics = getValidViewportMetrics(finalMetrics); |
| 954 |
|
| 955 |
bounce(finalMetrics, PanZoomState.ANIMATED_ZOOM); |
| 956 |
return true; |
| 957 |
} |
| 958 |
|
| 959 |
/** |
| 960 |
* Move the viewport to the top-left point to and zoom to the desired |
| 961 |
* zoom factor. Input zoom factor can be null, in this case leave the zoom unchanged. |
| 962 |
*/ |
| 963 |
boolean animatedMove(PointF topLeft, Float zoom) { |
| 964 |
RectF moveToRect = getMetrics().getCssViewport(); |
| 965 |
moveToRect.offsetTo(topLeft.x, topLeft.y); |
| 966 |
|
| 967 |
ImmutableViewportMetrics finalMetrics = getMetrics(); |
| 968 |
|
| 969 |
finalMetrics = finalMetrics.setViewportOrigin( |
| 970 |
moveToRect.left * finalMetrics.zoomFactor, |
| 971 |
moveToRect.top * finalMetrics.zoomFactor); |
| 972 |
|
| 973 |
if (zoom != null) { |
| 974 |
finalMetrics = finalMetrics.scaleTo(zoom, new PointF(0.0f, 0.0f)); |
| 975 |
} |
| 976 |
finalMetrics = getValidViewportMetrics(finalMetrics); |
| 977 |
|
| 978 |
bounce(finalMetrics, PanZoomState.ANIMATED_ZOOM); |
| 979 |
return true; |
| 980 |
} |
| 981 |
} |