Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 19 | import static android.app.ActivityTaskManager.RESIZE_MODE_USER; |
| 20 | import static android.app.ActivityTaskManager.RESIZE_MODE_USER_FORCED; |
Wale Ogunwale | cad05a0 | 2015-09-25 10:41:44 -0700 | [diff] [blame] | 21 | import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER; |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 22 | |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 23 | import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_BOTTOM; |
| 24 | import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_LEFT; |
| 25 | import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_NONE; |
| 26 | import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_RIGHT; |
| 27 | import static com.android.internal.policy.TaskResizingAlgorithm.CTRL_TOP; |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 28 | import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 29 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 30 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 31 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 32 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 33 | import static com.android.server.wm.WindowManagerService.dipToPixel; |
Wale Ogunwale | 231b06e | 2015-09-16 12:03:09 -0700 | [diff] [blame] | 34 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP; |
| 35 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 36 | |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 37 | import android.annotation.NonNull; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 38 | import android.graphics.Point; |
| 39 | import android.graphics.Rect; |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 40 | import android.os.Binder; |
yj81.kwon | 79c9767 | 2019-04-16 19:44:48 -0700 | [diff] [blame] | 41 | import android.os.IBinder; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 42 | import android.os.Looper; |
| 43 | import android.os.Process; |
| 44 | import android.os.RemoteException; |
Wale Ogunwale | cad05a0 | 2015-09-25 10:41:44 -0700 | [diff] [blame] | 45 | import android.os.Trace; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 46 | import android.util.DisplayMetrics; |
| 47 | import android.util.Slog; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 48 | import android.view.BatchedInputEventReceiver; |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 49 | import android.view.Choreographer; |
Evan Rosky | 0d654cb | 2019-02-26 10:59:10 -0800 | [diff] [blame] | 50 | import android.view.InputApplicationHandle; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 51 | import android.view.InputChannel; |
| 52 | import android.view.InputDevice; |
| 53 | import android.view.InputEvent; |
Evan Rosky | 0d654cb | 2019-02-26 10:59:10 -0800 | [diff] [blame] | 54 | import android.view.InputWindowHandle; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 55 | import android.view.MotionEvent; |
Arthur Hung | b62e500 | 2019-08-29 12:28:07 +0800 | [diff] [blame] | 56 | import android.view.SurfaceControl; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 57 | import android.view.WindowManager; |
| 58 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 59 | import com.android.internal.annotations.VisibleForTesting; |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 60 | import com.android.internal.policy.TaskResizingAlgorithm; |
| 61 | import com.android.internal.policy.TaskResizingAlgorithm.CtrlType; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 62 | import com.android.server.protolog.common.ProtoLog; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 63 | |
yj81.kwon | 79c9767 | 2019-04-16 19:44:48 -0700 | [diff] [blame] | 64 | class TaskPositioner implements IBinder.DeathRecipient { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 65 | private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false; |
Jorim Jaggi | bc5425c | 2016-03-01 13:51:16 +0100 | [diff] [blame] | 66 | private static final String TAG_LOCAL = "TaskPositioner"; |
| 67 | private static final String TAG = TAG_WITH_CLASS_NAME ? TAG_LOCAL : TAG_WM; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 68 | |
Garfield Tan | 6caf1d8c | 2018-01-18 12:37:50 -0800 | [diff] [blame] | 69 | private static Factory sFactory; |
| 70 | |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 71 | public static final float RESIZING_HINT_ALPHA = 0.5f; |
| 72 | |
| 73 | public static final int RESIZING_HINT_DURATION_MS = 0; |
| 74 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 75 | private final WindowManagerService mService; |
| 76 | private WindowPositionerEventReceiver mInputEventReceiver; |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 77 | private DisplayContent mDisplayContent; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 78 | private Rect mTmpRect = new Rect(); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 79 | private int mMinVisibleWidth; |
| 80 | private int mMinVisibleHeight; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 81 | |
Charles Chen | 2cb5a0f | 2019-03-22 20:58:00 +0800 | [diff] [blame] | 82 | @VisibleForTesting |
| 83 | Task mTask; |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 84 | WindowState mWindow; |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 85 | private boolean mResizing; |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 86 | private boolean mPreserveOrientation; |
| 87 | private boolean mStartOrientationWasLandscape; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 88 | private final Rect mWindowOriginalBounds = new Rect(); |
| 89 | private final Rect mWindowDragBounds = new Rect(); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 90 | private final Point mMaxVisibleSize = new Point(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 91 | private float mStartDragX; |
| 92 | private float mStartDragY; |
| 93 | @CtrlType |
| 94 | private int mCtrlType = CTRL_NONE; |
yj81.kwon | 79c9767 | 2019-04-16 19:44:48 -0700 | [diff] [blame] | 95 | @VisibleForTesting |
| 96 | boolean mDragEnded; |
yj81.kwon | 19585ff | 2019-04-23 18:53:57 -0700 | [diff] [blame] | 97 | IBinder mClientCallback; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 98 | |
| 99 | InputChannel mServerChannel; |
| 100 | InputChannel mClientChannel; |
| 101 | InputApplicationHandle mDragApplicationHandle; |
| 102 | InputWindowHandle mDragWindowHandle; |
| 103 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 104 | private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver { |
| 105 | public WindowPositionerEventReceiver( |
| 106 | InputChannel inputChannel, Looper looper, Choreographer choreographer) { |
| 107 | super(inputChannel, looper, choreographer); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 111 | public void onInputEvent(InputEvent event) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 112 | boolean handled = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 113 | try { |
Yunfan Chen | 54872c3 | 2019-11-14 19:41:47 -0800 | [diff] [blame] | 114 | // All returns need to be in the try block to make sure the finishInputEvent is |
| 115 | // called correctly. |
| 116 | if (!(event instanceof MotionEvent) |
| 117 | || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) { |
| 118 | return; |
| 119 | } |
| 120 | final MotionEvent motionEvent = (MotionEvent) event; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 121 | if (mDragEnded) { |
| 122 | // The drag has ended but the clean-up message has not been processed by |
| 123 | // window manager. Drop events that occur after this until window manager |
| 124 | // has a chance to clean-up the input handle. |
| 125 | handled = true; |
| 126 | return; |
| 127 | } |
| 128 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 129 | final float newX = motionEvent.getRawX(); |
| 130 | final float newY = motionEvent.getRawY(); |
| 131 | |
| 132 | switch (motionEvent.getAction()) { |
| 133 | case MotionEvent.ACTION_DOWN: { |
| 134 | if (DEBUG_TASK_POSITIONING) { |
| 135 | Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}"); |
| 136 | } |
| 137 | } break; |
| 138 | |
| 139 | case MotionEvent.ACTION_MOVE: { |
| 140 | if (DEBUG_TASK_POSITIONING){ |
| 141 | Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}"); |
| 142 | } |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 143 | synchronized (mService.mGlobalLock) { |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 144 | mDragEnded = notifyMoveLocked(newX, newY); |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 145 | mTask.getDimBounds(mTmpRect); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 146 | } |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 147 | if (!mTmpRect.equals(mWindowDragBounds)) { |
| 148 | Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, |
| 149 | "wm.TaskPositioner.resizeTask"); |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 150 | mService.mAtmService.resizeTask( |
| 151 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER); |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 152 | Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER); |
| 153 | } |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 154 | } break; |
| 155 | |
| 156 | case MotionEvent.ACTION_UP: { |
| 157 | if (DEBUG_TASK_POSITIONING) { |
| 158 | Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}"); |
| 159 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 160 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 161 | } break; |
| 162 | |
| 163 | case MotionEvent.ACTION_CANCEL: { |
| 164 | if (DEBUG_TASK_POSITIONING) { |
| 165 | Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}"); |
| 166 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 167 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 168 | } break; |
| 169 | } |
| 170 | |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 171 | if (mDragEnded) { |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 172 | final boolean wasResizing = mResizing; |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 173 | synchronized (mService.mGlobalLock) { |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 174 | endDragLocked(); |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 175 | mTask.getDimBounds(mTmpRect); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 176 | } |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 177 | if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) { |
| 178 | // We were using fullscreen surface during resizing. Request |
| 179 | // resizeTask() one last time to restore surface to window size. |
| 180 | mService.mAtmService.resizeTask( |
| 181 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED); |
| 182 | } |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 183 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 184 | // Post back to WM to handle clean-ups. We still need the input |
| 185 | // event handler for the last finishInputEvent()! |
Daichi Hirono | ce2f97a | 2017-11-30 16:44:15 +0900 | [diff] [blame] | 186 | mService.mTaskPositioningController.finishTaskPositioning(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 187 | } |
| 188 | handled = true; |
| 189 | } catch (Exception e) { |
| 190 | Slog.e(TAG, "Exception caught by drag handleMotion", e); |
| 191 | } finally { |
| 192 | finishInputEvent(event, handled); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 197 | /** Use {@link #create(WindowManagerService)} instead. */ |
Daichi Hirono | 67f2f4b | 2018-05-25 16:07:30 +0900 | [diff] [blame] | 198 | @VisibleForTesting |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 199 | TaskPositioner(WindowManagerService service) { |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 200 | mService = service; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 201 | } |
| 202 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 203 | @VisibleForTesting |
| 204 | Rect getWindowDragBounds() { |
| 205 | return mWindowDragBounds; |
| 206 | } |
| 207 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 208 | /** |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 209 | * @param displayContent The Display that the window being dragged is on. |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 210 | * @param win The window which will be dragged. |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 211 | */ |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 212 | void register(DisplayContent displayContent, @NonNull WindowState win) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 213 | if (DEBUG_TASK_POSITIONING) { |
| 214 | Slog.d(TAG, "Registering task positioner"); |
| 215 | } |
| 216 | |
| 217 | if (mClientChannel != null) { |
| 218 | Slog.e(TAG, "Task positioner already registered"); |
| 219 | return; |
| 220 | } |
| 221 | |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 222 | mDisplayContent = displayContent; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 223 | final InputChannel[] channels = InputChannel.openInputChannelPair(TAG); |
| 224 | mServerChannel = channels[0]; |
| 225 | mClientChannel = channels[1]; |
Vishnu Nair | 1878216 | 2019-10-08 14:57:16 -0700 | [diff] [blame] | 226 | mService.mInputManager.registerInputChannel(mServerChannel); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 227 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 228 | mInputEventReceiver = new WindowPositionerEventReceiver( |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 229 | mClientChannel, mService.mAnimationHandler.getLooper(), |
| 230 | mService.mAnimator.getChoreographer()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 231 | |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 232 | mDragApplicationHandle = new InputApplicationHandle(new Binder()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 233 | mDragApplicationHandle.name = TAG; |
| 234 | mDragApplicationHandle.dispatchingTimeoutNanos = |
| 235 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 236 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 237 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, |
| 238 | displayContent.getDisplayId()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 239 | mDragWindowHandle.name = TAG; |
Robert Carr | eadae82 | 2018-10-11 19:07:03 -0700 | [diff] [blame] | 240 | mDragWindowHandle.token = mServerChannel.getToken(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 241 | mDragWindowHandle.layoutParamsFlags = 0; |
| 242 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 243 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 244 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 245 | mDragWindowHandle.visible = true; |
| 246 | mDragWindowHandle.canReceiveKeys = false; |
| 247 | mDragWindowHandle.hasFocus = true; |
| 248 | mDragWindowHandle.hasWallpaper = false; |
| 249 | mDragWindowHandle.paused = false; |
| 250 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 251 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 252 | mDragWindowHandle.inputFeatures = 0; |
| 253 | mDragWindowHandle.scaleFactor = 1.0f; |
| 254 | |
| 255 | // The drag window cannot receive new touches. |
| 256 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 257 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 258 | // The drag window covers the entire display. |
| 259 | final Rect displayBounds = mTmpRect; |
| 260 | displayContent.getBounds(mTmpRect); |
| 261 | mDragWindowHandle.frameLeft = displayBounds.left; |
| 262 | mDragWindowHandle.frameTop = displayBounds.top; |
| 263 | mDragWindowHandle.frameRight = displayBounds.right; |
| 264 | mDragWindowHandle.frameBottom = displayBounds.bottom; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 265 | |
| 266 | // Pause rotations before a drag. |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 267 | ProtoLog.d(WM_DEBUG_ORIENTATION, "Pausing rotation during re-position"); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 268 | mDisplayContent.getDisplayRotation().pause(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 269 | |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 270 | // Notify InputMonitor to take mDragWindowHandle. |
Arthur Hung | b62e500 | 2019-08-29 12:28:07 +0800 | [diff] [blame] | 271 | mDisplayContent.getInputMonitor().updateInputWindowsImmediately(); |
| 272 | new SurfaceControl.Transaction().syncInputWindows().apply(true); |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 273 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 274 | final DisplayMetrics displayMetrics = displayContent.getDisplayMetrics(); |
| 275 | mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, displayMetrics); |
| 276 | mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, displayMetrics); |
| 277 | mMaxVisibleSize.set(displayBounds.width(), displayBounds.height()); |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 278 | |
| 279 | mDragEnded = false; |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 280 | |
| 281 | try { |
| 282 | mClientCallback = win.mClient.asBinder(); |
| 283 | mClientCallback.linkToDeath(this, 0 /* flags */); |
| 284 | } catch (RemoteException e) { |
| 285 | // The caller has died, so clean up TaskPositioningController. |
| 286 | mService.mTaskPositioningController.finishTaskPositioning(); |
| 287 | return; |
| 288 | } |
| 289 | mWindow = win; |
| 290 | mTask = win.getTask(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void unregister() { |
| 294 | if (DEBUG_TASK_POSITIONING) { |
| 295 | Slog.d(TAG, "Unregistering task positioner"); |
| 296 | } |
| 297 | |
| 298 | if (mClientChannel == null) { |
| 299 | Slog.e(TAG, "Task positioner not registered"); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
| 304 | |
| 305 | mInputEventReceiver.dispose(); |
| 306 | mInputEventReceiver = null; |
| 307 | mClientChannel.dispose(); |
| 308 | mServerChannel.dispose(); |
| 309 | mClientChannel = null; |
| 310 | mServerChannel = null; |
| 311 | |
| 312 | mDragWindowHandle = null; |
| 313 | mDragApplicationHandle = null; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 314 | mDragEnded = true; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 315 | |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 316 | // Notify InputMonitor to remove mDragWindowHandle. |
| 317 | mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/); |
| 318 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 319 | // Resume rotations after a drag. |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 320 | ProtoLog.d(WM_DEBUG_ORIENTATION, "Resuming rotation after re-position"); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 321 | mDisplayContent.getDisplayRotation().resume(); |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 322 | mDisplayContent = null; |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 323 | if (mClientCallback != null) { |
| 324 | mClientCallback.unlinkToDeath(this, 0 /* flags */); |
| 325 | } |
| 326 | mWindow = null; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 329 | /** |
| 330 | * Starts moving or resizing the task. This method should be only called from |
| 331 | * {@link TaskPositioningController#startPositioningLocked} or unit tests. |
| 332 | */ |
| 333 | void startDrag(boolean resize, boolean preserveOrientation, float startX, float startY) { |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 334 | if (DEBUG_TASK_POSITIONING) { |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 335 | Slog.d(TAG, "startDrag: win=" + mWindow + ", resize=" + resize |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 336 | + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", " |
| 337 | + startY + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 338 | } |
Evan Rosky | 0d654cb | 2019-02-26 10:59:10 -0800 | [diff] [blame] | 339 | // Use the bounds of the task which accounts for |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 340 | // multiple app windows. Don't use any bounds from win itself as it |
| 341 | // may not be the same size as the task. |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 342 | final Rect startBounds = mTmpRect; |
| 343 | mTask.getBounds(startBounds); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 344 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 345 | mCtrlType = CTRL_NONE; |
| 346 | mStartDragX = startX; |
| 347 | mStartDragY = startY; |
| 348 | mPreserveOrientation = preserveOrientation; |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 349 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 350 | if (resize) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 351 | if (startX < startBounds.left) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 352 | mCtrlType |= CTRL_LEFT; |
| 353 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 354 | if (startX > startBounds.right) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 355 | mCtrlType |= CTRL_RIGHT; |
| 356 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 357 | if (startY < startBounds.top) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 358 | mCtrlType |= CTRL_TOP; |
| 359 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 360 | if (startY > startBounds.bottom) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 361 | mCtrlType |= CTRL_BOTTOM; |
| 362 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 363 | mResizing = mCtrlType != CTRL_NONE; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 364 | } |
| 365 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 366 | // In case of !isDockedInEffect we are using the union of all task bounds. These might be |
| 367 | // made up out of multiple windows which are only partially overlapping. When that happens, |
| 368 | // the orientation from the window of interest to the entire stack might diverge. However |
| 369 | // for now we treat them as the same. |
| 370 | mStartOrientationWasLandscape = startBounds.width() >= startBounds.height(); |
| 371 | mWindowOriginalBounds.set(startBounds); |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 372 | |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 373 | // Notify the app that resizing has started, even though we haven't received any new |
| 374 | // bounds yet. This will guarantee that the app starts the backdrop renderer before |
| 375 | // configuration changes which could cause an activity restart. |
| 376 | if (mResizing) { |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 377 | notifyMoveLocked(startX, startY); |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 378 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 379 | // The WindowPositionerEventReceiver callbacks are delivered on the same handler so this |
| 380 | // initial resize is always guaranteed to happen before subsequent drag resizes. |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 381 | mService.mH.post(() -> { |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 382 | mService.mAtmService.resizeTask( |
| 383 | mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED); |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 384 | }); |
| 385 | } |
| 386 | |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 387 | // Make sure we always have valid drag bounds even if the drag ends before any move events |
| 388 | // have been handled. |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 389 | mWindowDragBounds.set(startBounds); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private void endDragLocked() { |
| 393 | mResizing = false; |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 394 | mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 397 | /** Returns true if the move operation should be ended. */ |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 398 | @VisibleForTesting |
| 399 | boolean notifyMoveLocked(float x, float y) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 400 | if (DEBUG_TASK_POSITIONING) { |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 401 | Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if (mCtrlType != CTRL_NONE) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 405 | resizeDrag(x, y); |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 406 | mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 407 | return false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 408 | } |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 409 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 410 | // This is a moving or scrolling operation. |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame^] | 411 | // Only allow to move in stable area so the target window won't be covered by system bar. |
| 412 | // Though {@link Task#resolveOverrideConfiguration} should also avoid the case. |
| 413 | mDisplayContent.getStableRect(mTmpRect); |
| 414 | // The task may be put in a limited display area. |
| 415 | mTmpRect.intersect(mTask.getRootTask().getParent().getBounds()); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 416 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 417 | int nX = (int) x; |
| 418 | int nY = (int) y; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 419 | if (!mTmpRect.contains(nX, nY)) { |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 420 | // For a moving operation we allow the pointer to go out of the stack bounds, but |
| 421 | // use the clamped pointer position for the drag bounds computation. |
| 422 | nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right); |
| 423 | nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 426 | updateWindowDragBounds(nX, nY, mTmpRect); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 427 | return false; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 428 | } |
| 429 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 430 | /** |
| 431 | * The user is drag - resizing the window. |
| 432 | * |
| 433 | * @param x The x coordinate of the current drag coordinate. |
| 434 | * @param y the y coordinate of the current drag coordinate. |
| 435 | */ |
| 436 | @VisibleForTesting |
| 437 | void resizeDrag(float x, float y) { |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 438 | updateDraggedBounds(TaskResizingAlgorithm.resizeDrag(x, y, mStartDragX, mStartDragY, |
| 439 | mWindowOriginalBounds, mCtrlType, mMinVisibleWidth, mMinVisibleHeight, |
| 440 | mMaxVisibleSize, mPreserveOrientation, mStartOrientationWasLandscape)); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 443 | private void updateDraggedBounds(Rect newBounds) { |
| 444 | mWindowDragBounds.set(newBounds); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 445 | |
| 446 | checkBoundsForOrientationViolations(mWindowDragBounds); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set). |
| 451 | * |
| 452 | * @param bounds The bounds to be checked. |
| 453 | */ |
| 454 | private void checkBoundsForOrientationViolations(Rect bounds) { |
| 455 | // When using debug check that we are not violating the given constraints. |
| 456 | if (DEBUG_ORIENTATION_VIOLATIONS) { |
| 457 | if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) { |
| 458 | Slog.e(TAG, "Orientation violation detected! should be " |
| 459 | + (mStartOrientationWasLandscape ? "landscape" : "portrait") |
| 460 | + " but is the other"); |
| 461 | } else { |
| 462 | Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height()); |
| 463 | } |
| 464 | if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) { |
| 465 | Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth |
| 466 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 467 | + mMinVisibleHeight + ", " + bounds.height() + ")"); |
| 468 | } |
| 469 | if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) { |
| 470 | Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x |
| 471 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 472 | + mMaxVisibleSize.y + ", " + bounds.height() + ")"); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 477 | private void updateWindowDragBounds(int x, int y, Rect stackBounds) { |
| 478 | final int offsetX = Math.round(x - mStartDragX); |
| 479 | final int offsetY = Math.round(y - mStartDragY); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 480 | mWindowDragBounds.set(mWindowOriginalBounds); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 481 | // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible. |
| 482 | final int maxLeft = stackBounds.right - mMinVisibleWidth; |
| 483 | final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width(); |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 484 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 485 | // Vertically, the top mMinVisibleHeight of the window should remain visible. |
| 486 | // (This assumes that the window caption bar is at the top of the window). |
| 487 | final int minTop = stackBounds.top; |
| 488 | final int maxTop = stackBounds.bottom - mMinVisibleHeight; |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 489 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 490 | mWindowDragBounds.offsetTo( |
| 491 | Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft), |
| 492 | Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop)); |
| 493 | |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 494 | if (DEBUG_TASK_POSITIONING) Slog.d(TAG, |
| 495 | "updateWindowDragBounds: " + mWindowDragBounds); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 498 | public String toShortString() { |
| 499 | return TAG; |
| 500 | } |
Garfield Tan | 6caf1d8c | 2018-01-18 12:37:50 -0800 | [diff] [blame] | 501 | |
| 502 | static void setFactory(Factory factory) { |
| 503 | sFactory = factory; |
| 504 | } |
| 505 | |
| 506 | static TaskPositioner create(WindowManagerService service) { |
| 507 | if (sFactory == null) { |
| 508 | sFactory = new Factory() {}; |
| 509 | } |
| 510 | |
| 511 | return sFactory.create(service); |
| 512 | } |
| 513 | |
yj81.kwon | 79c9767 | 2019-04-16 19:44:48 -0700 | [diff] [blame] | 514 | @Override |
| 515 | public void binderDied() { |
| 516 | mService.mTaskPositioningController.finishTaskPositioning(); |
| 517 | } |
| 518 | |
Garfield Tan | 6caf1d8c | 2018-01-18 12:37:50 -0800 | [diff] [blame] | 519 | interface Factory { |
| 520 | default TaskPositioner create(WindowManagerService service) { |
| 521 | return new TaskPositioner(service); |
| 522 | } |
| 523 | } |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 524 | } |