| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.Point; | 8 import android.graphics.Point; |
| 9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
| 10 import android.view.MotionEvent; | 10 import android.view.MotionEvent; |
| 11 import android.view.ViewConfiguration; | 11 import android.view.ViewConfiguration; |
| 12 | 12 |
| 13 import org.chromium.chromoting.jni.Client; | |
| 14 | |
| 15 /** | 13 /** |
| 16 * This class receives local touch events and translates them into the appropria
te mouse based | 14 * This class receives local touch events and translates them into the appropria
te mouse based |
| 17 * events for the remote host. The net result is that the local input method fe
els like a touch | 15 * events for the remote host. The net result is that the local input method fe
els like a touch |
| 18 * interface but the remote host will be given mouse events to inject. | 16 * interface but the remote host will be given mouse events to inject. |
| 19 */ | 17 */ |
| 20 public class SimulatedTouchInputStrategy implements InputStrategyInterface { | 18 public class SimulatedTouchInputStrategy implements InputStrategyInterface { |
| 21 /** Used to adjust the size of the region used for double tap detection. */ | 19 /** Used to adjust the size of the region used for double tap detection. */ |
| 22 private static final float DOUBLE_TAP_SLOP_SCALE_FACTOR = 0.25f; | 20 private static final float DOUBLE_TAP_SLOP_SCALE_FACTOR = 0.25f; |
| 23 | 21 |
| 24 private final RenderData mRenderData; | 22 private final RenderData mRenderData; |
| 25 private final Client mClient; | 23 private final InputEventSender mInjector; |
| 26 | 24 |
| 27 /** | 25 /** |
| 28 * Stores the time of the most recent left button single tap processed. | 26 * Stores the time of the most recent left button single tap processed. |
| 29 */ | 27 */ |
| 30 private long mLastTapTimeInMs = 0; | 28 private long mLastTapTimeInMs = 0; |
| 31 | 29 |
| 32 /** | 30 /** |
| 33 * Stores the position of the last left button single tap processed. | 31 * Stores the position of the last left button single tap processed. |
| 34 */ | 32 */ |
| 35 private Point mLastTapPoint; | 33 private Point mLastTapPoint; |
| 36 | 34 |
| 37 /** | 35 /** |
| 38 * The maximum distance, in pixels, between two points in order for them to
be considered a | 36 * The maximum distance, in pixels, between two points in order for them to
be considered a |
| 39 * double tap gesture. | 37 * double tap gesture. |
| 40 */ | 38 */ |
| 41 private final int mDoubleTapSlopSquareInPx; | 39 private final int mDoubleTapSlopSquareInPx; |
| 42 | 40 |
| 43 /** | 41 /** |
| 44 * The interval, measured in milliseconds, in which two consecutive left but
ton taps must | 42 * The interval, measured in milliseconds, in which two consecutive left but
ton taps must |
| 45 * occur in order to be considered a double tap gesture. | 43 * occur in order to be considered a double tap gesture. |
| 46 */ | 44 */ |
| 47 private final long mDoubleTapDurationInMs; | 45 private final long mDoubleTapDurationInMs; |
| 48 | 46 |
| 49 /** Mouse-button currently held down, or BUTTON_UNDEFINED otherwise. */ | 47 /** Mouse-button currently held down, or BUTTON_UNDEFINED otherwise. */ |
| 50 private int mHeldButton = TouchInputHandlerInterface.BUTTON_UNDEFINED; | 48 private int mHeldButton = InputStub.BUTTON_UNDEFINED; |
| 51 | 49 |
| 52 public SimulatedTouchInputStrategy(RenderData renderData, Client client, Con
text context) { | 50 public SimulatedTouchInputStrategy( |
| 51 RenderData renderData, InputEventSender injector, Context context) { |
| 52 Preconditions.notNull(injector); |
| 53 mRenderData = renderData; | 53 mRenderData = renderData; |
| 54 mClient = client; | 54 mInjector = injector; |
| 55 | 55 |
| 56 ViewConfiguration config = ViewConfiguration.get(context); | 56 ViewConfiguration config = ViewConfiguration.get(context); |
| 57 mDoubleTapDurationInMs = config.getDoubleTapTimeout(); | 57 mDoubleTapDurationInMs = config.getDoubleTapTimeout(); |
| 58 | 58 |
| 59 // In order to detect whether the user is attempting to double tap a tar
get, we define a | 59 // In order to detect whether the user is attempting to double tap a tar
get, we define a |
| 60 // region around the first point within which the second tap must occur.
The standard way | 60 // region around the first point within which the second tap must occur.
The standard way |
| 61 // to do this in an Android UI (meaning a UI comprised of UI elements wh
ich conform to the | 61 // to do this in an Android UI (meaning a UI comprised of UI elements wh
ich conform to the |
| 62 // visual guidelines for the platform which are 'Touch Friendly') is to
use the | 62 // visual guidelines for the platform which are 'Touch Friendly') is to
use the |
| 63 // getScaledDoubleTapSlop() value for checking this distance (or use a G
estureDetector). | 63 // getScaledDoubleTapSlop() value for checking this distance (or use a G
estureDetector). |
| 64 // Our scenario is a bit different as our UI consists of an image of a r
emote machine where | 64 // Our scenario is a bit different as our UI consists of an image of a r
emote machine where |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 mDoubleTapSlopSquareInPx = scaledDoubleTapSlopInPx * scaledDoubleTapSlop
InPx; | 78 mDoubleTapSlopSquareInPx = scaledDoubleTapSlopInPx * scaledDoubleTapSlop
InPx; |
| 79 | 79 |
| 80 synchronized (mRenderData) { | 80 synchronized (mRenderData) { |
| 81 mRenderData.drawCursor = false; | 81 mRenderData.drawCursor = false; |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 @Override | 85 @Override |
| 86 public boolean onTap(int button) { | 86 public boolean onTap(int button) { |
| 87 Point currentTapPoint = getCursorPosition(); | 87 Point currentTapPoint = getCursorPosition(); |
| 88 if (button == TouchInputHandlerInterface.BUTTON_LEFT) { | 88 if (button == InputStub.BUTTON_LEFT) { |
| 89 // Left clicks are handled a little differently than the events for
other buttons. | 89 // Left clicks are handled a little differently than the events for
other buttons. |
| 90 // This is needed because translating touch events to mouse events h
as a problem with | 90 // This is needed because translating touch events to mouse events h
as a problem with |
| 91 // location consistency for double clicks. If you take the center l
ocation of each tap | 91 // location consistency for double clicks. If you take the center l
ocation of each tap |
| 92 // and inject them as mouse clicks, the distance between those two p
oints will often | 92 // and inject them as mouse clicks, the distance between those two p
oints will often |
| 93 // cause the remote OS to recognize the gesture as two distinct clic
ks instead of a | 93 // cause the remote OS to recognize the gesture as two distinct clic
ks instead of a |
| 94 // double click. In order to increase the success rate of double ta
ps/clicks, we | 94 // double click. In order to increase the success rate of double ta
ps/clicks, we |
| 95 // squirrel away the time and coordinates of each single tap and if
we detect the user | 95 // squirrel away the time and coordinates of each single tap and if
we detect the user |
| 96 // attempting a double tap, we use the original event's location for
that second tap. | 96 // attempting a double tap, we use the original event's location for
that second tap. |
| 97 long tapInterval = SystemClock.uptimeMillis() - mLastTapTimeInMs; | 97 long tapInterval = SystemClock.uptimeMillis() - mLastTapTimeInMs; |
| 98 if (isDoubleTap(currentTapPoint.x, currentTapPoint.y, tapInterval))
{ | 98 if (isDoubleTap(currentTapPoint.x, currentTapPoint.y, tapInterval))
{ |
| 99 currentTapPoint = new Point(mLastTapPoint); | 99 currentTapPoint = new Point(mLastTapPoint); |
| 100 mLastTapPoint = null; | 100 mLastTapPoint = null; |
| 101 mLastTapTimeInMs = 0; | 101 mLastTapTimeInMs = 0; |
| 102 } else { | 102 } else { |
| 103 mLastTapPoint = currentTapPoint; | 103 mLastTapPoint = currentTapPoint; |
| 104 mLastTapTimeInMs = SystemClock.uptimeMillis(); | 104 mLastTapTimeInMs = SystemClock.uptimeMillis(); |
| 105 } | 105 } |
| 106 } else { | 106 } else { |
| 107 mLastTapPoint = null; | 107 mLastTapPoint = null; |
| 108 mLastTapTimeInMs = 0; | 108 mLastTapTimeInMs = 0; |
| 109 } | 109 } |
| 110 | 110 |
| 111 injectMouseButtonEvent(button, true, currentTapPoint); | 111 mInjector.sendMouseClick(currentTapPoint, button); |
| 112 injectMouseButtonEvent(button, false, currentTapPoint); | |
| 113 | |
| 114 return true; | 112 return true; |
| 115 } | 113 } |
| 116 | 114 |
| 117 @Override | 115 @Override |
| 118 public boolean onPressAndHold(int button) { | 116 public boolean onPressAndHold(int button) { |
| 119 injectMouseButtonEvent(button, true, getCursorPosition()); | 117 mInjector.sendMouseDown(getCursorPosition(), button); |
| 120 mHeldButton = button; | 118 mHeldButton = button; |
| 121 return true; | 119 return true; |
| 122 } | 120 } |
| 123 | 121 |
| 124 @Override | 122 @Override |
| 125 public void onScroll(float distanceX, float distanceY) { | 123 public void onScroll(float distanceX, float distanceY) { |
| 126 mClient.sendMouseWheelEvent((int) -distanceX, (int) -distanceY); | 124 mInjector.sendReverseMouseWheelEvent(distanceX, distanceY); |
| 127 } | 125 } |
| 128 | 126 |
| 129 @Override | 127 @Override |
| 130 public void onMotionEvent(MotionEvent event) { | 128 public void onMotionEvent(MotionEvent event) { |
| 131 if (event.getActionMasked() == MotionEvent.ACTION_UP | 129 if (event.getActionMasked() == MotionEvent.ACTION_UP |
| 132 && mHeldButton != TouchInputHandlerInterface.BUTTON_UNDEFINED) { | 130 && mHeldButton != InputStub.BUTTON_UNDEFINED) { |
| 133 injectMouseButtonEvent(mHeldButton, false, getCursorPosition()); | 131 mInjector.sendMouseUp(getCursorPosition(), mHeldButton); |
| 134 mHeldButton = TouchInputHandlerInterface.BUTTON_UNDEFINED; | 132 mHeldButton = InputStub.BUTTON_UNDEFINED; |
| 135 } | 133 } |
| 136 } | 134 } |
| 137 | 135 |
| 138 @Override | 136 @Override |
| 139 public void injectCursorMoveEvent(int x, int y) { | 137 public void injectCursorMoveEvent(int x, int y) { |
| 140 mClient.sendMouseEvent(x, y, TouchInputHandlerInterface.BUTTON_UNDEFINED
, false); | 138 mInjector.sendCursorMove(x, y); |
| 141 } | 139 } |
| 142 | 140 |
| 143 @Override | 141 @Override |
| 144 public DesktopView.InputFeedbackType getShortPressFeedbackType() { | 142 public DesktopView.InputFeedbackType getShortPressFeedbackType() { |
| 145 return DesktopView.InputFeedbackType.SMALL_ANIMATION; | 143 return DesktopView.InputFeedbackType.SMALL_ANIMATION; |
| 146 } | 144 } |
| 147 | 145 |
| 148 @Override | 146 @Override |
| 149 public DesktopView.InputFeedbackType getLongPressFeedbackType() { | 147 public DesktopView.InputFeedbackType getLongPressFeedbackType() { |
| 150 return DesktopView.InputFeedbackType.LARGE_ANIMATION; | 148 return DesktopView.InputFeedbackType.LARGE_ANIMATION; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 173 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y}; | 171 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y}; |
| 174 synchronized (mRenderData) { | 172 synchronized (mRenderData) { |
| 175 mRenderData.transform.mapPoints(currentValues); | 173 mRenderData.transform.mapPoints(currentValues); |
| 176 mRenderData.transform.mapPoints(previousValues); | 174 mRenderData.transform.mapPoints(previousValues); |
| 177 } | 175 } |
| 178 | 176 |
| 179 int deltaX = (int) (currentValues[0] - previousValues[0]); | 177 int deltaX = (int) (currentValues[0] - previousValues[0]); |
| 180 int deltaY = (int) (currentValues[1] - previousValues[1]); | 178 int deltaY = (int) (currentValues[1] - previousValues[1]); |
| 181 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx)
; | 179 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx)
; |
| 182 } | 180 } |
| 183 | |
| 184 private void injectMouseButtonEvent(int button, boolean pressed, Point tapPo
int) { | |
| 185 mClient.sendMouseEvent(tapPoint.x, tapPoint.y, button, pressed); | |
| 186 } | |
| 187 } | 181 } |
| OLD | NEW |