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 | 59a73ca | 2015-09-14 12:54:50 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT; |
| 20 | import static android.app.ActivityManager.DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT; |
Wale Ogunwale | 3797c22 | 2015-10-27 14:21:58 -0700 | [diff] [blame] | 21 | import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID; |
Chong Zhang | 87b2172 | 2015-09-21 15:39:51 -0700 | [diff] [blame] | 22 | import static android.app.ActivityManager.RESIZE_MODE_USER; |
Chong Zhang | 6de2ae8 | 2015-09-30 18:25:21 -0700 | [diff] [blame] | 23 | import static android.app.ActivityManager.RESIZE_MODE_USER_FORCED; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 24 | import static android.content.res.Configuration.ORIENTATION_LANDSCAPE; |
Wale Ogunwale | cad05a0 | 2015-09-25 10:41:44 -0700 | [diff] [blame] | 25 | import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 26 | import static com.android.server.wm.DimLayer.RESIZING_HINT_ALPHA; |
| 27 | import static com.android.server.wm.DimLayer.RESIZING_HINT_DURATION_MS; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 28 | import static com.android.server.wm.WindowManagerService.DEBUG_TASK_POSITIONING; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 29 | import static com.android.server.wm.WindowManagerService.SHOW_TRANSACTIONS; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 30 | import static com.android.server.wm.WindowManagerService.dipToPixel; |
Wale Ogunwale | 231b06e | 2015-09-16 12:03:09 -0700 | [diff] [blame] | 31 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_HEIGHT_IN_DP; |
| 32 | import static com.android.server.wm.WindowState.MINIMUM_VISIBLE_WIDTH_IN_DP; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 33 | |
| 34 | import android.annotation.IntDef; |
| 35 | import android.graphics.Point; |
| 36 | import android.graphics.Rect; |
| 37 | import android.os.Looper; |
| 38 | import android.os.Process; |
| 39 | import android.os.RemoteException; |
Wale Ogunwale | cad05a0 | 2015-09-25 10:41:44 -0700 | [diff] [blame] | 40 | import android.os.Trace; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 41 | import android.util.DisplayMetrics; |
| 42 | import android.util.Slog; |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 43 | import android.view.Choreographer; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 44 | import android.view.Display; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 45 | import android.view.DisplayInfo; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 46 | import android.view.InputChannel; |
| 47 | import android.view.InputDevice; |
| 48 | import android.view.InputEvent; |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 49 | import android.view.BatchedInputEventReceiver; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 50 | import android.view.MotionEvent; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 51 | import android.view.SurfaceControl; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 52 | import android.view.WindowManager; |
| 53 | |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 54 | import com.android.server.input.InputApplicationHandle; |
| 55 | import com.android.server.input.InputWindowHandle; |
| 56 | import com.android.server.wm.WindowManagerService.H; |
| 57 | |
| 58 | import java.lang.annotation.Retention; |
| 59 | import java.lang.annotation.RetentionPolicy; |
| 60 | |
| 61 | class TaskPositioner implements DimLayer.DimLayerUser { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 62 | private static final String TAG = "TaskPositioner"; |
| 63 | |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 64 | // The margin the pointer position has to be within the side of the screen to be |
| 65 | // considered at the side of the screen. |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 66 | static final int SIDE_MARGIN_DIP = 100; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 67 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 68 | @IntDef(flag = true, |
| 69 | value = { |
| 70 | CTRL_NONE, |
| 71 | CTRL_LEFT, |
| 72 | CTRL_RIGHT, |
| 73 | CTRL_TOP, |
| 74 | CTRL_BOTTOM |
| 75 | }) |
| 76 | @Retention(RetentionPolicy.SOURCE) |
| 77 | @interface CtrlType {} |
| 78 | |
| 79 | private static final int CTRL_NONE = 0x0; |
| 80 | private static final int CTRL_LEFT = 0x1; |
| 81 | private static final int CTRL_RIGHT = 0x2; |
| 82 | private static final int CTRL_TOP = 0x4; |
| 83 | private static final int CTRL_BOTTOM = 0x8; |
| 84 | |
| 85 | private final WindowManagerService mService; |
| 86 | private WindowPositionerEventReceiver mInputEventReceiver; |
| 87 | private Display mDisplay; |
| 88 | private final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 89 | private DimLayer mDimLayer; |
| 90 | @CtrlType |
| 91 | private int mCurrentDimSide; |
| 92 | private Rect mTmpRect = new Rect(); |
| 93 | private int mSideMargin; |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 94 | private int mMinVisibleWidth; |
| 95 | private int mMinVisibleHeight; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 96 | |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 97 | private Task mTask; |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 98 | private boolean mResizing; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 99 | private final Rect mWindowOriginalBounds = new Rect(); |
| 100 | private final Rect mWindowDragBounds = new Rect(); |
| 101 | private float mStartDragX; |
| 102 | private float mStartDragY; |
| 103 | @CtrlType |
| 104 | private int mCtrlType = CTRL_NONE; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 105 | private boolean mDragEnded = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 106 | |
| 107 | InputChannel mServerChannel; |
| 108 | InputChannel mClientChannel; |
| 109 | InputApplicationHandle mDragApplicationHandle; |
| 110 | InputWindowHandle mDragWindowHandle; |
| 111 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 112 | private final class WindowPositionerEventReceiver extends BatchedInputEventReceiver { |
| 113 | public WindowPositionerEventReceiver( |
| 114 | InputChannel inputChannel, Looper looper, Choreographer choreographer) { |
| 115 | super(inputChannel, looper, choreographer); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onInputEvent(InputEvent event) { |
| 120 | if (!(event instanceof MotionEvent) |
| 121 | || (event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) { |
| 122 | return; |
| 123 | } |
| 124 | final MotionEvent motionEvent = (MotionEvent) event; |
| 125 | boolean handled = false; |
| 126 | |
| 127 | try { |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 128 | if (mDragEnded) { |
| 129 | // The drag has ended but the clean-up message has not been processed by |
| 130 | // window manager. Drop events that occur after this until window manager |
| 131 | // has a chance to clean-up the input handle. |
| 132 | handled = true; |
| 133 | return; |
| 134 | } |
| 135 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 136 | final float newX = motionEvent.getRawX(); |
| 137 | final float newY = motionEvent.getRawY(); |
| 138 | |
| 139 | switch (motionEvent.getAction()) { |
| 140 | case MotionEvent.ACTION_DOWN: { |
| 141 | if (DEBUG_TASK_POSITIONING) { |
| 142 | Slog.w(TAG, "ACTION_DOWN @ {" + newX + ", " + newY + "}"); |
| 143 | } |
| 144 | } break; |
| 145 | |
| 146 | case MotionEvent.ACTION_MOVE: { |
| 147 | if (DEBUG_TASK_POSITIONING){ |
| 148 | Slog.w(TAG, "ACTION_MOVE @ {" + newX + ", " + newY + "}"); |
| 149 | } |
| 150 | synchronized (mService.mWindowMap) { |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 151 | mDragEnded = notifyMoveLocked(newX, newY); |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 152 | mTask.getDimBounds(mTmpRect); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 153 | } |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 154 | if (!mTmpRect.equals(mWindowDragBounds)) { |
| 155 | Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, |
| 156 | "wm.TaskPositioner.resizeTask"); |
| 157 | try { |
| 158 | mService.mActivityManager.resizeTask( |
| 159 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER); |
| 160 | } catch (RemoteException e) { |
| 161 | } |
| 162 | Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER); |
| 163 | } |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 164 | } break; |
| 165 | |
| 166 | case MotionEvent.ACTION_UP: { |
| 167 | if (DEBUG_TASK_POSITIONING) { |
| 168 | Slog.w(TAG, "ACTION_UP @ {" + newX + ", " + newY + "}"); |
| 169 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 170 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 171 | } break; |
| 172 | |
| 173 | case MotionEvent.ACTION_CANCEL: { |
| 174 | if (DEBUG_TASK_POSITIONING) { |
| 175 | Slog.w(TAG, "ACTION_CANCEL @ {" + newX + ", " + newY + "}"); |
| 176 | } |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 177 | mDragEnded = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 178 | } break; |
| 179 | } |
| 180 | |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 181 | if (mDragEnded) { |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 182 | final boolean wasResizing = mResizing; |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 183 | synchronized (mService.mWindowMap) { |
| 184 | endDragLocked(); |
| 185 | } |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 186 | try { |
Wale Ogunwale | 04ad7b1 | 2015-10-02 12:43:27 -0700 | [diff] [blame] | 187 | if (wasResizing) { |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 188 | // We were using fullscreen surface during resizing. Request |
| 189 | // resizeTask() one last time to restore surface to window size. |
| 190 | mService.mActivityManager.resizeTask( |
Chong Zhang | 6de2ae8 | 2015-09-30 18:25:21 -0700 | [diff] [blame] | 191 | mTask.mTaskId, mWindowDragBounds, RESIZE_MODE_USER_FORCED); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (mCurrentDimSide != CTRL_NONE) { |
| 195 | final int createMode = mCurrentDimSide == CTRL_LEFT |
| 196 | ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT |
| 197 | : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT; |
| 198 | mService.mActivityManager.moveTaskToDockedStack( |
Jorim Jaggi | 9ea2f7b | 2015-11-23 18:08:28 -0800 | [diff] [blame] | 199 | mTask.mTaskId, createMode, true /*toTop*/, true /* animate */, |
| 200 | null /* initialBounds */); |
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()! |
| 206 | mService.mH.sendEmptyMessage(H.FINISH_TASK_POSITIONING); |
| 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 | |
| 221 | /** |
| 222 | * @param display The Display that the window being dragged is on. |
| 223 | */ |
| 224 | void register(Display display) { |
| 225 | if (DEBUG_TASK_POSITIONING) { |
| 226 | Slog.d(TAG, "Registering task positioner"); |
| 227 | } |
| 228 | |
| 229 | if (mClientChannel != null) { |
| 230 | Slog.e(TAG, "Task positioner already registered"); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | mDisplay = display; |
| 235 | mDisplay.getMetrics(mDisplayMetrics); |
| 236 | final InputChannel[] channels = InputChannel.openInputChannelPair(TAG); |
| 237 | mServerChannel = channels[0]; |
| 238 | mClientChannel = channels[1]; |
| 239 | mService.mInputManager.registerInputChannel(mServerChannel, null); |
| 240 | |
Wale Ogunwale | 4f52bc6 | 2015-09-24 13:47:31 -0700 | [diff] [blame] | 241 | mInputEventReceiver = new WindowPositionerEventReceiver( |
| 242 | mClientChannel, mService.mH.getLooper(), mService.mChoreographer); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 243 | |
| 244 | mDragApplicationHandle = new InputApplicationHandle(null); |
| 245 | mDragApplicationHandle.name = TAG; |
| 246 | mDragApplicationHandle.dispatchingTimeoutNanos = |
| 247 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 248 | |
| 249 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, |
| 250 | mDisplay.getDisplayId()); |
| 251 | mDragWindowHandle.name = TAG; |
| 252 | mDragWindowHandle.inputChannel = mServerChannel; |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 253 | mDragWindowHandle.layer = mService.getDragLayerLocked(); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 254 | mDragWindowHandle.layoutParamsFlags = 0; |
| 255 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 256 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 257 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 258 | mDragWindowHandle.visible = true; |
| 259 | mDragWindowHandle.canReceiveKeys = false; |
| 260 | mDragWindowHandle.hasFocus = true; |
| 261 | mDragWindowHandle.hasWallpaper = false; |
| 262 | mDragWindowHandle.paused = false; |
| 263 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 264 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 265 | mDragWindowHandle.inputFeatures = 0; |
| 266 | mDragWindowHandle.scaleFactor = 1.0f; |
| 267 | |
| 268 | // The drag window cannot receive new touches. |
| 269 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 270 | |
| 271 | // The drag window covers the entire display |
| 272 | mDragWindowHandle.frameLeft = 0; |
| 273 | mDragWindowHandle.frameTop = 0; |
| 274 | final Point p = new Point(); |
| 275 | mDisplay.getRealSize(p); |
| 276 | mDragWindowHandle.frameRight = p.x; |
| 277 | mDragWindowHandle.frameBottom = p.y; |
| 278 | |
| 279 | // Pause rotations before a drag. |
| 280 | if (WindowManagerService.DEBUG_ORIENTATION) { |
| 281 | Slog.d(TAG, "Pausing rotation during re-position"); |
| 282 | } |
| 283 | mService.pauseRotationLocked(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 284 | |
| 285 | mDimLayer = new DimLayer(mService, this, mDisplay.getDisplayId()); |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 286 | mSideMargin = dipToPixel(SIDE_MARGIN_DIP, mDisplayMetrics); |
| 287 | mMinVisibleWidth = dipToPixel(MINIMUM_VISIBLE_WIDTH_IN_DP, mDisplayMetrics); |
| 288 | mMinVisibleHeight = dipToPixel(MINIMUM_VISIBLE_HEIGHT_IN_DP, mDisplayMetrics); |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 289 | |
| 290 | mDragEnded = false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void unregister() { |
| 294 | if (DEBUG_TASK_POSITIONING) { |
| 295 | Slog.d(TAG, "Unregistering task positioner"); |
| 296 | } |
| 297 | |
| 298 | if (mClientChannel == null) { |
| 299 | Slog.e(TAG, "Task positioner not registered"); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
| 304 | |
| 305 | mInputEventReceiver.dispose(); |
| 306 | mInputEventReceiver = null; |
| 307 | mClientChannel.dispose(); |
| 308 | mServerChannel.dispose(); |
| 309 | mClientChannel = null; |
| 310 | mServerChannel = null; |
| 311 | |
| 312 | mDragWindowHandle = null; |
| 313 | mDragApplicationHandle = null; |
| 314 | mDisplay = null; |
| 315 | |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 316 | if (mDimLayer != null) { |
| 317 | mDimLayer.destroySurface(); |
| 318 | mDimLayer = null; |
| 319 | } |
| 320 | mCurrentDimSide = CTRL_NONE; |
Wale Ogunwale | 6a804b8 | 2015-09-23 21:04:21 -0700 | [diff] [blame] | 321 | mDragEnded = true; |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 322 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 323 | // Resume rotations after a drag. |
| 324 | if (WindowManagerService.DEBUG_ORIENTATION) { |
| 325 | Slog.d(TAG, "Resuming rotation after re-position"); |
| 326 | } |
| 327 | mService.resumeRotationLocked(); |
| 328 | } |
| 329 | |
| 330 | void startDragLocked(WindowState win, boolean resize, float startX, float startY) { |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 331 | if (DEBUG_TASK_POSITIONING) { |
| 332 | Slog.d(TAG, "startDragLocked: win=" + win + ", resize=" + resize |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 333 | + ", {" + startX + ", " + startY + "}"); |
| 334 | } |
| 335 | mCtrlType = CTRL_NONE; |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 336 | mTask = win.getTask(); |
| 337 | mStartDragX = startX; |
| 338 | mStartDragY = startY; |
| 339 | |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame^] | 340 | if (mTask.isDockedInEffect()) { |
| 341 | // If this is a docked task or if task size is affected by docked stack changing size, |
| 342 | // we can only be here if the task is not resizeable and we're handling a two-finger |
| 343 | // scrolling. Use the original task bounds to position the task, the dim bounds |
| 344 | // is cropped and doesn't move. |
| 345 | mTask.getBounds(mTmpRect); |
| 346 | } else { |
| 347 | // Use the dim bounds, not the original task bounds. The cursor |
| 348 | // movement should be calculated relative to the visible bounds. |
| 349 | // Also, use the dim bounds of the task which accounts for |
| 350 | // multiple app windows. Don't use any bounds from win itself as it |
| 351 | // may not be the same size as the task. |
| 352 | mTask.getDimBounds(mTmpRect); |
| 353 | } |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 354 | |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 355 | if (resize) { |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 356 | if (startX < mTmpRect.left) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 357 | mCtrlType |= CTRL_LEFT; |
| 358 | } |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 359 | if (startX > mTmpRect.right) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 360 | mCtrlType |= CTRL_RIGHT; |
| 361 | } |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 362 | if (startY < mTmpRect.top) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 363 | mCtrlType |= CTRL_TOP; |
| 364 | } |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 365 | if (startY > mTmpRect.bottom) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 366 | mCtrlType |= CTRL_BOTTOM; |
| 367 | } |
Chong Zhang | 09b21ef | 2015-09-14 10:20:21 -0700 | [diff] [blame] | 368 | mResizing = true; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Chong Zhang | d8ceb85 | 2015-11-11 14:53:41 -0800 | [diff] [blame] | 371 | mWindowOriginalBounds.set(mTmpRect); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | private void endDragLocked() { |
| 375 | mResizing = false; |
| 376 | mTask.setDragResizing(false); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 379 | /** Returns true if the move operation should be ended. */ |
| 380 | private boolean notifyMoveLocked(float x, float y) { |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 381 | if (DEBUG_TASK_POSITIONING) { |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame^] | 382 | Slog.d(TAG, "notifyMoveLocked: {" + x + "," + y + "}"); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (mCtrlType != CTRL_NONE) { |
| 386 | // This is a resizing operation. |
| 387 | final int deltaX = Math.round(x - mStartDragX); |
| 388 | final int deltaY = Math.round(y - mStartDragY); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 389 | int left = mWindowOriginalBounds.left; |
| 390 | int top = mWindowOriginalBounds.top; |
| 391 | int right = mWindowOriginalBounds.right; |
| 392 | int bottom = mWindowOriginalBounds.bottom; |
| 393 | if ((mCtrlType & CTRL_LEFT) != 0) { |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 394 | left = Math.min(left + deltaX, right - mMinVisibleWidth); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 395 | } |
| 396 | if ((mCtrlType & CTRL_TOP) != 0) { |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 397 | top = Math.min(top + deltaY, bottom - mMinVisibleHeight); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 398 | } |
| 399 | if ((mCtrlType & CTRL_RIGHT) != 0) { |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 400 | right = Math.max(left + mMinVisibleWidth, right + deltaX); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 401 | } |
| 402 | if ((mCtrlType & CTRL_BOTTOM) != 0) { |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 403 | bottom = Math.max(top + mMinVisibleHeight, bottom + deltaY); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 404 | } |
| 405 | mWindowDragBounds.set(left, top, right, bottom); |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 406 | mTask.setDragResizing(true); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 407 | return false; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 408 | } |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 409 | |
| 410 | // This is a moving operation. |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 411 | mTask.mStack.getDimBounds(mTmpRect); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame^] | 412 | |
| 413 | // If this is a non-resizeable task put into side-by-side mode, we are |
| 414 | // handling a two-finger scrolling action. No need to shrink the bounds. |
| 415 | if (!mTask.isDockedInEffect()) { |
| 416 | mTmpRect.inset(mMinVisibleWidth, mMinVisibleHeight); |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 417 | } |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame^] | 418 | |
| 419 | boolean dragEnded = false; |
| 420 | final int nX = (int) x; |
| 421 | final int nY = (int) y; |
| 422 | if (!mTmpRect.contains(nX, nY)) { |
| 423 | // We end the moving operation if position is outside the stack bounds. |
| 424 | // In this case we need to clamp the position to stack bounds and calculate |
| 425 | // the final window drag bounds. |
| 426 | x = Math.min(Math.max(x, mTmpRect.left), mTmpRect.right); |
| 427 | y = Math.min(Math.max(y, mTmpRect.top), mTmpRect.bottom); |
| 428 | dragEnded = true; |
| 429 | } |
| 430 | |
| 431 | updateWindowDragBounds(nX, nY); |
| 432 | updateDimLayerVisibility(nX); |
| 433 | return dragEnded; |
| 434 | } |
| 435 | |
| 436 | private void updateWindowDragBounds(int x, int y) { |
Wale Ogunwale | b8051b8 | 2015-09-17 15:41:52 -0700 | [diff] [blame] | 437 | mWindowDragBounds.set(mWindowOriginalBounds); |
Chong Zhang | b15758a | 2015-11-17 12:12:03 -0800 | [diff] [blame^] | 438 | if (mTask.isDockedInEffect()) { |
| 439 | // Offset the bounds without clamp, the bounds will be shifted later |
| 440 | // by window manager before applying the scrolling. |
| 441 | if (mService.mCurConfiguration.orientation == ORIENTATION_LANDSCAPE) { |
| 442 | mWindowDragBounds.offset(Math.round(x - mStartDragX), 0); |
| 443 | } else { |
| 444 | mWindowDragBounds.offset(0, Math.round(y - mStartDragY)); |
| 445 | } |
| 446 | } else { |
| 447 | mWindowDragBounds.offset(Math.round(x - mStartDragX), Math.round(y - mStartDragY)); |
| 448 | } |
| 449 | if (DEBUG_TASK_POSITIONING) Slog.d(TAG, |
| 450 | "updateWindowDragBounds: " + mWindowDragBounds); |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 453 | private void updateDimLayerVisibility(int x) { |
| 454 | @CtrlType |
| 455 | int dimSide = getDimSide(x); |
| 456 | if (dimSide == mCurrentDimSide) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | mCurrentDimSide = dimSide; |
| 461 | |
| 462 | if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION updateDimLayerVisibility"); |
| 463 | SurfaceControl.openTransaction(); |
| 464 | if (mCurrentDimSide == CTRL_NONE) { |
| 465 | mDimLayer.hide(); |
| 466 | } else { |
| 467 | showDimLayer(); |
| 468 | } |
| 469 | SurfaceControl.closeTransaction(); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Returns the side of the screen the dim layer should be shown. |
| 474 | * @param x horizontal coordinate used to determine if the dim layer should be shown |
| 475 | * @return Returns {@link #CTRL_LEFT} if the dim layer should be shown on the left half of the |
| 476 | * screen, {@link #CTRL_RIGHT} if on the right side, or {@link #CTRL_NONE} if the dim layer |
| 477 | * shouldn't be shown. |
| 478 | */ |
| 479 | private int getDimSide(int x) { |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 480 | if (mTask.mStack.mStackId != FREEFORM_WORKSPACE_STACK_ID |
| 481 | || !mTask.mStack.isFullscreen() |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 482 | || mService.mCurConfiguration.orientation != ORIENTATION_LANDSCAPE) { |
| 483 | return CTRL_NONE; |
| 484 | } |
| 485 | |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 486 | mTask.mStack.getDimBounds(mTmpRect); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 487 | if (x - mSideMargin <= mTmpRect.left) { |
| 488 | return CTRL_LEFT; |
| 489 | } |
| 490 | if (x + mSideMargin >= mTmpRect.right) { |
| 491 | return CTRL_RIGHT; |
| 492 | } |
| 493 | |
| 494 | return CTRL_NONE; |
| 495 | } |
| 496 | |
| 497 | private void showDimLayer() { |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 498 | mTask.mStack.getDimBounds(mTmpRect); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 499 | if (mCurrentDimSide == CTRL_LEFT) { |
| 500 | mTmpRect.right = mTmpRect.centerX(); |
| 501 | } else if (mCurrentDimSide == CTRL_RIGHT) { |
| 502 | mTmpRect.left = mTmpRect.centerX(); |
| 503 | } |
| 504 | |
| 505 | mDimLayer.setBounds(mTmpRect); |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 506 | mDimLayer.show(mService.getDragLayerLocked(), RESIZING_HINT_ALPHA, |
| 507 | RESIZING_HINT_DURATION_MS); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | @Override /** {@link DimLayer.DimLayerUser} */ |
| 511 | public boolean isFullscreen() { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | @Override /** {@link DimLayer.DimLayerUser} */ |
| 516 | public DisplayInfo getDisplayInfo() { |
Chong Zhang | 3005e75 | 2015-09-18 18:46:28 -0700 | [diff] [blame] | 517 | return mTask.mStack.getDisplayInfo(); |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 520 | @Override |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 521 | public void getDimBounds(Rect out) { |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 522 | // This dim layer user doesn't need this. |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public String toShortString() { |
| 527 | return TAG; |
| 528 | } |
Wale Ogunwale | 228d404 | 2015-09-13 10:17:34 -0700 | [diff] [blame] | 529 | } |