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 | |
Chong Zhang | 87b2172 | 2015-09-21 15:39:51 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.RESIZE_MODE_USER; |
Chong Zhang | 6de2ae8 | 2015-09-30 18:25:21 -0700 | [diff] [blame] | 20 | import static android.app.ActivityManager.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; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TASK_POSITIONING; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 24 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 25 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 26 | import static com.android.server.wm.WindowManagerService.dipToPixel; |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 27 | import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_FREEFORM; |
Wale Ogunwale | 231b06e | 2015-09-16 12:03:09 -0700 | [diff] [blame] | 28 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP; |
| 29 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 30 | |
| 31 | import android.annotation.IntDef; |
| 32 | import android.graphics.Point; |
| 33 | import android.graphics.Rect; |
| 34 | import android.os.Looper; |
| 35 | import android.os.Process; |
| 36 | import android.os.RemoteException; |
Wale Ogunwale | cad05a0 | 2015-09-25 10:41:44 -0700 | [diff] [blame] | 37 | import android.os.Trace; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 38 | import android.util.DisplayMetrics; |
| 39 | import android.util.Slog; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 40 | import android.view.BatchedInputEventReceiver; |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 41 | import android.view.Choreographer; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 42 | import android.view.Display; |
| 43 | import android.view.InputChannel; |
| 44 | import android.view.InputDevice; |
| 45 | import android.view.InputEvent; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 46 | import android.view.MotionEvent; |
| 47 | import android.view.WindowManager; |
| 48 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 49 | import com.android.internal.annotations.VisibleForTesting; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 50 | import com.android.server.input.InputApplicationHandle; |
| 51 | import com.android.server.input.InputWindowHandle; |
| 52 | import com.android.server.wm.WindowManagerService.H; |
| 53 | |
| 54 | import java.lang.annotation.Retention; |
| 55 | import java.lang.annotation.RetentionPolicy; |
| 56 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 57 | class TaskPositioner { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 58 | private static final boolean DEBUG_ORIENTATION_VIOLATIONS = false; |
Jorim Jaggi | bc5425c | 2016-03-01 13:51:16 +0100 | [diff] [blame] | 59 | private static final String TAG_LOCAL = "TaskPositioner"; |
| 60 | 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] | 61 | |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 62 | // The margin the pointer position has to be within the side of the screen to be |
| 63 | // considered at the side of the screen. |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 64 | static final int SIDE_MARGIN_DIP = 100; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 65 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 66 | @IntDef(flag = true, |
| 67 | value = { |
| 68 | CTRL_NONE, |
| 69 | CTRL_LEFT, |
| 70 | CTRL_RIGHT, |
| 71 | CTRL_TOP, |
| 72 | CTRL_BOTTOM |
| 73 | }) |
| 74 | @Retention(RetentionPolicy.SOURCE) |
| 75 | @interface CtrlType {} |
| 76 | |
| 77 | private static final int CTRL_NONE = 0x0; |
| 78 | private static final int CTRL_LEFT = 0x1; |
| 79 | private static final int CTRL_RIGHT = 0x2; |
| 80 | private static final int CTRL_TOP = 0x4; |
| 81 | private static final int CTRL_BOTTOM = 0x8; |
| 82 | |
Filip Gruszczynski | 64b6b44 | 2016-01-18 13:20:58 -0800 | [diff] [blame] | 83 | public static final float RESIZING_HINT_ALPHA = 0.5f; |
| 84 | |
| 85 | public static final int RESIZING_HINT_DURATION_MS = 0; |
| 86 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 87 | // The minimal aspect ratio which needs to be met to count as landscape (or 1/.. for portrait). |
| 88 | // Note: We do not use the 1.33 from the CDD here since the user is allowed to use what ever |
| 89 | // aspect he desires. |
| 90 | @VisibleForTesting |
| 91 | static final float MIN_ASPECT = 1.2f; |
| 92 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 93 | private final WindowManagerService mService; |
| 94 | private WindowPositionerEventReceiver mInputEventReceiver; |
| 95 | private Display mDisplay; |
| 96 | private final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 97 | private Rect mTmpRect = new Rect(); |
| 98 | private int mSideMargin; |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 99 | private int mMinVisibleWidth; |
| 100 | private int mMinVisibleHeight; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 101 | |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 102 | private Task mTask; |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 103 | private boolean mResizing; |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 104 | private boolean mPreserveOrientation; |
| 105 | private boolean mStartOrientationWasLandscape; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 106 | private final Rect mWindowOriginalBounds = new Rect(); |
| 107 | private final Rect mWindowDragBounds = new Rect(); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 108 | private final Point mMaxVisibleSize = new Point(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 109 | private float mStartDragX; |
| 110 | private float mStartDragY; |
| 111 | @CtrlType |
| 112 | private int mCtrlType = CTRL_NONE; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 113 | private boolean mDragEnded = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 114 | |
| 115 | InputChannel mServerChannel; |
| 116 | InputChannel mClientChannel; |
| 117 | InputApplicationHandle mDragApplicationHandle; |
| 118 | InputWindowHandle mDragWindowHandle; |
| 119 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 120 | private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver { |
| 121 | public WindowPositionerEventReceiver( |
| 122 | InputChannel inputChannel, Looper looper, Choreographer choreographer) { |
| 123 | super(inputChannel, looper, choreographer); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 127 | public void onInputEvent(InputEvent event, int displayId) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 128 | if (!(event instanceof MotionEvent) |
| 129 | || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) { |
| 130 | return; |
| 131 | } |
| 132 | final MotionEvent motionEvent = (MotionEvent) event; |
| 133 | boolean handled = false; |
| 134 | |
| 135 | try { |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 136 | if (mDragEnded) { |
| 137 | // The drag has ended but the clean-up message has not been processed by |
| 138 | // window manager. Drop events that occur after this until window manager |
| 139 | // has a chance to clean-up the input handle. |
| 140 | handled = true; |
| 141 | return; |
| 142 | } |
| 143 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 144 | final float newX = motionEvent.getRawX(); |
| 145 | final float newY = motionEvent.getRawY(); |
| 146 | |
| 147 | switch (motionEvent.getAction()) { |
| 148 | case MotionEvent.ACTION_DOWN: { |
| 149 | if (DEBUG_TASK_POSITIONING) { |
| 150 | Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}"); |
| 151 | } |
| 152 | } break; |
| 153 | |
| 154 | case MotionEvent.ACTION_MOVE: { |
| 155 | if (DEBUG_TASK_POSITIONING){ |
| 156 | Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}"); |
| 157 | } |
| 158 | synchronized (mService.mWindowMap) { |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 159 | mDragEnded = notifyMoveLocked(newX, newY); |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 160 | mTask.getDimBounds(mTmpRect); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 161 | } |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 162 | if (!mTmpRect.equals(mWindowDragBounds)) { |
| 163 | Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, |
| 164 | "wm.TaskPositioner.resizeTask"); |
| 165 | try { |
| 166 | mService.mActivityManager.resizeTask( |
| 167 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER); |
| 168 | } catch (RemoteException e) { |
| 169 | } |
| 170 | Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER); |
| 171 | } |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 172 | } break; |
| 173 | |
| 174 | case MotionEvent.ACTION_UP: { |
| 175 | if (DEBUG_TASK_POSITIONING) { |
| 176 | Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}"); |
| 177 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 178 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 179 | } break; |
| 180 | |
| 181 | case MotionEvent.ACTION_CANCEL: { |
| 182 | if (DEBUG_TASK_POSITIONING) { |
| 183 | Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}"); |
| 184 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 185 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 186 | } break; |
| 187 | } |
| 188 | |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 189 | if (mDragEnded) { |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 190 | final boolean wasResizing = mResizing; |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 191 | synchronized (mService.mWindowMap) { |
| 192 | endDragLocked(); |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 193 | mTask.getDimBounds(mTmpRect); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 194 | } |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 195 | try { |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 196 | if (wasResizing && !mTmpRect.equals(mWindowDragBounds)) { |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 197 | // We were using fullscreen surface during resizing. Request |
| 198 | // resizeTask() one last time to restore surface to window size. |
| 199 | mService.mActivityManager.resizeTask( |
Chong Zhang | 6de2ae8 | 2015-09-30 18:25:21 -0700 | [diff] [blame] | 200 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 201 | } |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 202 | } catch(RemoteException e) {} |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 203 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 204 | // Post back to WM to handle clean-ups. We still need the input |
| 205 | // event handler for the last finishInputEvent()! |
Daichi Hirono | ce2f97a | 2017-11-30 16:44:15 +0900 | [diff] [blame^] | 206 | mService.mTaskPositioningController.finishTaskPositioning(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 207 | } |
| 208 | handled = true; |
| 209 | } catch (Exception e) { |
| 210 | Slog.e(TAG, "Exception caught by drag handleMotion", e); |
| 211 | } finally { |
| 212 | finishInputEvent(event, handled); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | TaskPositioner(WindowManagerService service) { |
| 218 | mService = service; |
| 219 | } |
| 220 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 221 | @VisibleForTesting |
| 222 | Rect getWindowDragBounds() { |
| 223 | return mWindowDragBounds; |
| 224 | } |
| 225 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 226 | /** |
| 227 | * @param display The Display that the window being dragged is on. |
| 228 | */ |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 229 | void register(DisplayContent displayContent) { |
| 230 | final Display display = displayContent.getDisplay(); |
| 231 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 232 | if (DEBUG_TASK_POSITIONING) { |
| 233 | Slog.d(TAG, "Registering task positioner"); |
| 234 | } |
| 235 | |
| 236 | if (mClientChannel != null) { |
| 237 | Slog.e(TAG, "Task positioner already registered"); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | mDisplay = display; |
| 242 | mDisplay.getMetrics(mDisplayMetrics); |
| 243 | final InputChannel[] channels = InputChannel.openInputChannelPair(TAG); |
| 244 | mServerChannel = channels[0]; |
| 245 | mClientChannel = channels[1]; |
| 246 | mService.mInputManager.registerInputChannel(mServerChannel, null); |
| 247 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 248 | mInputEventReceiver = new WindowPositionerEventReceiver( |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 249 | mClientChannel, mService.mAnimationHandler.getLooper(), |
| 250 | mService.mAnimator.getChoreographer()); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 251 | |
| 252 | mDragApplicationHandle = new InputApplicationHandle(null); |
| 253 | mDragApplicationHandle.name = TAG; |
| 254 | mDragApplicationHandle.dispatchingTimeoutNanos = |
| 255 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 256 | |
Vladislav Kaznacheev | 3787de1 | 2016-12-21 10:36:35 -0800 | [diff] [blame] | 257 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null, |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 258 | mDisplay.getDisplayId()); |
| 259 | mDragWindowHandle.name = TAG; |
| 260 | mDragWindowHandle.inputChannel = mServerChannel; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 261 | mDragWindowHandle.layer = mService.getDragLayerLocked(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 262 | mDragWindowHandle.layoutParamsFlags = 0; |
| 263 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 264 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 265 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 266 | mDragWindowHandle.visible = true; |
| 267 | mDragWindowHandle.canReceiveKeys = false; |
| 268 | mDragWindowHandle.hasFocus = true; |
| 269 | mDragWindowHandle.hasWallpaper = false; |
| 270 | mDragWindowHandle.paused = false; |
| 271 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 272 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 273 | mDragWindowHandle.inputFeatures = 0; |
| 274 | mDragWindowHandle.scaleFactor = 1.0f; |
| 275 | |
| 276 | // The drag window cannot receive new touches. |
| 277 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 278 | |
| 279 | // The drag window covers the entire display |
| 280 | mDragWindowHandle.frameLeft = 0; |
| 281 | mDragWindowHandle.frameTop = 0; |
| 282 | final Point p = new Point(); |
| 283 | mDisplay.getRealSize(p); |
| 284 | mDragWindowHandle.frameRight = p.x; |
| 285 | mDragWindowHandle.frameBottom = p.y; |
| 286 | |
| 287 | // Pause rotations before a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 288 | if (DEBUG_ORIENTATION) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 289 | Slog.d(TAG, "Pausing rotation during re-position"); |
| 290 | } |
| 291 | mService.pauseRotationLocked(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 292 | |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 293 | mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics); |
| 294 | mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics); |
| 295 | mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 296 | mDisplay.getRealSize(mMaxVisibleSize); |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 297 | |
| 298 | mDragEnded = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void unregister() { |
| 302 | if (DEBUG_TASK_POSITIONING) { |
| 303 | Slog.d(TAG, "Unregistering task positioner"); |
| 304 | } |
| 305 | |
| 306 | if (mClientChannel == null) { |
| 307 | Slog.e(TAG, "Task positioner not registered"); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
| 312 | |
| 313 | mInputEventReceiver.dispose(); |
| 314 | mInputEventReceiver = null; |
| 315 | mClientChannel.dispose(); |
| 316 | mServerChannel.dispose(); |
| 317 | mClientChannel = null; |
| 318 | mServerChannel = null; |
| 319 | |
| 320 | mDragWindowHandle = null; |
| 321 | mDragApplicationHandle = null; |
| 322 | mDisplay = null; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 323 | mDragEnded = true; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 324 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 325 | // Resume rotations after a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 326 | if (DEBUG_ORIENTATION) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 327 | Slog.d(TAG, "Resuming rotation after re-position"); |
| 328 | } |
| 329 | mService.resumeRotationLocked(); |
| 330 | } |
| 331 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 332 | void startDrag(WindowState win, boolean resize, boolean preserveOrientation, float startX, |
| 333 | float startY) { |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 334 | if (DEBUG_TASK_POSITIONING) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 335 | Slog.d(TAG, "startDrag: win=" + win + ", resize=" + resize |
| 336 | + ", preserveOrientation=" + preserveOrientation + ", {" + startX + ", " |
| 337 | + startY + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 338 | } |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 339 | mTask = win.getTask(); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 340 | // Use the dim bounds, not the original task bounds. The cursor |
| 341 | // movement should be calculated relative to the visible bounds. |
| 342 | // Also, use the dim bounds of the task which accounts for |
| 343 | // multiple app windows. Don't use any bounds from win itself as it |
| 344 | // may not be the same size as the task. |
| 345 | mTask.getDimBounds(mTmpRect); |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 346 | startDrag(resize, preserveOrientation, startX, startY, mTmpRect); |
| 347 | } |
| 348 | |
| 349 | @VisibleForTesting |
| 350 | void startDrag(boolean resize, boolean preserveOrientation, |
| 351 | float startX, float startY, Rect startBounds) { |
| 352 | mCtrlType = CTRL_NONE; |
| 353 | mStartDragX = startX; |
| 354 | mStartDragY = startY; |
| 355 | mPreserveOrientation = preserveOrientation; |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 356 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 357 | if (resize) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 358 | if (startX < startBounds.left) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 359 | mCtrlType |= CTRL_LEFT; |
| 360 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 361 | if (startX > startBounds.right) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 362 | mCtrlType |= CTRL_RIGHT; |
| 363 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 364 | if (startY < startBounds.top) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 365 | mCtrlType |= CTRL_TOP; |
| 366 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 367 | if (startY > startBounds.bottom) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 368 | mCtrlType |= CTRL_BOTTOM; |
| 369 | } |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 370 | mResizing = mCtrlType != CTRL_NONE; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 371 | } |
| 372 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 373 | // In case of !isDockedInEffect we are using the union of all task bounds. These might be |
| 374 | // made up out of multiple windows which are only partially overlapping. When that happens, |
| 375 | // the orientation from the window of interest to the entire stack might diverge. However |
| 376 | // for now we treat them as the same. |
| 377 | mStartOrientationWasLandscape = startBounds.width() >= startBounds.height(); |
| 378 | mWindowOriginalBounds.set(startBounds); |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 379 | |
Tomasz Mikolajewski | 79e8d17 | 2017-11-21 11:21:42 +0900 | [diff] [blame] | 380 | // Notify the app that resizing has started, even though we haven't received any new |
| 381 | // bounds yet. This will guarantee that the app starts the backdrop renderer before |
| 382 | // configuration changes which could cause an activity restart. |
| 383 | if (mResizing) { |
| 384 | synchronized (mService.mWindowMap) { |
| 385 | notifyMoveLocked(startX, startY); |
| 386 | } |
| 387 | |
| 388 | // Perform the resize on the WMS handler thread when we don't have the WMS lock held |
| 389 | // to ensure that we don't deadlock WMS and AMS. Note that WindowPositionerEventReceiver |
| 390 | // callbacks are delivered on the same handler so this initial resize is always |
| 391 | // guaranteed to happen before subsequent drag resizes. |
| 392 | mService.mH.post(() -> { |
| 393 | try { |
| 394 | mService.mActivityManager.resizeTask( |
| 395 | mTask.mTaskId, startBounds, RESIZE_MODE_USER_FORCED); |
| 396 | } catch (RemoteException e) { |
| 397 | } |
| 398 | }); |
| 399 | } |
| 400 | |
Vladislav Kaznacheev | 824bc5d | 2016-04-22 17:48:35 -0700 | [diff] [blame] | 401 | // Make sure we always have valid drag bounds even if the drag ends before any move events |
| 402 | // have been handled. |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 403 | mWindowDragBounds.set(startBounds); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | private void endDragLocked() { |
| 407 | mResizing = false; |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 408 | mTask.setDragResizing(false, DRAG_RESIZE_MODE_FREEFORM); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 411 | /** Returns true if the move operation should be ended. */ |
| 412 | private boolean notifyMoveLocked(float x, float y) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 413 | if (DEBUG_TASK_POSITIONING) { |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 414 | Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (mCtrlType != CTRL_NONE) { |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 418 | resizeDrag(x, y); |
Jorim Jaggi | 0b46f3c | 2016-03-14 12:21:37 +0100 | [diff] [blame] | 419 | mTask.setDragResizing(true, DRAG_RESIZE_MODE_FREEFORM); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 420 | return false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 421 | } |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 422 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 423 | // This is a moving or scrolling operation. |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 424 | mTask.mStack.getDimBounds(mTmpRect); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 425 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 426 | int nX = (int) x; |
| 427 | int nY = (int) y; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 428 | if (!mTmpRect.contains(nX, nY)) { |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 429 | // For a moving operation we allow the pointer to go out of the stack bounds, but |
| 430 | // use the clamped pointer position for the drag bounds computation. |
| 431 | nX = Math.min(Math.max(nX, mTmpRect.left), mTmpRect.right); |
| 432 | nY = Math.min(Math.max(nY, mTmpRect.top), mTmpRect.bottom); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 435 | updateWindowDragBounds(nX, nY, mTmpRect); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 436 | return false; |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 437 | } |
| 438 | |
skuhne@google.com | 322347b | 2016-12-02 12:54:03 -0800 | [diff] [blame] | 439 | /** |
| 440 | * The user is drag - resizing the window. |
| 441 | * |
| 442 | * @param x The x coordinate of the current drag coordinate. |
| 443 | * @param y the y coordinate of the current drag coordinate. |
| 444 | */ |
| 445 | @VisibleForTesting |
| 446 | void resizeDrag(float x, float y) { |
| 447 | // This is a resizing operation. |
| 448 | // We need to keep various constraints: |
| 449 | // 1. mMinVisible[Width/Height] <= [width/height] <= mMaxVisibleSize.[x/y] |
| 450 | // 2. The orientation is kept - if required. |
| 451 | final int deltaX = Math.round(x - mStartDragX); |
| 452 | final int deltaY = Math.round(y - mStartDragY); |
| 453 | int left = mWindowOriginalBounds.left; |
| 454 | int top = mWindowOriginalBounds.top; |
| 455 | int right = mWindowOriginalBounds.right; |
| 456 | int bottom = mWindowOriginalBounds.bottom; |
| 457 | |
| 458 | // The aspect which we have to respect. Note that if the orientation does not need to be |
| 459 | // preserved the aspect will be calculated as 1.0 which neutralizes the following |
| 460 | // computations. |
| 461 | final float minAspect = !mPreserveOrientation |
| 462 | ? 1.0f |
| 463 | : (mStartOrientationWasLandscape ? MIN_ASPECT : (1.0f / MIN_ASPECT)); |
| 464 | // Calculate the resulting width and height of the drag operation. |
| 465 | int width = right - left; |
| 466 | int height = bottom - top; |
| 467 | if ((mCtrlType & CTRL_LEFT) != 0) { |
| 468 | width = Math.max(mMinVisibleWidth, width - deltaX); |
| 469 | } else if ((mCtrlType & CTRL_RIGHT) != 0) { |
| 470 | width = Math.max(mMinVisibleWidth, width + deltaX); |
| 471 | } |
| 472 | if ((mCtrlType & CTRL_TOP) != 0) { |
| 473 | height = Math.max(mMinVisibleHeight, height - deltaY); |
| 474 | } else if ((mCtrlType & CTRL_BOTTOM) != 0) { |
| 475 | height = Math.max(mMinVisibleHeight, height + deltaY); |
| 476 | } |
| 477 | |
| 478 | // If we have to preserve the orientation - check that we are doing so. |
| 479 | final float aspect = (float) width / (float) height; |
| 480 | if (mPreserveOrientation && ((mStartOrientationWasLandscape && aspect < MIN_ASPECT) |
| 481 | || (!mStartOrientationWasLandscape && aspect > (1.0 / MIN_ASPECT)))) { |
| 482 | // Calculate 2 rectangles fulfilling all requirements for either X or Y being the major |
| 483 | // drag axis. What ever is producing the bigger rectangle will be chosen. |
| 484 | int width1; |
| 485 | int width2; |
| 486 | int height1; |
| 487 | int height2; |
| 488 | if (mStartOrientationWasLandscape) { |
| 489 | // Assuming that the width is our target we calculate the height. |
| 490 | width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width)); |
| 491 | height1 = Math.min(height, Math.round((float)width1 / MIN_ASPECT)); |
| 492 | if (height1 < mMinVisibleHeight) { |
| 493 | // If the resulting height is too small we adjust to the minimal size. |
| 494 | height1 = mMinVisibleHeight; |
| 495 | width1 = Math.max(mMinVisibleWidth, |
| 496 | Math.min(mMaxVisibleSize.x, Math.round((float)height1 * MIN_ASPECT))); |
| 497 | } |
| 498 | // Assuming that the height is our target we calculate the width. |
| 499 | height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height)); |
| 500 | width2 = Math.max(width, Math.round((float)height2 * MIN_ASPECT)); |
| 501 | if (width2 < mMinVisibleWidth) { |
| 502 | // If the resulting width is too small we adjust to the minimal size. |
| 503 | width2 = mMinVisibleWidth; |
| 504 | height2 = Math.max(mMinVisibleHeight, |
| 505 | Math.min(mMaxVisibleSize.y, Math.round((float)width2 / MIN_ASPECT))); |
| 506 | } |
| 507 | } else { |
| 508 | // Assuming that the width is our target we calculate the height. |
| 509 | width1 = Math.max(mMinVisibleWidth, Math.min(mMaxVisibleSize.x, width)); |
| 510 | height1 = Math.max(height, Math.round((float)width1 * MIN_ASPECT)); |
| 511 | if (height1 < mMinVisibleHeight) { |
| 512 | // If the resulting height is too small we adjust to the minimal size. |
| 513 | height1 = mMinVisibleHeight; |
| 514 | width1 = Math.max(mMinVisibleWidth, |
| 515 | Math.min(mMaxVisibleSize.x, Math.round((float)height1 / MIN_ASPECT))); |
| 516 | } |
| 517 | // Assuming that the height is our target we calculate the width. |
| 518 | height2 = Math.max(mMinVisibleHeight, Math.min(mMaxVisibleSize.y, height)); |
| 519 | width2 = Math.min(width, Math.round((float)height2 / MIN_ASPECT)); |
| 520 | if (width2 < mMinVisibleWidth) { |
| 521 | // If the resulting width is too small we adjust to the minimal size. |
| 522 | width2 = mMinVisibleWidth; |
| 523 | height2 = Math.max(mMinVisibleHeight, |
| 524 | Math.min(mMaxVisibleSize.y, Math.round((float)width2 * MIN_ASPECT))); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Use the bigger of the two rectangles if the major change was positive, otherwise |
| 529 | // do the opposite. |
| 530 | final boolean grows = width > (right - left) || height > (bottom - top); |
| 531 | if (grows == (width1 * height1 > width2 * height2)) { |
| 532 | width = width1; |
| 533 | height = height1; |
| 534 | } else { |
| 535 | width = width2; |
| 536 | height = height2; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Update mWindowDragBounds to the new drag size. |
| 541 | updateDraggedBounds(left, top, right, bottom, width, height); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Given the old coordinates and the new width and height, update the mWindowDragBounds. |
| 546 | * |
| 547 | * @param left The original left bound before the user started dragging. |
| 548 | * @param top The original top bound before the user started dragging. |
| 549 | * @param right The original right bound before the user started dragging. |
| 550 | * @param bottom The original bottom bound before the user started dragging. |
| 551 | * @param newWidth The new dragged width. |
| 552 | * @param newHeight The new dragged height. |
| 553 | */ |
| 554 | void updateDraggedBounds(int left, int top, int right, int bottom, int newWidth, |
| 555 | int newHeight) { |
| 556 | // Generate the final bounds by keeping the opposite drag edge constant. |
| 557 | if ((mCtrlType & CTRL_LEFT) != 0) { |
| 558 | left = right - newWidth; |
| 559 | } else { // Note: The right might have changed - if we pulled at the right or not. |
| 560 | right = left + newWidth; |
| 561 | } |
| 562 | if ((mCtrlType & CTRL_TOP) != 0) { |
| 563 | top = bottom - newHeight; |
| 564 | } else { // Note: The height might have changed - if we pulled at the bottom or not. |
| 565 | bottom = top + newHeight; |
| 566 | } |
| 567 | |
| 568 | mWindowDragBounds.set(left, top, right, bottom); |
| 569 | |
| 570 | checkBoundsForOrientationViolations(mWindowDragBounds); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Validate bounds against orientation violations (if DEBUG_ORIENTATION_VIOLATIONS is set). |
| 575 | * |
| 576 | * @param bounds The bounds to be checked. |
| 577 | */ |
| 578 | private void checkBoundsForOrientationViolations(Rect bounds) { |
| 579 | // When using debug check that we are not violating the given constraints. |
| 580 | if (DEBUG_ORIENTATION_VIOLATIONS) { |
| 581 | if (mStartOrientationWasLandscape != (bounds.width() >= bounds.height())) { |
| 582 | Slog.e(TAG, "Orientation violation detected! should be " |
| 583 | + (mStartOrientationWasLandscape ? "landscape" : "portrait") |
| 584 | + " but is the other"); |
| 585 | } else { |
| 586 | Slog.v(TAG, "new bounds size: " + bounds.width() + " x " + bounds.height()); |
| 587 | } |
| 588 | if (mMinVisibleWidth > bounds.width() || mMinVisibleHeight > bounds.height()) { |
| 589 | Slog.v(TAG, "Minimum requirement violated: Width(min, is)=(" + mMinVisibleWidth |
| 590 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 591 | + mMinVisibleHeight + ", " + bounds.height() + ")"); |
| 592 | } |
| 593 | if (mMaxVisibleSize.x < bounds.width() || mMaxVisibleSize.y < bounds.height()) { |
| 594 | Slog.v(TAG, "Maximum requirement violated: Width(min, is)=(" + mMaxVisibleSize.x |
| 595 | + ", " + bounds.width() + ") Height(min,is)=(" |
| 596 | + mMaxVisibleSize.y + ", " + bounds.height() + ")"); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 601 | private void updateWindowDragBounds(int x, int y, Rect stackBounds) { |
| 602 | final int offsetX = Math.round(x - mStartDragX); |
| 603 | final int offsetY = Math.round(y - mStartDragY); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 604 | mWindowDragBounds.set(mWindowOriginalBounds); |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 605 | // Horizontally, at least mMinVisibleWidth pixels of the window should remain visible. |
| 606 | final int maxLeft = stackBounds.right - mMinVisibleWidth; |
| 607 | final int minLeft = stackBounds.left + mMinVisibleWidth - mWindowOriginalBounds.width(); |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 608 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 609 | // Vertically, the top mMinVisibleHeight of the window should remain visible. |
| 610 | // (This assumes that the window caption bar is at the top of the window). |
| 611 | final int minTop = stackBounds.top; |
| 612 | final int maxTop = stackBounds.bottom - mMinVisibleHeight; |
Vladislav Kaznacheev | a90a3b8 | 2016-04-27 14:54:53 -0700 | [diff] [blame] | 613 | |
Chong Zhang | 2e2c81a | 2016-07-15 11:28:17 -0700 | [diff] [blame] | 614 | mWindowDragBounds.offsetTo( |
| 615 | Math.min(Math.max(mWindowOriginalBounds.left + offsetX, minLeft), maxLeft), |
| 616 | Math.min(Math.max(mWindowOriginalBounds.top + offsetY, minTop), maxTop)); |
| 617 | |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame] | 618 | if (DEBUG_TASK_POSITIONING) Slog.d(TAG, |
| 619 | "updateWindowDragBounds: " + mWindowDragBounds); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 622 | public String toShortString() { |
| 623 | return TAG; |
| 624 | } |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 625 | } |