| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.graphics.Matrix; | 8 import android.graphics.Matrix; |
| 9 import android.graphics.Point; | 9 import android.graphics.Point; |
| 10 import android.graphics.PointF; | 10 import android.graphics.PointF; |
| 11 import android.graphics.Rect; | 11 import android.graphics.Rect; |
| 12 import android.view.GestureDetector; | 12 import android.view.GestureDetector; |
| 13 import android.view.MotionEvent; | 13 import android.view.MotionEvent; |
| 14 import android.view.ScaleGestureDetector; | 14 import android.view.ScaleGestureDetector; |
| 15 import android.view.ViewConfiguration; | 15 import android.view.ViewConfiguration; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * This class is responsible for handling Touch input from the user. Touch even
ts which manipulate | 18 * This class is responsible for handling Touch input from the user. Touch even
ts which manipulate |
| 19 * the local canvas are handled in this class and any input which should be sent
to the remote host | 19 * the local canvas are handled in this class and any input which should be sent
to the remote host |
| 20 * are passed to the InputStrategyInterface implementation set by the DesktopVie
w. | 20 * are passed to the InputStrategyInterface implementation set by the DesktopVie
w. |
| 21 */ | 21 */ |
| 22 public class TouchInputHandler { | 22 public class TouchInputHandler { |
| 23 private final DesktopViewInterface mViewer; | 23 private final DesktopViewInterface mViewer; |
| 24 |
| 24 private final Context mContext; | 25 private final Context mContext; |
| 25 private final RenderData mRenderData; | 26 private final RenderData mRenderData; |
| 26 private final DesktopCanvas mDesktopCanvas; | 27 private final DesktopCanvas mDesktopCanvas; |
| 27 private InputStrategyInterface mInputStrategy; | 28 private InputStrategyInterface mInputStrategy; |
| 28 | 29 |
| 29 private GestureDetector mScroller; | 30 private GestureDetector mScroller; |
| 30 private ScaleGestureDetector mZoomer; | 31 private ScaleGestureDetector mZoomer; |
| 31 private TapGestureDetector mTapDetector; | 32 private TapGestureDetector mTapDetector; |
| 32 | 33 |
| 33 /** Used to disambiguate a 2-finger gesture as a swipe or a pinch. */ | 34 /** Used to disambiguate a 2-finger gesture as a swipe or a pinch. */ |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 resizeImageToFitScreen(); | 355 resizeImageToFitScreen(); |
| 355 } | 356 } |
| 356 | 357 |
| 357 private void resizeImageToFitScreen() { | 358 private void resizeImageToFitScreen() { |
| 358 mDesktopCanvas.resizeImageToFitScreen(); | 359 mDesktopCanvas.resizeImageToFitScreen(); |
| 359 float screenCenterX; | 360 float screenCenterX; |
| 360 float screenCenterY; | 361 float screenCenterY; |
| 361 synchronized (mRenderData) { | 362 synchronized (mRenderData) { |
| 362 screenCenterX = (float) mRenderData.screenWidth / 2; | 363 screenCenterX = (float) mRenderData.screenWidth / 2; |
| 363 screenCenterY = (float) mRenderData.screenHeight / 2; | 364 screenCenterY = (float) mRenderData.screenHeight / 2; |
| 365 |
| 366 float[] imagePoint = mapScreenPointToImagePoint(screenCenterX, scree
nCenterY); |
| 367 mDesktopCanvas.setViewportPosition(imagePoint[0], imagePoint[1]); |
| 364 } | 368 } |
| 365 moveCursorToScreenPoint(screenCenterX, screenCenterY); | 369 moveCursorToScreenPoint(screenCenterX, screenCenterY); |
| 370 mDesktopCanvas.repositionImage(true); |
| 366 } | 371 } |
| 367 | 372 |
| 368 private void setInputStrategy(InputStrategyInterface inputStrategy) { | 373 private void setInputStrategy(InputStrategyInterface inputStrategy) { |
| 369 // Since the rules for flinging differ between input modes, we want to s
top running the | 374 // Since the rules for flinging differ between input modes, we want to s
top running the |
| 370 // current fling animation when the mode changes to prevent a wonky expe
rience. | 375 // current fling animation when the mode changes to prevent a wonky expe
rience. |
| 371 mCursorAnimationJob.abortAnimation(); | 376 mCursorAnimationJob.abortAnimation(); |
| 372 mScrollAnimationJob.abortAnimation(); | 377 mScrollAnimationJob.abortAnimation(); |
| 373 mInputStrategy = inputStrategy; | 378 mInputStrategy = inputStrategy; |
| 374 } | 379 } |
| 375 | 380 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 392 // keep the cursor centered, if possible, as the viewport moves. | 397 // keep the cursor centered, if possible, as the viewport moves. |
| 393 if (followCursor) { | 398 if (followCursor) { |
| 394 moveCursor((int) newPos.x, (int) newPos.y); | 399 moveCursor((int) newPos.x, (int) newPos.y); |
| 395 } | 400 } |
| 396 | 401 |
| 397 mDesktopCanvas.repositionImage(true); | 402 mDesktopCanvas.repositionImage(true); |
| 398 } | 403 } |
| 399 | 404 |
| 400 /** Moves the cursor to the specified position on the screen. */ | 405 /** Moves the cursor to the specified position on the screen. */ |
| 401 private void moveCursorToScreenPoint(float screenX, float screenY) { | 406 private void moveCursorToScreenPoint(float screenX, float screenY) { |
| 402 float[] mappedValues = {screenX, screenY}; | 407 float[] imagePoint = mapScreenPointToImagePoint(screenX, screenY); |
| 403 synchronized (mRenderData) { | 408 moveCursor((int) imagePoint[0], (int) imagePoint[1]); |
| 404 Matrix canvasToImage = new Matrix(); | |
| 405 mRenderData.transform.invert(canvasToImage); | |
| 406 canvasToImage.mapPoints(mappedValues); | |
| 407 } | |
| 408 moveCursor((int) mappedValues[0], (int) mappedValues[1]); | |
| 409 } | 409 } |
| 410 | 410 |
| 411 /** Moves the cursor to the specified position on the remote host. */ | 411 /** Moves the cursor to the specified position on the remote host. */ |
| 412 private void moveCursor(int newX, int newY) { | 412 private void moveCursor(int newX, int newY) { |
| 413 synchronized (mRenderData) { | 413 synchronized (mRenderData) { |
| 414 boolean cursorMoved = mRenderData.setCursorPosition(newX, newY); | 414 boolean cursorMoved = mRenderData.setCursorPosition(newX, newY); |
| 415 if (cursorMoved) { | 415 if (cursorMoved) { |
| 416 mInputStrategy.injectCursorMoveEvent(newX, newY); | 416 mInputStrategy.injectCursorMoveEvent(newX, newY); |
| 417 } | 417 } |
| 418 } | 418 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 430 } else { | 430 } else { |
| 431 return false; | 431 return false; |
| 432 } | 432 } |
| 433 | 433 |
| 434 mSuppressCursorMovement = true; | 434 mSuppressCursorMovement = true; |
| 435 mSuppressFling = true; | 435 mSuppressFling = true; |
| 436 mSwipeCompleted = true; | 436 mSwipeCompleted = true; |
| 437 return true; | 437 return true; |
| 438 } | 438 } |
| 439 | 439 |
| 440 /** Translates a point in screen coordinates to a location on the desktop im
age. */ |
| 441 private float[] mapScreenPointToImagePoint(float screenX, float screenY) { |
| 442 float[] mappedPoints = {screenX, screenY}; |
| 443 Matrix screenToImage = new Matrix(); |
| 444 synchronized (mRenderData) { |
| 445 mRenderData.transform.invert(screenToImage); |
| 446 } |
| 447 screenToImage.mapPoints(mappedPoints); |
| 448 |
| 449 return mappedPoints; |
| 450 } |
| 451 |
| 440 /** Responds to touch events filtered by the gesture detectors. */ | 452 /** Responds to touch events filtered by the gesture detectors. */ |
| 441 private class GestureListener extends GestureDetector.SimpleOnGestureListene
r | 453 private class GestureListener extends GestureDetector.SimpleOnGestureListene
r |
| 442 implements ScaleGestureDetector.OnScaleGestureListener, | 454 implements ScaleGestureDetector.OnScaleGestureListener, |
| 443 TapGestureDetector.OnTapListener { | 455 TapGestureDetector.OnTapListener { |
| 444 /** | 456 /** |
| 445 * Called when the user drags one or more fingers across the touchscreen
. | 457 * Called when the user drags one or more fingers across the touchscreen
. |
| 446 */ | 458 */ |
| 447 @Override | 459 @Override |
| 448 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) { | 460 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) { |
| 449 int pointerCount = e2.getPointerCount(); | 461 int pointerCount = e2.getPointerCount(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 case 2: | 626 case 2: |
| 615 return InputStub.BUTTON_RIGHT; | 627 return InputStub.BUTTON_RIGHT; |
| 616 case 3: | 628 case 3: |
| 617 return InputStub.BUTTON_MIDDLE; | 629 return InputStub.BUTTON_MIDDLE; |
| 618 default: | 630 default: |
| 619 return InputStub.BUTTON_UNDEFINED; | 631 return InputStub.BUTTON_UNDEFINED; |
| 620 } | 632 } |
| 621 } | 633 } |
| 622 } | 634 } |
| 623 } | 635 } |
| OLD | NEW |