blob: 35e40929efd81128e6b6d5a863b376db831e48e0 [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;
Daichi Hirono67f2f4b2018-05-25 16:07:30 +090032import android.app.IActivityManager;
Chong Zhang8e89b312015-09-09 15:09:30 -070033import android.graphics.Point;
34import android.graphics.Rect;
35import android.os.Looper;
36import android.os.Process;
37import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070038import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070039import android.util.DisplayMetrics;
40import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080041import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070042import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070043import android.view.Display;
44import android.view.InputChannel;
45import android.view.InputDevice;
46import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070047import android.view.MotionEvent;
48import android.view.WindowManager;
49
skuhne@google.com322347b2016-12-02 12:54:03 -080050import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale228d4042015-09-13 10:17:34 -070051import com.android.server.input.InputApplicationHandle;
52import com.android.server.input.InputWindowHandle;
53import com.android.server.wm.WindowManagerService.H;
54
55import java.lang.annotation.Retention;
56import java.lang.annotation.RetentionPolicy;
57
Robert Carrf59b8dd2017-10-02 18:58:36 -070058class TaskPositioner {
skuhne@google.com322347b2016-12-02 12:54:03 -080059 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010060 private static final String TAG_LOCAL = "TaskPositioner";
61 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070062
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080063 private static Factory sFactory;
64
Wale Ogunwale228d4042015-09-13 10:17:34 -070065 // The margin the pointer position has to be within the side of the screen to be
66 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070067 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070068
Chong Zhang8e89b312015-09-09 15:09:30 -070069 @IntDef(flag = true,
70 value = {
71 CTRL_NONE,
72 CTRL_LEFT,
73 CTRL_RIGHT,
74 CTRL_TOP,
75 CTRL_BOTTOM
76 })
77 @Retention(RetentionPolicy.SOURCE)
78 @interface CtrlType {}
79
80 private static final int CTRL_NONE = 0x0;
81 private static final int CTRL_LEFT = 0x1;
82 private static final int CTRL_RIGHT = 0x2;
83 private static final int CTRL_TOP = 0x4;
84 private static final int CTRL_BOTTOM = 0x8;
85
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080086 public static final float RESIZING_HINT_ALPHA = 0.5f;
87
88 public static final int RESIZING_HINT_DURATION_MS = 0;
89
skuhne@google.com322347b2016-12-02 12:54:03 -080090 // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait).
91 // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever
92 // aspect he desires.
93 @VisibleForTesting
94 static final float MIN_ASPECT = 1.2f;
95
Chong Zhang8e89b312015-09-09 15:09:30 -070096 private final WindowManagerService mService;
Daichi Hirono67f2f4b2018-05-25 16:07:30 +090097 private final IActivityManager mActivityManager;
Chong Zhang8e89b312015-09-09 15:09:30 -070098 private WindowPositionerEventReceiver mInputEventReceiver;
99 private Display mDisplay;
100 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700101 private Rect mTmpRect = new Rect();
102 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700103 private int mMinVisibleWidth;
104 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700105
Chong Zhang3005e752015-09-18 18:46:28 -0700106 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700107 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -0800108 private boolean mPreserveOrientation;
109 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -0700110 private final Rect mWindowOriginalBounds = new Rect();
111 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -0800112 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -0700113 private float mStartDragX;
114 private float mStartDragY;
115 @CtrlType
116 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700117 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700118
119 InputChannel mServerChannel;
120 InputChannel mClientChannel;
121 InputApplicationHandle mDragApplicationHandle;
122 InputWindowHandle mDragWindowHandle;
123
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700124 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
125 public WindowPositionerEventReceiver(
126 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
127 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700128 }
129
130 @Override
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -0800131 public void onInputEvent(InputEvent event) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700132 if (!(event instanceof MotionEvent)
133 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
134 return;
135 }
136 final MotionEvent motionEvent = (MotionEvent) event;
137 boolean handled = false;
138
139 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700140 if (mDragEnded) {
141 // The drag has ended but the clean-up message has not been processed by
142 // window manager. Drop events that occur after this until window manager
143 // has a chance to clean-up the input handle.
144 handled = true;
145 return;
146 }
147
Chong Zhang8e89b312015-09-09 15:09:30 -0700148 final float newX = motionEvent.getRawX();
149 final float newY = motionEvent.getRawY();
150
151 switch (motionEvent.getAction()) {
152 case MotionEvent.ACTION_DOWN: {
153 if (DEBUG_TASK_POSITIONING) {
154 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
155 }
156 } break;
157
158 case MotionEvent.ACTION_MOVE: {
159 if (DEBUG_TASK_POSITIONING){
160 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
161 }
162 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700163 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800164 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700165 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700166 if (!mTmpRect.equals(mWindowDragBounds)) {
167 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
168 "wm.TaskPositioner.resizeTask");
169 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900170 mActivityManager.resizeTask(
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700171 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
172 } catch (RemoteException e) {
173 }
174 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
175 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700176 } break;
177
178 case MotionEvent.ACTION_UP: {
179 if (DEBUG_TASK_POSITIONING) {
180 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
181 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700182 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700183 } break;
184
185 case MotionEvent.ACTION_CANCEL: {
186 if (DEBUG_TASK_POSITIONING) {
187 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
188 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700189 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700190 } break;
191 }
192
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700193 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700194 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700195 synchronized (mService.mWindowMap) {
196 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700197 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700198 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700199 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700200 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700201 // We were using fullscreen surface during resizing. Request
202 // resizeTask() one last time to restore surface to window size.
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900203 mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700204 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700205 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700206 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700207
Chong Zhang8e89b312015-09-09 15:09:30 -0700208 // Post back to WM to handle clean-ups. We still need the input
209 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900210 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700211 }
212 handled = true;
213 } catch (Exception e) {
214 Slog.e(TAG, "Exception caught by drag handleMotion", e);
215 } finally {
216 finishInputEvent(event, handled);
217 }
218 }
219 }
220
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900221 @VisibleForTesting
222 TaskPositioner(WindowManagerService service, IActivityManager activityManager) {
223 mService = service;
224 mActivityManager = activityManager;
225 }
226
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800227 /** Use {@link #create(WindowManagerService)} instead **/
Chong Zhang8e89b312015-09-09 15:09:30 -0700228 TaskPositioner(WindowManagerService service) {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900229 this(service, service.mActivityManager);
Chong Zhang8e89b312015-09-09 15:09:30 -0700230 }
231
skuhne@google.com322347b2016-12-02 12:54:03 -0800232 @VisibleForTesting
233 Rect getWindowDragBounds() {
234 return mWindowDragBounds;
235 }
236
Chong Zhang8e89b312015-09-09 15:09:30 -0700237 /**
238 * @param display The Display that the window being dragged is on.
239 */
Robert Carrb1579c82017-09-05 14:54:47 -0700240 void register(DisplayContent displayContent) {
241 final Display display = displayContent.getDisplay();
242
Chong Zhang8e89b312015-09-09 15:09:30 -0700243 if (DEBUG_TASK_POSITIONING) {
244 Slog.d(TAG, "Registering task positioner");
245 }
246
247 if (mClientChannel != null) {
248 Slog.e(TAG, "Task positioner already registered");
249 return;
250 }
251
252 mDisplay = display;
253 mDisplay.getMetrics(mDisplayMetrics);
254 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
255 mServerChannel = channels[0];
256 mClientChannel = channels[1];
257 mService.mInputManager.registerInputChannel(mServerChannel, null);
258
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700259 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100260 mClientChannel, mService.mAnimationHandler.getLooper(),
261 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700262
263 mDragApplicationHandle = new InputApplicationHandle(null);
264 mDragApplicationHandle.name = TAG;
265 mDragApplicationHandle.dispatchingTimeoutNanos =
266 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
267
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800268 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null,
Chong Zhang8e89b312015-09-09 15:09:30 -0700269 mDisplay.getDisplayId());
270 mDragWindowHandle.name = TAG;
271 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700272 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700273 mDragWindowHandle.layoutParamsFlags = 0;
274 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
275 mDragWindowHandle.dispatchingTimeoutNanos =
276 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
277 mDragWindowHandle.visible = true;
278 mDragWindowHandle.canReceiveKeys = false;
279 mDragWindowHandle.hasFocus = true;
280 mDragWindowHandle.hasWallpaper = false;
281 mDragWindowHandle.paused = false;
282 mDragWindowHandle.ownerPid = Process.myPid();
283 mDragWindowHandle.ownerUid = Process.myUid();
284 mDragWindowHandle.inputFeatures = 0;
285 mDragWindowHandle.scaleFactor = 1.0f;
286
287 // The drag window cannot receive new touches.
288 mDragWindowHandle.touchableRegion.setEmpty();
289
290 // The drag window covers the entire display
291 mDragWindowHandle.frameLeft = 0;
292 mDragWindowHandle.frameTop = 0;
293 final Point p = new Point();
294 mDisplay.getRealSize(p);
295 mDragWindowHandle.frameRight = p.x;
296 mDragWindowHandle.frameBottom = p.y;
297
298 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800299 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700300 Slog.d(TAG, "Pausing rotation during re-position");
301 }
302 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700303
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700304 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
305 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
306 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
skuhne@google.com322347b2016-12-02 12:54:03 -0800307 mDisplay.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700308
309 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700310 }
311
312 void unregister() {
313 if (DEBUG_TASK_POSITIONING) {
314 Slog.d(TAG, "Unregistering task positioner");
315 }
316
317 if (mClientChannel == null) {
318 Slog.e(TAG, "Task positioner not registered");
319 return;
320 }
321
322 mService.mInputManager.unregisterInputChannel(mServerChannel);
323
324 mInputEventReceiver.dispose();
325 mInputEventReceiver = null;
326 mClientChannel.dispose();
327 mServerChannel.dispose();
328 mClientChannel = null;
329 mServerChannel = null;
330
331 mDragWindowHandle = null;
332 mDragApplicationHandle = null;
333 mDisplay = 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 }
340 mService.resumeRotationLocked();
341 }
342
skuhne@google.com322347b2016-12-02 12:54:03 -0800343 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
344 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700345 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800346 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
347 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
348 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700349 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800350 mTask = win.getTask();
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700351 // Use the dim bounds, not the original task bounds. The cursor
352 // movement should be calculated relative to the visible bounds.
353 // Also, use the dim bounds of the task which accounts for
354 // multiple app windows. Don't use any bounds from win itself as it
355 // may not be the same size as the task.
356 mTask.getDimBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800357 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
358 }
359
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900360 private void startDrag(boolean resize, boolean preserveOrientation,
skuhne@google.com322347b2016-12-02 12:54:03 -0800361 float startX, float startY, Rect startBounds) {
362 mCtrlType = CTRL_NONE;
363 mStartDragX = startX;
364 mStartDragY = startY;
365 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800366
Chong Zhang8e89b312015-09-09 15:09:30 -0700367 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800368 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700369 mCtrlType |= CTRL_LEFT;
370 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800371 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700372 mCtrlType |= CTRL_RIGHT;
373 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800374 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700375 mCtrlType |= CTRL_TOP;
376 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800377 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700378 mCtrlType |= CTRL_BOTTOM;
379 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800380 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700381 }
382
skuhne@google.com322347b2016-12-02 12:54:03 -0800383 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
384 // made up out of multiple windows which are only partially overlapping. When that happens,
385 // the orientation from the window of interest to the entire stack might diverge. However
386 // for now we treat them as the same.
387 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
388 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700389
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900390 // Notify the app that resizing has started, even though we haven't received any new
391 // bounds yet. This will guarantee that the app starts the backdrop renderer before
392 // configuration changes which could cause an activity restart.
393 if (mResizing) {
394 synchronized (mService.mWindowMap) {
395 notifyMoveLocked(startX, startY);
396 }
397
398 // Perform the resize on the WMS handler thread when we don't have the WMS lock held
399 // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver
400 // callbacks are delivered on the same handler so this initial resize is always
401 // guaranteed to happen before subsequent drag resizes.
402 mService.mH.post(() -> {
403 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900404 mActivityManager.resizeTask(
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900405 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
406 } catch (RemoteException e) {
407 }
408 });
409 }
410
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700411 // Make sure we always have valid drag bounds even if the drag ends before any move events
412 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800413 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700414 }
415
416 private void endDragLocked() {
417 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100418 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700419 }
420
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700421 /** Returns true if the move operation should be ended. */
422 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700423 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800424 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700425 }
426
427 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800428 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100429 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700430 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700431 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700432
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700433 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800434 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800435
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700436 int nX = (int) x;
437 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800438 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700439 // For a moving operation we allow the pointer to go out of the stack bounds, but
440 // use the clamped pointer position for the drag bounds computation.
441 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
442 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800443 }
444
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700445 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700446 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800447 }
448
skuhne@google.com322347b2016-12-02 12:54:03 -0800449 /**
450 * The user is drag - resizing the window.
451 *
452 * @param x The x coordinate of the current drag coordinate.
453 * @param y the y coordinate of the current drag coordinate.
454 */
455 @VisibleForTesting
456 void resizeDrag(float x, float y) {
457 // This is a resizing operation.
458 // We need to keep various constraints:
459 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
460 // 2. The orientation is kept - if required.
461 final int deltaX = Math.round(x - mStartDragX);
462 final int deltaY = Math.round(y - mStartDragY);
463 int left = mWindowOriginalBounds.left;
464 int top = mWindowOriginalBounds.top;
465 int right = mWindowOriginalBounds.right;
466 int bottom = mWindowOriginalBounds.bottom;
467
468 // The aspect which we have to respect. Note that if the orientation does not need to be
469 // preserved the aspect will be calculated as 1.0 which neutralizes the following
470 // computations.
471 final float minAspect = !mPreserveOrientation
472 ? 1.0f
473 : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT));
474 // Calculate the resulting width and height of the drag operation.
475 int width = right - left;
476 int height = bottom - top;
477 if ((mCtrlType & CTRL_LEFT) != 0) {
478 width = Math.max(mMinVisibleWidth, width - deltaX);
479 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
480 width = Math.max(mMinVisibleWidth, width + deltaX);
481 }
482 if ((mCtrlType & CTRL_TOP) != 0) {
483 height = Math.max(mMinVisibleHeight, height - deltaY);
484 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
485 height = Math.max(mMinVisibleHeight, height + deltaY);
486 }
487
488 // If we have to preserve the orientation - check that we are doing so.
489 final float aspect = (float) width / (float) height;
490 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
491 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
492 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
493 // drag axis. What ever is producing the bigger rectangle will be chosen.
494 int width1;
495 int width2;
496 int height1;
497 int height2;
498 if (mStartOrientationWasLandscape) {
499 // Assuming that the width is our target we calculate the height.
500 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
501 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
502 if (height1 < mMinVisibleHeight) {
503 // If the resulting height is too small we adjust to the minimal size.
504 height1 = mMinVisibleHeight;
505 width1 = Math.max(mMinVisibleWidth,
506 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
507 }
508 // Assuming that the height is our target we calculate the width.
509 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
510 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
511 if (width2 < mMinVisibleWidth) {
512 // If the resulting width is too small we adjust to the minimal size.
513 width2 = mMinVisibleWidth;
514 height2 = Math.max(mMinVisibleHeight,
515 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
516 }
517 } else {
518 // Assuming that the width is our target we calculate the height.
519 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
520 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
521 if (height1 < mMinVisibleHeight) {
522 // If the resulting height is too small we adjust to the minimal size.
523 height1 = mMinVisibleHeight;
524 width1 = Math.max(mMinVisibleWidth,
525 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
526 }
527 // Assuming that the height is our target we calculate the width.
528 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
529 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
530 if (width2 < mMinVisibleWidth) {
531 // If the resulting width is too small we adjust to the minimal size.
532 width2 = mMinVisibleWidth;
533 height2 = Math.max(mMinVisibleHeight,
534 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
535 }
536 }
537
538 // Use the bigger of the two rectangles if the major change was positive, otherwise
539 // do the opposite.
540 final boolean grows = width > (right - left) || height > (bottom - top);
541 if (grows == (width1 * height1 > width2 * height2)) {
542 width = width1;
543 height = height1;
544 } else {
545 width = width2;
546 height = height2;
547 }
548 }
549
550 // Update mWindowDragBounds to the new drag size.
551 updateDraggedBounds(left, top, right, bottom, width, height);
552 }
553
554 /**
555 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
556 *
557 * @param left The original left bound before the user started dragging.
558 * @param top The original top bound before the user started dragging.
559 * @param right The original right bound before the user started dragging.
560 * @param bottom The original bottom bound before the user started dragging.
561 * @param newWidth The new dragged width.
562 * @param newHeight The new dragged height.
563 */
564 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
565 int newHeight) {
566 // Generate the final bounds by keeping the opposite drag edge constant.
567 if ((mCtrlType & CTRL_LEFT) != 0) {
568 left = right - newWidth;
569 } else { // Note: The right might have changed - if we pulled at the right or not.
570 right = left + newWidth;
571 }
572 if ((mCtrlType & CTRL_TOP) != 0) {
573 top = bottom - newHeight;
574 } else { // Note: The height might have changed - if we pulled at the bottom or not.
575 bottom = top + newHeight;
576 }
577
578 mWindowDragBounds.set(left, top, right, bottom);
579
580 checkBoundsForOrientationViolations(mWindowDragBounds);
581 }
582
583 /**
584 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
585 *
586 * @param bounds The bounds to be checked.
587 */
588 private void checkBoundsForOrientationViolations(Rect bounds) {
589 // When using debug check that we are not violating the given constraints.
590 if (DEBUG_ORIENTATION_VIOLATIONS) {
591 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
592 Slog.e(TAG, "Orientation violation detected! should be "
593 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
594 + " but is the other");
595 } else {
596 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
597 }
598 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
599 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
600 + ", " + bounds.width() + ") Height(min,is)=("
601 + mMinVisibleHeight + ", " + bounds.height() + ")");
602 }
603 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
604 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
605 + ", " + bounds.width() + ") Height(min,is)=("
606 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
607 }
608 }
609 }
610
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700611 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
612 final int offsetX = Math.round(x - mStartDragX);
613 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700614 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700615 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
616 final int maxLeft = stackBounds.right - mMinVisibleWidth;
617 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700618
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700619 // Vertically, the top mMinVisibleHeight of the window should remain visible.
620 // (This assumes that the window caption bar is at the top of the window).
621 final int minTop = stackBounds.top;
622 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700623
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700624 mWindowDragBounds.offsetTo(
625 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
626 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
627
Chong Zhangb15758a2015-11-17 12:12:03 -0800628 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
629 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700630 }
631
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700632 public String toShortString() {
633 return TAG;
634 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800635
636 static void setFactory(Factory factory) {
637 sFactory = factory;
638 }
639
640 static TaskPositioner create(WindowManagerService service) {
641 if (sFactory == null) {
642 sFactory = new Factory() {};
643 }
644
645 return sFactory.create(service);
646 }
647
648 interface Factory {
649 default TaskPositioner create(WindowManagerService service) {
650 return new TaskPositioner(service);
651 }
652 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700653}