blob: bd7e61cc34e5a62a8f6958681fabc8deb089b467 [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;
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;
Daichi Hirono67f2f4b2018-05-25 16:07:30 +090032import android.app.IActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070033import android.app.IActivityTaskManager;
Chong Zhang8e89b312015-09-09 15:09:30 -070034import android.graphics.Point;
35import android.graphics.Rect;
36import android.os.Looper;
37import android.os.Process;
38import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070039import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070040import android.util.DisplayMetrics;
41import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080042import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070043import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070044import android.view.Display;
45import android.view.InputChannel;
46import android.view.InputDevice;
47import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070048import android.view.MotionEvent;
49import android.view.WindowManager;
50
skuhne@google.com322347b2016-12-02 12:54:03 -080051import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale228d4042015-09-13 10:17:34 -070052import com.android.server.input.InputApplicationHandle;
53import com.android.server.input.InputWindowHandle;
54import com.android.server.wm.WindowManagerService.H;
55
56import java.lang.annotation.Retention;
57import java.lang.annotation.RetentionPolicy;
58
Robert Carrf59b8dd2017-10-02 18:58:36 -070059class TaskPositioner {
skuhne@google.com322347b2016-12-02 12:54:03 -080060 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010061 private static final String TAG_LOCAL = "TaskPositioner";
62 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070063
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080064 private static Factory sFactory;
65
Wale Ogunwale228d4042015-09-13 10:17:34 -070066 // The margin the pointer position has to be within the side of the screen to be
67 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070068 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070069
Chong Zhang8e89b312015-09-09 15:09:30 -070070 @IntDef(flag = true,
71 value = {
72 CTRL_NONE,
73 CTRL_LEFT,
74 CTRL_RIGHT,
75 CTRL_TOP,
76 CTRL_BOTTOM
77 })
78 @Retention(RetentionPolicy.SOURCE)
79 @interface CtrlType {}
80
81 private static final int CTRL_NONE = 0x0;
82 private static final int CTRL_LEFT = 0x1;
83 private static final int CTRL_RIGHT = 0x2;
84 private static final int CTRL_TOP = 0x4;
85 private static final int CTRL_BOTTOM = 0x8;
86
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080087 public static final float RESIZING_HINT_ALPHA = 0.5f;
88
89 public static final int RESIZING_HINT_DURATION_MS = 0;
90
skuhne@google.com322347b2016-12-02 12:54:03 -080091 // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait).
92 // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever
93 // aspect he desires.
94 @VisibleForTesting
95 static final float MIN_ASPECT = 1.2f;
96
Chong Zhang8e89b312015-09-09 15:09:30 -070097 private final WindowManagerService mService;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070098 private final IActivityTaskManager mActivityManager;
Chong Zhang8e89b312015-09-09 15:09:30 -070099 private WindowPositionerEventReceiver mInputEventReceiver;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800100 private DisplayContent mDisplayContent;
Chong Zhang8e89b312015-09-09 15:09:30 -0700101 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700102 private Rect mTmpRect = new Rect();
103 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700104 private int mMinVisibleWidth;
105 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700106
Chong Zhang3005e752015-09-18 18:46:28 -0700107 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700108 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -0800109 private boolean mPreserveOrientation;
110 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -0700111 private final Rect mWindowOriginalBounds = new Rect();
112 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -0800113 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -0700114 private float mStartDragX;
115 private float mStartDragY;
116 @CtrlType
117 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700118 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700119
120 InputChannel mServerChannel;
121 InputChannel mClientChannel;
122 InputApplicationHandle mDragApplicationHandle;
123 InputWindowHandle mDragWindowHandle;
124
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700125 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
126 public WindowPositionerEventReceiver(
127 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
128 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700129 }
130
131 @Override
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -0800132 public void onInputEvent(InputEvent event) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700133 if (!(event instanceof MotionEvent)
134 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
135 return;
136 }
137 final MotionEvent motionEvent = (MotionEvent) event;
138 boolean handled = false;
139
140 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700141 if (mDragEnded) {
142 // The drag has ended but the clean-up message has not been processed by
143 // window manager. Drop events that occur after this until window manager
144 // has a chance to clean-up the input handle.
145 handled = true;
146 return;
147 }
148
Chong Zhang8e89b312015-09-09 15:09:30 -0700149 final float newX = motionEvent.getRawX();
150 final float newY = motionEvent.getRawY();
151
152 switch (motionEvent.getAction()) {
153 case MotionEvent.ACTION_DOWN: {
154 if (DEBUG_TASK_POSITIONING) {
155 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
156 }
157 } break;
158
159 case MotionEvent.ACTION_MOVE: {
160 if (DEBUG_TASK_POSITIONING){
161 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
162 }
163 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700164 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800165 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700166 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700167 if (!mTmpRect.equals(mWindowDragBounds)) {
168 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
169 "wm.TaskPositioner.resizeTask");
170 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900171 mActivityManager.resizeTask(
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700172 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
173 } catch (RemoteException e) {
174 }
175 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
176 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700177 } break;
178
179 case MotionEvent.ACTION_UP: {
180 if (DEBUG_TASK_POSITIONING) {
181 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
182 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700183 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700184 } break;
185
186 case MotionEvent.ACTION_CANCEL: {
187 if (DEBUG_TASK_POSITIONING) {
188 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
189 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700190 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700191 } break;
192 }
193
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700194 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700195 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700196 synchronized (mService.mWindowMap) {
197 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700198 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700199 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700200 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700201 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700202 // We were using fullscreen surface during resizing. Request
203 // resizeTask() one last time to restore surface to window size.
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900204 mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700205 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700206 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700207 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700208
Chong Zhang8e89b312015-09-09 15:09:30 -0700209 // Post back to WM to handle clean-ups. We still need the input
210 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900211 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700212 }
213 handled = true;
214 } catch (Exception e) {
215 Slog.e(TAG, "Exception caught by drag handleMotion", e);
216 } finally {
217 finishInputEvent(event, handled);
218 }
219 }
220 }
221
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900222 @VisibleForTesting
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700223 TaskPositioner(WindowManagerService service, IActivityTaskManager activityManager) {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900224 mService = service;
225 mActivityManager = activityManager;
226 }
227
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800228 /** Use {@link #create(WindowManagerService)} instead **/
Chong Zhang8e89b312015-09-09 15:09:30 -0700229 TaskPositioner(WindowManagerService service) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700230 this(service, service.mActivityTaskManager);
Chong Zhang8e89b312015-09-09 15:09:30 -0700231 }
232
skuhne@google.com322347b2016-12-02 12:54:03 -0800233 @VisibleForTesting
234 Rect getWindowDragBounds() {
235 return mWindowDragBounds;
236 }
237
Chong Zhang8e89b312015-09-09 15:09:30 -0700238 /**
239 * @param display The Display that the window being dragged is on.
240 */
Robert Carrb1579c82017-09-05 14:54:47 -0700241 void register(DisplayContent displayContent) {
242 final Display display = displayContent.getDisplay();
243
Chong Zhang8e89b312015-09-09 15:09:30 -0700244 if (DEBUG_TASK_POSITIONING) {
245 Slog.d(TAG, "Registering task positioner");
246 }
247
248 if (mClientChannel != null) {
249 Slog.e(TAG, "Task positioner already registered");
250 return;
251 }
252
Riddle Hsu654a6f92018-07-13 22:59:36 +0800253 mDisplayContent = displayContent;
254 display.getMetrics(mDisplayMetrics);
Chong Zhang8e89b312015-09-09 15:09:30 -0700255 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
256 mServerChannel = channels[0];
257 mClientChannel = channels[1];
258 mService.mInputManager.registerInputChannel(mServerChannel, null);
259
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700260 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100261 mClientChannel, mService.mAnimationHandler.getLooper(),
262 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700263
264 mDragApplicationHandle = new InputApplicationHandle(null);
265 mDragApplicationHandle.name = TAG;
266 mDragApplicationHandle.dispatchingTimeoutNanos =
267 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
268
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800269 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null,
Riddle Hsu654a6f92018-07-13 22:59:36 +0800270 display.getDisplayId());
Chong Zhang8e89b312015-09-09 15:09:30 -0700271 mDragWindowHandle.name = TAG;
272 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700273 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700274 mDragWindowHandle.layoutParamsFlags = 0;
275 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
276 mDragWindowHandle.dispatchingTimeoutNanos =
277 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
278 mDragWindowHandle.visible = true;
279 mDragWindowHandle.canReceiveKeys = false;
280 mDragWindowHandle.hasFocus = true;
281 mDragWindowHandle.hasWallpaper = false;
282 mDragWindowHandle.paused = false;
283 mDragWindowHandle.ownerPid = Process.myPid();
284 mDragWindowHandle.ownerUid = Process.myUid();
285 mDragWindowHandle.inputFeatures = 0;
286 mDragWindowHandle.scaleFactor = 1.0f;
287
288 // The drag window cannot receive new touches.
289 mDragWindowHandle.touchableRegion.setEmpty();
290
291 // The drag window covers the entire display
292 mDragWindowHandle.frameLeft = 0;
293 mDragWindowHandle.frameTop = 0;
294 final Point p = new Point();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800295 display.getRealSize(p);
Chong Zhang8e89b312015-09-09 15:09:30 -0700296 mDragWindowHandle.frameRight = p.x;
297 mDragWindowHandle.frameBottom = p.y;
298
299 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800300 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700301 Slog.d(TAG, "Pausing rotation during re-position");
302 }
Riddle Hsu654a6f92018-07-13 22:59:36 +0800303 mDisplayContent.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700304
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700305 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
306 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
307 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Riddle Hsu654a6f92018-07-13 22:59:36 +0800308 display.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700309
310 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700311 }
312
313 void unregister() {
314 if (DEBUG_TASK_POSITIONING) {
315 Slog.d(TAG, "Unregistering task positioner");
316 }
317
318 if (mClientChannel == null) {
319 Slog.e(TAG, "Task positioner not registered");
320 return;
321 }
322
323 mService.mInputManager.unregisterInputChannel(mServerChannel);
324
325 mInputEventReceiver.dispose();
326 mInputEventReceiver = null;
327 mClientChannel.dispose();
328 mServerChannel.dispose();
329 mClientChannel = null;
330 mServerChannel = null;
331
332 mDragWindowHandle = null;
333 mDragApplicationHandle = null;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700334 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700335
Chong Zhang8e89b312015-09-09 15:09:30 -0700336 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800337 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700338 Slog.d(TAG, "Resuming rotation after re-position");
339 }
Riddle Hsu654a6f92018-07-13 22:59:36 +0800340 mDisplayContent.resumeRotationLocked();
341 mDisplayContent = null;
Chong Zhang8e89b312015-09-09 15:09:30 -0700342 }
343
skuhne@google.com322347b2016-12-02 12:54:03 -0800344 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
345 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700346 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800347 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
348 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
349 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700350 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800351 mTask = win.getTask();
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700352 // Use the dim bounds, not the original task bounds. The cursor
353 // movement should be calculated relative to the visible bounds.
354 // Also, use the dim bounds of the task which accounts for
355 // multiple app windows. Don't use any bounds from win itself as it
356 // may not be the same size as the task.
357 mTask.getDimBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800358 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
359 }
360
Daichi Hirono43094a12018-05-31 12:32:23 +0900361 protected void startDrag(boolean resize, boolean preserveOrientation,
skuhne@google.com322347b2016-12-02 12:54:03 -0800362 float startX, float startY, Rect startBounds) {
363 mCtrlType = CTRL_NONE;
364 mStartDragX = startX;
365 mStartDragY = startY;
366 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800367
Chong Zhang8e89b312015-09-09 15:09:30 -0700368 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800369 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700370 mCtrlType |= CTRL_LEFT;
371 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800372 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700373 mCtrlType |= CTRL_RIGHT;
374 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800375 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700376 mCtrlType |= CTRL_TOP;
377 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800378 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700379 mCtrlType |= CTRL_BOTTOM;
380 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800381 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700382 }
383
skuhne@google.com322347b2016-12-02 12:54:03 -0800384 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
385 // made up out of multiple windows which are only partially overlapping. When that happens,
386 // the orientation from the window of interest to the entire stack might diverge. However
387 // for now we treat them as the same.
388 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
389 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700390
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900391 // Notify the app that resizing has started, even though we haven't received any new
392 // bounds yet. This will guarantee that the app starts the backdrop renderer before
393 // configuration changes which could cause an activity restart.
394 if (mResizing) {
395 synchronized (mService.mWindowMap) {
396 notifyMoveLocked(startX, startY);
397 }
398
399 // Perform the resize on the WMS handler thread when we don't have the WMS lock held
400 // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver
401 // callbacks are delivered on the same handler so this initial resize is always
402 // guaranteed to happen before subsequent drag resizes.
403 mService.mH.post(() -> {
404 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900405 mActivityManager.resizeTask(
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900406 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
407 } catch (RemoteException e) {
408 }
409 });
410 }
411
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700412 // Make sure we always have valid drag bounds even if the drag ends before any move events
413 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800414 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700415 }
416
417 private void endDragLocked() {
418 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100419 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700420 }
421
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700422 /** Returns true if the move operation should be ended. */
423 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700424 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800425 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700426 }
427
428 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800429 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100430 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700431 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700432 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700433
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700434 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800435 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800436
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700437 int nX = (int) x;
438 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800439 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700440 // For a moving operation we allow the pointer to go out of the stack bounds, but
441 // use the clamped pointer position for the drag bounds computation.
442 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
443 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800444 }
445
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700446 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700447 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800448 }
449
skuhne@google.com322347b2016-12-02 12:54:03 -0800450 /**
451 * The user is drag - resizing the window.
452 *
453 * @param x The x coordinate of the current drag coordinate.
454 * @param y the y coordinate of the current drag coordinate.
455 */
456 @VisibleForTesting
457 void resizeDrag(float x, float y) {
458 // This is a resizing operation.
459 // We need to keep various constraints:
460 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
461 // 2. The orientation is kept - if required.
462 final int deltaX = Math.round(x - mStartDragX);
463 final int deltaY = Math.round(y - mStartDragY);
464 int left = mWindowOriginalBounds.left;
465 int top = mWindowOriginalBounds.top;
466 int right = mWindowOriginalBounds.right;
467 int bottom = mWindowOriginalBounds.bottom;
468
469 // The aspect which we have to respect. Note that if the orientation does not need to be
470 // preserved the aspect will be calculated as 1.0 which neutralizes the following
471 // computations.
472 final float minAspect = !mPreserveOrientation
473 ? 1.0f
474 : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT));
475 // Calculate the resulting width and height of the drag operation.
476 int width = right - left;
477 int height = bottom - top;
478 if ((mCtrlType & CTRL_LEFT) != 0) {
479 width = Math.max(mMinVisibleWidth, width - deltaX);
480 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
481 width = Math.max(mMinVisibleWidth, width + deltaX);
482 }
483 if ((mCtrlType & CTRL_TOP) != 0) {
484 height = Math.max(mMinVisibleHeight, height - deltaY);
485 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
486 height = Math.max(mMinVisibleHeight, height + deltaY);
487 }
488
489 // If we have to preserve the orientation - check that we are doing so.
490 final float aspect = (float) width / (float) height;
491 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
492 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
493 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
494 // drag axis. What ever is producing the bigger rectangle will be chosen.
495 int width1;
496 int width2;
497 int height1;
498 int height2;
499 if (mStartOrientationWasLandscape) {
500 // Assuming that the width is our target we calculate the height.
501 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
502 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
503 if (height1 < mMinVisibleHeight) {
504 // If the resulting height is too small we adjust to the minimal size.
505 height1 = mMinVisibleHeight;
506 width1 = Math.max(mMinVisibleWidth,
507 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
508 }
509 // Assuming that the height is our target we calculate the width.
510 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
511 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
512 if (width2 < mMinVisibleWidth) {
513 // If the resulting width is too small we adjust to the minimal size.
514 width2 = mMinVisibleWidth;
515 height2 = Math.max(mMinVisibleHeight,
516 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
517 }
518 } else {
519 // Assuming that the width is our target we calculate the height.
520 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
521 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
522 if (height1 < mMinVisibleHeight) {
523 // If the resulting height is too small we adjust to the minimal size.
524 height1 = mMinVisibleHeight;
525 width1 = Math.max(mMinVisibleWidth,
526 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
527 }
528 // Assuming that the height is our target we calculate the width.
529 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
530 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
531 if (width2 < mMinVisibleWidth) {
532 // If the resulting width is too small we adjust to the minimal size.
533 width2 = mMinVisibleWidth;
534 height2 = Math.max(mMinVisibleHeight,
535 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
536 }
537 }
538
539 // Use the bigger of the two rectangles if the major change was positive, otherwise
540 // do the opposite.
541 final boolean grows = width > (right - left) || height > (bottom - top);
542 if (grows == (width1 * height1 > width2 * height2)) {
543 width = width1;
544 height = height1;
545 } else {
546 width = width2;
547 height = height2;
548 }
549 }
550
551 // Update mWindowDragBounds to the new drag size.
552 updateDraggedBounds(left, top, right, bottom, width, height);
553 }
554
555 /**
556 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
557 *
558 * @param left The original left bound before the user started dragging.
559 * @param top The original top bound before the user started dragging.
560 * @param right The original right bound before the user started dragging.
561 * @param bottom The original bottom bound before the user started dragging.
562 * @param newWidth The new dragged width.
563 * @param newHeight The new dragged height.
564 */
565 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
566 int newHeight) {
567 // Generate the final bounds by keeping the opposite drag edge constant.
568 if ((mCtrlType & CTRL_LEFT) != 0) {
569 left = right - newWidth;
570 } else { // Note: The right might have changed - if we pulled at the right or not.
571 right = left + newWidth;
572 }
573 if ((mCtrlType & CTRL_TOP) != 0) {
574 top = bottom - newHeight;
575 } else { // Note: The height might have changed - if we pulled at the bottom or not.
576 bottom = top + newHeight;
577 }
578
579 mWindowDragBounds.set(left, top, right, bottom);
580
581 checkBoundsForOrientationViolations(mWindowDragBounds);
582 }
583
584 /**
585 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
586 *
587 * @param bounds The bounds to be checked.
588 */
589 private void checkBoundsForOrientationViolations(Rect bounds) {
590 // When using debug check that we are not violating the given constraints.
591 if (DEBUG_ORIENTATION_VIOLATIONS) {
592 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
593 Slog.e(TAG, "Orientation violation detected! should be "
594 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
595 + " but is the other");
596 } else {
597 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
598 }
599 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
600 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
601 + ", " + bounds.width() + ") Height(min,is)=("
602 + mMinVisibleHeight + ", " + bounds.height() + ")");
603 }
604 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
605 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
606 + ", " + bounds.width() + ") Height(min,is)=("
607 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
608 }
609 }
610 }
611
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700612 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
613 final int offsetX = Math.round(x - mStartDragX);
614 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700615 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700616 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
617 final int maxLeft = stackBounds.right - mMinVisibleWidth;
618 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700619
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700620 // Vertically, the top mMinVisibleHeight of the window should remain visible.
621 // (This assumes that the window caption bar is at the top of the window).
622 final int minTop = stackBounds.top;
623 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700624
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700625 mWindowDragBounds.offsetTo(
626 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
627 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
628
Chong Zhangb15758a2015-11-17 12:12:03 -0800629 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
630 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700631 }
632
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700633 public String toShortString() {
634 return TAG;
635 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800636
637 static void setFactory(Factory factory) {
638 sFactory = factory;
639 }
640
641 static TaskPositioner create(WindowManagerService service) {
642 if (sFactory == null) {
643 sFactory = new Factory() {};
644 }
645
646 return sFactory.create(service);
647 }
648
649 interface Factory {
650 default TaskPositioner create(WindowManagerService service) {
651 return new TaskPositioner(service);
652 }
653 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700654}