blob: b680fa45f00f9584225f35ddfcc14c270468e3f5 [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;
Adrian Roosb125e0b2019-10-02 14:55:14 +020024import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import 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;
Arthur Hungb62e5002019-08-29 12:28:07 +080053import android.view.SurfaceControl;
Chong Zhang8e89b312015-09-09 15:09:30 -070054import android.view.WindowManager;
55
skuhne@google.com322347b2016-12-02 12:54:03 -080056import com.android.internal.annotations.VisibleForTesting;
Adrian Roosb125e0b2019-10-02 14:55:14 +020057import com.android.server.protolog.common.ProtoLog;
Wale Ogunwale228d4042015-09-13 10:17:34 -070058
59import java.lang.annotation.Retention;
60import java.lang.annotation.RetentionPolicy;
61
yj81.kwon79c97672019-04-16 19:44:48 -070062class TaskPositioner implements IBinder.DeathRecipient {
skuhne@google.com322347b2016-12-02 12:54:03 -080063 private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false;
Jorim Jaggibc5425c2016-03-01 13:51:16 +010064 private static final String TAG_LOCAL = "TaskPositioner";
65 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070066
Garfield Tan6caf1d8c2018-01-18 12:37:50 -080067 private static Factory sFactory;
68
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;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070097 private final IActivityTaskManager mActivityManager;
Chong Zhang8e89b312015-09-09 15:09:30 -070098 private WindowPositionerEventReceiver mInputEventReceiver;
Riddle Hsu654a6f92018-07-13 22:59:36 +080099 private DisplayContent mDisplayContent;
Chong Zhang8e89b312015-09-09 15:09:30 -0700100 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700101 private Rect mTmpRect = new Rect();
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700102 private int mMinVisibleWidth;
103 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700104
Charles Chen2cb5a0f2019-03-22 20:58:00 +0800105 @VisibleForTesting
106 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;
yj81.kwon79c97672019-04-16 19:44:48 -0700117 @VisibleForTesting
118 boolean mDragEnded;
yj81.kwon19585ff2019-04-23 18:53:57 -0700119 IBinder mClientCallback;
Chong Zhang8e89b312015-09-09 15:09:30 -0700120
121 InputChannel mServerChannel;
122 InputChannel mClientChannel;
123 InputApplicationHandle mDragApplicationHandle;
124 InputWindowHandle mDragWindowHandle;
125
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700126 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
127 public WindowPositionerEventReceiver(
128 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
129 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700130 }
131
132 @Override
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -0800133 public void onInputEvent(InputEvent event) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700134 if (!(event instanceof MotionEvent)
135 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
136 return;
137 }
138 final MotionEvent motionEvent = (MotionEvent) event;
139 boolean handled = false;
140
141 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700142 if (mDragEnded) {
143 // The drag has ended but the clean-up message has not been processed by
144 // window manager. Drop events that occur after this until window manager
145 // has a chance to clean-up the input handle.
146 handled = true;
147 return;
148 }
149
Chong Zhang8e89b312015-09-09 15:09:30 -0700150 final float newX = motionEvent.getRawX();
151 final float newY = motionEvent.getRawY();
152
153 switch (motionEvent.getAction()) {
154 case MotionEvent.ACTION_DOWN: {
155 if (DEBUG_TASK_POSITIONING) {
156 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
157 }
158 } break;
159
160 case MotionEvent.ACTION_MOVE: {
161 if (DEBUG_TASK_POSITIONING){
162 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
163 }
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700164 synchronized (mService.mGlobalLock) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700165 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800166 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700167 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700168 if (!mTmpRect.equals(mWindowDragBounds)) {
169 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
170 "wm.TaskPositioner.resizeTask");
171 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900172 mActivityManager.resizeTask(
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700173 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
174 } catch (RemoteException e) {
175 }
176 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
177 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700178 } break;
179
180 case MotionEvent.ACTION_UP: {
181 if (DEBUG_TASK_POSITIONING) {
182 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
183 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700184 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700185 } break;
186
187 case MotionEvent.ACTION_CANCEL: {
188 if (DEBUG_TASK_POSITIONING) {
189 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
190 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700191 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700192 } break;
193 }
194
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700195 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700196 final boolean wasResizing = mResizing;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700197 synchronized (mService.mGlobalLock) {
Chong Zhang3005e752015-09-18 18:46:28 -0700198 endDragLocked();
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700199 mTask.getDimBounds(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700200 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700201 try {
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700202 if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) {
Chong Zhang3005e752015-09-18 18:46:28 -0700203 // We were using fullscreen surface during resizing. Request
204 // resizeTask() one last time to restore surface to window size.
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900205 mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700206 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700207 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700208 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700209
Chong Zhang8e89b312015-09-09 15:09:30 -0700210 // Post back to WM to handle clean-ups. We still need the input
211 // event handler for the last finishInputEvent()!
Daichi Hironoce2f97a2017-11-30 16:44:15 +0900212 mService.mTaskPositioningController.finishTaskPositioning();
Chong Zhang8e89b312015-09-09 15:09:30 -0700213 }
214 handled = true;
215 } catch (Exception e) {
216 Slog.e(TAG, "Exception caught by drag handleMotion", e);
217 } finally {
218 finishInputEvent(event, handled);
219 }
220 }
221 }
222
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900223 @VisibleForTesting
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700224 TaskPositioner(WindowManagerService service, IActivityTaskManager activityManager) {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900225 mService = service;
226 mActivityManager = activityManager;
227 }
228
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800229 /** Use {@link #create(WindowManagerService)} instead **/
Chong Zhang8e89b312015-09-09 15:09:30 -0700230 TaskPositioner(WindowManagerService service) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700231 this(service, service.mActivityTaskManager);
Chong Zhang8e89b312015-09-09 15:09:30 -0700232 }
233
skuhne@google.com322347b2016-12-02 12:54:03 -0800234 @VisibleForTesting
235 Rect getWindowDragBounds() {
236 return mWindowDragBounds;
237 }
238
Chong Zhang8e89b312015-09-09 15:09:30 -0700239 /**
Garfield Tan07544cd2018-09-12 16:16:54 -0700240 * @param displayContent The Display that the window being dragged is on.
Chong Zhang8e89b312015-09-09 15:09:30 -0700241 */
Robert Carrb1579c82017-09-05 14:54:47 -0700242 void register(DisplayContent displayContent) {
243 final Display display = displayContent.getDisplay();
244
Chong Zhang8e89b312015-09-09 15:09:30 -0700245 if (DEBUG_TASK_POSITIONING) {
246 Slog.d(TAG, "Registering task positioner");
247 }
248
249 if (mClientChannel != null) {
250 Slog.e(TAG, "Task positioner already registered");
251 return;
252 }
253
Riddle Hsu654a6f92018-07-13 22:59:36 +0800254 mDisplayContent = displayContent;
255 display.getMetrics(mDisplayMetrics);
Chong Zhang8e89b312015-09-09 15:09:30 -0700256 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
257 mServerChannel = channels[0];
258 mClientChannel = channels[1];
259 mService.mInputManager.registerInputChannel(mServerChannel, null);
260
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700261 mInputEventReceiver = new WindowPositionerEventReceiver(
Jorim Jaggied7993b2017-03-28 18:50:01 +0100262 mClientChannel, mService.mAnimationHandler.getLooper(),
263 mService.mAnimator.getChoreographer());
Chong Zhang8e89b312015-09-09 15:09:30 -0700264
Robert Carr0bcbe642018-10-11 19:07:43 -0700265 mDragApplicationHandle = new InputApplicationHandle(new Binder());
Chong Zhang8e89b312015-09-09 15:09:30 -0700266 mDragApplicationHandle.name = TAG;
267 mDragApplicationHandle.dispatchingTimeoutNanos =
268 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
269
Robert Carre0a353c2018-08-02 16:38:04 -0700270 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
Riddle Hsu654a6f92018-07-13 22:59:36 +0800271 display.getDisplayId());
Chong Zhang8e89b312015-09-09 15:09:30 -0700272 mDragWindowHandle.name = TAG;
Robert Carreadae822018-10-11 19:07:03 -0700273 mDragWindowHandle.token = mServerChannel.getToken();
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700274 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700275 mDragWindowHandle.layoutParamsFlags = 0;
276 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
277 mDragWindowHandle.dispatchingTimeoutNanos =
278 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
279 mDragWindowHandle.visible = true;
280 mDragWindowHandle.canReceiveKeys = false;
281 mDragWindowHandle.hasFocus = true;
282 mDragWindowHandle.hasWallpaper = false;
283 mDragWindowHandle.paused = false;
284 mDragWindowHandle.ownerPid = Process.myPid();
285 mDragWindowHandle.ownerUid = Process.myUid();
286 mDragWindowHandle.inputFeatures = 0;
287 mDragWindowHandle.scaleFactor = 1.0f;
288
289 // The drag window cannot receive new touches.
290 mDragWindowHandle.touchableRegion.setEmpty();
291
292 // The drag window covers the entire display
293 mDragWindowHandle.frameLeft = 0;
294 mDragWindowHandle.frameTop = 0;
295 final Point p = new Point();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800296 display.getRealSize(p);
Chong Zhang8e89b312015-09-09 15:09:30 -0700297 mDragWindowHandle.frameRight = p.x;
298 mDragWindowHandle.frameBottom = p.y;
299
300 // Pause rotations before a drag.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200301 ProtoLog.d(WM_DEBUG_ORIENTATION, "Pausing rotation during re-position");
Riddle Hsuccf09402019-08-13 00:33:06 +0800302 mDisplayContent.getDisplayRotation().pause();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700303
Garfield Tan07544cd2018-09-12 16:16:54 -0700304 // Notify InputMonitor to take mDragWindowHandle.
Arthur Hungb62e5002019-08-29 12:28:07 +0800305 mDisplayContent.getInputMonitor().updateInputWindowsImmediately();
306 new SurfaceControl.Transaction().syncInputWindows().apply(true);
Garfield Tan07544cd2018-09-12 16:16:54 -0700307
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700308 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
309 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Riddle Hsu654a6f92018-07-13 22:59:36 +0800310 display.getRealSize(mMaxVisibleSize);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700311
312 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700313 }
314
315 void unregister() {
316 if (DEBUG_TASK_POSITIONING) {
317 Slog.d(TAG, "Unregistering task positioner");
318 }
319
320 if (mClientChannel == null) {
321 Slog.e(TAG, "Task positioner not registered");
322 return;
323 }
324
325 mService.mInputManager.unregisterInputChannel(mServerChannel);
326
327 mInputEventReceiver.dispose();
328 mInputEventReceiver = null;
329 mClientChannel.dispose();
330 mServerChannel.dispose();
331 mClientChannel = null;
332 mServerChannel = null;
333
334 mDragWindowHandle = null;
335 mDragApplicationHandle = null;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700336 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700337
Garfield Tan07544cd2018-09-12 16:16:54 -0700338 // Notify InputMonitor to remove mDragWindowHandle.
339 mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/);
340
Chong Zhang8e89b312015-09-09 15:09:30 -0700341 // Resume rotations after a drag.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200342 ProtoLog.d(WM_DEBUG_ORIENTATION, "Resuming rotation after re-position");
Riddle Hsuccf09402019-08-13 00:33:06 +0800343 mDisplayContent.getDisplayRotation().resume();
Riddle Hsu654a6f92018-07-13 22:59:36 +0800344 mDisplayContent = null;
yj81.kwon79c97672019-04-16 19:44:48 -0700345 mClientCallback.unlinkToDeath(this, 0 /* flags */);
Chong Zhang8e89b312015-09-09 15:09:30 -0700346 }
347
skuhne@google.com322347b2016-12-02 12:54:03 -0800348 void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX,
349 float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700350 if (DEBUG_TASK_POSITIONING) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800351 Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize
352 + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", "
353 + startY + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700354 }
yj81.kwon79c97672019-04-16 19:44:48 -0700355 try {
356 mClientCallback = win.mClient.asBinder();
357 mClientCallback.linkToDeath(this, 0 /* flags */);
358 } catch (RemoteException e) {
359 // The caller has died, so clean up TaskPositioningController.
360 mService.mTaskPositioningController.finishTaskPositioning();
361 return;
362 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800363 mTask = win.getTask();
Evan Rosky0d654cb2019-02-26 10:59:10 -0800364 // Use the bounds of the task which accounts for
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700365 // multiple app windows. Don't use any bounds from win itself as it
366 // may not be the same size as the task.
Evan Rosky0d654cb2019-02-26 10:59:10 -0800367 mTask.getBounds(mTmpRect);
skuhne@google.com322347b2016-12-02 12:54:03 -0800368 startDrag(resize, preserveOrientation, startX, startY, mTmpRect);
369 }
370
Daichi Hirono43094a12018-05-31 12:32:23 +0900371 protected void startDrag(boolean resize, boolean preserveOrientation,
skuhne@google.com322347b2016-12-02 12:54:03 -0800372 float startX, float startY, Rect startBounds) {
373 mCtrlType = CTRL_NONE;
374 mStartDragX = startX;
375 mStartDragY = startY;
376 mPreserveOrientation = preserveOrientation;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800377
Chong Zhang8e89b312015-09-09 15:09:30 -0700378 if (resize) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800379 if (startX < startBounds.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700380 mCtrlType |= CTRL_LEFT;
381 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800382 if (startX > startBounds.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700383 mCtrlType |= CTRL_RIGHT;
384 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800385 if (startY < startBounds.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700386 mCtrlType |= CTRL_TOP;
387 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800388 if (startY > startBounds.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700389 mCtrlType |= CTRL_BOTTOM;
390 }
skuhne@google.com322347b2016-12-02 12:54:03 -0800391 mResizing = mCtrlType != CTRL_NONE;
Chong Zhang8e89b312015-09-09 15:09:30 -0700392 }
393
skuhne@google.com322347b2016-12-02 12:54:03 -0800394 // In case of !isDockedInEffect we are using the union of all task bounds. These might be
395 // made up out of multiple windows which are only partially overlapping. When that happens,
396 // the orientation from the window of interest to the entire stack might diverge. However
397 // for now we treat them as the same.
398 mStartOrientationWasLandscape = startBounds.width() >= startBounds.height();
399 mWindowOriginalBounds.set(startBounds);
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700400
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900401 // Notify the app that resizing has started, even though we haven't received any new
402 // bounds yet. This will guarantee that the app starts the backdrop renderer before
403 // configuration changes which could cause an activity restart.
404 if (mResizing) {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700405 synchronized (mService.mGlobalLock) {
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900406 notifyMoveLocked(startX, startY);
407 }
408
409 // Perform the resize on the WMS handler thread when we don't have the WMS lock held
410 // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver
411 // callbacks are delivered on the same handler so this initial resize is always
412 // guaranteed to happen before subsequent drag resizes.
413 mService.mH.post(() -> {
414 try {
Daichi Hirono67f2f4b2018-05-25 16:07:30 +0900415 mActivityManager.resizeTask(
Tomasz Mikolajewski79e8d172017-11-21 11:21:42 +0900416 mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED);
417 } catch (RemoteException e) {
418 }
419 });
420 }
421
Vladislav Kaznacheev824bc5d2016-04-22 17:48:35 -0700422 // Make sure we always have valid drag bounds even if the drag ends before any move events
423 // have been handled.
skuhne@google.com322347b2016-12-02 12:54:03 -0800424 mWindowDragBounds.set(startBounds);
Chong Zhang3005e752015-09-18 18:46:28 -0700425 }
426
427 private void endDragLocked() {
428 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100429 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700430 }
431
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700432 /** Returns true if the move operation should be ended. */
433 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700434 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800435 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700436 }
437
438 if (mCtrlType != CTRL_NONE) {
skuhne@google.com322347b2016-12-02 12:54:03 -0800439 resizeDrag(x, y);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100440 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700441 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700442 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700443
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700444 // This is a moving or scrolling operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800445 mTask.mStack.getDimBounds(mTmpRect);
GyeHun Jeon84b30d22019-04-24 14:20:15 -0700446 // If a target window is covered by system bar, there is no way to move it again by touch.
447 // So we exclude them from stack bounds. and then it will be shown inside stable area.
448 Rect stableBounds = new Rect();
449 mDisplayContent.getStableRect(stableBounds);
450 mTmpRect.intersect(stableBounds);
Chong Zhangb15758a2015-11-17 12:12:03 -0800451
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700452 int nX = (int) x;
453 int nY = (int) y;
Chong Zhangb15758a2015-11-17 12:12:03 -0800454 if (!mTmpRect.contains(nX, nY)) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700455 // For a moving operation we allow the pointer to go out of the stack bounds, but
456 // use the clamped pointer position for the drag bounds computation.
457 nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right);
458 nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom);
Chong Zhangb15758a2015-11-17 12:12:03 -0800459 }
460
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700461 updateWindowDragBounds(nX, nY, mTmpRect);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700462 return false;
Chong Zhangb15758a2015-11-17 12:12:03 -0800463 }
464
skuhne@google.com322347b2016-12-02 12:54:03 -0800465 /**
466 * The user is drag - resizing the window.
467 *
468 * @param x The x coordinate of the current drag coordinate.
469 * @param y the y coordinate of the current drag coordinate.
470 */
471 @VisibleForTesting
472 void resizeDrag(float x, float y) {
473 // This is a resizing operation.
474 // We need to keep various constraints:
475 // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y]
476 // 2. The orientation is kept - if required.
477 final int deltaX = Math.round(x - mStartDragX);
478 final int deltaY = Math.round(y - mStartDragY);
479 int left = mWindowOriginalBounds.left;
480 int top = mWindowOriginalBounds.top;
481 int right = mWindowOriginalBounds.right;
482 int bottom = mWindowOriginalBounds.bottom;
483
skuhne@google.com322347b2016-12-02 12:54:03 -0800484 // Calculate the resulting width and height of the drag operation.
485 int width = right - left;
486 int height = bottom - top;
487 if ((mCtrlType & CTRL_LEFT) != 0) {
488 width = Math.max(mMinVisibleWidth, width - deltaX);
489 } else if ((mCtrlType & CTRL_RIGHT) != 0) {
490 width = Math.max(mMinVisibleWidth, width + deltaX);
491 }
492 if ((mCtrlType & CTRL_TOP) != 0) {
493 height = Math.max(mMinVisibleHeight, height - deltaY);
494 } else if ((mCtrlType & CTRL_BOTTOM) != 0) {
495 height = Math.max(mMinVisibleHeight, height + deltaY);
496 }
497
498 // If we have to preserve the orientation - check that we are doing so.
499 final float aspect = (float) width / (float) height;
500 if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT)
501 || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) {
502 // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major
503 // drag axis. What ever is producing the bigger rectangle will be chosen.
504 int width1;
505 int width2;
506 int height1;
507 int height2;
508 if (mStartOrientationWasLandscape) {
509 // Assuming that the width is our target we calculate the height.
510 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
511 height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT));
512 if (height1 < mMinVisibleHeight) {
513 // If the resulting height is too small we adjust to the minimal size.
514 height1 = mMinVisibleHeight;
515 width1 = Math.max(mMinVisibleWidth,
516 Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT)));
517 }
518 // Assuming that the height is our target we calculate the width.
519 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
520 width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT));
521 if (width2 < mMinVisibleWidth) {
522 // If the resulting width is too small we adjust to the minimal size.
523 width2 = mMinVisibleWidth;
524 height2 = Math.max(mMinVisibleHeight,
525 Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT)));
526 }
527 } else {
528 // Assuming that the width is our target we calculate the height.
529 width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width));
530 height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT));
531 if (height1 < mMinVisibleHeight) {
532 // If the resulting height is too small we adjust to the minimal size.
533 height1 = mMinVisibleHeight;
534 width1 = Math.max(mMinVisibleWidth,
535 Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT)));
536 }
537 // Assuming that the height is our target we calculate the width.
538 height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height));
539 width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT));
540 if (width2 < mMinVisibleWidth) {
541 // If the resulting width is too small we adjust to the minimal size.
542 width2 = mMinVisibleWidth;
543 height2 = Math.max(mMinVisibleHeight,
544 Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT)));
545 }
546 }
547
548 // Use the bigger of the two rectangles if the major change was positive, otherwise
549 // do the opposite.
550 final boolean grows = width > (right - left) || height > (bottom - top);
551 if (grows == (width1 * height1 > width2 * height2)) {
552 width = width1;
553 height = height1;
554 } else {
555 width = width2;
556 height = height2;
557 }
558 }
559
560 // Update mWindowDragBounds to the new drag size.
561 updateDraggedBounds(left, top, right, bottom, width, height);
562 }
563
564 /**
565 * Given the old coordinates and the new width and height, update the mWindowDragBounds.
566 *
567 * @param left The original left bound before the user started dragging.
568 * @param top The original top bound before the user started dragging.
569 * @param right The original right bound before the user started dragging.
570 * @param bottom The original bottom bound before the user started dragging.
571 * @param newWidth The new dragged width.
572 * @param newHeight The new dragged height.
573 */
574 void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth,
575 int newHeight) {
576 // Generate the final bounds by keeping the opposite drag edge constant.
577 if ((mCtrlType & CTRL_LEFT) != 0) {
578 left = right - newWidth;
579 } else { // Note: The right might have changed - if we pulled at the right or not.
580 right = left + newWidth;
581 }
582 if ((mCtrlType & CTRL_TOP) != 0) {
583 top = bottom - newHeight;
584 } else { // Note: The height might have changed - if we pulled at the bottom or not.
585 bottom = top + newHeight;
586 }
587
588 mWindowDragBounds.set(left, top, right, bottom);
589
590 checkBoundsForOrientationViolations(mWindowDragBounds);
591 }
592
593 /**
594 * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set).
595 *
596 * @param bounds The bounds to be checked.
597 */
598 private void checkBoundsForOrientationViolations(Rect bounds) {
599 // When using debug check that we are not violating the given constraints.
600 if (DEBUG_ORIENTATION_VIOLATIONS) {
601 if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) {
602 Slog.e(TAG, "Orientation violation detected! should be "
603 + (mStartOrientationWasLandscape ? "landscape" : "portrait")
604 + " but is the other");
605 } else {
606 Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height());
607 }
608 if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) {
609 Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth
610 + ", " + bounds.width() + ") Height(min,is)=("
611 + mMinVisibleHeight + ", " + bounds.height() + ")");
612 }
613 if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) {
614 Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x
615 + ", " + bounds.width() + ") Height(min,is)=("
616 + mMaxVisibleSize.y + ", " + bounds.height() + ")");
617 }
618 }
619 }
620
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700621 private void updateWindowDragBounds(int x, int y, Rect stackBounds) {
622 final int offsetX = Math.round(x - mStartDragX);
623 final int offsetY = Math.round(y - mStartDragY);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700624 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700625 // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible.
626 final int maxLeft = stackBounds.right - mMinVisibleWidth;
627 final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width();
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700628
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700629 // Vertically, the top mMinVisibleHeight of the window should remain visible.
630 // (This assumes that the window caption bar is at the top of the window).
631 final int minTop = stackBounds.top;
632 final int maxTop = stackBounds.bottom - mMinVisibleHeight;
Vladislav Kaznacheeva90a3b82016-04-27 14:54:53 -0700633
Chong Zhang2e2c81a2016-07-15 11:28:17 -0700634 mWindowDragBounds.offsetTo(
635 Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft),
636 Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop));
637
Chong Zhangb15758a2015-11-17 12:12:03 -0800638 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
639 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700640 }
641
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700642 public String toShortString() {
643 return TAG;
644 }
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800645
646 static void setFactory(Factory factory) {
647 sFactory = factory;
648 }
649
650 static TaskPositioner create(WindowManagerService service) {
651 if (sFactory == null) {
652 sFactory = new Factory() {};
653 }
654
655 return sFactory.create(service);
656 }
657
yj81.kwon79c97672019-04-16 19:44:48 -0700658 @Override
659 public void binderDied() {
660 mService.mTaskPositioningController.finishTaskPositioning();
661 }
662
Garfield Tan6caf1d8c2018-01-18 12:37:50 -0800663 interface Factory {
664 default TaskPositioner create(WindowManagerService service) {
665 return new TaskPositioner(service);
666 }
667 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700668}