blob: 26c87b738f8c02e4e33f30fe9ee176a5ea38ce73 [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
Chong Zhang87b21722015-09-21 15:39:51 -070019import static android.app.ActivityManager.RESIZE_MODE_USER;
Chong Zhang6de2ae82015-09-30 18:25:21 -070020import static android.app.ActivityManager.RESIZE_MODE_USER_FORCED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070021import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080022import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
23import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
25import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070026import static com.android.server.wm.WindowManagerService.dipToPixel;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010027import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070028import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
29import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070030
31import android.annotation.IntDef;
32import android.graphics.Point;
33import android.graphics.Rect;
34import android.os.Looper;
35import android.os.Process;
36import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070037import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070038import android.util.DisplayMetrics;
39import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080040import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070041import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070042import android.view.Display;
43import android.view.InputChannel;
44import android.view.InputDevice;
45import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070046import android.view.MotionEvent;
47import android.view.WindowManager;
48
skuhne@google.com322347b2016-12-02 12:54:03 -080049import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale228d4042015-09-13 10:17:34 -070050import com.android.server.input.InputApplicationHandle;
51import com.android.server.input.InputWindowHandle;
52import com.android.server.wm.WindowManagerService.H;
53
54import java.lang.annotation.Retention;
55import java.lang.annotation.RetentionPolicy;
56
Robert Carrf59b8dd2017-10-02 18:58:36 -070057class TaskPositioner {
skuhne@google.com322347b2016-12-02 12:54:03 -080058 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010059 private static final String TAG_LOCAL = "TaskPositioner";
60 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070061
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080062 private static Factory sFactory;
63
Wale Ogunwale228d4042015-09-13 10:17:34 -070064 // The margin the pointer position has to be within the side of the screen to be
65 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070066 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070067
Chong Zhang8e89b312015-09-09 15:09:30 -070068 @IntDef(flag = true,
69 value = {
70 CTRL_NONE,
71 CTRL_LEFT,
72 CTRL_RIGHT,
73 CTRL_TOP,
74 CTRL_BOTTOM
75 })
76 @Retention(RetentionPolicy.SOURCE)
77 @interface CtrlType {}
78
79 private static final int CTRL_NONE = 0x0;
80 private static final int CTRL_LEFT = 0x1;
81 private static final int CTRL_RIGHT = 0x2;
82 private static final int CTRL_TOP = 0x4;
83 private static final int CTRL_BOTTOM = 0x8;
84
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080085 public static final float RESIZING_HINT_ALPHA = 0.5f;
86
87 public static final int RESIZING_HINT_DURATION_MS = 0;
88
skuhne@google.com322347b2016-12-02 12:54:03 -080089 // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait).
90 // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever
91 // aspect he desires.
92 @VisibleForTesting
93 static final float MIN_ASPECT = 1.2f;
94
Chong Zhang8e89b312015-09-09 15:09:30 -070095 private final WindowManagerService mService;
96 private WindowPositionerEventReceiver mInputEventReceiver;
97 private Display mDisplay;
98 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -070099 private Rect mTmpRect = new Rect();
100 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700101 private int mMinVisibleWidth;
102 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700103
Chong Zhang3005e752015-09-18 18:46:28 -0700104 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700105 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -0800106 private boolean mPreserveOrientation;
107 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -0700108 private final Rect mWindowOriginalBounds = new Rect();
109 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -0800110 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -0700111 private float mStartDragX;
112 private float mStartDragY;
113 @CtrlType
114 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700115 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700116
117 InputChannel mServerChannel;
118 InputChannel mClientChannel;
119 InputApplicationHandle mDragApplicationHandle;
120 InputWindowHandle mDragWindowHandle;
121
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700122 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
123 public WindowPositionerEventReceiver(
124 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
125 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700126 }
127
128 @Override
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700129 public void onInputEvent(InputEvent event, int displayId) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700130 if (!(event instanceof MotionEvent)
131 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
132 return;
133 }
134 final MotionEvent motionEvent = (MotionEvent) event;
135 boolean handled = false;
136
137 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700138 if (mDragEnded) {
139 // The drag has ended but the clean-up message has not been processed by
140 // window manager. Drop events that occur after this until window manager
141 // has a chance to clean-up the input handle.
142 handled = true;
143 return;
144 }
145
Chong Zhang8e89b312015-09-09 15:09:30 -0700146 final float newX = motionEvent.getRawX();
147 final float newY = motionEvent.getRawY();
148
149 switch (motionEvent.getAction()) {
150 case MotionEvent.ACTION_DOWN: {
151 if (DEBUG_TASK_POSITIONING) {
152 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
153 }
154 } break;
155
156 case MotionEvent.ACTION_MOVE: {
157 if (DEBUG_TASK_POSITIONING){
158 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
159 }
160 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700161 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800162 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700163 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700164 if (!mTmpRect.equals(mWindowDragBounds)) {
165 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
166 "wm.TaskPositioner.resizeTask");
167 try {
168 mService.mActivityManager.resizeTask(
169 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
170 } catch (RemoteException e) {
171 }
172 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
173 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700174 } break;
175
176 case MotionEvent.ACTION_UP: {
177 if (DEBUG_TASK_POSITIONING) {
178 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
179 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700180 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700181 } break;
182
183 case MotionEvent.ACTION_CANCEL: {
184 if (DEBUG_TASK_POSITIONING) {
185 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
186 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700187 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700188 } break;
189 }
190
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700191 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700192 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700193 synchronized (mService.mWindowMap) {
194 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700195 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700196 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700197 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700198 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700199 // We were using fullscreen surface during resizing. Request
200 // resizeTask() one last time to restore surface to window size.
201 mService.mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700202 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700203 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700204 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700205
Chong Zhang8e89b312015-09-09 15:09:30 -0700206 // Post back to WM to handle clean-ups. We still need the input
207 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900208 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700209 }
210 handled = true;
211 } catch (Exception e) {
212 Slog.e(TAG, "Exception caught by drag handleMotion", e);
213 } finally {
214 finishInputEvent(event, handled);
215 }
216 }
217 }
218
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800219 /** Use {@link #create(WindowManagerService)} instead **/
Chong Zhang8e89b312015-09-09 15:09:30 -0700220 TaskPositioner(WindowManagerService service) {
221 mService = service;
222 }
223
skuhne@google.com322347b2016-12-02 12:54:03 -0800224 @VisibleForTesting
225 Rect getWindowDragBounds() {
226 return mWindowDragBounds;
227 }
228
Chong Zhang8e89b312015-09-09 15:09:30 -0700229 /**
230 * @param display The Display that the window being dragged is on.
231 */
Robert Carrb1579c82017-09-05 14:54:47 -0700232 void register(DisplayContent displayContent) {
233 final Display display = displayContent.getDisplay();
234
Chong Zhang8e89b312015-09-09 15:09:30 -0700235 if (DEBUG_TASK_POSITIONING) {
236 Slog.d(TAG, "Registering task positioner");
237 }
238
239 if (mClientChannel != null) {
240 Slog.e(TAG, "Task positioner already registered");
241 return;
242 }
243
244 mDisplay = display;
245 mDisplay.getMetrics(mDisplayMetrics);
246 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
247 mServerChannel = channels[0];
248 mClientChannel = channels[1];
249 mService.mInputManager.registerInputChannel(mServerChannel, null);
250
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700251 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100252 mClientChannel, mService.mAnimationHandler.getLooper(),
253 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700254
255 mDragApplicationHandle = new InputApplicationHandle(null);
256 mDragApplicationHandle.name = TAG;
257 mDragApplicationHandle.dispatchingTimeoutNanos =
258 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
259
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800260 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null,
Chong Zhang8e89b312015-09-09 15:09:30 -0700261 mDisplay.getDisplayId());
262 mDragWindowHandle.name = TAG;
263 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700264 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700265 mDragWindowHandle.layoutParamsFlags = 0;
266 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
267 mDragWindowHandle.dispatchingTimeoutNanos =
268 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
269 mDragWindowHandle.visible = true;
270 mDragWindowHandle.canReceiveKeys = false;
271 mDragWindowHandle.hasFocus = true;
272 mDragWindowHandle.hasWallpaper = false;
273 mDragWindowHandle.paused = false;
274 mDragWindowHandle.ownerPid = Process.myPid();
275 mDragWindowHandle.ownerUid = Process.myUid();
276 mDragWindowHandle.inputFeatures = 0;
277 mDragWindowHandle.scaleFactor = 1.0f;
278
279 // The drag window cannot receive new touches.
280 mDragWindowHandle.touchableRegion.setEmpty();
281
282 // The drag window covers the entire display
283 mDragWindowHandle.frameLeft = 0;
284 mDragWindowHandle.frameTop = 0;
285 final Point p = new Point();
286 mDisplay.getRealSize(p);
287 mDragWindowHandle.frameRight = p.x;
288 mDragWindowHandle.frameBottom = p.y;
289
290 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800291 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700292 Slog.d(TAG, "Pausing rotation during re-position");
293 }
294 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700295
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700296 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
297 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
298 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
skuhne@google.com322347b2016-12-02 12:54:03 -0800299 mDisplay.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700300
301 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700302 }
303
304 void unregister() {
305 if (DEBUG_TASK_POSITIONING) {
306 Slog.d(TAG, "Unregistering task positioner");
307 }
308
309 if (mClientChannel == null) {
310 Slog.e(TAG, "Task positioner not registered");
311 return;
312 }
313
314 mService.mInputManager.unregisterInputChannel(mServerChannel);
315
316 mInputEventReceiver.dispose();
317 mInputEventReceiver = null;
318 mClientChannel.dispose();
319 mServerChannel.dispose();
320 mClientChannel = null;
321 mServerChannel = null;
322
323 mDragWindowHandle = null;
324 mDragApplicationHandle = null;
325 mDisplay = null;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700326 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700327
Chong Zhang8e89b312015-09-09 15:09:30 -0700328 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800329 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700330 Slog.d(TAG, "Resuming rotation after re-position");
331 }
332 mService.resumeRotationLocked();
333 }
334
skuhne@google.com322347b2016-12-02 12:54:03 -0800335 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
336 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700337 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800338 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
339 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
340 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700341 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800342 mTask = win.getTask();
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700343 // Use the dim bounds, not the original task bounds. The cursor
344 // movement should be calculated relative to the visible bounds.
345 // Also, use the dim bounds of the task which accounts for
346 // multiple app windows. Don't use any bounds from win itself as it
347 // may not be the same size as the task.
348 mTask.getDimBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800349 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
350 }
351
352 @VisibleForTesting
353 void startDrag(boolean resize, boolean preserveOrientation,
354 float startX, float startY, Rect startBounds) {
355 mCtrlType = CTRL_NONE;
356 mStartDragX = startX;
357 mStartDragY = startY;
358 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800359
Chong Zhang8e89b312015-09-09 15:09:30 -0700360 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800361 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700362 mCtrlType |= CTRL_LEFT;
363 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800364 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700365 mCtrlType |= CTRL_RIGHT;
366 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800367 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700368 mCtrlType |= CTRL_TOP;
369 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800370 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700371 mCtrlType |= CTRL_BOTTOM;
372 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800373 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700374 }
375
skuhne@google.com322347b2016-12-02 12:54:03 -0800376 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
377 // made up out of multiple windows which are only partially overlapping. When that happens,
378 // the orientation from the window of interest to the entire stack might diverge. However
379 // for now we treat them as the same.
380 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
381 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700382
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900383 // Notify the app that resizing has started, even though we haven't received any new
384 // bounds yet. This will guarantee that the app starts the backdrop renderer before
385 // configuration changes which could cause an activity restart.
386 if (mResizing) {
387 synchronized (mService.mWindowMap) {
388 notifyMoveLocked(startX, startY);
389 }
390
391 // Perform the resize on the WMS handler thread when we don't have the WMS lock held
392 // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver
393 // callbacks are delivered on the same handler so this initial resize is always
394 // guaranteed to happen before subsequent drag resizes.
395 mService.mH.post(() -> {
396 try {
397 mService.mActivityManager.resizeTask(
398 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
399 } catch (RemoteException e) {
400 }
401 });
402 }
403
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700404 // Make sure we always have valid drag bounds even if the drag ends before any move events
405 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800406 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700407 }
408
409 private void endDragLocked() {
410 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100411 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700412 }
413
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700414 /** Returns true if the move operation should be ended. */
415 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700416 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800417 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700418 }
419
420 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800421 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100422 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700423 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700424 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700425
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700426 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800427 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800428
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700429 int nX = (int) x;
430 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800431 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700432 // For a moving operation we allow the pointer to go out of the stack bounds, but
433 // use the clamped pointer position for the drag bounds computation.
434 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
435 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800436 }
437
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700438 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700439 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800440 }
441
skuhne@google.com322347b2016-12-02 12:54:03 -0800442 /**
443 * The user is drag - resizing the window.
444 *
445 * @param x The x coordinate of the current drag coordinate.
446 * @param y the y coordinate of the current drag coordinate.
447 */
448 @VisibleForTesting
449 void resizeDrag(float x, float y) {
450 // This is a resizing operation.
451 // We need to keep various constraints:
452 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
453 // 2. The orientation is kept - if required.
454 final int deltaX = Math.round(x - mStartDragX);
455 final int deltaY = Math.round(y - mStartDragY);
456 int left = mWindowOriginalBounds.left;
457 int top = mWindowOriginalBounds.top;
458 int right = mWindowOriginalBounds.right;
459 int bottom = mWindowOriginalBounds.bottom;
460
461 // The aspect which we have to respect. Note that if the orientation does not need to be
462 // preserved the aspect will be calculated as 1.0 which neutralizes the following
463 // computations.
464 final float minAspect = !mPreserveOrientation
465 ? 1.0f
466 : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT));
467 // Calculate the resulting width and height of the drag operation.
468 int width = right - left;
469 int height = bottom - top;
470 if ((mCtrlType & CTRL_LEFT) != 0) {
471 width = Math.max(mMinVisibleWidth, width - deltaX);
472 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
473 width = Math.max(mMinVisibleWidth, width + deltaX);
474 }
475 if ((mCtrlType & CTRL_TOP) != 0) {
476 height = Math.max(mMinVisibleHeight, height - deltaY);
477 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
478 height = Math.max(mMinVisibleHeight, height + deltaY);
479 }
480
481 // If we have to preserve the orientation - check that we are doing so.
482 final float aspect = (float) width / (float) height;
483 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
484 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
485 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
486 // drag axis. What ever is producing the bigger rectangle will be chosen.
487 int width1;
488 int width2;
489 int height1;
490 int height2;
491 if (mStartOrientationWasLandscape) {
492 // Assuming that the width is our target we calculate the height.
493 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
494 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
495 if (height1 < mMinVisibleHeight) {
496 // If the resulting height is too small we adjust to the minimal size.
497 height1 = mMinVisibleHeight;
498 width1 = Math.max(mMinVisibleWidth,
499 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
500 }
501 // Assuming that the height is our target we calculate the width.
502 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
503 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
504 if (width2 < mMinVisibleWidth) {
505 // If the resulting width is too small we adjust to the minimal size.
506 width2 = mMinVisibleWidth;
507 height2 = Math.max(mMinVisibleHeight,
508 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
509 }
510 } else {
511 // Assuming that the width is our target we calculate the height.
512 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
513 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
514 if (height1 < mMinVisibleHeight) {
515 // If the resulting height is too small we adjust to the minimal size.
516 height1 = mMinVisibleHeight;
517 width1 = Math.max(mMinVisibleWidth,
518 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
519 }
520 // Assuming that the height is our target we calculate the width.
521 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
522 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
523 if (width2 < mMinVisibleWidth) {
524 // If the resulting width is too small we adjust to the minimal size.
525 width2 = mMinVisibleWidth;
526 height2 = Math.max(mMinVisibleHeight,
527 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
528 }
529 }
530
531 // Use the bigger of the two rectangles if the major change was positive, otherwise
532 // do the opposite.
533 final boolean grows = width > (right - left) || height > (bottom - top);
534 if (grows == (width1 * height1 > width2 * height2)) {
535 width = width1;
536 height = height1;
537 } else {
538 width = width2;
539 height = height2;
540 }
541 }
542
543 // Update mWindowDragBounds to the new drag size.
544 updateDraggedBounds(left, top, right, bottom, width, height);
545 }
546
547 /**
548 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
549 *
550 * @param left The original left bound before the user started dragging.
551 * @param top The original top bound before the user started dragging.
552 * @param right The original right bound before the user started dragging.
553 * @param bottom The original bottom bound before the user started dragging.
554 * @param newWidth The new dragged width.
555 * @param newHeight The new dragged height.
556 */
557 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
558 int newHeight) {
559 // Generate the final bounds by keeping the opposite drag edge constant.
560 if ((mCtrlType & CTRL_LEFT) != 0) {
561 left = right - newWidth;
562 } else { // Note: The right might have changed - if we pulled at the right or not.
563 right = left + newWidth;
564 }
565 if ((mCtrlType & CTRL_TOP) != 0) {
566 top = bottom - newHeight;
567 } else { // Note: The height might have changed - if we pulled at the bottom or not.
568 bottom = top + newHeight;
569 }
570
571 mWindowDragBounds.set(left, top, right, bottom);
572
573 checkBoundsForOrientationViolations(mWindowDragBounds);
574 }
575
576 /**
577 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
578 *
579 * @param bounds The bounds to be checked.
580 */
581 private void checkBoundsForOrientationViolations(Rect bounds) {
582 // When using debug check that we are not violating the given constraints.
583 if (DEBUG_ORIENTATION_VIOLATIONS) {
584 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
585 Slog.e(TAG, "Orientation violation detected! should be "
586 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
587 + " but is the other");
588 } else {
589 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
590 }
591 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
592 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
593 + ", " + bounds.width() + ") Height(min,is)=("
594 + mMinVisibleHeight + ", " + bounds.height() + ")");
595 }
596 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
597 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
598 + ", " + bounds.width() + ") Height(min,is)=("
599 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
600 }
601 }
602 }
603
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700604 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
605 final int offsetX = Math.round(x - mStartDragX);
606 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700607 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700608 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
609 final int maxLeft = stackBounds.right - mMinVisibleWidth;
610 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700611
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700612 // Vertically, the top mMinVisibleHeight of the window should remain visible.
613 // (This assumes that the window caption bar is at the top of the window).
614 final int minTop = stackBounds.top;
615 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700616
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700617 mWindowDragBounds.offsetTo(
618 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
619 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
620
Chong Zhangb15758a2015-11-17 12:12:03 -0800621 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
622 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700623 }
624
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700625 public String toShortString() {
626 return TAG;
627 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800628
629 static void setFactory(Factory factory) {
630 sFactory = factory;
631 }
632
633 static TaskPositioner create(WindowManagerService service) {
634 if (sFactory == null) {
635 sFactory = new Factory() {};
636 }
637
638 return sFactory.create(service);
639 }
640
641 interface Factory {
642 default TaskPositioner create(WindowManagerService service) {
643 return new TaskPositioner(service);
644 }
645 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700646}