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.PointF; |
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 /** | 13 /** |
14 * 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 |
15 * 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 |
16 * 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. |
17 */ | 17 */ |
18 public class SimulatedTouchInputStrategy implements InputStrategyInterface { | 18 public class SimulatedTouchInputStrategy implements InputStrategyInterface { |
19 /** 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. */ |
20 private static final float DOUBLE_TAP_SLOP_SCALE_FACTOR = 0.25f; | 20 private static final float DOUBLE_TAP_SLOP_SCALE_FACTOR = 0.25f; |
21 | 21 |
22 private final RenderData mRenderData; | 22 private final RenderData mRenderData; |
23 private final InputEventSender mInjector; | 23 private final InputEventSender mInjector; |
24 | 24 |
25 /** | 25 /** |
26 * 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. |
27 */ | 27 */ |
28 private long mLastTapTimeInMs = 0; | 28 private long mLastTapTimeInMs = 0; |
29 | 29 |
30 /** | 30 /** |
31 * Stores the position of the last left button single tap processed. | 31 * Stores the position of the last left button single tap processed. |
32 */ | 32 */ |
33 private Point mLastTapPoint; | 33 private PointF mLastTapPoint; |
34 | 34 |
35 /** | 35 /** |
36 * 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 |
37 * double tap gesture. | 37 * double tap gesture. |
38 */ | 38 */ |
39 private final int mDoubleTapSlopSquareInPx; | 39 private final int mDoubleTapSlopSquareInPx; |
40 | 40 |
41 /** | 41 /** |
42 * 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 |
43 * occur in order to be considered a double tap gesture. | 43 * occur in order to be considered a double tap gesture. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 scaledDoubleTapSlopInPx *= DOUBLE_TAP_SLOP_SCALE_FACTOR; | 77 scaledDoubleTapSlopInPx *= DOUBLE_TAP_SLOP_SCALE_FACTOR; |
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 PointF currentTapPoint = getCursorPosition(); |
88 if (button == InputStub.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 PointF(mLastTapPoint.x, mLastTapPoint.y); |
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 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 @Override | 146 @Override |
147 public AbstractDesktopView.InputFeedbackType getLongPressFeedbackType() { | 147 public AbstractDesktopView.InputFeedbackType getLongPressFeedbackType() { |
148 return AbstractDesktopView.InputFeedbackType.LARGE_ANIMATION; | 148 return AbstractDesktopView.InputFeedbackType.LARGE_ANIMATION; |
149 } | 149 } |
150 | 150 |
151 @Override | 151 @Override |
152 public boolean isIndirectInputMode() { | 152 public boolean isIndirectInputMode() { |
153 return false; | 153 return false; |
154 } | 154 } |
155 | 155 |
156 private Point getCursorPosition() { | 156 private PointF getCursorPosition() { |
157 synchronized (mRenderData) { | 157 synchronized (mRenderData) { |
158 return mRenderData.getCursorPosition(); | 158 return mRenderData.getCursorPosition(); |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 private boolean isDoubleTap(int currentX, int currentY, long tapInterval) { | 162 private boolean isDoubleTap(float currentX, float currentY, long tapInterval
) { |
163 if (tapInterval > mDoubleTapDurationInMs || mLastTapPoint == null) { | 163 if (tapInterval > mDoubleTapDurationInMs || mLastTapPoint == null) { |
164 return false; | 164 return false; |
165 } | 165 } |
166 | 166 |
167 // Convert the image based coordinates back to screen coordinates so the
user experiences | 167 // Convert the image based coordinates back to screen coordinates so the
user experiences |
168 // consistent double tap behavior regardless of zoom level. | 168 // consistent double tap behavior regardless of zoom level. |
169 // | 169 // |
170 float[] currentValues = {currentX, currentY}; | 170 float[] currentValues = {currentX, currentY}; |
171 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y}; | 171 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y}; |
172 synchronized (mRenderData) { | 172 synchronized (mRenderData) { |
173 mRenderData.transform.mapPoints(currentValues); | 173 mRenderData.transform.mapPoints(currentValues); |
174 mRenderData.transform.mapPoints(previousValues); | 174 mRenderData.transform.mapPoints(previousValues); |
175 } | 175 } |
176 | 176 |
177 int deltaX = (int) (currentValues[0] - previousValues[0]); | 177 int deltaX = (int) (currentValues[0] - previousValues[0]); |
178 int deltaY = (int) (currentValues[1] - previousValues[1]); | 178 int deltaY = (int) (currentValues[1] - previousValues[1]); |
179 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx)
; | 179 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx)
; |
180 } | 180 } |
181 } | 181 } |
OLD | NEW |