blob: 267566bfd6f7d292b4ebbfebc3eeca24a88ca700 [file] [log] [blame]
Chong Zhang8e89b312015-09-09 15:09:30 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale59a73ca2015-09-14 12:54:50 -070019import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
20import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT;
Chong Zhang87b21722015-09-21 15:39:51 -070021import static android.app.ActivityManager.RESIZE_MODE_USER;
Chong Zhang6de2ae82015-09-30 18:25:21 -070022import static android.app.ActivityManager.RESIZE_MODE_USER_FORCED;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale228d4042015-09-13 10:17:34 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070025import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080026import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
28import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
30import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070031import static com.android.server.wm.WindowManagerService.dipToPixel;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010032import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070033import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
34import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070035
36import android.annotation.IntDef;
37import android.graphics.Point;
38import android.graphics.Rect;
39import android.os.Looper;
40import android.os.Process;
41import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070042import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070043import android.util.DisplayMetrics;
44import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080045import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070046import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070047import android.view.Display;
Wale Ogunwale228d4042015-09-13 10:17:34 -070048import android.view.DisplayInfo;
Chong Zhang8e89b312015-09-09 15:09:30 -070049import android.view.InputChannel;
50import android.view.InputDevice;
51import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070052import android.view.MotionEvent;
53import android.view.WindowManager;
54
skuhne@google.com322347b2016-12-02 12:54:03 -080055import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale228d4042015-09-13 10:17:34 -070056import com.android.server.input.InputApplicationHandle;
57import com.android.server.input.InputWindowHandle;
58import com.android.server.wm.WindowManagerService.H;
59
60import java.lang.annotation.Retention;
61import java.lang.annotation.RetentionPolicy;
62
63class TaskPositioner implements DimLayer.DimLayerUser {
skuhne@google.com322347b2016-12-02 12:54:03 -080064 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010065 private static final String TAG_LOCAL = "TaskPositioner";
66 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070067
Wale Ogunwale228d4042015-09-13 10:17:34 -070068 // The margin the pointer position has to be within the side of the screen to be
69 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070070 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070071
Chong Zhang8e89b312015-09-09 15:09:30 -070072 @IntDef(flag = true,
73 value = {
74 CTRL_NONE,
75 CTRL_LEFT,
76 CTRL_RIGHT,
77 CTRL_TOP,
78 CTRL_BOTTOM
79 })
80 @Retention(RetentionPolicy.SOURCE)
81 @interface CtrlType {}
82
83 private static final int CTRL_NONE = 0x0;
84 private static final int CTRL_LEFT = 0x1;
85 private static final int CTRL_RIGHT = 0x2;
86 private static final int CTRL_TOP = 0x4;
87 private static final int CTRL_BOTTOM = 0x8;
88
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080089 public static final float RESIZING_HINT_ALPHA = 0.5f;
90
91 public static final int RESIZING_HINT_DURATION_MS = 0;
92
skuhne@google.com322347b2016-12-02 12:54:03 -080093 // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait).
94 // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever
95 // aspect he desires.
96 @VisibleForTesting
97 static final float MIN_ASPECT = 1.2f;
98
Chong Zhang8e89b312015-09-09 15:09:30 -070099 private final WindowManagerService mService;
100 private WindowPositionerEventReceiver mInputEventReceiver;
101 private Display mDisplay;
102 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700103 private DimLayer mDimLayer;
104 @CtrlType
105 private int mCurrentDimSide;
106 private Rect mTmpRect = new Rect();
107 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700108 private int mMinVisibleWidth;
109 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700110
Chong Zhang3005e752015-09-18 18:46:28 -0700111 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700112 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -0800113 private boolean mPreserveOrientation;
114 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -0700115 private final Rect mWindowOriginalBounds = new Rect();
116 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -0800117 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -0700118 private float mStartDragX;
119 private float mStartDragY;
120 @CtrlType
121 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700122 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700123
124 InputChannel mServerChannel;
125 InputChannel mClientChannel;
126 InputApplicationHandle mDragApplicationHandle;
127 InputWindowHandle mDragWindowHandle;
128
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700129 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
130 public WindowPositionerEventReceiver(
131 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
132 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700133 }
134
135 @Override
136 public void onInputEvent(InputEvent event) {
137 if (!(event instanceof MotionEvent)
138 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
139 return;
140 }
141 final MotionEvent motionEvent = (MotionEvent) event;
142 boolean handled = false;
143
144 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700145 if (mDragEnded) {
146 // The drag has ended but the clean-up message has not been processed by
147 // window manager. Drop events that occur after this until window manager
148 // has a chance to clean-up the input handle.
149 handled = true;
150 return;
151 }
152
Chong Zhang8e89b312015-09-09 15:09:30 -0700153 final float newX = motionEvent.getRawX();
154 final float newY = motionEvent.getRawY();
155
156 switch (motionEvent.getAction()) {
157 case MotionEvent.ACTION_DOWN: {
158 if (DEBUG_TASK_POSITIONING) {
159 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
160 }
161 } break;
162
163 case MotionEvent.ACTION_MOVE: {
164 if (DEBUG_TASK_POSITIONING){
165 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
166 }
167 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700168 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800169 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700170 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700171 if (!mTmpRect.equals(mWindowDragBounds)) {
172 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
173 "wm.TaskPositioner.resizeTask");
174 try {
175 mService.mActivityManager.resizeTask(
176 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
177 } catch (RemoteException e) {
178 }
179 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
180 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700181 } break;
182
183 case MotionEvent.ACTION_UP: {
184 if (DEBUG_TASK_POSITIONING) {
185 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
186 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700187 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700188 } break;
189
190 case MotionEvent.ACTION_CANCEL: {
191 if (DEBUG_TASK_POSITIONING) {
192 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
193 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700194 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700195 } break;
196 }
197
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700198 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700199 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700200 synchronized (mService.mWindowMap) {
201 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700202 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700203 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700204 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700205 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700206 // We were using fullscreen surface during resizing. Request
207 // resizeTask() one last time to restore surface to window size.
208 mService.mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700209 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700210 }
211
212 if (mCurrentDimSide != CTRL_NONE) {
213 final int createMode = mCurrentDimSide == CTRL_LEFT
214 ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
215 : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
216 mService.mActivityManager.moveTaskToDockedStack(
Jorim Jaggi9ea2f7b2015-11-23 18:08:28 -0800217 mTask.mTaskId, createMode, true /*toTop*/, true /* animate */,
Wale Ogunwalea4d92a02016-05-01 16:02:16 -0700218 null /* initialBounds */, false /* moveHomeStackFront */);
Chong Zhang3005e752015-09-18 18:46:28 -0700219 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700220 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700221
Chong Zhang8e89b312015-09-09 15:09:30 -0700222 // Post back to WM to handle clean-ups. We still need the input
223 // event handler for the last finishInputEvent()!
224 mService.mH.sendEmptyMessage(H.FINISH_TASK_POSITIONING);
225 }
226 handled = true;
227 } catch (Exception e) {
228 Slog.e(TAG, "Exception caught by drag handleMotion", e);
229 } finally {
230 finishInputEvent(event, handled);
231 }
232 }
233 }
234
235 TaskPositioner(WindowManagerService service) {
236 mService = service;
237 }
238
skuhne@google.com322347b2016-12-02 12:54:03 -0800239 @VisibleForTesting
240 Rect getWindowDragBounds() {
241 return mWindowDragBounds;
242 }
243
Chong Zhang8e89b312015-09-09 15:09:30 -0700244 /**
245 * @param display The Display that the window being dragged is on.
246 */
247 void register(Display display) {
248 if (DEBUG_TASK_POSITIONING) {
249 Slog.d(TAG, "Registering task positioner");
250 }
251
252 if (mClientChannel != null) {
253 Slog.e(TAG, "Task positioner already registered");
254 return;
255 }
256
257 mDisplay = display;
258 mDisplay.getMetrics(mDisplayMetrics);
259 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
260 mServerChannel = channels[0];
261 mClientChannel = channels[1];
262 mService.mInputManager.registerInputChannel(mServerChannel, null);
263
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700264 mInputEventReceiver = new WindowPositionerEventReceiver(
265 mClientChannel, mService.mH.getLooper(), mService.mChoreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700266
267 mDragApplicationHandle = new InputApplicationHandle(null);
268 mDragApplicationHandle.name = TAG;
269 mDragApplicationHandle.dispatchingTimeoutNanos =
270 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
271
272 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
273 mDisplay.getDisplayId());
274 mDragWindowHandle.name = TAG;
275 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700276 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700277 mDragWindowHandle.layoutParamsFlags = 0;
278 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
279 mDragWindowHandle.dispatchingTimeoutNanos =
280 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
281 mDragWindowHandle.visible = true;
282 mDragWindowHandle.canReceiveKeys = false;
283 mDragWindowHandle.hasFocus = true;
284 mDragWindowHandle.hasWallpaper = false;
285 mDragWindowHandle.paused = false;
286 mDragWindowHandle.ownerPid = Process.myPid();
287 mDragWindowHandle.ownerUid = Process.myUid();
288 mDragWindowHandle.inputFeatures = 0;
289 mDragWindowHandle.scaleFactor = 1.0f;
290
291 // The drag window cannot receive new touches.
292 mDragWindowHandle.touchableRegion.setEmpty();
293
294 // The drag window covers the entire display
295 mDragWindowHandle.frameLeft = 0;
296 mDragWindowHandle.frameTop = 0;
297 final Point p = new Point();
298 mDisplay.getRealSize(p);
299 mDragWindowHandle.frameRight = p.x;
300 mDragWindowHandle.frameBottom = p.y;
301
302 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800303 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700304 Slog.d(TAG, "Pausing rotation during re-position");
305 }
306 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700307
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100308 mDimLayer = new DimLayer(mService, this, mDisplay.getDisplayId(), TAG_LOCAL);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700309 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
310 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
311 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
skuhne@google.com322347b2016-12-02 12:54:03 -0800312 mDisplay.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700313
314 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700315 }
316
317 void unregister() {
318 if (DEBUG_TASK_POSITIONING) {
319 Slog.d(TAG, "Unregistering task positioner");
320 }
321
322 if (mClientChannel == null) {
323 Slog.e(TAG, "Task positioner not registered");
324 return;
325 }
326
327 mService.mInputManager.unregisterInputChannel(mServerChannel);
328
329 mInputEventReceiver.dispose();
330 mInputEventReceiver = null;
331 mClientChannel.dispose();
332 mServerChannel.dispose();
333 mClientChannel = null;
334 mServerChannel = null;
335
336 mDragWindowHandle = null;
337 mDragApplicationHandle = null;
338 mDisplay = null;
339
Wale Ogunwale228d4042015-09-13 10:17:34 -0700340 if (mDimLayer != null) {
341 mDimLayer.destroySurface();
342 mDimLayer = null;
343 }
344 mCurrentDimSide = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700345 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700346
Chong Zhang8e89b312015-09-09 15:09:30 -0700347 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800348 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700349 Slog.d(TAG, "Resuming rotation after re-position");
350 }
351 mService.resumeRotationLocked();
352 }
353
skuhne@google.com322347b2016-12-02 12:54:03 -0800354 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
355 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700356 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800357 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
358 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
359 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700360 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800361 mTask = win.getTask();
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700362 // Use the dim bounds, not the original task bounds. The cursor
363 // movement should be calculated relative to the visible bounds.
364 // Also, use the dim bounds of the task which accounts for
365 // multiple app windows. Don't use any bounds from win itself as it
366 // may not be the same size as the task.
367 mTask.getDimBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800368 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
369 }
370
371 @VisibleForTesting
372 void startDrag(boolean resize, boolean preserveOrientation,
373 float startX, float startY, Rect startBounds) {
374 mCtrlType = CTRL_NONE;
375 mStartDragX = startX;
376 mStartDragY = startY;
377 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800378
Chong Zhang8e89b312015-09-09 15:09:30 -0700379 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800380 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700381 mCtrlType |= CTRL_LEFT;
382 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800383 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700384 mCtrlType |= CTRL_RIGHT;
385 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800386 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700387 mCtrlType |= CTRL_TOP;
388 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800389 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700390 mCtrlType |= CTRL_BOTTOM;
391 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800392 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700393 }
394
skuhne@google.com322347b2016-12-02 12:54:03 -0800395 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
396 // made up out of multiple windows which are only partially overlapping. When that happens,
397 // the orientation from the window of interest to the entire stack might diverge. However
398 // for now we treat them as the same.
399 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
400 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700401
402 // Make sure we always have valid drag bounds even if the drag ends before any move events
403 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800404 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700405 }
406
407 private void endDragLocked() {
408 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100409 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700410 }
411
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700412 /** Returns true if the move operation should be ended. */
413 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700414 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800415 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700416 }
417
418 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800419 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100420 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700421 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700422 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700423
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700424 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800425 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800426
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700427 int nX = (int) x;
428 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800429 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700430 // For a moving operation we allow the pointer to go out of the stack bounds, but
431 // use the clamped pointer position for the drag bounds computation.
432 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
433 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800434 }
435
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700436 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800437 updateDimLayerVisibility(nX);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700438 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800439 }
440
skuhne@google.com322347b2016-12-02 12:54:03 -0800441 /**
442 * The user is drag - resizing the window.
443 *
444 * @param x The x coordinate of the current drag coordinate.
445 * @param y the y coordinate of the current drag coordinate.
446 */
447 @VisibleForTesting
448 void resizeDrag(float x, float y) {
449 // This is a resizing operation.
450 // We need to keep various constraints:
451 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
452 // 2. The orientation is kept - if required.
453 final int deltaX = Math.round(x - mStartDragX);
454 final int deltaY = Math.round(y - mStartDragY);
455 int left = mWindowOriginalBounds.left;
456 int top = mWindowOriginalBounds.top;
457 int right = mWindowOriginalBounds.right;
458 int bottom = mWindowOriginalBounds.bottom;
459
460 // The aspect which we have to respect. Note that if the orientation does not need to be
461 // preserved the aspect will be calculated as 1.0 which neutralizes the following
462 // computations.
463 final float minAspect = !mPreserveOrientation
464 ? 1.0f
465 : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT));
466 // Calculate the resulting width and height of the drag operation.
467 int width = right - left;
468 int height = bottom - top;
469 if ((mCtrlType & CTRL_LEFT) != 0) {
470 width = Math.max(mMinVisibleWidth, width - deltaX);
471 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
472 width = Math.max(mMinVisibleWidth, width + deltaX);
473 }
474 if ((mCtrlType & CTRL_TOP) != 0) {
475 height = Math.max(mMinVisibleHeight, height - deltaY);
476 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
477 height = Math.max(mMinVisibleHeight, height + deltaY);
478 }
479
480 // If we have to preserve the orientation - check that we are doing so.
481 final float aspect = (float) width / (float) height;
482 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
483 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
484 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
485 // drag axis. What ever is producing the bigger rectangle will be chosen.
486 int width1;
487 int width2;
488 int height1;
489 int height2;
490 if (mStartOrientationWasLandscape) {
491 // Assuming that the width is our target we calculate the height.
492 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
493 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
494 if (height1 < mMinVisibleHeight) {
495 // If the resulting height is too small we adjust to the minimal size.
496 height1 = mMinVisibleHeight;
497 width1 = Math.max(mMinVisibleWidth,
498 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
499 }
500 // Assuming that the height is our target we calculate the width.
501 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
502 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
503 if (width2 < mMinVisibleWidth) {
504 // If the resulting width is too small we adjust to the minimal size.
505 width2 = mMinVisibleWidth;
506 height2 = Math.max(mMinVisibleHeight,
507 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
508 }
509 } else {
510 // Assuming that the width is our target we calculate the height.
511 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
512 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
513 if (height1 < mMinVisibleHeight) {
514 // If the resulting height is too small we adjust to the minimal size.
515 height1 = mMinVisibleHeight;
516 width1 = Math.max(mMinVisibleWidth,
517 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
518 }
519 // Assuming that the height is our target we calculate the width.
520 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
521 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
522 if (width2 < mMinVisibleWidth) {
523 // If the resulting width is too small we adjust to the minimal size.
524 width2 = mMinVisibleWidth;
525 height2 = Math.max(mMinVisibleHeight,
526 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
527 }
528 }
529
530 // Use the bigger of the two rectangles if the major change was positive, otherwise
531 // do the opposite.
532 final boolean grows = width > (right - left) || height > (bottom - top);
533 if (grows == (width1 * height1 > width2 * height2)) {
534 width = width1;
535 height = height1;
536 } else {
537 width = width2;
538 height = height2;
539 }
540 }
541
542 // Update mWindowDragBounds to the new drag size.
543 updateDraggedBounds(left, top, right, bottom, width, height);
544 }
545
546 /**
547 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
548 *
549 * @param left The original left bound before the user started dragging.
550 * @param top The original top bound before the user started dragging.
551 * @param right The original right bound before the user started dragging.
552 * @param bottom The original bottom bound before the user started dragging.
553 * @param newWidth The new dragged width.
554 * @param newHeight The new dragged height.
555 */
556 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
557 int newHeight) {
558 // Generate the final bounds by keeping the opposite drag edge constant.
559 if ((mCtrlType & CTRL_LEFT) != 0) {
560 left = right - newWidth;
561 } else { // Note: The right might have changed - if we pulled at the right or not.
562 right = left + newWidth;
563 }
564 if ((mCtrlType & CTRL_TOP) != 0) {
565 top = bottom - newHeight;
566 } else { // Note: The height might have changed - if we pulled at the bottom or not.
567 bottom = top + newHeight;
568 }
569
570 mWindowDragBounds.set(left, top, right, bottom);
571
572 checkBoundsForOrientationViolations(mWindowDragBounds);
573 }
574
575 /**
576 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
577 *
578 * @param bounds The bounds to be checked.
579 */
580 private void checkBoundsForOrientationViolations(Rect bounds) {
581 // When using debug check that we are not violating the given constraints.
582 if (DEBUG_ORIENTATION_VIOLATIONS) {
583 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
584 Slog.e(TAG, "Orientation violation detected! should be "
585 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
586 + " but is the other");
587 } else {
588 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
589 }
590 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
591 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
592 + ", " + bounds.width() + ") Height(min,is)=("
593 + mMinVisibleHeight + ", " + bounds.height() + ")");
594 }
595 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
596 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
597 + ", " + bounds.width() + ") Height(min,is)=("
598 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
599 }
600 }
601 }
602
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700603 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
604 final int offsetX = Math.round(x - mStartDragX);
605 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700606 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700607 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
608 final int maxLeft = stackBounds.right - mMinVisibleWidth;
609 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700610
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700611 // Vertically, the top mMinVisibleHeight of the window should remain visible.
612 // (This assumes that the window caption bar is at the top of the window).
613 final int minTop = stackBounds.top;
614 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700615
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700616 mWindowDragBounds.offsetTo(
617 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
618 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
619
Chong Zhangb15758a2015-11-17 12:12:03 -0800620 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
621 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700622 }
623
Wale Ogunwale228d4042015-09-13 10:17:34 -0700624 private void updateDimLayerVisibility(int x) {
625 @CtrlType
626 int dimSide = getDimSide(x);
627 if (dimSide == mCurrentDimSide) {
628 return;
629 }
630
631 mCurrentDimSide = dimSide;
632
633 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION updateDimLayerVisibility");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700634 mService.openSurfaceTransaction();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700635 if (mCurrentDimSide == CTRL_NONE) {
636 mDimLayer.hide();
637 } else {
638 showDimLayer();
639 }
Robert Carr68e5c9e2016-09-14 10:50:09 -0700640 mService.closeSurfaceTransaction();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700641 }
642
643 /**
644 * Returns the side of the screen the dim layer should be shown.
645 * @param x horizontal coordinate used to determine if the dim layer should be shown
646 * @return Returns {@link #CTRL_LEFT} if the dim layer should be shown on the left half of the
647 * screen, {@link #CTRL_RIGHT} if on the right side, or {@link #CTRL_NONE} if the dim layer
648 * shouldn't be shown.
649 */
650 private int getDimSide(int x) {
Chong Zhang3005e752015-09-18 18:46:28 -0700651 if (mTask.mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
Wale Ogunwale14a3fb92016-09-11 15:19:05 -0700652 || !mTask.mStack.fillsParent()
Andrii Kulian441e4492016-09-29 15:25:00 -0700653 || mTask.mStack.getConfiguration().orientation != ORIENTATION_LANDSCAPE) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700654 return CTRL_NONE;
655 }
656
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800657 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700658 if (x - mSideMargin <= mTmpRect.left) {
659 return CTRL_LEFT;
660 }
661 if (x + mSideMargin >= mTmpRect.right) {
662 return CTRL_RIGHT;
663 }
664
665 return CTRL_NONE;
666 }
667
668 private void showDimLayer() {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800669 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700670 if (mCurrentDimSide == CTRL_LEFT) {
671 mTmpRect.right = mTmpRect.centerX();
672 } else if (mCurrentDimSide == CTRL_RIGHT) {
673 mTmpRect.left = mTmpRect.centerX();
674 }
675
676 mDimLayer.setBounds(mTmpRect);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700677 mDimLayer.show(mService.getDragLayerLocked(), RESIZING_HINT_ALPHA,
678 RESIZING_HINT_DURATION_MS);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700679 }
680
681 @Override /** {@link DimLayer.DimLayerUser} */
Wale Ogunwale29bfbb82016-05-12 15:13:52 -0700682 public boolean dimFullscreen() {
683 return isFullscreen();
684 }
685
686 boolean isFullscreen() {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700687 return false;
688 }
689
690 @Override /** {@link DimLayer.DimLayerUser} */
691 public DisplayInfo getDisplayInfo() {
Chong Zhang3005e752015-09-18 18:46:28 -0700692 return mTask.mStack.getDisplayInfo();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700693 }
694
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700695 @Override
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800696 public void getDimBounds(Rect out) {
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700697 // This dim layer user doesn't need this.
698 }
699
700 @Override
701 public String toShortString() {
702 return TAG;
703 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700704}