blob: ae70aa8fa6b7d5ddcba6caa19c4a5ceae4efb48c [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 Ogunwale59a73ca2015-09-14 12:54:50 -070019import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
20import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT;
Chong Zhang87b21722015-09-21 15:39:51 -070021import static android.app.ActivityManager.RESIZE_MODE_USER;
Chong Zhang6de2ae82015-09-30 18:25:21 -070022import static android.app.ActivityManager.RESIZE_MODE_USER_FORCED;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale228d4042015-09-13 10:17:34 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070025import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080026import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
27import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING;
28import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
30import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070031import static com.android.server.wm.WindowManagerService.dipToPixel;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +010032import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070033import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
34import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070035
36import android.annotation.IntDef;
37import android.graphics.Point;
38import android.graphics.Rect;
39import android.os.Looper;
40import android.os.Process;
41import android.os.RemoteException;
Wale Ogunwalecad05a02015-09-25 10:41:44 -070042import android.os.Trace;
Chong Zhang8e89b312015-09-09 15:09:30 -070043import android.util.DisplayMetrics;
44import android.util.Slog;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080045import android.view.BatchedInputEventReceiver;
Wale Ogunwale4f52bc62015-09-24 13:47:31 -070046import android.view.Choreographer;
Chong Zhang8e89b312015-09-09 15:09:30 -070047import android.view.Display;
Wale Ogunwale228d4042015-09-13 10:17:34 -070048import android.view.DisplayInfo;
Chong Zhang8e89b312015-09-09 15:09:30 -070049import android.view.InputChannel;
50import android.view.InputDevice;
51import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070052import android.view.MotionEvent;
Wale Ogunwale228d4042015-09-13 10:17:34 -070053import android.view.SurfaceControl;
Chong Zhang8e89b312015-09-09 15:09:30 -070054import android.view.WindowManager;
55
Wale Ogunwale228d4042015-09-13 10:17:34 -070056import com.android.server.input.InputApplicationHandle;
57import com.android.server.input.InputWindowHandle;
58import com.android.server.wm.WindowManagerService.H;
59
60import java.lang.annotation.Retention;
61import java.lang.annotation.RetentionPolicy;
62
63class TaskPositioner implements DimLayer.DimLayerUser {
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
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
Chong Zhang8e89b312015-09-09 15:09:30 -070092 private final WindowManagerService mService;
93 private WindowPositionerEventReceiver mInputEventReceiver;
94 private Display mDisplay;
95 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -070096 private DimLayer mDimLayer;
97 @CtrlType
98 private int mCurrentDimSide;
99 private Rect mTmpRect = new Rect();
100 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700101 private int mMinVisibleWidth;
102 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700103
Chong Zhang3005e752015-09-18 18:46:28 -0700104 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700105 private boolean mResizing;
Chong Zhang8e89b312015-09-09 15:09:30 -0700106 private final Rect mWindowOriginalBounds = new Rect();
107 private final Rect mWindowDragBounds = new Rect();
108 private float mStartDragX;
109 private float mStartDragY;
110 @CtrlType
111 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700112 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700113
114 InputChannel mServerChannel;
115 InputChannel mClientChannel;
116 InputApplicationHandle mDragApplicationHandle;
117 InputWindowHandle mDragWindowHandle;
118
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700119 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
120 public WindowPositionerEventReceiver(
121 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
122 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700123 }
124
125 @Override
126 public void onInputEvent(InputEvent event) {
127 if (!(event instanceof MotionEvent)
128 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
129 return;
130 }
131 final MotionEvent motionEvent = (MotionEvent) event;
132 boolean handled = false;
133
134 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700135 if (mDragEnded) {
136 // The drag has ended but the clean-up message has not been processed by
137 // window manager. Drop events that occur after this until window manager
138 // has a chance to clean-up the input handle.
139 handled = true;
140 return;
141 }
142
Chong Zhang8e89b312015-09-09 15:09:30 -0700143 final float newX = motionEvent.getRawX();
144 final float newY = motionEvent.getRawY();
145
146 switch (motionEvent.getAction()) {
147 case MotionEvent.ACTION_DOWN: {
148 if (DEBUG_TASK_POSITIONING) {
149 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
150 }
151 } break;
152
153 case MotionEvent.ACTION_MOVE: {
154 if (DEBUG_TASK_POSITIONING){
155 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
156 }
157 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700158 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800159 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700160 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700161 if (!mTmpRect.equals(mWindowDragBounds)) {
162 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
163 "wm.TaskPositioner.resizeTask");
164 try {
165 mService.mActivityManager.resizeTask(
166 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
167 } catch (RemoteException e) {
168 }
169 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
170 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700171 } break;
172
173 case MotionEvent.ACTION_UP: {
174 if (DEBUG_TASK_POSITIONING) {
175 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
176 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700177 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700178 } break;
179
180 case MotionEvent.ACTION_CANCEL: {
181 if (DEBUG_TASK_POSITIONING) {
182 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
183 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700184 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700185 } break;
186 }
187
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700188 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700189 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700190 synchronized (mService.mWindowMap) {
191 endDragLocked();
192 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700193 try {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700194 if (wasResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700195 // We were using fullscreen surface during resizing. Request
196 // resizeTask() one last time to restore surface to window size.
197 mService.mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700198 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700199 }
200
201 if (mCurrentDimSide != CTRL_NONE) {
202 final int createMode = mCurrentDimSide == CTRL_LEFT
203 ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
204 : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
205 mService.mActivityManager.moveTaskToDockedStack(
Jorim Jaggi9ea2f7b2015-11-23 18:08:28 -0800206 mTask.mTaskId, createMode, true /*toTop*/, true /* animate */,
207 null /* initialBounds */);
Chong Zhang3005e752015-09-18 18:46:28 -0700208 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700209 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700210
Chong Zhang8e89b312015-09-09 15:09:30 -0700211 // Post back to WM to handle clean-ups. We still need the input
212 // event handler for the last finishInputEvent()!
213 mService.mH.sendEmptyMessage(H.FINISH_TASK_POSITIONING);
214 }
215 handled = true;
216 } catch (Exception e) {
217 Slog.e(TAG, "Exception caught by drag handleMotion", e);
218 } finally {
219 finishInputEvent(event, handled);
220 }
221 }
222 }
223
224 TaskPositioner(WindowManagerService service) {
225 mService = service;
226 }
227
228 /**
229 * @param display The Display that the window being dragged is on.
230 */
231 void register(Display display) {
232 if (DEBUG_TASK_POSITIONING) {
233 Slog.d(TAG, "Registering task positioner");
234 }
235
236 if (mClientChannel != null) {
237 Slog.e(TAG, "Task positioner already registered");
238 return;
239 }
240
241 mDisplay = display;
242 mDisplay.getMetrics(mDisplayMetrics);
243 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
244 mServerChannel = channels[0];
245 mClientChannel = channels[1];
246 mService.mInputManager.registerInputChannel(mServerChannel, null);
247
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700248 mInputEventReceiver = new WindowPositionerEventReceiver(
249 mClientChannel, mService.mH.getLooper(), mService.mChoreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700250
251 mDragApplicationHandle = new InputApplicationHandle(null);
252 mDragApplicationHandle.name = TAG;
253 mDragApplicationHandle.dispatchingTimeoutNanos =
254 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
255
256 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
257 mDisplay.getDisplayId());
258 mDragWindowHandle.name = TAG;
259 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700260 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700261 mDragWindowHandle.layoutParamsFlags = 0;
262 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
263 mDragWindowHandle.dispatchingTimeoutNanos =
264 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
265 mDragWindowHandle.visible = true;
266 mDragWindowHandle.canReceiveKeys = false;
267 mDragWindowHandle.hasFocus = true;
268 mDragWindowHandle.hasWallpaper = false;
269 mDragWindowHandle.paused = false;
270 mDragWindowHandle.ownerPid = Process.myPid();
271 mDragWindowHandle.ownerUid = Process.myUid();
272 mDragWindowHandle.inputFeatures = 0;
273 mDragWindowHandle.scaleFactor = 1.0f;
274
275 // The drag window cannot receive new touches.
276 mDragWindowHandle.touchableRegion.setEmpty();
277
278 // The drag window covers the entire display
279 mDragWindowHandle.frameLeft = 0;
280 mDragWindowHandle.frameTop = 0;
281 final Point p = new Point();
282 mDisplay.getRealSize(p);
283 mDragWindowHandle.frameRight = p.x;
284 mDragWindowHandle.frameBottom = p.y;
285
286 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800287 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700288 Slog.d(TAG, "Pausing rotation during re-position");
289 }
290 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700291
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100292 mDimLayer = new DimLayer(mService, this, mDisplay.getDisplayId(), TAG_LOCAL);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700293 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
294 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
295 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700296
297 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700298 }
299
300 void unregister() {
301 if (DEBUG_TASK_POSITIONING) {
302 Slog.d(TAG, "Unregistering task positioner");
303 }
304
305 if (mClientChannel == null) {
306 Slog.e(TAG, "Task positioner not registered");
307 return;
308 }
309
310 mService.mInputManager.unregisterInputChannel(mServerChannel);
311
312 mInputEventReceiver.dispose();
313 mInputEventReceiver = null;
314 mClientChannel.dispose();
315 mServerChannel.dispose();
316 mClientChannel = null;
317 mServerChannel = null;
318
319 mDragWindowHandle = null;
320 mDragApplicationHandle = null;
321 mDisplay = null;
322
Wale Ogunwale228d4042015-09-13 10:17:34 -0700323 if (mDimLayer != null) {
324 mDimLayer.destroySurface();
325 mDimLayer = null;
326 }
327 mCurrentDimSide = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700328 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700329
Chong Zhang8e89b312015-09-09 15:09:30 -0700330 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800331 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700332 Slog.d(TAG, "Resuming rotation after re-position");
333 }
334 mService.resumeRotationLocked();
335 }
336
337 void startDragLocked(WindowState win, boolean resize, float startX, float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700338 if (DEBUG_TASK_POSITIONING) {
339 Slog.d(TAG, "startDragLocked: win=" + win + ", resize=" + resize
Chong Zhang8e89b312015-09-09 15:09:30 -0700340 + ", {" + startX + ", " + startY + "}");
341 }
342 mCtrlType = CTRL_NONE;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800343 mTask = win.getTask();
344 mStartDragX = startX;
345 mStartDragY = startY;
346
Chong Zhangb15758a2015-11-17 12:12:03 -0800347 if (mTask.isDockedInEffect()) {
348 // If this is a docked task or if task size is affected by docked stack changing size,
349 // we can only be here if the task is not resizeable and we're handling a two-finger
350 // scrolling. Use the original task bounds to position the task, the dim bounds
351 // is cropped and doesn't move.
352 mTask.getBounds(mTmpRect);
353 } else {
354 // Use the dim bounds, not the original task bounds. The cursor
355 // movement should be calculated relative to the visible bounds.
356 // Also, use the dim bounds of the task which accounts for
357 // multiple app windows. Don't use any bounds from win itself as it
358 // may not be the same size as the task.
359 mTask.getDimBounds(mTmpRect);
360 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800361
Chong Zhang8e89b312015-09-09 15:09:30 -0700362 if (resize) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800363 if (startX < mTmpRect.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700364 mCtrlType |= CTRL_LEFT;
365 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800366 if (startX > mTmpRect.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700367 mCtrlType |= CTRL_RIGHT;
368 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800369 if (startY < mTmpRect.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700370 mCtrlType |= CTRL_TOP;
371 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800372 if (startY > mTmpRect.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700373 mCtrlType |= CTRL_BOTTOM;
374 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700375 mResizing = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700376 }
377
Chong Zhangd8ceb852015-11-11 14:53:41 -0800378 mWindowOriginalBounds.set(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700379 }
380
381 private void endDragLocked() {
382 mResizing = false;
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100383 mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM);
Chong Zhang8e89b312015-09-09 15:09:30 -0700384 }
385
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700386 /** Returns true if the move operation should be ended. */
387 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700388 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800389 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700390 }
391
392 if (mCtrlType != CTRL_NONE) {
393 // This is a resizing operation.
394 final int deltaX = Math.round(x - mStartDragX);
395 final int deltaY = Math.round(y - mStartDragY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700396 int left = mWindowOriginalBounds.left;
397 int top = mWindowOriginalBounds.top;
398 int right = mWindowOriginalBounds.right;
399 int bottom = mWindowOriginalBounds.bottom;
400 if ((mCtrlType & CTRL_LEFT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700401 left = Math.min(left + deltaX, right - mMinVisibleWidth);
Chong Zhang8e89b312015-09-09 15:09:30 -0700402 }
403 if ((mCtrlType & CTRL_TOP) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700404 top = Math.min(top + deltaY, bottom - mMinVisibleHeight);
Chong Zhang8e89b312015-09-09 15:09:30 -0700405 }
406 if ((mCtrlType & CTRL_RIGHT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700407 right = Math.max(left + mMinVisibleWidth, right + deltaX);
Chong Zhang8e89b312015-09-09 15:09:30 -0700408 }
409 if ((mCtrlType & CTRL_BOTTOM) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700410 bottom = Math.max(top + mMinVisibleHeight, bottom + deltaY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700411 }
412 mWindowDragBounds.set(left, top, right, bottom);
Jorim Jaggi0b46f3c2016-03-14 12:21:37 +0100413 mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700414 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700415 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700416
417 // This is a moving operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800418 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800419
420 // If this is a non-resizeable task put into side-by-side mode, we are
421 // handling a two-finger scrolling action. No need to shrink the bounds.
422 if (!mTask.isDockedInEffect()) {
423 mTmpRect.inset(mMinVisibleWidth, mMinVisibleHeight);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700424 }
Chong Zhangb15758a2015-11-17 12:12:03 -0800425
426 boolean dragEnded = false;
427 final int nX = (int) x;
428 final int nY = (int) y;
429 if (!mTmpRect.contains(nX, nY)) {
430 // We end the moving operation if position is outside the stack bounds.
431 // In this case we need to clamp the position to stack bounds and calculate
432 // the final window drag bounds.
433 x = Math.min(Math.max(x, mTmpRect.left), mTmpRect.right);
434 y = Math.min(Math.max(y, mTmpRect.top), mTmpRect.bottom);
435 dragEnded = true;
436 }
437
438 updateWindowDragBounds(nX, nY);
439 updateDimLayerVisibility(nX);
440 return dragEnded;
441 }
442
443 private void updateWindowDragBounds(int x, int y) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700444 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhangb15758a2015-11-17 12:12:03 -0800445 if (mTask.isDockedInEffect()) {
446 // Offset the bounds without clamp, the bounds will be shifted later
447 // by window manager before applying the scrolling.
448 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
449 mWindowDragBounds.offset(Math.round(x - mStartDragX), 0);
450 } else {
451 mWindowDragBounds.offset(0, Math.round(y - mStartDragY));
452 }
453 } else {
454 mWindowDragBounds.offset(Math.round(x - mStartDragX), Math.round(y - mStartDragY));
455 }
456 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
457 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700458 }
459
Wale Ogunwale228d4042015-09-13 10:17:34 -0700460 private void updateDimLayerVisibility(int x) {
461 @CtrlType
462 int dimSide = getDimSide(x);
463 if (dimSide == mCurrentDimSide) {
464 return;
465 }
466
467 mCurrentDimSide = dimSide;
468
469 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION updateDimLayerVisibility");
470 SurfaceControl.openTransaction();
471 if (mCurrentDimSide == CTRL_NONE) {
472 mDimLayer.hide();
473 } else {
474 showDimLayer();
475 }
476 SurfaceControl.closeTransaction();
477 }
478
479 /**
480 * Returns the side of the screen the dim layer should be shown.
481 * @param x horizontal coordinate used to determine if the dim layer should be shown
482 * @return Returns {@link #CTRL_LEFT} if the dim layer should be shown on the left half of the
483 * screen, {@link #CTRL_RIGHT} if on the right side, or {@link #CTRL_NONE} if the dim layer
484 * shouldn't be shown.
485 */
486 private int getDimSide(int x) {
Chong Zhang3005e752015-09-18 18:46:28 -0700487 if (mTask.mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
488 || !mTask.mStack.isFullscreen()
Wale Ogunwale228d4042015-09-13 10:17:34 -0700489 || mService.mCurConfiguration.orientation != ORIENTATION_LANDSCAPE) {
490 return CTRL_NONE;
491 }
492
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800493 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700494 if (x - mSideMargin <= mTmpRect.left) {
495 return CTRL_LEFT;
496 }
497 if (x + mSideMargin >= mTmpRect.right) {
498 return CTRL_RIGHT;
499 }
500
501 return CTRL_NONE;
502 }
503
504 private void showDimLayer() {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800505 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700506 if (mCurrentDimSide == CTRL_LEFT) {
507 mTmpRect.right = mTmpRect.centerX();
508 } else if (mCurrentDimSide == CTRL_RIGHT) {
509 mTmpRect.left = mTmpRect.centerX();
510 }
511
512 mDimLayer.setBounds(mTmpRect);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700513 mDimLayer.show(mService.getDragLayerLocked(), RESIZING_HINT_ALPHA,
514 RESIZING_HINT_DURATION_MS);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700515 }
516
517 @Override /** {@link DimLayer.DimLayerUser} */
518 public boolean isFullscreen() {
519 return false;
520 }
521
522 @Override /** {@link DimLayer.DimLayerUser} */
523 public DisplayInfo getDisplayInfo() {
Chong Zhang3005e752015-09-18 18:46:28 -0700524 return mTask.mStack.getDisplayInfo();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700525 }
526
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700527 @Override
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800528 public void getDimBounds(Rect out) {
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700529 // This dim layer user doesn't need this.
530 }
531
532 @Override
533 public String toShortString() {
534 return TAG;
535 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700536}