blob: a94be834897ecafc085046653de8491e9f00b0d3 [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;
Wale Ogunwale228d4042015-09-13 10:17:34 -070021import static android.app.ActivityManager.FREEFORM_WORKSPACE_STACK_ID;
Chong Zhang87b21722015-09-21 15:39:51 -070022import static android.app.ActivityManager.RESIZE_MODE_FORCED;
23import static android.app.ActivityManager.RESIZE_MODE_USER;
Wale Ogunwale228d4042015-09-13 10:17:34 -070024import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
Chong Zhang8e89b312015-09-09 15:09:30 -070025import static com.android.server.wm.WindowManagerService.DEBUG_TASK_POSITIONING;
Wale Ogunwale228d4042015-09-13 10:17:34 -070026import static com.android.server.wm.WindowManagerService.SHOW_TRANSACTIONS;
Wale Ogunwale231b06e2015-09-16 12:03:09 -070027import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP;
28import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP;
Chong Zhang8e89b312015-09-09 15:09:30 -070029
30import android.annotation.IntDef;
31import android.graphics.Point;
32import android.graphics.Rect;
33import android.os.Looper;
34import android.os.Process;
35import android.os.RemoteException;
36import android.util.DisplayMetrics;
37import android.util.Slog;
Chong Zhang8e89b312015-09-09 15:09:30 -070038import android.view.Display;
Wale Ogunwale228d4042015-09-13 10:17:34 -070039import android.view.DisplayInfo;
Chong Zhang8e89b312015-09-09 15:09:30 -070040import android.view.InputChannel;
41import android.view.InputDevice;
42import android.view.InputEvent;
43import android.view.InputEventReceiver;
44import android.view.MotionEvent;
Wale Ogunwale228d4042015-09-13 10:17:34 -070045import android.view.SurfaceControl;
Chong Zhang8e89b312015-09-09 15:09:30 -070046import android.view.WindowManager;
47
Wale Ogunwale228d4042015-09-13 10:17:34 -070048import com.android.server.input.InputApplicationHandle;
49import com.android.server.input.InputWindowHandle;
50import com.android.server.wm.WindowManagerService.H;
51
52import java.lang.annotation.Retention;
53import java.lang.annotation.RetentionPolicy;
54
55class TaskPositioner implements DimLayer.DimLayerUser {
Chong Zhang8e89b312015-09-09 15:09:30 -070056 private static final String TAG = "TaskPositioner";
57
Wale Ogunwale228d4042015-09-13 10:17:34 -070058 // The margin the pointer position has to be within the side of the screen to be
59 // considered at the side of the screen.
Wale Ogunwalef5a67f52015-09-15 09:54:33 -070060 private static final int SIDE_MARGIN_DIP = 100;
Wale Ogunwale228d4042015-09-13 10:17:34 -070061
Chong Zhang8e89b312015-09-09 15:09:30 -070062 @IntDef(flag = true,
63 value = {
64 CTRL_NONE,
65 CTRL_LEFT,
66 CTRL_RIGHT,
67 CTRL_TOP,
68 CTRL_BOTTOM
69 })
70 @Retention(RetentionPolicy.SOURCE)
71 @interface CtrlType {}
72
73 private static final int CTRL_NONE = 0x0;
74 private static final int CTRL_LEFT = 0x1;
75 private static final int CTRL_RIGHT = 0x2;
76 private static final int CTRL_TOP = 0x4;
77 private static final int CTRL_BOTTOM = 0x8;
78
79 private final WindowManagerService mService;
80 private WindowPositionerEventReceiver mInputEventReceiver;
81 private Display mDisplay;
82 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale228d4042015-09-13 10:17:34 -070083 private DimLayer mDimLayer;
84 @CtrlType
85 private int mCurrentDimSide;
86 private Rect mTmpRect = new Rect();
87 private int mSideMargin;
Wale Ogunwaleb8051b82015-09-17 15:41:52 -070088 private int mMinVisibleWidth;
89 private int mMinVisibleHeight;
Chong Zhang8e89b312015-09-09 15:09:30 -070090
Chong Zhang3005e752015-09-18 18:46:28 -070091 private Task mTask;
Chong Zhang09b21ef2015-09-14 10:20:21 -070092 private boolean mResizing;
Chong Zhang8e89b312015-09-09 15:09:30 -070093 private final Rect mWindowOriginalBounds = new Rect();
94 private final Rect mWindowDragBounds = new Rect();
95 private float mStartDragX;
96 private float mStartDragY;
97 @CtrlType
98 private int mCtrlType = CTRL_NONE;
99
100 InputChannel mServerChannel;
101 InputChannel mClientChannel;
102 InputApplicationHandle mDragApplicationHandle;
103 InputWindowHandle mDragWindowHandle;
104
105 private final class WindowPositionerEventReceiver extends InputEventReceiver {
106 public WindowPositionerEventReceiver(InputChannel inputChannel, Looper looper) {
107 super(inputChannel, looper);
108 }
109
110 @Override
111 public void onInputEvent(InputEvent event) {
112 if (!(event instanceof MotionEvent)
113 || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
114 return;
115 }
116 final MotionEvent motionEvent = (MotionEvent) event;
117 boolean handled = false;
118
119 try {
120 boolean endDrag = false;
121 final float newX = motionEvent.getRawX();
122 final float newY = motionEvent.getRawY();
123
124 switch (motionEvent.getAction()) {
125 case MotionEvent.ACTION_DOWN: {
126 if (DEBUG_TASK_POSITIONING) {
127 Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}");
128 }
129 } break;
130
131 case MotionEvent.ACTION_MOVE: {
132 if (DEBUG_TASK_POSITIONING){
133 Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}");
134 }
135 synchronized (mService.mWindowMap) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700136 endDrag = notifyMoveLocked(newX, newY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700137 }
138 try {
Chong Zhang09b21ef2015-09-14 10:20:21 -0700139 mService.mActivityManager.resizeTask(
Chong Zhang87b21722015-09-21 15:39:51 -0700140 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER);
Chong Zhang8e89b312015-09-09 15:09:30 -0700141 } catch(RemoteException e) {}
142 } break;
143
144 case MotionEvent.ACTION_UP: {
145 if (DEBUG_TASK_POSITIONING) {
146 Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}");
147 }
148 endDrag = true;
149 } break;
150
151 case MotionEvent.ACTION_CANCEL: {
152 if (DEBUG_TASK_POSITIONING) {
153 Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}");
154 }
155 endDrag = true;
156 } break;
157 }
158
159 if (endDrag) {
Chong Zhang3005e752015-09-18 18:46:28 -0700160 synchronized (mService.mWindowMap) {
161 endDragLocked();
162 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700163 try {
Chong Zhang3005e752015-09-18 18:46:28 -0700164 if (mResizing) {
165 // We were using fullscreen surface during resizing. Request
166 // resizeTask() one last time to restore surface to window size.
167 mService.mActivityManager.resizeTask(
Chong Zhang87b21722015-09-21 15:39:51 -0700168 mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_FORCED);
Chong Zhang3005e752015-09-18 18:46:28 -0700169 }
170
171 if (mCurrentDimSide != CTRL_NONE) {
172 final int createMode = mCurrentDimSide == CTRL_LEFT
173 ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
174 : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT;
175 mService.mActivityManager.moveTaskToDockedStack(
176 mTask.mTaskId, createMode, true /*toTop*/);
177 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700178 } catch(RemoteException e) {}
Chong Zhang3005e752015-09-18 18:46:28 -0700179
Chong Zhang8e89b312015-09-09 15:09:30 -0700180 // Post back to WM to handle clean-ups. We still need the input
181 // event handler for the last finishInputEvent()!
182 mService.mH.sendEmptyMessage(H.FINISH_TASK_POSITIONING);
183 }
184 handled = true;
185 } catch (Exception e) {
186 Slog.e(TAG, "Exception caught by drag handleMotion", e);
187 } finally {
188 finishInputEvent(event, handled);
189 }
190 }
191 }
192
193 TaskPositioner(WindowManagerService service) {
194 mService = service;
195 }
196
197 /**
198 * @param display The Display that the window being dragged is on.
199 */
200 void register(Display display) {
201 if (DEBUG_TASK_POSITIONING) {
202 Slog.d(TAG, "Registering task positioner");
203 }
204
205 if (mClientChannel != null) {
206 Slog.e(TAG, "Task positioner already registered");
207 return;
208 }
209
210 mDisplay = display;
211 mDisplay.getMetrics(mDisplayMetrics);
212 final InputChannel[] channels = InputChannel.openInputChannelPair(TAG);
213 mServerChannel = channels[0];
214 mClientChannel = channels[1];
215 mService.mInputManager.registerInputChannel(mServerChannel, null);
216
217 mInputEventReceiver = new WindowPositionerEventReceiver(mClientChannel,
218 mService.mH.getLooper());
219
220 mDragApplicationHandle = new InputApplicationHandle(null);
221 mDragApplicationHandle.name = TAG;
222 mDragApplicationHandle.dispatchingTimeoutNanos =
223 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
224
225 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
226 mDisplay.getDisplayId());
227 mDragWindowHandle.name = TAG;
228 mDragWindowHandle.inputChannel = mServerChannel;
229 mDragWindowHandle.layer = getDragLayerLocked();
230 mDragWindowHandle.layoutParamsFlags = 0;
231 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
232 mDragWindowHandle.dispatchingTimeoutNanos =
233 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
234 mDragWindowHandle.visible = true;
235 mDragWindowHandle.canReceiveKeys = false;
236 mDragWindowHandle.hasFocus = true;
237 mDragWindowHandle.hasWallpaper = false;
238 mDragWindowHandle.paused = false;
239 mDragWindowHandle.ownerPid = Process.myPid();
240 mDragWindowHandle.ownerUid = Process.myUid();
241 mDragWindowHandle.inputFeatures = 0;
242 mDragWindowHandle.scaleFactor = 1.0f;
243
244 // The drag window cannot receive new touches.
245 mDragWindowHandle.touchableRegion.setEmpty();
246
247 // The drag window covers the entire display
248 mDragWindowHandle.frameLeft = 0;
249 mDragWindowHandle.frameTop = 0;
250 final Point p = new Point();
251 mDisplay.getRealSize(p);
252 mDragWindowHandle.frameRight = p.x;
253 mDragWindowHandle.frameBottom = p.y;
254
255 // Pause rotations before a drag.
256 if (WindowManagerService.DEBUG_ORIENTATION) {
257 Slog.d(TAG, "Pausing rotation during re-position");
258 }
259 mService.pauseRotationLocked();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700260
261 mDimLayer = new DimLayer(mService, this, mDisplay.getDisplayId());
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700262 mSideMargin = mService.dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700263 mMinVisibleWidth = mService.dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics);
264 mMinVisibleHeight = mService.dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics);
Chong Zhang8e89b312015-09-09 15:09:30 -0700265 }
266
267 void unregister() {
268 if (DEBUG_TASK_POSITIONING) {
269 Slog.d(TAG, "Unregistering task positioner");
270 }
271
272 if (mClientChannel == null) {
273 Slog.e(TAG, "Task positioner not registered");
274 return;
275 }
276
277 mService.mInputManager.unregisterInputChannel(mServerChannel);
278
279 mInputEventReceiver.dispose();
280 mInputEventReceiver = null;
281 mClientChannel.dispose();
282 mServerChannel.dispose();
283 mClientChannel = null;
284 mServerChannel = null;
285
286 mDragWindowHandle = null;
287 mDragApplicationHandle = null;
288 mDisplay = null;
289
Wale Ogunwale228d4042015-09-13 10:17:34 -0700290 if (mDimLayer != null) {
291 mDimLayer.destroySurface();
292 mDimLayer = null;
293 }
294 mCurrentDimSide = CTRL_NONE;
295
Chong Zhang8e89b312015-09-09 15:09:30 -0700296 // Resume rotations after a drag.
297 if (WindowManagerService.DEBUG_ORIENTATION) {
298 Slog.d(TAG, "Resuming rotation after re-position");
299 }
300 mService.resumeRotationLocked();
301 }
302
303 void startDragLocked(WindowState win, boolean resize, float startX, float startY) {
Wale Ogunwale228d4042015-09-13 10:17:34 -0700304 if (DEBUG_TASK_POSITIONING) {
305 Slog.d(TAG, "startDragLocked: win=" + win + ", resize=" + resize
Chong Zhang8e89b312015-09-09 15:09:30 -0700306 + ", {" + startX + ", " + startY + "}");
307 }
308 mCtrlType = CTRL_NONE;
309 if (resize) {
310 final Rect visibleFrame = win.mVisibleFrame;
311 if (startX < visibleFrame.left) {
312 mCtrlType |= CTRL_LEFT;
313 }
314 if (startX > visibleFrame.right) {
315 mCtrlType |= CTRL_RIGHT;
316 }
317 if (startY < visibleFrame.top) {
318 mCtrlType |= CTRL_TOP;
319 }
320 if (startY > visibleFrame.bottom) {
321 mCtrlType |= CTRL_BOTTOM;
322 }
Chong Zhang09b21ef2015-09-14 10:20:21 -0700323 mResizing = true;
Chong Zhang8e89b312015-09-09 15:09:30 -0700324 }
325
Chong Zhang3005e752015-09-18 18:46:28 -0700326 mTask = win.getTask();
Chong Zhang8e89b312015-09-09 15:09:30 -0700327 mStartDragX = startX;
328 mStartDragY = startY;
329
Chong Zhang3005e752015-09-18 18:46:28 -0700330 mService.getTaskBounds(mTask.mTaskId, mWindowOriginalBounds);
331 }
332
333 private void endDragLocked() {
334 mResizing = false;
335 mTask.setDragResizing(false);
Chong Zhang8e89b312015-09-09 15:09:30 -0700336 }
337
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700338 /** Returns true if the move operation should be ended. */
339 private boolean notifyMoveLocked(float x, float y) {
Chong Zhang8e89b312015-09-09 15:09:30 -0700340 if (DEBUG_TASK_POSITIONING) {
341 Slog.d(TAG, "notifyMoveLw: {" + x + "," + y + "}");
342 }
343
344 if (mCtrlType != CTRL_NONE) {
345 // This is a resizing operation.
346 final int deltaX = Math.round(x - mStartDragX);
347 final int deltaY = Math.round(y - mStartDragY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700348 int left = mWindowOriginalBounds.left;
349 int top = mWindowOriginalBounds.top;
350 int right = mWindowOriginalBounds.right;
351 int bottom = mWindowOriginalBounds.bottom;
352 if ((mCtrlType & CTRL_LEFT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700353 left = Math.min(left + deltaX, right - mMinVisibleWidth);
Chong Zhang8e89b312015-09-09 15:09:30 -0700354 }
355 if ((mCtrlType & CTRL_TOP) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700356 top = Math.min(top + deltaY, bottom - mMinVisibleHeight);
Chong Zhang8e89b312015-09-09 15:09:30 -0700357 }
358 if ((mCtrlType & CTRL_RIGHT) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700359 right = Math.max(left + mMinVisibleWidth, right + deltaX);
Chong Zhang8e89b312015-09-09 15:09:30 -0700360 }
361 if ((mCtrlType & CTRL_BOTTOM) != 0) {
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700362 bottom = Math.max(top + mMinVisibleHeight, bottom + deltaY);
Chong Zhang8e89b312015-09-09 15:09:30 -0700363 }
364 mWindowDragBounds.set(left, top, right, bottom);
Chong Zhang3005e752015-09-18 18:46:28 -0700365 mTask.setDragResizing(true);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700366 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700367 }
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700368
369 // This is a moving operation.
Chong Zhang3005e752015-09-18 18:46:28 -0700370 mTask.mStack.getBounds(mTmpRect);
Wale Ogunwaleb8051b82015-09-17 15:41:52 -0700371 mTmpRect.inset(mMinVisibleWidth, mMinVisibleHeight);
372 if (!mTmpRect.contains((int) x, (int) y)) {
373 // We end the moving operation if position is outside the stack bounds.
374 return true;
375 }
376 mWindowDragBounds.set(mWindowOriginalBounds);
377 mWindowDragBounds.offset(Math.round(x - mStartDragX), Math.round(y - mStartDragY));
378 updateDimLayerVisibility((int) x);
379 return false;
Chong Zhang8e89b312015-09-09 15:09:30 -0700380 }
381
Wale Ogunwale228d4042015-09-13 10:17:34 -0700382 private void updateDimLayerVisibility(int x) {
383 @CtrlType
384 int dimSide = getDimSide(x);
385 if (dimSide == mCurrentDimSide) {
386 return;
387 }
388
389 mCurrentDimSide = dimSide;
390
391 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION updateDimLayerVisibility");
392 SurfaceControl.openTransaction();
393 if (mCurrentDimSide == CTRL_NONE) {
394 mDimLayer.hide();
395 } else {
396 showDimLayer();
397 }
398 SurfaceControl.closeTransaction();
399 }
400
401 /**
402 * Returns the side of the screen the dim layer should be shown.
403 * @param x horizontal coordinate used to determine if the dim layer should be shown
404 * @return Returns {@link #CTRL_LEFT} if the dim layer should be shown on the left half of the
405 * screen, {@link #CTRL_RIGHT} if on the right side, or {@link #CTRL_NONE} if the dim layer
406 * shouldn't be shown.
407 */
408 private int getDimSide(int x) {
Chong Zhang3005e752015-09-18 18:46:28 -0700409 if (mTask.mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID
410 || !mTask.mStack.isFullscreen()
Wale Ogunwale228d4042015-09-13 10:17:34 -0700411 || mService.mCurConfiguration.orientation != ORIENTATION_LANDSCAPE) {
412 return CTRL_NONE;
413 }
414
Chong Zhang3005e752015-09-18 18:46:28 -0700415 mTask.mStack.getBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700416 if (x - mSideMargin <= mTmpRect.left) {
417 return CTRL_LEFT;
418 }
419 if (x + mSideMargin >= mTmpRect.right) {
420 return CTRL_RIGHT;
421 }
422
423 return CTRL_NONE;
424 }
425
426 private void showDimLayer() {
Chong Zhang3005e752015-09-18 18:46:28 -0700427 mTask.mStack.getBounds(mTmpRect);
Wale Ogunwale228d4042015-09-13 10:17:34 -0700428 if (mCurrentDimSide == CTRL_LEFT) {
429 mTmpRect.right = mTmpRect.centerX();
430 } else if (mCurrentDimSide == CTRL_RIGHT) {
431 mTmpRect.left = mTmpRect.centerX();
432 }
433
434 mDimLayer.setBounds(mTmpRect);
435 mDimLayer.show(getDragLayerLocked(), 0.5f, 0);
436 }
437
438 @Override /** {@link DimLayer.DimLayerUser} */
439 public boolean isFullscreen() {
440 return false;
441 }
442
443 @Override /** {@link DimLayer.DimLayerUser} */
444 public DisplayInfo getDisplayInfo() {
Chong Zhang3005e752015-09-18 18:46:28 -0700445 return mTask.mStack.getDisplayInfo();
Wale Ogunwale228d4042015-09-13 10:17:34 -0700446 }
447
Chong Zhang8e89b312015-09-09 15:09:30 -0700448 private int getDragLayerLocked() {
449 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_DRAG)
450 * WindowManagerService.TYPE_LAYER_MULTIPLIER
451 + WindowManagerService.TYPE_LAYER_OFFSET;
452 }
Wale Ogunwale228d4042015-09-13 10:17:34 -0700453}