blob: 8b0b6ce8ce32a793d04b5ec5e2c4152289de682f [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
23import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
25import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080026import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070028import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070029import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
30import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070031
32import android.annotation.IntDef;
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;
Robert Carr0bcbe642018-10-11 19:07:43 -070036import android.os.Binder;
yj81.kwon79c97672019-04-16 19:44:48 -070037import android.os.IBinder;
Chong Zhang8e89b312015-09-09 15:09:30 -070038import android.os.Looper;
39import android.os.Process;
40import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070041import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070042import android.util.DisplayMetrics;
43import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080044import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070045import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070046import android.view.Display;
Evan Rosky0d654cb2019-02-26 10:59:10 -080047import android.view.InputApplicationHandle;
Chong Zhang8e89b312015-09-09 15:09:30 -070048import android.view.InputChannel;
49import android.view.InputDevice;
50import android.view.InputEvent;
Evan Rosky0d654cb2019-02-26 10:59:10 -080051import android.view.InputWindowHandle;
Chong Zhang8e89b312015-09-09 15:09:30 -070052import android.view.MotionEvent;
53import android.view.WindowManager;
54
skuhne@google.com322347b2016-12-02 12:54:03 -080055import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwale228d4042015-09-13 10:17:34 -070056
57import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
59
yj81.kwon79c97672019-04-16 19:44:48 -070060class TaskPositioner implements IBinder.DeathRecipient {
skuhne@google.com322347b2016-12-02 12:54:03 -080061 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010062 private static final String TAG_LOCAL = "TaskPositioner";
63 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070064
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080065 private static Factory sFactory;
66
Wale Ogunwale228d4042015-09-13 10:17:34 -070067 // The margin the pointer position has to be within the side of the screen to be
68 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070069 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070070
Chong Zhang8e89b312015-09-09 15:09:30 -070071 @IntDef(flag = true,
72 value = {
73 CTRL_NONE,
74 CTRL_LEFT,
75 CTRL_RIGHT,
76 CTRL_TOP,
77 CTRL_BOTTOM
78 })
79 @Retention(RetentionPolicy.SOURCE)
80 @interface CtrlType {}
81
82 private static final int CTRL_NONE = 0x0;
83 private static final int CTRL_LEFT = 0x1;
84 private static final int CTRL_RIGHT = 0x2;
85 private static final int CTRL_TOP = 0x4;
86 private static final int CTRL_BOTTOM = 0x8;
87
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080088 public static final float RESIZING_HINT_ALPHA = 0.5f;
89
90 public static final int RESIZING_HINT_DURATION_MS = 0;
91
skuhne@google.com322347b2016-12-02 12:54:03 -080092 // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait).
93 // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever
94 // aspect he desires.
95 @VisibleForTesting
96 static final float MIN_ASPECT = 1.2f;
97
Chong Zhang8e89b312015-09-09 15:09:30 -070098 private final WindowManagerService mService;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070099 private final IActivityTaskManager mActivityManager;
Chong Zhang8e89b312015-09-09 15:09:30 -0700100 private WindowPositionerEventReceiver mInputEventReceiver;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800101 private DisplayContent mDisplayContent;
Chong Zhang8e89b312015-09-09 15:09:30 -0700102 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700103 private Rect mTmpRect = new Rect();
104 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700105 private int mMinVisibleWidth;
106 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700107
Charles Chen2cb5a0f2019-03-22 20:58:00 +0800108 @VisibleForTesting
109 Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700110 private boolean mResizing;
skuhne@google.com322347b2016-12-02 12:54:03 -0800111 private boolean mPreserveOrientation;
112 private boolean mStartOrientationWasLandscape;
Chong Zhang8e89b312015-09-09 15:09:30 -0700113 private final Rect mWindowOriginalBounds = new Rect();
114 private final Rect mWindowDragBounds = new Rect();
skuhne@google.com322347b2016-12-02 12:54:03 -0800115 private final Point mMaxVisibleSize = new Point();
Chong Zhang8e89b312015-09-09 15:09:30 -0700116 private float mStartDragX;
117 private float mStartDragY;
118 @CtrlType
119 private int mCtrlType = CTRL_NONE;
yj81.kwon79c97672019-04-16 19:44:48 -0700120 @VisibleForTesting
121 boolean mDragEnded;
yj81.kwon19585ff2019-04-23 18:53:57 -0700122 IBinder mClientCallback;
Chong Zhang8e89b312015-09-09 15:09:30 -0700123
124 InputChannel mServerChannel;
125 InputChannel mClientChannel;
126 InputApplicationHandle mDragApplicationHandle;
127 InputWindowHandle mDragWindowHandle;
128
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700129 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
130 public WindowPositionerEventReceiver(
131 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
132 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700133 }
134
135 @Override
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -0800136 public void onInputEvent(InputEvent event) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700137 if (!(event instanceof MotionEvent)
138 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
139 return;
140 }
141 final MotionEvent motionEvent = (MotionEvent) event;
142 boolean handled = false;
143
144 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700145 if (mDragEnded) {
146 // The drag has ended but the clean-up message has not been processed by
147 // window manager. Drop events that occur after this until window manager
148 // has a chance to clean-up the input handle.
149 handled = true;
150 return;
151 }
152
Chong Zhang8e89b312015-09-09 15:09:30 -0700153 final float newX = motionEvent.getRawX();
154 final float newY = motionEvent.getRawY();
155
156 switch (motionEvent.getAction()) {
157 case MotionEvent.ACTION_DOWN: {
158 if (DEBUG_TASK_POSITIONING) {
159 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
160 }
161 } break;
162
163 case MotionEvent.ACTION_MOVE: {
164 if (DEBUG_TASK_POSITIONING){
165 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
166 }
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700167 synchronized (mService.mGlobalLock) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700168 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800169 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700170 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700171 if (!mTmpRect.equals(mWindowDragBounds)) {
172 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
173 "wm.TaskPositioner.resizeTask");
174 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900175 mActivityManager.resizeTask(
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700176 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
177 } catch (RemoteException e) {
178 }
179 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
180 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700181 } break;
182
183 case MotionEvent.ACTION_UP: {
184 if (DEBUG_TASK_POSITIONING) {
185 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
186 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700187 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700188 } break;
189
190 case MotionEvent.ACTION_CANCEL: {
191 if (DEBUG_TASK_POSITIONING) {
192 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
193 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700194 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700195 } break;
196 }
197
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700198 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700199 final boolean wasResizing = mResizing;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700200 synchronized (mService.mGlobalLock) {
Chong Zhang3005e752015-09-18 18:46:28 -0700201 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700202 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700203 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700204 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700205 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700206 // We were using fullscreen surface during resizing. Request
207 // resizeTask() one last time to restore surface to window size.
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900208 mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700209 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700210 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700211 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700212
Chong Zhang8e89b312015-09-09 15:09:30 -0700213 // Post back to WM to handle clean-ups. We still need the input
214 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900215 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700216 }
217 handled = true;
218 } catch (Exception e) {
219 Slog.e(TAG, "Exception caught by drag handleMotion", e);
220 } finally {
221 finishInputEvent(event, handled);
222 }
223 }
224 }
225
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900226 @VisibleForTesting
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700227 TaskPositioner(WindowManagerService service, IActivityTaskManager activityManager) {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900228 mService = service;
229 mActivityManager = activityManager;
230 }
231
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800232 /** Use {@link #create(WindowManagerService)} instead **/
Chong Zhang8e89b312015-09-09 15:09:30 -0700233 TaskPositioner(WindowManagerService service) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700234 this(service, service.mActivityTaskManager);
Chong Zhang8e89b312015-09-09 15:09:30 -0700235 }
236
skuhne@google.com322347b2016-12-02 12:54:03 -0800237 @VisibleForTesting
238 Rect getWindowDragBounds() {
239 return mWindowDragBounds;
240 }
241
Chong Zhang8e89b312015-09-09 15:09:30 -0700242 /**
Garfield Tan07544cd2018-09-12 16:16:54 -0700243 * @param displayContent The Display that the window being dragged is on.
Chong Zhang8e89b312015-09-09 15:09:30 -0700244 */
Robert Carrb1579c82017-09-05 14:54:47 -0700245 void register(DisplayContent displayContent) {
246 final Display display = displayContent.getDisplay();
247
Chong Zhang8e89b312015-09-09 15:09:30 -0700248 if (DEBUG_TASK_POSITIONING) {
249 Slog.d(TAG, "Registering task positioner");
250 }
251
252 if (mClientChannel != null) {
253 Slog.e(TAG, "Task positioner already registered");
254 return;
255 }
256
Riddle Hsu654a6f92018-07-13 22:59:36 +0800257 mDisplayContent = displayContent;
258 display.getMetrics(mDisplayMetrics);
Chong Zhang8e89b312015-09-09 15:09:30 -0700259 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
260 mServerChannel = channels[0];
261 mClientChannel = channels[1];
262 mService.mInputManager.registerInputChannel(mServerChannel, null);
263
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700264 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100265 mClientChannel, mService.mAnimationHandler.getLooper(),
266 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700267
Robert Carr0bcbe642018-10-11 19:07:43 -0700268 mDragApplicationHandle = new InputApplicationHandle(new Binder());
Chong Zhang8e89b312015-09-09 15:09:30 -0700269 mDragApplicationHandle.name = TAG;
270 mDragApplicationHandle.dispatchingTimeoutNanos =
271 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
272
Robert Carre0a353c2018-08-02 16:38:04 -0700273 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
Riddle Hsu654a6f92018-07-13 22:59:36 +0800274 display.getDisplayId());
Chong Zhang8e89b312015-09-09 15:09:30 -0700275 mDragWindowHandle.name = TAG;
Robert Carreadae822018-10-11 19:07:03 -0700276 mDragWindowHandle.token = mServerChannel.getToken();
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700277 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700278 mDragWindowHandle.layoutParamsFlags = 0;
279 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
280 mDragWindowHandle.dispatchingTimeoutNanos =
281 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
282 mDragWindowHandle.visible = true;
283 mDragWindowHandle.canReceiveKeys = false;
284 mDragWindowHandle.hasFocus = true;
285 mDragWindowHandle.hasWallpaper = false;
286 mDragWindowHandle.paused = false;
287 mDragWindowHandle.ownerPid = Process.myPid();
288 mDragWindowHandle.ownerUid = Process.myUid();
289 mDragWindowHandle.inputFeatures = 0;
290 mDragWindowHandle.scaleFactor = 1.0f;
291
292 // The drag window cannot receive new touches.
293 mDragWindowHandle.touchableRegion.setEmpty();
294
295 // The drag window covers the entire display
296 mDragWindowHandle.frameLeft = 0;
297 mDragWindowHandle.frameTop = 0;
298 final Point p = new Point();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800299 display.getRealSize(p);
Chong Zhang8e89b312015-09-09 15:09:30 -0700300 mDragWindowHandle.frameRight = p.x;
301 mDragWindowHandle.frameBottom = p.y;
302
303 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800304 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700305 Slog.d(TAG, "Pausing rotation during re-position");
306 }
Riddle Hsuccf09402019-08-13 00:33:06 +0800307 mDisplayContent.getDisplayRotation().pause();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700308
Garfield Tan07544cd2018-09-12 16:16:54 -0700309 // Notify InputMonitor to take mDragWindowHandle.
310 mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/);
311
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700312 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
313 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
314 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Riddle Hsu654a6f92018-07-13 22:59:36 +0800315 display.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700316
317 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700318 }
319
320 void unregister() {
321 if (DEBUG_TASK_POSITIONING) {
322 Slog.d(TAG, "Unregistering task positioner");
323 }
324
325 if (mClientChannel == null) {
326 Slog.e(TAG, "Task positioner not registered");
327 return;
328 }
329
330 mService.mInputManager.unregisterInputChannel(mServerChannel);
331
332 mInputEventReceiver.dispose();
333 mInputEventReceiver = null;
334 mClientChannel.dispose();
335 mServerChannel.dispose();
336 mClientChannel = null;
337 mServerChannel = null;
338
339 mDragWindowHandle = null;
340 mDragApplicationHandle = null;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700341 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700342
Garfield Tan07544cd2018-09-12 16:16:54 -0700343 // Notify InputMonitor to remove mDragWindowHandle.
344 mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/);
345
Chong Zhang8e89b312015-09-09 15:09:30 -0700346 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800347 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700348 Slog.d(TAG, "Resuming rotation after re-position");
349 }
Riddle Hsuccf09402019-08-13 00:33:06 +0800350 mDisplayContent.getDisplayRotation().resume();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800351 mDisplayContent = null;
yj81.kwon79c97672019-04-16 19:44:48 -0700352 mClientCallback.unlinkToDeath(this, 0 /* flags */);
Chong Zhang8e89b312015-09-09 15:09:30 -0700353 }
354
skuhne@google.com322347b2016-12-02 12:54:03 -0800355 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
356 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700357 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800358 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
359 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
360 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700361 }
yj81.kwon79c97672019-04-16 19:44:48 -0700362 try {
363 mClientCallback = win.mClient.asBinder();
364 mClientCallback.linkToDeath(this, 0 /* flags */);
365 } catch (RemoteException e) {
366 // The caller has died, so clean up TaskPositioningController.
367 mService.mTaskPositioningController.finishTaskPositioning();
368 return;
369 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800370 mTask = win.getTask();
Evan Rosky0d654cb2019-02-26 10:59:10 -0800371 // Use the bounds of the task which accounts for
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700372 // multiple app windows. Don't use any bounds from win itself as it
373 // may not be the same size as the task.
Evan Rosky0d654cb2019-02-26 10:59:10 -0800374 mTask.getBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800375 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
376 }
377
Daichi Hirono43094a12018-05-31 12:32:23 +0900378 protected void startDrag(boolean resize, boolean preserveOrientation,
skuhne@google.com322347b2016-12-02 12:54:03 -0800379 float startX, float startY, Rect startBounds) {
380 mCtrlType = CTRL_NONE;
381 mStartDragX = startX;
382 mStartDragY = startY;
383 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800384
Chong Zhang8e89b312015-09-09 15:09:30 -0700385 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800386 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700387 mCtrlType |= CTRL_LEFT;
388 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800389 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700390 mCtrlType |= CTRL_RIGHT;
391 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800392 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700393 mCtrlType |= CTRL_TOP;
394 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800395 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700396 mCtrlType |= CTRL_BOTTOM;
397 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800398 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700399 }
400
skuhne@google.com322347b2016-12-02 12:54:03 -0800401 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
402 // made up out of multiple windows which are only partially overlapping. When that happens,
403 // the orientation from the window of interest to the entire stack might diverge. However
404 // for now we treat them as the same.
405 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
406 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700407
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900408 // Notify the app that resizing has started, even though we haven't received any new
409 // bounds yet. This will guarantee that the app starts the backdrop renderer before
410 // configuration changes which could cause an activity restart.
411 if (mResizing) {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700412 synchronized (mService.mGlobalLock) {
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900413 notifyMoveLocked(startX, startY);
414 }
415
416 // Perform the resize on the WMS handler thread when we don't have the WMS lock held
417 // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver
418 // callbacks are delivered on the same handler so this initial resize is always
419 // guaranteed to happen before subsequent drag resizes.
420 mService.mH.post(() -> {
421 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900422 mActivityManager.resizeTask(
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900423 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
424 } catch (RemoteException e) {
425 }
426 });
427 }
428
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700429 // Make sure we always have valid drag bounds even if the drag ends before any move events
430 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800431 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700432 }
433
434 private void endDragLocked() {
435 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100436 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700437 }
438
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700439 /** Returns true if the move operation should be ended. */
440 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700441 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800442 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700443 }
444
445 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800446 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100447 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700448 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700449 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700450
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700451 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800452 mTask.mStack.getDimBounds(mTmpRect);
GyeHun Jeon84b30d22019-04-24 14:20:15 -0700453 // If a target window is covered by system bar, there is no way to move it again by touch.
454 // So we exclude them from stack bounds. and then it will be shown inside stable area.
455 Rect stableBounds = new Rect();
456 mDisplayContent.getStableRect(stableBounds);
457 mTmpRect.intersect(stableBounds);
Chong Zhangb15758a2015-11-17 12:12:03 -0800458
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700459 int nX = (int) x;
460 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800461 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700462 // For a moving operation we allow the pointer to go out of the stack bounds, but
463 // use the clamped pointer position for the drag bounds computation.
464 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
465 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800466 }
467
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700468 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700469 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800470 }
471
skuhne@google.com322347b2016-12-02 12:54:03 -0800472 /**
473 * The user is drag - resizing the window.
474 *
475 * @param x The x coordinate of the current drag coordinate.
476 * @param y the y coordinate of the current drag coordinate.
477 */
478 @VisibleForTesting
479 void resizeDrag(float x, float y) {
480 // This is a resizing operation.
481 // We need to keep various constraints:
482 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
483 // 2. The orientation is kept - if required.
484 final int deltaX = Math.round(x - mStartDragX);
485 final int deltaY = Math.round(y - mStartDragY);
486 int left = mWindowOriginalBounds.left;
487 int top = mWindowOriginalBounds.top;
488 int right = mWindowOriginalBounds.right;
489 int bottom = mWindowOriginalBounds.bottom;
490
491 // The aspect which we have to respect. Note that if the orientation does not need to be
492 // preserved the aspect will be calculated as 1.0 which neutralizes the following
493 // computations.
494 final float minAspect = !mPreserveOrientation
495 ? 1.0f
496 : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT));
497 // Calculate the resulting width and height of the drag operation.
498 int width = right - left;
499 int height = bottom - top;
500 if ((mCtrlType & CTRL_LEFT) != 0) {
501 width = Math.max(mMinVisibleWidth, width - deltaX);
502 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
503 width = Math.max(mMinVisibleWidth, width + deltaX);
504 }
505 if ((mCtrlType & CTRL_TOP) != 0) {
506 height = Math.max(mMinVisibleHeight, height - deltaY);
507 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
508 height = Math.max(mMinVisibleHeight, height + deltaY);
509 }
510
511 // If we have to preserve the orientation - check that we are doing so.
512 final float aspect = (float) width / (float) height;
513 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
514 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
515 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
516 // drag axis. What ever is producing the bigger rectangle will be chosen.
517 int width1;
518 int width2;
519 int height1;
520 int height2;
521 if (mStartOrientationWasLandscape) {
522 // Assuming that the width is our target we calculate the height.
523 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
524 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
525 if (height1 < mMinVisibleHeight) {
526 // If the resulting height is too small we adjust to the minimal size.
527 height1 = mMinVisibleHeight;
528 width1 = Math.max(mMinVisibleWidth,
529 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
530 }
531 // Assuming that the height is our target we calculate the width.
532 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
533 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
534 if (width2 < mMinVisibleWidth) {
535 // If the resulting width is too small we adjust to the minimal size.
536 width2 = mMinVisibleWidth;
537 height2 = Math.max(mMinVisibleHeight,
538 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
539 }
540 } else {
541 // Assuming that the width is our target we calculate the height.
542 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
543 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
544 if (height1 < mMinVisibleHeight) {
545 // If the resulting height is too small we adjust to the minimal size.
546 height1 = mMinVisibleHeight;
547 width1 = Math.max(mMinVisibleWidth,
548 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
549 }
550 // Assuming that the height is our target we calculate the width.
551 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
552 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
553 if (width2 < mMinVisibleWidth) {
554 // If the resulting width is too small we adjust to the minimal size.
555 width2 = mMinVisibleWidth;
556 height2 = Math.max(mMinVisibleHeight,
557 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
558 }
559 }
560
561 // Use the bigger of the two rectangles if the major change was positive, otherwise
562 // do the opposite.
563 final boolean grows = width > (right - left) || height > (bottom - top);
564 if (grows == (width1 * height1 > width2 * height2)) {
565 width = width1;
566 height = height1;
567 } else {
568 width = width2;
569 height = height2;
570 }
571 }
572
573 // Update mWindowDragBounds to the new drag size.
574 updateDraggedBounds(left, top, right, bottom, width, height);
575 }
576
577 /**
578 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
579 *
580 * @param left The original left bound before the user started dragging.
581 * @param top The original top bound before the user started dragging.
582 * @param right The original right bound before the user started dragging.
583 * @param bottom The original bottom bound before the user started dragging.
584 * @param newWidth The new dragged width.
585 * @param newHeight The new dragged height.
586 */
587 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
588 int newHeight) {
589 // Generate the final bounds by keeping the opposite drag edge constant.
590 if ((mCtrlType & CTRL_LEFT) != 0) {
591 left = right - newWidth;
592 } else { // Note: The right might have changed - if we pulled at the right or not.
593 right = left + newWidth;
594 }
595 if ((mCtrlType & CTRL_TOP) != 0) {
596 top = bottom - newHeight;
597 } else { // Note: The height might have changed - if we pulled at the bottom or not.
598 bottom = top + newHeight;
599 }
600
601 mWindowDragBounds.set(left, top, right, bottom);
602
603 checkBoundsForOrientationViolations(mWindowDragBounds);
604 }
605
606 /**
607 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
608 *
609 * @param bounds The bounds to be checked.
610 */
611 private void checkBoundsForOrientationViolations(Rect bounds) {
612 // When using debug check that we are not violating the given constraints.
613 if (DEBUG_ORIENTATION_VIOLATIONS) {
614 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
615 Slog.e(TAG, "Orientation violation detected! should be "
616 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
617 + " but is the other");
618 } else {
619 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
620 }
621 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
622 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
623 + ", " + bounds.width() + ") Height(min,is)=("
624 + mMinVisibleHeight + ", " + bounds.height() + ")");
625 }
626 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
627 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
628 + ", " + bounds.width() + ") Height(min,is)=("
629 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
630 }
631 }
632 }
633
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700634 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
635 final int offsetX = Math.round(x - mStartDragX);
636 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700637 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700638 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
639 final int maxLeft = stackBounds.right - mMinVisibleWidth;
640 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700641
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700642 // Vertically, the top mMinVisibleHeight of the window should remain visible.
643 // (This assumes that the window caption bar is at the top of the window).
644 final int minTop = stackBounds.top;
645 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700646
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700647 mWindowDragBounds.offsetTo(
648 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
649 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
650
Chong Zhangb15758a2015-11-17 12:12:03 -0800651 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
652 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700653 }
654
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700655 public String toShortString() {
656 return TAG;
657 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800658
659 static void setFactory(Factory factory) {
660 sFactory = factory;
661 }
662
663 static TaskPositioner create(WindowManagerService service) {
664 if (sFactory == null) {
665 sFactory = new Factory() {};
666 }
667
668 return sFactory.create(service);
669 }
670
yj81.kwon79c97672019-04-16 19:44:48 -0700671 @Override
672 public void binderDied() {
673 mService.mTaskPositioningController.finishTaskPositioning();
674 }
675
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800676 interface Factory {
677 default TaskPositioner create(WindowManagerService service) {
678 return new TaskPositioner(service);
679 }
680 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700681}