blob: 92701dee699dc242e6e13240b9f6722eb583b794 [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;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070032import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
33import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070034
35import android.annotation.IntDef;
36import android.graphics.Point;
37import android.graphics.Rect;
38import 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;
Wale Ogunwale228d4042015-09-13 10:17:34 -070047import android.view.DisplayInfo;
Chong Zhang8e89b312015-09-09 15:09:30 -070048import android.view.InputChannel;
49import android.view.InputDevice;
50import android.view.InputEvent;
Chong Zhang8e89b312015-09-09 15:09:30 -070051import android.view.MotionEvent;
Wale Ogunwale228d4042015-09-13 10:17:34 -070052import android.view.SurfaceControl;
Chong Zhang8e89b312015-09-09 15:09:30 -070053import android.view.WindowManager;
54
Wale Ogunwale228d4042015-09-13 10:17:34 -070055import com.android.server.input.InputApplicationHandle;
56import com.android.server.input.InputWindowHandle;
57import com.android.server.wm.WindowManagerService.H;
58
59import java.lang.annotation.Retention;
60import java.lang.annotation.RetentionPolicy;
61
62class TaskPositioner implements DimLayer.DimLayerUser {
Jorim Jaggibc5425c2016-03-01 13:51:16 +010063 private static final String TAG_LOCAL = "TaskPositioner";
64 private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM;
Chong Zhang8e89b312015-09-09 15:09:30 -070065
Wale Ogunwale228d4042015-09-13 10:17:34 -070066 // The margin the pointer position has to be within the side of the screen to be
67 // considered at the side of the screen.
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -070068 static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070069
Chong Zhang8e89b312015-09-09 15:09:30 -070070 @IntDef(flag = true,
71 value = {
72 CTRL_NONE,
73 CTRL_LEFT,
74 CTRL_RIGHT,
75 CTRL_TOP,
76 CTRL_BOTTOM
77 })
78 @Retention(RetentionPolicy.SOURCE)
79 @interface CtrlType {}
80
81 private static final int CTRL_NONE = 0x0;
82 private static final int CTRL_LEFT = 0x1;
83 private static final int CTRL_RIGHT = 0x2;
84 private static final int CTRL_TOP = 0x4;
85 private static final int CTRL_BOTTOM = 0x8;
86
Filip Gruszczynski64b6b442016-01-18 13:20:58 -080087 public static final float RESIZING_HINT_ALPHA = 0.5f;
88
89 public static final int RESIZING_HINT_DURATION_MS = 0;
90
Chong Zhang8e89b312015-09-09 15:09:30 -070091 private final WindowManagerService mService;
92 private WindowPositionerEventReceiver mInputEventReceiver;
93 private Display mDisplay;
94 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -070095 private DimLayer mDimLayer;
96 @CtrlType
97 private int mCurrentDimSide;
98 private Rect mTmpRect = new Rect();
99 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700100 private int mMinVisibleWidth;
101 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -0700102
Chong Zhang3005e752015-09-18 18:46:28 -0700103 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -0700104 private boolean mResizing;
Chong Zhang8e89b312015-09-09 15:09:30 -0700105 private final Rect mWindowOriginalBounds = new Rect();
106 private final Rect mWindowDragBounds = new Rect();
107 private float mStartDragX;
108 private float mStartDragY;
109 @CtrlType
110 private int mCtrlType = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700111 private boolean mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700112
113 InputChannel mServerChannel;
114 InputChannel mClientChannel;
115 InputApplicationHandle mDragApplicationHandle;
116 InputWindowHandle mDragWindowHandle;
117
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700118 private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver {
119 public WindowPositionerEventReceiver(
120 InputChannel inputChannel, Looper looper, Choreographer choreographer) {
121 super(inputChannel, looper, choreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700122 }
123
124 @Override
125 public void onInputEvent(InputEvent event) {
126 if (!(event instanceof MotionEvent)
127 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
128 return;
129 }
130 final MotionEvent motionEvent = (MotionEvent) event;
131 boolean handled = false;
132
133 try {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700134 if (mDragEnded) {
135 // The drag has ended but the clean-up message has not been processed by
136 // window manager. Drop events that occur after this until window manager
137 // has a chance to clean-up the input handle.
138 handled = true;
139 return;
140 }
141
Chong Zhang8e89b312015-09-09 15:09:30 -0700142 final float newX = motionEvent.getRawX();
143 final float newY = motionEvent.getRawY();
144
145 switch (motionEvent.getAction()) {
146 case MotionEvent.ACTION_DOWN: {
147 if (DEBUG_TASK_POSITIONING) {
148 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
149 }
150 } break;
151
152 case MotionEvent.ACTION_MOVE: {
153 if (DEBUG_TASK_POSITIONING){
154 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
155 }
156 synchronized (mService.mWindowMap) {
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700157 mDragEnded = notifyMoveLocked(newX, newY);
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800158 mTask.getDimBounds(mTmpRect);
Chong Zhang8e89b312015-09-09 15:09:30 -0700159 }
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700160 if (!mTmpRect.equals(mWindowDragBounds)) {
161 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER,
162 "wm.TaskPositioner.resizeTask");
163 try {
164 mService.mActivityManager.resizeTask(
165 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
166 } catch (RemoteException e) {
167 }
168 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
169 }
Chong Zhang8e89b312015-09-09 15:09:30 -0700170 } break;
171
172 case MotionEvent.ACTION_UP: {
173 if (DEBUG_TASK_POSITIONING) {
174 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
175 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700176 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700177 } break;
178
179 case MotionEvent.ACTION_CANCEL: {
180 if (DEBUG_TASK_POSITIONING) {
181 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
182 }
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700183 mDragEnded = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700184 } break;
185 }
186
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700187 if (mDragEnded) {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700188 final boolean wasResizing = mResizing;
Chong Zhang3005e752015-09-18 18:46:28 -0700189 synchronized (mService.mWindowMap) {
190 endDragLocked();
191 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700192 try {
Wale Ogunwale04ad7b12015-10-02 12:43:27 -0700193 if (wasResizing) {
Chong Zhang3005e752015-09-18 18:46:28 -0700194 // We were using fullscreen surface during resizing. Request
195 // resizeTask() one last time to restore surface to window size.
196 mService.mActivityManager.resizeTask(
Chong Zhang6de2ae82015-09-30 18:25:21 -0700197 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700198 }
199
200 if (mCurrentDimSide != CTRL_NONE) {
201 final int createMode = mCurrentDimSide == CTRL_LEFT
202 ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
203 : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
204 mService.mActivityManager.moveTaskToDockedStack(
Jorim Jaggi9ea2f7b2015-11-23 18:08:28 -0800205 mTask.mTaskId, createMode, true /*toTop*/, true /* animate */,
206 null /* initialBounds */);
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()!
212 mService.mH.sendEmptyMessage(H.FINISH_TASK_POSITIONING);
213 }
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
223 TaskPositioner(WindowManagerService service) {
224 mService = service;
225 }
226
227 /**
228 * @param display The Display that the window being dragged is on.
229 */
230 void register(Display display) {
231 if (DEBUG_TASK_POSITIONING) {
232 Slog.d(TAG, "Registering task positioner");
233 }
234
235 if (mClientChannel != null) {
236 Slog.e(TAG, "Task positioner already registered");
237 return;
238 }
239
240 mDisplay = display;
241 mDisplay.getMetrics(mDisplayMetrics);
242 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
243 mServerChannel = channels[0];
244 mClientChannel = channels[1];
245 mService.mInputManager.registerInputChannel(mServerChannel, null);
246
Wale Ogunwale4f52bc62015-09-24 13:47:31 -0700247 mInputEventReceiver = new WindowPositionerEventReceiver(
248 mClientChannel, mService.mH.getLooper(), mService.mChoreographer);
Chong Zhang8e89b312015-09-09 15:09:30 -0700249
250 mDragApplicationHandle = new InputApplicationHandle(null);
251 mDragApplicationHandle.name = TAG;
252 mDragApplicationHandle.dispatchingTimeoutNanos =
253 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
254
255 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
256 mDisplay.getDisplayId());
257 mDragWindowHandle.name = TAG;
258 mDragWindowHandle.inputChannel = mServerChannel;
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700259 mDragWindowHandle.layer = mService.getDragLayerLocked();
Chong Zhang8e89b312015-09-09 15:09:30 -0700260 mDragWindowHandle.layoutParamsFlags = 0;
261 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
262 mDragWindowHandle.dispatchingTimeoutNanos =
263 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
264 mDragWindowHandle.visible = true;
265 mDragWindowHandle.canReceiveKeys = false;
266 mDragWindowHandle.hasFocus = true;
267 mDragWindowHandle.hasWallpaper = false;
268 mDragWindowHandle.paused = false;
269 mDragWindowHandle.ownerPid = Process.myPid();
270 mDragWindowHandle.ownerUid = Process.myUid();
271 mDragWindowHandle.inputFeatures = 0;
272 mDragWindowHandle.scaleFactor = 1.0f;
273
274 // The drag window cannot receive new touches.
275 mDragWindowHandle.touchableRegion.setEmpty();
276
277 // The drag window covers the entire display
278 mDragWindowHandle.frameLeft = 0;
279 mDragWindowHandle.frameTop = 0;
280 final Point p = new Point();
281 mDisplay.getRealSize(p);
282 mDragWindowHandle.frameRight = p.x;
283 mDragWindowHandle.frameBottom = p.y;
284
285 // Pause rotations before a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800286 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700287 Slog.d(TAG, "Pausing rotation during re-position");
288 }
289 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700290
Jorim Jaggibc5425c2016-03-01 13:51:16 +0100291 mDimLayer = new DimLayer(mService, this, mDisplay.getDisplayId(), TAG_LOCAL);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700292 mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
293 mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
294 mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700295
296 mDragEnded = false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700297 }
298
299 void unregister() {
300 if (DEBUG_TASK_POSITIONING) {
301 Slog.d(TAG, "Unregistering task positioner");
302 }
303
304 if (mClientChannel == null) {
305 Slog.e(TAG, "Task positioner not registered");
306 return;
307 }
308
309 mService.mInputManager.unregisterInputChannel(mServerChannel);
310
311 mInputEventReceiver.dispose();
312 mInputEventReceiver = null;
313 mClientChannel.dispose();
314 mServerChannel.dispose();
315 mClientChannel = null;
316 mServerChannel = null;
317
318 mDragWindowHandle = null;
319 mDragApplicationHandle = null;
320 mDisplay = null;
321
Wale Ogunwale228d4042015-09-13 10:17:34 -0700322 if (mDimLayer != null) {
323 mDimLayer.destroySurface();
324 mDimLayer = null;
325 }
326 mCurrentDimSide = CTRL_NONE;
Wale Ogunwale6a804b82015-09-23 21:04:21 -0700327 mDragEnded = true;
Wale Ogunwale228d4042015-09-13 10:17:34 -0700328
Chong Zhang8e89b312015-09-09 15:09:30 -0700329 // Resume rotations after a drag.
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800330 if (DEBUG_ORIENTATION) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700331 Slog.d(TAG, "Resuming rotation after re-position");
332 }
333 mService.resumeRotationLocked();
334 }
335
336 void startDragLocked(WindowState win, boolean resize, float startX, float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700337 if (DEBUG_TASK_POSITIONING) {
338 Slog.d(TAG, "startDragLocked: win=" + win + ", resize=" + resize
Chong Zhang8e89b312015-09-09 15:09:30 -0700339 + ", {" + startX + ", " + startY + "}");
340 }
341 mCtrlType = CTRL_NONE;
Chong Zhangd8ceb852015-11-11 14:53:41 -0800342 mTask = win.getTask();
343 mStartDragX = startX;
344 mStartDragY = startY;
345
Chong Zhangb15758a2015-11-17 12:12:03 -0800346 if (mTask.isDockedInEffect()) {
347 // If this is a docked task or if task size is affected by docked stack changing size,
348 // we can only be here if the task is not resizeable and we're handling a two-finger
349 // scrolling. Use the original task bounds to position the task, the dim bounds
350 // is cropped and doesn't move.
351 mTask.getBounds(mTmpRect);
352 } else {
353 // Use the dim bounds, not the original task bounds. The cursor
354 // movement should be calculated relative to the visible bounds.
355 // Also, use the dim bounds of the task which accounts for
356 // multiple app windows. Don't use any bounds from win itself as it
357 // may not be the same size as the task.
358 mTask.getDimBounds(mTmpRect);
359 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800360
Chong Zhang8e89b312015-09-09 15:09:30 -0700361 if (resize) {
Chong Zhangd8ceb852015-11-11 14:53:41 -0800362 if (startX < mTmpRect.left) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700363 mCtrlType |= CTRL_LEFT;
364 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800365 if (startX > mTmpRect.right) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700366 mCtrlType |= CTRL_RIGHT;
367 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800368 if (startY < mTmpRect.top) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700369 mCtrlType |= CTRL_TOP;
370 }
Chong Zhangd8ceb852015-11-11 14:53:41 -0800371 if (startY > mTmpRect.bottom) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700372 mCtrlType |= CTRL_BOTTOM;
373 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700374 mResizing = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700375 }
376
Chong Zhangd8ceb852015-11-11 14:53:41 -0800377 mWindowOriginalBounds.set(mTmpRect);
Chong Zhang3005e752015-09-18 18:46:28 -0700378 }
379
380 private void endDragLocked() {
381 mResizing = false;
382 mTask.setDragResizing(false);
Chong Zhang8e89b312015-09-09 15:09:30 -0700383 }
384
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700385 /** Returns true if the move operation should be ended. */
386 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700387 if (DEBUG_TASK_POSITIONING) {
Chong Zhangb15758a2015-11-17 12:12:03 -0800388 Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}");
Chong Zhang8e89b312015-09-09 15:09:30 -0700389 }
390
391 if (mCtrlType != CTRL_NONE) {
392 // This is a resizing operation.
393 final int deltaX = Math.round(x - mStartDragX);
394 final int deltaY = Math.round(y - mStartDragY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700395 int left = mWindowOriginalBounds.left;
396 int top = mWindowOriginalBounds.top;
397 int right = mWindowOriginalBounds.right;
398 int bottom = mWindowOriginalBounds.bottom;
399 if ((mCtrlType & CTRL_LEFT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700400 left = Math.min(left + deltaX, right - mMinVisibleWidth);
Chong Zhang8e89b312015-09-09 15:09:30 -0700401 }
402 if ((mCtrlType & CTRL_TOP) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700403 top = Math.min(top + deltaY, bottom - mMinVisibleHeight);
Chong Zhang8e89b312015-09-09 15:09:30 -0700404 }
405 if ((mCtrlType & CTRL_RIGHT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700406 right = Math.max(left + mMinVisibleWidth, right + deltaX);
Chong Zhang8e89b312015-09-09 15:09:30 -0700407 }
408 if ((mCtrlType & CTRL_BOTTOM) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700409 bottom = Math.max(top + mMinVisibleHeight, bottom + deltaY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700410 }
411 mWindowDragBounds.set(left, top, right, bottom);
Chong Zhang3005e752015-09-18 18:46:28 -0700412 mTask.setDragResizing(true);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700413 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700414 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700415
416 // This is a moving operation.
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800417 mTask.mStack.getDimBounds(mTmpRect);
Chong Zhangb15758a2015-11-17 12:12:03 -0800418
419 // If this is a non-resizeable task put into side-by-side mode, we are
420 // handling a two-finger scrolling action. No need to shrink the bounds.
421 if (!mTask.isDockedInEffect()) {
422 mTmpRect.inset(mMinVisibleWidth, mMinVisibleHeight);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700423 }
Chong Zhangb15758a2015-11-17 12:12:03 -0800424
425 boolean dragEnded = false;
426 final int nX = (int) x;
427 final int nY = (int) y;
428 if (!mTmpRect.contains(nX, nY)) {
429 // We end the moving operation if position is outside the stack bounds.
430 // In this case we need to clamp the position to stack bounds and calculate
431 // the final window drag bounds.
432 x = Math.min(Math.max(x, mTmpRect.left), mTmpRect.right);
433 y = Math.min(Math.max(y, mTmpRect.top), mTmpRect.bottom);
434 dragEnded = true;
435 }
436
437 updateWindowDragBounds(nX, nY);
438 updateDimLayerVisibility(nX);
439 return dragEnded;
440 }
441
442 private void updateWindowDragBounds(int x, int y) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700443 mWindowDragBounds.set(mWindowOriginalBounds);
Chong Zhangb15758a2015-11-17 12:12:03 -0800444 if (mTask.isDockedInEffect()) {
445 // Offset the bounds without clamp, the bounds will be shifted later
446 // by window manager before applying the scrolling.
447 if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) {
448 mWindowDragBounds.offset(Math.round(x - mStartDragX), 0);
449 } else {
450 mWindowDragBounds.offset(0, Math.round(y - mStartDragY));
451 }
452 } else {
453 mWindowDragBounds.offset(Math.round(x - mStartDragX), Math.round(y - mStartDragY));
454 }
455 if (DEBUG_TASK_POSITIONING) Slog.d(TAG,
456 "updateWindowDragBounds: " + mWindowDragBounds);
Chong Zhang8e89b312015-09-09 15:09:30 -0700457 }
458
Wale Ogunwale228d4042015-09-13 10:17:34 -0700459 private void updateDimLayerVisibility(int x) {
460 @CtrlType
461 int dimSide = getDimSide(x);
462 if (dimSide == mCurrentDimSide) {
463 return;
464 }
465
466 mCurrentDimSide = dimSide;
467
468 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION updateDimLayerVisibility");
469 SurfaceControl.openTransaction();
470 if (mCurrentDimSide == CTRL_NONE) {
471 mDimLayer.hide();
472 } else {
473 showDimLayer();
474 }
475 SurfaceControl.closeTransaction();
476 }
477
478 /**
479 * Returns the side of the screen the dim layer should be shown.
480 * @param x horizontal coordinate used to determine if the dim layer should be shown
481 * @return Returns {@link #CTRL_LEFT} if the dim layer should be shown on the left half of the
482 * screen, {@link #CTRL_RIGHT} if on the right side, or {@link #CTRL_NONE} if the dim layer
483 * shouldn't be shown.
484 */
485 private int getDimSide(int x) {
Chong Zhang3005e752015-09-18 18:46:28 -0700486 if (mTask.mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
487 || !mTask.mStack.isFullscreen()
Wale Ogunwale228d4042015-09-13 10:17:34 -0700488 || mService.mCurConfiguration.orientation != ORIENTATION_LANDSCAPE) {
489 return CTRL_NONE;
490 }
491
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800492 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700493 if (x - mSideMargin <= mTmpRect.left) {
494 return CTRL_LEFT;
495 }
496 if (x + mSideMargin >= mTmpRect.right) {
497 return CTRL_RIGHT;
498 }
499
500 return CTRL_NONE;
501 }
502
503 private void showDimLayer() {
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800504 mTask.mStack.getDimBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700505 if (mCurrentDimSide == CTRL_LEFT) {
506 mTmpRect.right = mTmpRect.centerX();
507 } else if (mCurrentDimSide == CTRL_RIGHT) {
508 mTmpRect.left = mTmpRect.centerX();
509 }
510
511 mDimLayer.setBounds(mTmpRect);
Filip Gruszczynski57b6cce2015-10-06 09:50:51 -0700512 mDimLayer.show(mService.getDragLayerLocked(), RESIZING_HINT_ALPHA,
513 RESIZING_HINT_DURATION_MS);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700514 }
515
516 @Override /** {@link DimLayer.DimLayerUser} */
517 public boolean isFullscreen() {
518 return false;
519 }
520
521 @Override /** {@link DimLayer.DimLayerUser} */
522 public DisplayInfo getDisplayInfo() {
Chong Zhang3005e752015-09-18 18:46:28 -0700523 return mTask.mStack.getDisplayInfo();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700524 }
525
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700526 @Override
Chong Zhang4c9ba52a2015-11-10 18:36:33 -0800527 public void getDimBounds(Rect out) {
Filip Gruszczynski0689ae92015-10-01 12:30:31 -0700528 // This dim layer user doesn't need this.
529 }
530
531 @Override
532 public String toShortString() {
533 return TAG;
534 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700535}