blob: f046e8adc4783072ac7870dae8404a79efce3f47 [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 Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.RESIZE_MODE_USER;
20import static android.app.ActivityTaskManager.RESIZE_MODE_USER_FORCED;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070021import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Garfield Tan07544cd2018-09-12 16:16:54 -070022
Ben Lin2e306af2020-02-03 14:23:57 -080023import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_BOTTOM;
24import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_LEFT;
25import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_NONE;
26import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_RIGHT;
27import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_TOP;
Garfield Tan07544cd2018-09-12 16:16:54 -070028import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Adrian Roosb125e0b2019-10-02 14:55:14 +020029import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080030import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080031import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
32import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070033import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070034import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
35import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070036
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +090037import android.annotation.NonNull;
Chong Zhang8e89b312015-09-09 15:09:30 -070038import android.graphics.Point;
39import android.graphics.Rect;
Robert Carr0bcbe642018-10-11 19:07:43 -070040import android.os.Binder;
yj81.kwon79c97672019-04-16 19:44:48 -070041import android.os.IBinder;
Chong Zhang8e89b312015-09-09 15:09:30 -070042import android.os.Looper;
43import android.os.Process;
44import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070045import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070046import android.util.DisplayMetrics;
47import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080048import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070049import android.view.Choreographer;
Evan Rosky0d654cb2019-02-26 10:59:10 -080050import android.view.InputApplicationHandle;
Chong Zhang8e89b312015-09-09 15:09:30 -070051import android.view.InputChannel;
52import android.view.InputDevice;
53import android.view.InputEvent;
Evan Rosky0d654cb2019-02-26 10:59:10 -080054import android.view.InputWindowHandle;
Chong Zhang8e89b312015-09-09 15:09:30 -070055import android.view.MotionEvent;
Arthur Hungb62e5002019-08-29 12:28:07 +080056import android.view.SurfaceControl;
Chong Zhang8e89b312015-09-09 15:09:30 -070057import android.view.WindowManager;
58
skuhne@google.com322347b2016-12-02 12:54:03 -080059import com.android.internal.annotations.VisibleForTesting;
Ben Lin2e306af2020-02-03 14:23:57 -080060import com.android.internal.policy.TaskResizingAlgorithm;
61import com.android.internal.policy.TaskResizingAlgorithm.CtrlType;
Adrian Roosb125e0b2019-10-02 14:55:14 +020062import com.android.server.protolog.common.ProtoLog;
Wale Ogunwale228d4042015-09-13 10:17:34 -070063
yj81.kwon79c97672019-04-16 19:44:48 -070064class TaskPositioner implements IBinder.DeathRecipient {
skuhne@google.com322347b2016-12-02 12:54:03 -080065 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010066 private static final String TAG_LOCAL = "TaskPositioner";
67 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070068
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080069 private static Factory sFactory;
70
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080071 public static final float RESIZING_HINT_ALPHA = 0.5f;
72
73 public static final int RESIZING_HINT_DURATION_MS = 0;
74
Chong Zhang8e89b312015-09-09 15:09:30 -070075 private final WindowManagerService mService;
76 private WindowPositionerEventReceiver mInputEventReceiver;
Riddle Hsu654a6f92018-07-13 22:59:36 +080077 private DisplayContent mDisplayContent;
Wale Ogunwale228d4042015-09-13 10:17:34 -070078 private Rect mTmpRect = new Rect();
Wale Ogunwaleb8051b82015-09-17 15:41:52 -070079 private int mMinVisibleWidth;
80 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -070081
Charles Chen2cb5a0f2019-03-22 20:58:00 +080082 @VisibleForTesting
83 Task mTask;
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +090084 WindowState mWindow;
Chong Zhang09b21ef2015-09-14 10:20:21 -070085 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -080086 private boolean mPreserveOrientation;
87 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -070088 private final Rect mWindowOriginalBounds = new Rect();
89 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -080090 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -070091 private float mStartDragX;
92 private float mStartDragY;
93 @CtrlType
94 private int mCtrlType = CTRL_NONE;
yj81.kwon79c97672019-04-16 19:44:48 -070095 @VisibleForTesting
96 boolean mDragEnded;
yj81.kwon19585ff2019-04-23 18:53:57 -070097 IBinder mClientCallback;
Chong Zhang8e89b312015-09-09 15:09:30 -070098
99 InputChannel mServerChannel;
100 InputChannel mClientChannel;
101 InputApplicationHandle mDragApplicationHandle;
102 InputWindowHandle mDragWindowHandle;
103
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700104 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
105 public WindowPositionerEventReceiver(
106 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
107 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700108 }
109
110 @Override
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -0800111 public void onInputEvent(InputEvent event) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700112 boolean handled = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700113 try {
Yunfan Chen54872c32019-11-14 19:41:47 -0800114 // All returns need to be in the try block to make sure the finishInputEvent is
115 // called correctly.
116 if (!(event instanceof MotionEvent)
117 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
118 return;
119 }
120 final MotionEvent motionEvent = (MotionEvent) event;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700121 if (mDragEnded) {
122 // The drag has ended but the clean-up message has not been processed by
123 // window manager. Drop events that occur after this until window manager
124 // has a chance to clean-up the input handle.
125 handled = true;
126 return;
127 }
128
Chong Zhang8e89b312015-09-09 15:09:30 -0700129 final float newX = motionEvent.getRawX();
130 final float newY = motionEvent.getRawY();
131
132 switch (motionEvent.getAction()) {
133 case MotionEvent.ACTION_DOWN: {
134 if (DEBUG_TASK_POSITIONING) {
135 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
136 }
137 } break;
138
139 case MotionEvent.ACTION_MOVE: {
140 if (DEBUG_TASK_POSITIONING){
141 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
142 }
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700143 synchronized (mService.mGlobalLock) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700144 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800145 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700146 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700147 if (!mTmpRect.equals(mWindowDragBounds)) {
148 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
149 "wm.TaskPositioner.resizeTask");
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800150 mService.mAtmService.resizeTask(
151 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700152 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
153 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700154 } break;
155
156 case MotionEvent.ACTION_UP: {
157 if (DEBUG_TASK_POSITIONING) {
158 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
159 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700160 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700161 } break;
162
163 case MotionEvent.ACTION_CANCEL: {
164 if (DEBUG_TASK_POSITIONING) {
165 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
166 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700167 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700168 } break;
169 }
170
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700171 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700172 final boolean wasResizing = mResizing;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700173 synchronized (mService.mGlobalLock) {
Chong Zhang3005e752015-09-18 18:46:28 -0700174 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700175 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700176 }
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800177 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
178 // We were using fullscreen surface during resizing. Request
179 // resizeTask() one last time to restore surface to window size.
180 mService.mAtmService.resizeTask(
181 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
182 }
Chong Zhang3005e752015-09-18 18:46:28 -0700183
Chong Zhang8e89b312015-09-09 15:09:30 -0700184 // Post back to WM to handle clean-ups. We still need the input
185 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900186 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700187 }
188 handled = true;
189 } catch (Exception e) {
190 Slog.e(TAG, "Exception caught by drag handleMotion", e);
191 } finally {
192 finishInputEvent(event, handled);
193 }
194 }
195 }
196
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800197 /** Use {@link #create(WindowManagerService)} instead. */
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900198 @VisibleForTesting
Chong Zhang8e89b312015-09-09 15:09:30 -0700199 TaskPositioner(WindowManagerService service) {
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800200 mService = service;
Chong Zhang8e89b312015-09-09 15:09:30 -0700201 }
202
skuhne@google.com322347b2016-12-02 12:54:03 -0800203 @VisibleForTesting
204 Rect getWindowDragBounds() {
205 return mWindowDragBounds;
206 }
207
Chong Zhang8e89b312015-09-09 15:09:30 -0700208 /**
Garfield Tan07544cd2018-09-12 16:16:54 -0700209 * @param displayContent The Display that the window being dragged is on.
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +0900210 * @param win The window which will be dragged.
Chong Zhang8e89b312015-09-09 15:09:30 -0700211 */
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +0900212 void register(DisplayContent displayContent, @NonNull WindowState win) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700213 if (DEBUG_TASK_POSITIONING) {
214 Slog.d(TAG, "Registering task positioner");
215 }
216
217 if (mClientChannel != null) {
218 Slog.e(TAG, "Task positioner already registered");
219 return;
220 }
221
Riddle Hsu654a6f92018-07-13 22:59:36 +0800222 mDisplayContent = displayContent;
Chong Zhang8e89b312015-09-09 15:09:30 -0700223 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
224 mServerChannel = channels[0];
225 mClientChannel = channels[1];
Vishnu Nair18782162019-10-08 14:57:16 -0700226 mService.mInputManager.registerInputChannel(mServerChannel);
Chong Zhang8e89b312015-09-09 15:09:30 -0700227
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700228 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100229 mClientChannel, mService.mAnimationHandler.getLooper(),
230 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700231
Robert Carr0bcbe642018-10-11 19:07:43 -0700232 mDragApplicationHandle = new InputApplicationHandle(new Binder());
Chong Zhang8e89b312015-09-09 15:09:30 -0700233 mDragApplicationHandle.name = TAG;
234 mDragApplicationHandle.dispatchingTimeoutNanos =
235 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
236
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800237 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle,
238 displayContent.getDisplayId());
Chong Zhang8e89b312015-09-09 15:09:30 -0700239 mDragWindowHandle.name = TAG;
Robert Carreadae822018-10-11 19:07:03 -0700240 mDragWindowHandle.token = mServerChannel.getToken();
Chong Zhang8e89b312015-09-09 15:09:30 -0700241 mDragWindowHandle.layoutParamsFlags = 0;
242 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
243 mDragWindowHandle.dispatchingTimeoutNanos =
244 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
245 mDragWindowHandle.visible = true;
246 mDragWindowHandle.canReceiveKeys = false;
247 mDragWindowHandle.hasFocus = true;
248 mDragWindowHandle.hasWallpaper = false;
249 mDragWindowHandle.paused = false;
250 mDragWindowHandle.ownerPid = Process.myPid();
251 mDragWindowHandle.ownerUid = Process.myUid();
252 mDragWindowHandle.inputFeatures = 0;
253 mDragWindowHandle.scaleFactor = 1.0f;
254
255 // The drag window cannot receive new touches.
256 mDragWindowHandle.touchableRegion.setEmpty();
257
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800258 // The drag window covers the entire display.
259 final Rect displayBounds = mTmpRect;
260 displayContent.getBounds(mTmpRect);
261 mDragWindowHandle.frameLeft = displayBounds.left;
262 mDragWindowHandle.frameTop = displayBounds.top;
263 mDragWindowHandle.frameRight = displayBounds.right;
264 mDragWindowHandle.frameBottom = displayBounds.bottom;
Chong Zhang8e89b312015-09-09 15:09:30 -0700265
266 // Pause rotations before a drag.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200267 ProtoLog.d(WM_DEBUG_ORIENTATION, "Pausing rotation during re-position");
Riddle Hsuccf09402019-08-13 00:33:06 +0800268 mDisplayContent.getDisplayRotation().pause();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700269
Garfield Tan07544cd2018-09-12 16:16:54 -0700270 // Notify InputMonitor to take mDragWindowHandle.
Arthur Hungb62e5002019-08-29 12:28:07 +0800271 mDisplayContent.getInputMonitor().updateInputWindowsImmediately();
272 new SurfaceControl.Transaction().syncInputWindows().apply(true);
Garfield Tan07544cd2018-09-12 16:16:54 -0700273
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800274 final DisplayMetrics displayMetrics = displayContent.getDisplayMetrics();
275 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, displayMetrics);
276 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, displayMetrics);
277 mMaxVisibleSize.set(displayBounds.width(), displayBounds.height());
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700278
279 mDragEnded = false;
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +0900280
281 try {
282 mClientCallback = win.mClient.asBinder();
283 mClientCallback.linkToDeath(this, 0 /* flags */);
284 } catch (RemoteException e) {
285 // The caller has died, so clean up TaskPositioningController.
286 mService.mTaskPositioningController.finishTaskPositioning();
287 return;
288 }
289 mWindow = win;
290 mTask = win.getTask();
Chong Zhang8e89b312015-09-09 15:09:30 -0700291 }
292
293 void unregister() {
294 if (DEBUG_TASK_POSITIONING) {
295 Slog.d(TAG, "Unregistering task positioner");
296 }
297
298 if (mClientChannel == null) {
299 Slog.e(TAG, "Task positioner not registered");
300 return;
301 }
302
303 mService.mInputManager.unregisterInputChannel(mServerChannel);
304
305 mInputEventReceiver.dispose();
306 mInputEventReceiver = null;
307 mClientChannel.dispose();
308 mServerChannel.dispose();
309 mClientChannel = null;
310 mServerChannel = null;
311
312 mDragWindowHandle = null;
313 mDragApplicationHandle = null;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700314 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700315
Garfield Tan07544cd2018-09-12 16:16:54 -0700316 // Notify InputMonitor to remove mDragWindowHandle.
317 mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/);
318
Chong Zhang8e89b312015-09-09 15:09:30 -0700319 // Resume rotations after a drag.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200320 ProtoLog.d(WM_DEBUG_ORIENTATION, "Resuming rotation after re-position");
Riddle Hsuccf09402019-08-13 00:33:06 +0800321 mDisplayContent.getDisplayRotation().resume();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800322 mDisplayContent = null;
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +0900323 if (mClientCallback != null) {
324 mClientCallback.unlinkToDeath(this, 0 /* flags */);
325 }
326 mWindow = null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700327 }
328
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800329 /**
330 * Starts moving or resizing the task. This method should be only called from
331 * {@link TaskPositioningController#startPositioningLocked} or unit tests.
332 */
333 void startDrag(boolean resize, boolean preserveOrientation, float startX, float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700334 if (DEBUG_TASK_POSITIONING) {
Yongjin Kwon9c5f0aa2019-11-06 17:36:24 +0900335 Slog.d(TAG, "startDrag: win=" + mWindow + ", resize=" + resize
skuhne@google.com322347b2016-12-02 12:54:03 -0800336 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
337 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700338 }
Evan Rosky0d654cb2019-02-26 10:59:10 -0800339 // Use the bounds of the task which accounts for
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700340 // multiple app windows. Don't use any bounds from win itself as it
341 // may not be the same size as the task.
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800342 final Rect startBounds = mTmpRect;
343 mTask.getBounds(startBounds);
skuhne@google.com322347b2016-12-02 12:54:03 -0800344
skuhne@google.com322347b2016-12-02 12:54:03 -0800345 mCtrlType = CTRL_NONE;
346 mStartDragX = startX;
347 mStartDragY = startY;
348 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800349
Chong Zhang8e89b312015-09-09 15:09:30 -0700350 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800351 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700352 mCtrlType |= CTRL_LEFT;
353 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800354 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700355 mCtrlType |= CTRL_RIGHT;
356 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800357 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700358 mCtrlType |= CTRL_TOP;
359 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800360 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700361 mCtrlType |= CTRL_BOTTOM;
362 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800363 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700364 }
365
skuhne@google.com322347b2016-12-02 12:54:03 -0800366 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
367 // made up out of multiple windows which are only partially overlapping. When that happens,
368 // the orientation from the window of interest to the entire stack might diverge. However
369 // for now we treat them as the same.
370 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
371 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700372
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900373 // Notify the app that resizing has started, even though we haven't received any new
374 // bounds yet. This will guarantee that the app starts the backdrop renderer before
375 // configuration changes which could cause an activity restart.
376 if (mResizing) {
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800377 notifyMoveLocked(startX, startY);
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900378
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800379 // The WindowPositionerEventReceiver callbacks are delivered on the same handler so this
380 // initial resize is always guaranteed to happen before subsequent drag resizes.
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900381 mService.mH.post(() -> {
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800382 mService.mAtmService.resizeTask(
383 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900384 });
385 }
386
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700387 // Make sure we always have valid drag bounds even if the drag ends before any move events
388 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800389 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700390 }
391
392 private void endDragLocked() {
393 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100394 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700395 }
396
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700397 /** Returns true if the move operation should be ended. */
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800398 @VisibleForTesting
399 boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700400 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800401 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700402 }
403
404 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800405 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100406 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700407 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700408 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700409
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700410 // This is a moving or scrolling operation.
Riddle Hsu3ae6ebb2020-03-06 15:15:42 +0800411 // Only allow to move in stable area so the target window won't be covered by system bar.
412 // Though {@link Task#resolveOverrideConfiguration} should also avoid the case.
413 mDisplayContent.getStableRect(mTmpRect);
414 // The task may be put in a limited display area.
415 mTmpRect.intersect(mTask.getRootTask().getParent().getBounds());
Chong Zhangb15758a2015-11-17 12:12:03 -0800416
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700417 int nX = (int) x;
418 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800419 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700420 // For a moving operation we allow the pointer to go out of the stack bounds, but
421 // use the clamped pointer position for the drag bounds computation.
422 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
423 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800424 }
425
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700426 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700427 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800428 }
429
skuhne@google.com322347b2016-12-02 12:54:03 -0800430 /**
431 * The user is drag - resizing the window.
432 *
433 * @param x The x coordinate of the current drag coordinate.
434 * @param y the y coordinate of the current drag coordinate.
435 */
436 @VisibleForTesting
437 void resizeDrag(float x, float y) {
Ben Lin2e306af2020-02-03 14:23:57 -0800438 updateDraggedBounds(TaskResizingAlgorithm.resizeDrag(x, y, mStartDragX, mStartDragY,
439 mWindowOriginalBounds, mCtrlType, mMinVisibleWidth, mMinVisibleHeight,
440 mMaxVisibleSize, mPreserveOrientation, mStartOrientationWasLandscape));
skuhne@google.com322347b2016-12-02 12:54:03 -0800441 }
442
Ben Lin2e306af2020-02-03 14:23:57 -0800443 private void updateDraggedBounds(Rect newBounds) {
444 mWindowDragBounds.set(newBounds);
skuhne@google.com322347b2016-12-02 12:54:03 -0800445
446 checkBoundsForOrientationViolations(mWindowDragBounds);
447 }
448
449 /**
450 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
451 *
452 * @param bounds The bounds to be checked.
453 */
454 private void checkBoundsForOrientationViolations(Rect bounds) {
455 // When using debug check that we are not violating the given constraints.
456 if (DEBUG_ORIENTATION_VIOLATIONS) {
457 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
458 Slog.e(TAG, "Orientation violation detected! should be "
459 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
460 + " but is the other");
461 } else {
462 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
463 }
464 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
465 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
466 + ", " + bounds.width() + ") Height(min,is)=("
467 + mMinVisibleHeight + ", " + bounds.height() + ")");
468 }
469 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
470 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
471 + ", " + bounds.width() + ") Height(min,is)=("
472 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
473 }
474 }
475 }
476
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700477 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
478 final int offsetX = Math.round(x - mStartDragX);
479 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700480 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700481 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
482 final int maxLeft = stackBounds.right - mMinVisibleWidth;
483 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700484
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700485 // Vertically, the top mMinVisibleHeight of the window should remain visible.
486 // (This assumes that the window caption bar is at the top of the window).
487 final int minTop = stackBounds.top;
488 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700489
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700490 mWindowDragBounds.offsetTo(
491 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
492 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
493
Chong Zhangb15758a2015-11-17 12:12:03 -0800494 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
495 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700496 }
497
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700498 public String toShortString() {
499 return TAG;
500 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800501
502 static void setFactory(Factory factory) {
503 sFactory = factory;
504 }
505
506 static TaskPositioner create(WindowManagerService service) {
507 if (sFactory == null) {
508 sFactory = new Factory() {};
509 }
510
511 return sFactory.create(service);
512 }
513
yj81.kwon79c97672019-04-16 19:44:48 -0700514 @Override
515 public void binderDied() {
516 mService.mTaskPositioningController.finishTaskPositioning();
517 }
518
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800519 interface Factory {
520 default TaskPositioner create(WindowManagerService service) {
521 return new TaskPositioner(service);
522 }
523 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700524}