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