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

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

Issue 2103933002: Updating SystemUI visibility events in Adroid Client. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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.graphics.Matrix;
7 import android.graphics.PointF; 8 import android.graphics.PointF;
9 import android.graphics.Rect;
8 import android.graphics.RectF; 10 import android.graphics.RectF;
9 11
10 /** 12 /**
11 * This class is responsible for transforming the desktop image matrix. 13 * This class is responsible for transforming the desktop image matrix.
12 */ 14 */
13 public class DesktopCanvas { 15 public class DesktopCanvas {
14 /** 16 /**
15 * Maximum allowed zoom level - see {@link #repositionImageWithZoom()}. 17 * Maximum allowed zoom level - see {@link #repositionImageWithZoom()}.
16 */ 18 */
17 private static final float MAX_ZOOM_FACTOR = 100.0f; 19 private static final float MAX_ZOOM_FACTOR = 100.0f;
18 20
19 private final DesktopViewInterface mViewer; 21 private final DesktopViewInterface mViewer;
20 private final RenderData mRenderData; 22 private final RenderData mRenderData;
21 23
22 /** 24 /**
23 * Represents the desired center of the viewport. This value may not repres ent the actual 25 * Represents the desired center of the viewport. This value may not repres ent the actual
24 * center of the viewport as adjustments are made to ensure as much of the d esktop is visible as 26 * center of the viewport as adjustments are made to ensure as much of the d esktop is visible as
25 * possible. This value needs to be a pair of floats so the desktop image c an be positioned 27 * possible. This value needs to be a pair of floats so the desktop image c an be positioned
26 * with sub-pixel accuracy for smoother panning animations at high zoom leve ls. 28 * with sub-pixel accuracy for smoother panning animations at high zoom leve ls.
27 */ 29 */
28 private PointF mViewportPosition = new PointF(); 30 private PointF mViewportPosition = new PointF();
29 31
30 /** 32 /**
31 * Represents the amount of vertical space in pixels used by the soft input device and 33 * Represents the amount of space, in pixels, used by system UI.
32 * accompanying system UI.
33 */ 34 */
34 private int mInputMethodOffsetY = 0; 35 private Rect mSystemUiOffsetPx = new Rect();
35
36 /**
37 * Represents the amount of horizontal space in pixels used by the soft inpu t device and
38 * accompanying system UI.
39 */
40 private int mInputMethodOffsetX = 0;
41 36
42 public DesktopCanvas(DesktopViewInterface viewer, RenderData renderData) { 37 public DesktopCanvas(DesktopViewInterface viewer, RenderData renderData) {
43 mViewer = viewer; 38 mViewer = viewer;
44 mRenderData = renderData; 39 mRenderData = renderData;
45 } 40 }
46 41
47 /** 42 /**
48 * Returns the desired center position of the viewport. Note that this may not represent the 43 * Returns the true center position of the viewport in image coordinates.
49 * true center of the viewport as other calculations are done to maximize th e viewable area.
50 * 44 *
51 * @return A point representing the desired position of the viewport. 45 * @return A point representing the true center position of the viewport.
52 */ 46 */
53 public PointF getViewportPosition() { 47 private PointF getTrueViewportCenter() {
54 return new PointF(mViewportPosition.x, mViewportPosition.y); 48 synchronized (mRenderData) {
49 PointF viewportSize = getViewportSize();
50 // Find the center point of the viewport on the screen.
51 float[] viewportPosition = {((float) viewportSize.x / 2) + mSystemUi OffsetPx.left,
52 ((float) viewportSize.y / 2) + mSystemUiOffsetPx.top};
53 // Convert the screen position to an image position.
54 Matrix inverted = new Matrix();
55 mRenderData.transform.invert(inverted);
56 inverted.mapPoints(viewportPosition);
57 return new PointF(viewportPosition[0], viewportPosition[1]);
58 }
55 } 59 }
56 60
57 /** 61 /**
58 * Sets the desired center position of the viewport. 62 * Shifts the viewport by the passed in deltas (in image coordinates).
59 * 63 *
60 * @param newX The new x coordinate value for the desired center position. 64 *
61 * @param newY The new y coordinate value for the desired center position. 65 * @return A point representing the desired center position of the viewport.
62 */ 66 */
63 public void setViewportPosition(float newX, float newY) { 67 public PointF moveViewportCenter(boolean useScreenCenter, float deltaX, floa t deltaY) {
64 mViewportPosition.set(newX, newY); 68 PointF viewportCenter;
65 } 69 synchronized (mRenderData) {
70 RectF bounds;
71 if (useScreenCenter) {
72 viewportCenter = getTrueViewportCenter();
73 float[] mappedValues = {
74 (float) mRenderData.screenWidth / 2, (float) mRenderData .screenHeight / 2};
75 Matrix canvasToImage = new Matrix();
76 mRenderData.transform.invert(canvasToImage);
77 canvasToImage.mapVectors(mappedValues);
78 bounds = new RectF(mappedValues[0], mappedValues[1],
79 mRenderData.imageWidth - mappedValues[0],
80 mRenderData.imageHeight - mappedValues[1]);
81 } else {
82 viewportCenter = new PointF(mViewportPosition.x, mViewportPositi on.y);
83 bounds = new RectF(0, 0, mRenderData.imageWidth, mRenderData.ima geHeight);
84 }
66 85
67 /** 86 viewportCenter.set(viewportCenter.x + deltaX, viewportCenter.y + del taY);
68 * Sets the offset values used to calculate the space used by the current so ft input method. 87 if (viewportCenter.x < bounds.left) {
69 * 88 viewportCenter.x = bounds.left;
70 * @param offsetX The space used by the soft input method UI on the right ed ge of the screen. 89 } else if (viewportCenter.x > bounds.right) {
71 * @param offsetY The space used by the soft input method UI on the bottom e dge of the screen. 90 viewportCenter.x = bounds.right;
72 */ 91 }
73 public void setInputMethodOffsetValues(int offsetX, int offsetY) { 92
74 mInputMethodOffsetX = offsetX; 93 if (viewportCenter.y < bounds.top) {
75 mInputMethodOffsetY = offsetY; 94 viewportCenter.y = bounds.top;
95 } else if (viewportCenter.y > bounds.bottom) {
96 viewportCenter.y = bounds.bottom;
97 }
98
99 mViewportPosition.set(viewportCenter);
100 }
101
102 return viewportCenter;
76 } 103 }
77 104
78 /** 105 /**
79 * Returns the current size of the viewport. This size includes the offset calculations for 106 * Returns the current size of the viewport. This size includes the offset calculations for
80 * any visible Input Method UI. 107 * any visible system UI.
81 * 108 *
82 * @return A point representing the current size of the viewport. 109 * @return A point representing the current size of the viewport.
83 */ 110 */
84 public PointF getViewportSize() { 111 private PointF getViewportSize() {
85 float adjustedScreenWidth, adjustedScreenHeight; 112 float adjustedScreenWidth, adjustedScreenHeight;
86 synchronized (mRenderData) { 113 synchronized (mRenderData) {
87 adjustedScreenWidth = mRenderData.screenWidth - mInputMethodOffsetX; 114 adjustedScreenWidth =
88 adjustedScreenHeight = mRenderData.screenHeight - mInputMethodOffset Y; 115 mRenderData.screenWidth - mSystemUiOffsetPx.right - mSystemU iOffsetPx.left;
116 adjustedScreenHeight =
117 mRenderData.screenHeight - mSystemUiOffsetPx.top - mSystemUi OffsetPx.bottom;
89 } 118 }
90 119
91 return new PointF(adjustedScreenWidth, adjustedScreenHeight); 120 return new PointF(adjustedScreenWidth, adjustedScreenHeight);
92 } 121 }
93 122
123 /**
124 * Sets the offset values used to calculate the space used by system UI.
125 *
126 * @param left The space used by system UI on the left edge of the screen.
127 * @param top The space used by system UI on the top edge of the screen.
128 * @param right The space used by system UI on the right edge of the screen.
129 * @param bottom The space used by system UI on the bottom edge of the scree n.
130 */
131 public void setSystemUiOffsetValues(int left, int top, int right, int bottom ) {
132 synchronized (mRenderData) {
133 mSystemUiOffsetPx.set(left, top, right, bottom);
134 }
135 }
136
94 /** Repositions the image by zooming it such that the image is displayed wit hout borders. */ 137 /** Repositions the image by zooming it such that the image is displayed wit hout borders. */
95 public void resizeImageToFitScreen() { 138 public void resizeImageToFitScreen() {
96 synchronized (mRenderData) { 139 synchronized (mRenderData) {
97 // Protect against being called before the image has been initialize d. 140 // Protect against being called before the image has been initialize d.
98 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) { 141 if (mRenderData.imageWidth == 0 || mRenderData.imageHeight == 0) {
99 return; 142 return;
100 } 143 }
101 144
102 float widthRatio = (float) mRenderData.screenWidth / mRenderData.ima geWidth; 145 float widthRatio = (float) mRenderData.screenWidth / mRenderData.ima geWidth;
103 float heightRatio = (float) mRenderData.screenHeight / mRenderData.i mageHeight; 146 float heightRatio = (float) mRenderData.screenHeight / mRenderData.i mageHeight;
104 float screenToImageScale = Math.max(widthRatio, heightRatio); 147 float screenToImageScale = Math.max(widthRatio, heightRatio);
105 148
106 // If the image is smaller than the screen in either dimension, then we want to scale it 149 // If the image is smaller than the screen in either dimension, then we want to scale it
107 // up to fit both and fill the screen with the image of the remote d esktop. 150 // up to fit both and fill the screen with the image of the remote d esktop.
108 if (screenToImageScale > 1.0f) { 151 if (screenToImageScale > 1.0f) {
109 mRenderData.transform.setScale(screenToImageScale, screenToImage Scale); 152 mRenderData.transform.setScale(screenToImageScale, screenToImage Scale);
110 } 153 }
111 } 154 }
112 155
113 repositionImage(false); 156 repositionImage(false);
114 } 157 }
115 158
116 /** 159 /**
117 * Repositions the image by translating it (without affecting the zoom level ). 160 * Repositions the image by translating it (without affecting the zoom level ).
118 * 161 *
119 * @param centerViewport Determines whether the viewport will be translated to the desired 162 * @param centerViewport Determines whether the viewport will be translated to the desired
120 * center position before being adjusted to fit the sc reen boundaries. 163 * center position before being adjusted to fit the sc reen boundaries.
121 */ 164 */
122 public void repositionImage(boolean centerViewport) { 165 public void repositionImage(boolean centerViewport) {
123 PointF adjustedViewportSize = getViewportSize(); 166 PointF viewportSize = getViewportSize();
124 synchronized (mRenderData) { 167 synchronized (mRenderData) {
125 // The goal of the code below is to position the viewport as close t o the desired center 168 // The goal of the code below is to position the viewport as close t o the desired center
126 // position as possible whilst keeping as much of the desktop in vie w as possible. 169 // position as possible whilst keeping as much of the desktop in vie w as possible.
127 // To achieve these goals, we first position the desktop image at th e desired center 170 // To achieve these goals, we first position the desktop image at th e desired center
128 // point and then re-position it to maximize the viewable area. 171 // point and then re-position it to maximize the viewable area.
129 if (centerViewport) { 172 if (centerViewport) {
130 // Map the current viewport position to screen coordinates. 173 // Map the current viewport position to screen coordinates.
131 float[] viewportPosition = {mViewportPosition.x, mViewportPositi on.y}; 174 float[] viewportPosition = {mViewportPosition.x, mViewportPositi on.y};
132 mRenderData.transform.mapPoints(viewportPosition); 175 mRenderData.transform.mapPoints(viewportPosition);
133 176
134 // Translate so the viewport is displayed in the middle of the s creen. 177 // Translate so the viewport is displayed in the middle of the s creen.
135 mRenderData.transform.postTranslate( 178 mRenderData.transform.postTranslate(
136 (float) adjustedViewportSize.x / 2 - viewportPosition[0] , 179 ((float) viewportSize.x / 2) + mSystemUiOffsetPx.left - viewportPosition[0],
137 (float) adjustedViewportSize.y / 2 - viewportPosition[1] ); 180 ((float) viewportSize.y / 2) + mSystemUiOffsetPx.top - v iewportPosition[1]);
138 } 181 }
139 182
140 // Get the coordinates of the desktop rectangle (top-left/bottom-rig ht corners) in 183 // Get the coordinates of the desktop rectangle (top-left/bottom-rig ht corners) in
141 // screen coordinates. Order is: left, top, right, bottom. 184 // screen coordinates. Order is: left, top, right, bottom.
142 RectF rectScreen = new RectF(0, 0, mRenderData.imageWidth, mRenderDa ta.imageHeight); 185 RectF rectScreen = new RectF(0, 0, mRenderData.imageWidth, mRenderDa ta.imageHeight);
143 mRenderData.transform.mapRect(rectScreen); 186 mRenderData.transform.mapRect(rectScreen);
144 187
145 float leftDelta = rectScreen.left; 188 float leftDelta = rectScreen.left - mSystemUiOffsetPx.left;
146 float rightDelta = rectScreen.right - mRenderData.screenWidth + mInp utMethodOffsetX; 189 float rightDelta = rectScreen.right - mRenderData.screenWidth + mSys temUiOffsetPx.right;
147 float topDelta = rectScreen.top; 190 float topDelta = rectScreen.top - mSystemUiOffsetPx.top;
148 float bottomDelta = rectScreen.bottom - mRenderData.screenHeight + m InputMethodOffsetY; 191 float bottomDelta =
192 rectScreen.bottom - mRenderData.screenHeight + mSystemUiOffs etPx.bottom;
149 float xAdjust = 0; 193 float xAdjust = 0;
150 float yAdjust = 0; 194 float yAdjust = 0;
151 195
152 if (rectScreen.right - rectScreen.left < adjustedViewportSize.x) { 196 if (rectScreen.right - rectScreen.left < viewportSize.x) {
153 // Image is narrower than the screen, so center it. 197 // Image is narrower than the screen, so center it.
154 xAdjust = -(rightDelta + leftDelta) / 2; 198 xAdjust = -(rightDelta + leftDelta) / 2;
155 } else if (leftDelta > 0 && rightDelta > 0) { 199 } else if (leftDelta > 0 && rightDelta > 0) {
156 // Panning the image left will show more of it. 200 // Panning the image left will show more of it.
157 xAdjust = -Math.min(leftDelta, rightDelta); 201 xAdjust = -Math.min(leftDelta, rightDelta);
158 } else if (leftDelta < 0 && rightDelta < 0) { 202 } else if (leftDelta < 0 && rightDelta < 0) {
159 // Pan the image right. 203 // Pan the image right.
160 xAdjust = Math.min(-leftDelta, -rightDelta); 204 xAdjust = Math.min(-leftDelta, -rightDelta);
161 } 205 }
162 206
163 // Apply similar logic for yAdjust. 207 // Apply similar logic for yAdjust.
164 if (rectScreen.bottom - rectScreen.top < adjustedViewportSize.y) { 208 if (rectScreen.bottom - rectScreen.top < viewportSize.y) {
165 yAdjust = -(bottomDelta + topDelta) / 2; 209 yAdjust = -(bottomDelta + topDelta) / 2;
166 } else if (topDelta > 0 && bottomDelta > 0) { 210 } else if (topDelta > 0 && bottomDelta > 0) {
167 yAdjust = -Math.min(topDelta, bottomDelta); 211 yAdjust = -Math.min(topDelta, bottomDelta);
168 } else if (topDelta < 0 && bottomDelta < 0) { 212 } else if (topDelta < 0 && bottomDelta < 0) {
169 yAdjust = Math.min(-topDelta, -bottomDelta); 213 yAdjust = Math.min(-topDelta, -bottomDelta);
170 } 214 }
171 215
172 mRenderData.transform.postTranslate(xAdjust, yAdjust); 216 mRenderData.transform.postTranslate(xAdjust, yAdjust);
173 217
174 mViewer.transformationChanged(); 218 mViewer.transformationChanged();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // level needed to fit either the width or height. 250 // level needed to fit either the width or height.
207 float scale = Math.min((float) mRenderData.screenWidth / mRender Data.imageWidth, 251 float scale = Math.min((float) mRenderData.screenWidth / mRender Data.imageWidth,
208 (float) mRenderData.screenHeight / mRende rData.imageHeight); 252 (float) mRenderData.screenHeight / mRende rData.imageHeight);
209 mRenderData.transform.setScale(scale, scale); 253 mRenderData.transform.setScale(scale, scale);
210 } 254 }
211 } 255 }
212 256
213 repositionImage(centerViewport); 257 repositionImage(centerViewport);
214 } 258 }
215 } 259 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698