Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(645)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/SimulatedTouchInputStrategy.java

Issue 2066683003: [Chromoting] Add InputInjector and InputInjectorWrapper for easy unittesting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix FindBugs errors Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 InputInjectorWrapper 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 = TouchInputHandler.BUTTON_UNDEFINED;
51 49
52 public SimulatedTouchInputStrategy(RenderData renderData, Client client, Con text context) { 50 public SimulatedTouchInputStrategy(RenderData renderData,
51 InputInjectorWrapper injector,
Lambros 2016/06/17 23:00:10 Wrong indentation (should be +8 spaces from previo
Hzj_jie 2016/06/19 23:41:39 Done.
52 Context context) {
53 Preconditions.notNull(injector);
53 mRenderData = renderData; 54 mRenderData = renderData;
54 mClient = client; 55 mInjector = injector;
55 56
56 ViewConfiguration config = ViewConfiguration.get(context); 57 ViewConfiguration config = ViewConfiguration.get(context);
57 mDoubleTapDurationInMs = config.getDoubleTapTimeout(); 58 mDoubleTapDurationInMs = config.getDoubleTapTimeout();
58 59
59 // In order to detect whether the user is attempting to double tap a tar get, we define a 60 // 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 61 // 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 62 // 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 63 // 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). 64 // 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 65 // 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
78 mDoubleTapSlopSquareInPx = scaledDoubleTapSlopInPx * scaledDoubleTapSlop InPx; 79 mDoubleTapSlopSquareInPx = scaledDoubleTapSlopInPx * scaledDoubleTapSlop InPx;
79 80
80 synchronized (mRenderData) { 81 synchronized (mRenderData) {
81 mRenderData.drawCursor = false; 82 mRenderData.drawCursor = false;
82 } 83 }
83 } 84 }
84 85
85 @Override 86 @Override
86 public boolean onTap(int button) { 87 public boolean onTap(int button) {
87 Point currentTapPoint = getCursorPosition(); 88 Point currentTapPoint = getCursorPosition();
88 if (button == TouchInputHandlerInterface.BUTTON_LEFT) { 89 if (button == TouchInputHandler.BUTTON_LEFT) {
89 // Left clicks are handled a little differently than the events for other buttons. 90 // 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 91 // 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 92 // 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 93 // 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 94 // 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 95 // 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 96 // 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. 97 // attempting a double tap, we use the original event's location for that second tap.
97 long tapInterval = SystemClock.uptimeMillis() - mLastTapTimeInMs; 98 long tapInterval = SystemClock.uptimeMillis() - mLastTapTimeInMs;
98 if (isDoubleTap(currentTapPoint.x, currentTapPoint.y, tapInterval)) { 99 if (isDoubleTap(currentTapPoint.x, currentTapPoint.y, tapInterval)) {
99 currentTapPoint = new Point(mLastTapPoint); 100 currentTapPoint = new Point(mLastTapPoint);
100 mLastTapPoint = null; 101 mLastTapPoint = null;
101 mLastTapTimeInMs = 0; 102 mLastTapTimeInMs = 0;
102 } else { 103 } else {
103 mLastTapPoint = currentTapPoint; 104 mLastTapPoint = currentTapPoint;
104 mLastTapTimeInMs = SystemClock.uptimeMillis(); 105 mLastTapTimeInMs = SystemClock.uptimeMillis();
105 } 106 }
106 } else { 107 } else {
107 mLastTapPoint = null; 108 mLastTapPoint = null;
108 mLastTapTimeInMs = 0; 109 mLastTapTimeInMs = 0;
109 } 110 }
110 111
111 injectMouseButtonEvent(button, true, currentTapPoint); 112 mInjector.sendMouseClick(currentTapPoint, button);
112 injectMouseButtonEvent(button, false, currentTapPoint);
113
114 return true; 113 return true;
115 } 114 }
116 115
117 @Override 116 @Override
118 public boolean onPressAndHold(int button) { 117 public boolean onPressAndHold(int button) {
119 injectMouseButtonEvent(button, true, getCursorPosition()); 118 mInjector.sendMouseDown(getCursorPosition(), button);
120 mHeldButton = button; 119 mHeldButton = button;
121 return true; 120 return true;
122 } 121 }
123 122
124 @Override 123 @Override
125 public void onScroll(float distanceX, float distanceY) { 124 public void onScroll(float distanceX, float distanceY) {
126 mClient.sendMouseWheelEvent((int) -distanceX, (int) -distanceY); 125 mInjector.sendReverseMouseWheelEvent(distanceX, distanceY);
127 } 126 }
128 127
129 @Override 128 @Override
130 public void onMotionEvent(MotionEvent event) { 129 public void onMotionEvent(MotionEvent event) {
131 if (event.getActionMasked() == MotionEvent.ACTION_UP 130 if (event.getActionMasked() == MotionEvent.ACTION_UP
132 && mHeldButton != TouchInputHandlerInterface.BUTTON_UNDEFINED) { 131 && mHeldButton != TouchInputHandler.BUTTON_UNDEFINED) {
133 injectMouseButtonEvent(mHeldButton, false, getCursorPosition()); 132 mInjector.sendMouseUp(getCursorPosition(), mHeldButton);
134 mHeldButton = TouchInputHandlerInterface.BUTTON_UNDEFINED; 133 mHeldButton = TouchInputHandler.BUTTON_UNDEFINED;
135 } 134 }
136 } 135 }
137 136
138 @Override 137 @Override
139 public void injectCursorMoveEvent(int x, int y) { 138 public void injectCursorMoveEvent(int x, int y) {
140 mClient.sendMouseEvent(x, y, TouchInputHandlerInterface.BUTTON_UNDEFINED , false); 139 mInjector.sendCursorMove(x, y);
141 } 140 }
142 141
143 @Override 142 @Override
144 public DesktopView.InputFeedbackType getShortPressFeedbackType() { 143 public DesktopView.InputFeedbackType getShortPressFeedbackType() {
145 return DesktopView.InputFeedbackType.SMALL_ANIMATION; 144 return DesktopView.InputFeedbackType.SMALL_ANIMATION;
146 } 145 }
147 146
148 @Override 147 @Override
149 public DesktopView.InputFeedbackType getLongPressFeedbackType() { 148 public DesktopView.InputFeedbackType getLongPressFeedbackType() {
150 return DesktopView.InputFeedbackType.LARGE_ANIMATION; 149 return DesktopView.InputFeedbackType.LARGE_ANIMATION;
(...skipping 22 matching lines...) Expand all
173 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y}; 172 float[] previousValues = {mLastTapPoint.x, mLastTapPoint.y};
174 synchronized (mRenderData) { 173 synchronized (mRenderData) {
175 mRenderData.transform.mapPoints(currentValues); 174 mRenderData.transform.mapPoints(currentValues);
176 mRenderData.transform.mapPoints(previousValues); 175 mRenderData.transform.mapPoints(previousValues);
177 } 176 }
178 177
179 int deltaX = (int) (currentValues[0] - previousValues[0]); 178 int deltaX = (int) (currentValues[0] - previousValues[0]);
180 int deltaY = (int) (currentValues[1] - previousValues[1]); 179 int deltaY = (int) (currentValues[1] - previousValues[1]);
181 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx) ; 180 return ((deltaX * deltaX + deltaY * deltaY) <= mDoubleTapSlopSquareInPx) ;
182 } 181 }
183
184 private void injectMouseButtonEvent(int button, boolean pressed, Point tapPo int) {
185 mClient.sendMouseEvent(tapPoint.x, tapPoint.y, button, pressed);
186 }
187 } 182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698