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 | |
Riddle Hsu | a68473c | 2020-12-03 15:57:36 +0800 | [diff] [blame] | 231 | mDragApplicationHandle = new InputApplicationHandle(new Binder(), TAG, |
| 232 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 233 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 234 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, |
| 235 | displayContent.getDisplayId()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 236 | mDragWindowHandle.name = TAG; |
Robert Carr | eadae82 | 2018-10-11 19:07:03 -0700 | [diff] [blame] | 237 | mDragWindowHandle.token = mServerChannel.getToken(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 238 | mDragWindowHandle.layoutParamsFlags = 0; |
| 239 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 240 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 241 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 242 | mDragWindowHandle.visible = true; |
| 243 | mDragWindowHandle.canReceiveKeys = false; |
| 244 | mDragWindowHandle.hasFocus = true; |
| 245 | mDragWindowHandle.hasWallpaper = false; |
| 246 | mDragWindowHandle.paused = false; |
| 247 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 248 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 249 | mDragWindowHandle.inputFeatures = 0; |
| 250 | mDragWindowHandle.scaleFactor = 1.0f; |
| 251 | |
| 252 | // The drag window cannot receive new touches. |
| 253 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 254 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 255 | // The drag window covers the entire display. |
| 256 | final Rect displayBounds = mTmpRect; |
| 257 | displayContent.getBounds(mTmpRect); |
| 258 | mDragWindowHandle.frameLeft = displayBounds.left; |
| 259 | mDragWindowHandle.frameTop = displayBounds.top; |
| 260 | mDragWindowHandle.frameRight = displayBounds.right; |
| 261 | mDragWindowHandle.frameBottom = displayBounds.bottom; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 262 | |
| 263 | // Pause rotations before a drag. |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 264 | ProtoLog.d(WM_DEBUG_ORIENTATION, "Pausing rotation during re-position"); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 265 | mDisplayContent.getDisplayRotation().pause(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 266 | |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 267 | // Notify InputMonitor to take mDragWindowHandle. |
arthurhung | da4c2d6 | 2020-04-21 14:38:54 +0800 | [diff] [blame] | 268 | mService.mTaskPositioningController.showInputSurface(win.getDisplayId()); |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 269 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 270 | final DisplayMetrics displayMetrics = displayContent.getDisplayMetrics(); |
| 271 | mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, displayMetrics); |
| 272 | mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, displayMetrics); |
| 273 | mMaxVisibleSize.set(displayBounds.width(), displayBounds.height()); |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 274 | |
| 275 | mDragEnded = false; |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 276 | |
| 277 | try { |
| 278 | mClientCallback = win.mClient.asBinder(); |
| 279 | mClientCallback.linkToDeath(this, 0 /* flags */); |
| 280 | } catch (RemoteException e) { |
| 281 | // The caller has died, so clean up TaskPositioningController. |
| 282 | mService.mTaskPositioningController.finishTaskPositioning(); |
| 283 | return; |
| 284 | } |
| 285 | mWindow = win; |
| 286 | mTask = win.getTask(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void unregister() { |
| 290 | if (DEBUG_TASK_POSITIONING) { |
| 291 | Slog.d(TAG, "Unregistering task positioner"); |
| 292 | } |
| 293 | |
| 294 | if (mClientChannel == null) { |
| 295 | Slog.e(TAG, "Task positioner not registered"); |
| 296 | return; |
| 297 | } |
| 298 | |
arthurhung | da4c2d6 | 2020-04-21 14:38:54 +0800 | [diff] [blame] | 299 | mService.mTaskPositioningController.hideInputSurface(mDisplayContent.getDisplayId()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 300 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
| 301 | |
| 302 | mInputEventReceiver.dispose(); |
| 303 | mInputEventReceiver = null; |
| 304 | mClientChannel.dispose(); |
| 305 | mServerChannel.dispose(); |
| 306 | mClientChannel = null; |
| 307 | mServerChannel = null; |
| 308 | |
| 309 | mDragWindowHandle = null; |
| 310 | mDragApplicationHandle = null; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 311 | mDragEnded = true; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 312 | |
Garfield Tan | 07544cd | 2018-09-12 16:16:54 -0700 | [diff] [blame] | 313 | // Notify InputMonitor to remove mDragWindowHandle. |
| 314 | mDisplayContent.getInputMonitor().updateInputWindowsLw(true /*force*/); |
| 315 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 316 | // Resume rotations after a drag. |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 317 | ProtoLog.d(WM_DEBUG_ORIENTATION, "Resuming rotation after re-position"); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 318 | mDisplayContent.getDisplayRotation().resume(); |
Riddle Hsu | 654a6f9 | 2018-07-13 22:59:36 +0800 | [diff] [blame] | 319 | mDisplayContent = null; |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 320 | if (mClientCallback != null) { |
| 321 | mClientCallback.unlinkToDeath(this, 0 /* flags */); |
| 322 | } |
| 323 | mWindow = null; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 326 | /** |
| 327 | * Starts moving or resizing the task. This method should be only called from |
| 328 | * {@link TaskPositioningController#startPositioningLocked} or unit tests. |
| 329 | */ |
| 330 | void startDrag(boolean resize, boolean preserveOrientation, float startX, float startY) { |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 331 | if (DEBUG_TASK_POSITIONING) { |
Yongjin Kwon | 9c5f0aa | 2019-11-06 17:36:24 +0900 | [diff] [blame] | 332 | Slog.d(TAG, "startDrag: win=" + mWindow + ", resize=" + resize |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 333 | + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", " |
| 334 | + startY + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 335 | } |
Evan Rosky | 0d654cb | 2019-02-26 10:59:10 -0800 | [diff] [blame] | 336 | // Use the bounds of the task which accounts for |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 337 | // multiple app windows. Don't use any bounds from win itself as it |
| 338 | // may not be the same size as the task. |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 339 | final Rect startBounds = mTmpRect; |
| 340 | mTask.getBounds(startBounds); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 341 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 342 | mCtrlType = CTRL_NONE; |
| 343 | mStartDragX = startX; |
| 344 | mStartDragY = startY; |
| 345 | mPreserveOrientation = preserveOrientation; |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 346 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 347 | if (resize) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 348 | if (startX < startBounds.left) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 349 | mCtrlType |= CTRL_LEFT; |
| 350 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 351 | if (startX > startBounds.right) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 352 | mCtrlType |= CTRL_RIGHT; |
| 353 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 354 | if (startY < startBounds.top) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 355 | mCtrlType |= CTRL_TOP; |
| 356 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 357 | if (startY > startBounds.bottom) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 358 | mCtrlType |= CTRL_BOTTOM; |
| 359 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 360 | mResizing = mCtrlType != CTRL_NONE; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 361 | } |
| 362 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 363 | // In case of !isDockedInEffect we are using the union of all task bounds. These might be |
| 364 | // made up out of multiple windows which are only partially overlapping. When that happens, |
| 365 | // the orientation from the window of interest to the entire stack might diverge. However |
| 366 | // for now we treat them as the same. |
| 367 | mStartOrientationWasLandscape = startBounds.width() >= startBounds.height(); |
| 368 | mWindowOriginalBounds.set(startBounds); |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 369 | |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 370 | // Notify the app that resizing has started, even though we haven't received any new |
| 371 | // bounds yet. This will guarantee that the app starts the backdrop renderer before |
| 372 | // configuration changes which could cause an activity restart. |
| 373 | if (mResizing) { |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 374 | notifyMoveLocked(startX, startY); |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 375 | |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 376 | // The WindowPositionerEventReceiver callbacks are delivered on the same handler so this |
| 377 | // initial resize is always guaranteed to happen before subsequent drag resizes. |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 378 | mService.mH.post(() -> { |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 379 | mService.mAtmService.resizeTask( |
| 380 | mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED); |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 381 | }); |
| 382 | } |
| 383 | |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 384 | // Make sure we always have valid drag bounds even if the drag ends before any move events |
| 385 | // have been handled. |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 386 | mWindowDragBounds.set(startBounds); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | private void endDragLocked() { |
| 390 | mResizing = false; |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 391 | mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 394 | /** Returns true if the move operation should be ended. */ |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 395 | @VisibleForTesting |
| 396 | boolean notifyMoveLocked(float x, float y) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 397 | if (DEBUG_TASK_POSITIONING) { |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 398 | Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (mCtrlType != CTRL_NONE) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 402 | resizeDrag(x, y); |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 403 | mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 404 | return false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 405 | } |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 406 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 407 | // This is a moving or scrolling operation. |
Riddle Hsu | 3ae6ebb | 2020-03-06 15:15:42 +0800 | [diff] [blame] | 408 | // Only allow to move in stable area so the target window won't be covered by system bar. |
| 409 | // Though {@link Task#resolveOverrideConfiguration} should also avoid the case. |
| 410 | mDisplayContent.getStableRect(mTmpRect); |
| 411 | // The task may be put in a limited display area. |
| 412 | mTmpRect.intersect(mTask.getRootTask().getParent().getBounds()); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 413 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 414 | int nX = (int) x; |
| 415 | int nY = (int) y; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 416 | if (!mTmpRect.contains(nX, nY)) { |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 417 | // For a moving operation we allow the pointer to go out of the stack bounds, but |
| 418 | // use the clamped pointer position for the drag bounds computation. |
| 419 | nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right); |
| 420 | nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 423 | updateWindowDragBounds(nX, nY, mTmpRect); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 424 | return false; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 425 | } |
| 426 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 427 | /** |
| 428 | * The user is drag - resizing the window. |
| 429 | * |
| 430 | * @param x The x coordinate of the current drag coordinate. |
| 431 | * @param y the y coordinate of the current drag coordinate. |
| 432 | */ |
| 433 | @VisibleForTesting |
| 434 | void resizeDrag(float x, float y) { |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 435 | updateDraggedBounds(TaskResizingAlgorithm.resizeDrag(x, y, mStartDragX, mStartDragY, |
| 436 | mWindowOriginalBounds, mCtrlType, mMinVisibleWidth, mMinVisibleHeight, |
| 437 | mMaxVisibleSize, mPreserveOrientation, mStartOrientationWasLandscape)); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Ben Lin | 2e306af | 2020-02-03 14:23:57 -0800 | [diff] [blame] | 440 | private void updateDraggedBounds(Rect newBounds) { |
| 441 | mWindowDragBounds.set(newBounds); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 442 | |
| 443 | checkBoundsForOrientationViolations(mWindowDragBounds); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set). |
| 448 | * |
| 449 | * @param bounds The bounds to be checked. |
| 450 | */ |
| 451 | private void checkBoundsForOrientationViolations(Rect bounds) { |
| 452 | // When using debug check that we are not violating the given constraints. |
| 453 | if (DEBUG_ORIENTATION_VIOLATIONS) { |
| 454 | if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) { |
| 455 | Slog.e(TAG, "Orientation violation detected! should be " |
| 456 | + (mStartOrientationWasLandscape ? "landscape" : "portrait") |
| 457 | + " but is the other"); |
| 458 | } else { |
| 459 | Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height()); |
| 460 | } |
| 461 | if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) { |
| 462 | Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth |
| 463 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 464 | + mMinVisibleHeight + ", " + bounds.height() + ")"); |
| 465 | } |
| 466 | if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) { |
| 467 | Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x |
| 468 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 469 | + mMaxVisibleSize.y + ", " + bounds.height() + ")"); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 474 | private void updateWindowDragBounds(int x, int y, Rect stackBounds) { |
| 475 | final int offsetX = Math.round(x - mStartDragX); |
| 476 | final int offsetY = Math.round(y - mStartDragY); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 477 | mWindowDragBounds.set(mWindowOriginalBounds); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 478 | // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible. |
| 479 | final int maxLeft = stackBounds.right - mMinVisibleWidth; |
| 480 | final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width(); |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 481 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 482 | // Vertically, the top mMinVisibleHeight of the window should remain visible. |
| 483 | // (This assumes that the window caption bar is at the top of the window). |
| 484 | final int minTop = stackBounds.top; |
| 485 | final int maxTop = stackBounds.bottom - mMinVisibleHeight; |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 486 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 487 | mWindowDragBounds.offsetTo( |
| 488 | Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft), |
| 489 | Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop)); |
| 490 | |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 491 | if (DEBUG_TASK_POSITIONING) Slog.d(TAG, |
| 492 | "updateWindowDragBounds: " + mWindowDragBounds); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 495 | public String toShortString() { |
| 496 | return TAG; |
| 497 | } |
Garfield Tan | 6caf1d8c | 2018-01-18 12:37:50 -0800 | [diff] [blame] | 498 | |
| 499 | static void setFactory(Factory factory) { |
| 500 | sFactory = factory; |
| 501 | } |
| 502 | |
| 503 | static TaskPositioner create(WindowManagerService service) { |
| 504 | if (sFactory == null) { |
| 505 | sFactory = new Factory() {}; |
| 506 | } |
| 507 | |
| 508 | return sFactory.create(service); |
| 509 | } |
| 510 | |
yj81.kwon | 79c9767 | 2019-04-16 19:44:48 -0700 | [diff] [blame] | 511 | @Override |
| 512 | public void binderDied() { |
| 513 | mService.mTaskPositioningController.finishTaskPositioning(); |
| 514 | } |
| 515 | |
Garfield Tan | 6caf1d8c | 2018-01-18 12:37:50 -0800 | [diff] [blame] | 516 | interface Factory { |
| 517 | default TaskPositioner create(WindowManagerService service) { |
| 518 | return new TaskPositioner(service); |
| 519 | } |
| 520 | } |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 521 | } |