Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 23 | |
| 24 | import android.content.ClipData; |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 25 | import android.graphics.PixelFormat; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 26 | import android.os.Binder; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 27 | import android.os.Handler; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 28 | import android.os.IBinder; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 29 | import android.os.Looper; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 30 | import android.os.Message; |
| 31 | import android.util.Slog; |
| 32 | import android.view.Display; |
| 33 | import android.view.IWindow; |
| 34 | import android.view.Surface; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 35 | import android.view.SurfaceControl; |
| 36 | import android.view.SurfaceSession; |
| 37 | import android.view.View; |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame^] | 38 | import com.android.server.input.InputWindowHandle; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Managing drag and drop operations initiated by View#startDragAndDrop. |
| 42 | */ |
| 43 | class DragDropController { |
| 44 | private static final float DRAG_SHADOW_ALPHA_TRANSPARENT = .7071f; |
| 45 | private static final long DRAG_TIMEOUT_MS = 5000; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 46 | |
| 47 | // Messages for Handler. |
| 48 | private static final int MSG_DRAG_START_TIMEOUT = 0; |
| 49 | static final int MSG_DRAG_END_TIMEOUT = 1; |
| 50 | static final int MSG_TEAR_DOWN_DRAG_AND_DROP_INPUT = 2; |
| 51 | static final int MSG_ANIMATION_END = 3; |
| 52 | |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 53 | /** |
| 54 | * Drag state per operation. |
| 55 | * The variable is cleared by {@code #onDragStateClosedLocked} which is invoked by DragState |
| 56 | * itself, thus the variable can be null after calling DragState's methods. |
| 57 | */ |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame^] | 58 | private DragState mDragState; |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 59 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 60 | private WindowManagerService mService; |
| 61 | private final Handler mHandler; |
| 62 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 63 | boolean dragDropActiveLocked() { |
| 64 | return mDragState != null; |
| 65 | } |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 66 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame^] | 67 | InputWindowHandle getInputWindowHandleLocked() { |
| 68 | return mDragState.getInputWindowHandle(); |
| 69 | } |
| 70 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 71 | DragDropController(WindowManagerService service, Looper looper) { |
| 72 | mService = service; |
| 73 | mHandler = new DragHandler(service, looper); |
| 74 | } |
| 75 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame^] | 76 | void sendDragStartedIfNeededLocked(WindowState window) { |
| 77 | mDragState.sendDragStartedIfNeededLocked(window); |
| 78 | } |
| 79 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 80 | IBinder prepareDrag(SurfaceSession session, int callerPid, |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 81 | int callerUid, IWindow window, int flags, int width, int height, Surface outSurface) { |
| 82 | if (DEBUG_DRAG) { |
| 83 | Slog.d(TAG_WM, "prepare drag surface: w=" + width + " h=" + height |
| 84 | + " flags=" + Integer.toHexString(flags) + " win=" + window |
| 85 | + " asbinder=" + window.asBinder()); |
| 86 | } |
| 87 | |
| 88 | IBinder token = null; |
| 89 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 90 | synchronized (mService.mWindowMap) { |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 91 | if (dragDropActiveLocked()) { |
| 92 | Slog.w(TAG_WM, "Drag already in progress"); |
| 93 | return null; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 94 | } |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 95 | |
| 96 | // TODO(multi-display): support other displays |
| 97 | final DisplayContent displayContent = |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 98 | mService.getDefaultDisplayContentLocked(); |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 99 | final Display display = displayContent.getDisplay(); |
| 100 | |
| 101 | final SurfaceControl surface = new SurfaceControl.Builder(session) |
| 102 | .setName("drag surface") |
| 103 | .setSize(width, height) |
| 104 | .setFormat(PixelFormat.TRANSLUCENT) |
| 105 | .build(); |
| 106 | surface.setLayerStack(display.getLayerStack()); |
| 107 | float alpha = 1; |
| 108 | if ((flags & View.DRAG_FLAG_OPAQUE) == 0) { |
| 109 | alpha = DRAG_SHADOW_ALPHA_TRANSPARENT; |
| 110 | } |
| 111 | surface.setAlpha(alpha); |
| 112 | |
| 113 | if (SHOW_TRANSACTIONS) Slog.i(TAG_WM, " DRAG " + surface + ": CREATE"); |
| 114 | outSurface.copyFrom(surface); |
| 115 | final IBinder winBinder = window.asBinder(); |
| 116 | token = new Binder(); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 117 | mDragState = new DragState(mService, token, surface, flags, winBinder); |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 118 | mDragState.mPid = callerPid; |
| 119 | mDragState.mUid = callerUid; |
| 120 | mDragState.mOriginalAlpha = alpha; |
| 121 | token = mDragState.mToken = new Binder(); |
| 122 | |
| 123 | // 5 second timeout for this window to actually begin the drag |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 124 | sendTimeoutMessage(MSG_DRAG_START_TIMEOUT, winBinder); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return token; |
| 128 | } |
| 129 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 130 | boolean performDrag(IWindow window, IBinder dragToken, |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 131 | int touchSource, float touchX, float touchY, float thumbCenterX, float thumbCenterY, |
| 132 | ClipData data) { |
| 133 | if (DEBUG_DRAG) { |
| 134 | Slog.d(TAG_WM, "perform drag: win=" + window + " data=" + data); |
| 135 | } |
| 136 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 137 | synchronized (mService.mWindowMap) { |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 138 | if (mDragState == null) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 139 | Slog.w(TAG_WM, "No drag prepared"); |
| 140 | throw new IllegalStateException("performDrag() without prepareDrag()"); |
| 141 | } |
| 142 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 143 | if (dragToken != mDragState.mToken) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 144 | Slog.w(TAG_WM, "Performing mismatched drag"); |
| 145 | throw new IllegalStateException("performDrag() does not match prepareDrag()"); |
| 146 | } |
| 147 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 148 | final WindowState callingWin = mService.windowForClientLocked(null, window, false); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 149 | if (callingWin == null) { |
| 150 | Slog.w(TAG_WM, "Bad requesting window " + window); |
| 151 | return false; // !!! TODO: throw here? |
| 152 | } |
| 153 | |
| 154 | // !!! TODO: if input is not still focused on the initiating window, fail |
| 155 | // the drag initiation (e.g. an alarm window popped up just as the application |
| 156 | // called performDrag() |
| 157 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 158 | mHandler.removeMessages(MSG_DRAG_START_TIMEOUT, window.asBinder()); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 159 | |
| 160 | // !!! TODO: extract the current touch (x, y) in screen coordinates. That |
| 161 | // will let us eliminate the (touchX,touchY) parameters from the API. |
| 162 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 163 | // !!! FIXME: put all this heavy stuff onto the mHandler looper, as well as |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 164 | // the actual drag event dispatch stuff in the dragstate |
| 165 | |
| 166 | final DisplayContent displayContent = callingWin.getDisplayContent(); |
| 167 | if (displayContent == null) { |
| 168 | return false; |
| 169 | } |
| 170 | Display display = displayContent.getDisplay(); |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 171 | mDragState.register(display); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 172 | if (!mService.mInputManager.transferTouchFocus(callingWin.mInputChannel, |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 173 | mDragState.getInputChannel())) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 174 | Slog.e(TAG_WM, "Unable to transfer touch focus"); |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 175 | mDragState.closeLocked(); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 179 | mDragState.mDisplayContent = displayContent; |
| 180 | mDragState.mData = data; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 181 | mDragState.broadcastDragStartedLocked(touchX, touchY); |
| 182 | mDragState.overridePointerIconLocked(touchSource); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 183 | |
| 184 | // remember the thumb offsets for later |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 185 | mDragState.mThumbOffsetX = thumbCenterX; |
| 186 | mDragState.mThumbOffsetY = thumbCenterY; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 187 | |
| 188 | // Make the surface visible at the proper location |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 189 | final SurfaceControl surfaceControl = mDragState.mSurfaceControl; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 190 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
| 191 | TAG_WM, ">>> OPEN TRANSACTION performDrag"); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 192 | mService.openSurfaceTransaction(); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 193 | try { |
| 194 | surfaceControl.setPosition(touchX - thumbCenterX, |
| 195 | touchY - thumbCenterY); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 196 | surfaceControl.setLayer(mDragState.getDragLayerLocked()); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 197 | surfaceControl.setLayerStack(display.getLayerStack()); |
| 198 | surfaceControl.show(); |
| 199 | } finally { |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 200 | mService.closeSurfaceTransaction(); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 201 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
| 202 | TAG_WM, "<<< CLOSE TRANSACTION performDrag"); |
| 203 | } |
| 204 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 205 | mDragState.notifyLocationLocked(touchX, touchY); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return true; // success! |
| 209 | } |
| 210 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 211 | void reportDropResult(IWindow window, boolean consumed) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 212 | IBinder token = window.asBinder(); |
| 213 | if (DEBUG_DRAG) { |
| 214 | Slog.d(TAG_WM, "Drop result=" + consumed + " reported by " + token); |
| 215 | } |
| 216 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 217 | synchronized (mService.mWindowMap) { |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 218 | if (mDragState == null) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 219 | // Most likely the drop recipient ANRed and we ended the drag |
| 220 | // out from under it. Log the issue and move on. |
| 221 | Slog.w(TAG_WM, "Drop result given but no drag in progress"); |
| 222 | return; |
| 223 | } |
| 224 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 225 | if (mDragState.mToken != token) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 226 | // We're in a drag, but the wrong window has responded. |
| 227 | Slog.w(TAG_WM, "Invalid drop-result claim by " + window); |
| 228 | throw new IllegalStateException("reportDropResult() by non-recipient"); |
| 229 | } |
| 230 | |
| 231 | // The right window has responded, even if it's no longer around, |
| 232 | // so be sure to halt the timeout even if the later WindowState |
| 233 | // lookup fails. |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 234 | mHandler.removeMessages(MSG_DRAG_END_TIMEOUT, window.asBinder()); |
| 235 | WindowState callingWin = mService.windowForClientLocked(null, window, false); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 236 | if (callingWin == null) { |
| 237 | Slog.w(TAG_WM, "Bad result-reporting window " + window); |
| 238 | return; // !!! TODO: throw here? |
| 239 | } |
| 240 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 241 | mDragState.mDragResult = consumed; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 242 | mDragState.endDragLocked(); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 246 | void cancelDragAndDrop(IBinder dragToken) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 247 | if (DEBUG_DRAG) { |
| 248 | Slog.d(TAG_WM, "cancelDragAndDrop"); |
| 249 | } |
| 250 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 251 | synchronized (mService.mWindowMap) { |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 252 | if (mDragState == null) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 253 | Slog.w(TAG_WM, "cancelDragAndDrop() without prepareDrag()"); |
| 254 | throw new IllegalStateException("cancelDragAndDrop() without prepareDrag()"); |
| 255 | } |
| 256 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 257 | if (mDragState.mToken != dragToken) { |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 258 | Slog.w(TAG_WM, |
| 259 | "cancelDragAndDrop() does not match prepareDrag()"); |
| 260 | throw new IllegalStateException( |
| 261 | "cancelDragAndDrop() does not match prepareDrag()"); |
| 262 | } |
| 263 | |
Daichi Hirono | 76a26aa | 2017-09-11 15:13:38 +0900 | [diff] [blame] | 264 | mDragState.mDragResult = false; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 265 | mDragState.cancelDragLocked(); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame^] | 269 | /** |
| 270 | * Handles motion events. |
| 271 | * @param keepHandling Whether if the drag operation is continuing or this is the last motion |
| 272 | * event. |
| 273 | * @param newX X coordinate value in dp in the screen coordinate |
| 274 | * @param newY Y coordinate value in dp in the screen coordinate |
| 275 | */ |
| 276 | void handleMotionEvent(boolean keepHandling, float newX, float newY) { |
| 277 | synchronized (mService.mWindowMap) { |
| 278 | if (!dragDropActiveLocked()) { |
| 279 | // The drag has ended but the clean-up message has not been processed by |
| 280 | // window manager. Drop events that occur after this until window manager |
| 281 | // has a chance to clean-up the input handle. |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | if (keepHandling) { |
| 286 | mDragState.notifyMoveLocked(newX, newY); |
| 287 | } else { |
| 288 | mDragState.notifyDropLocked(newX, newY); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 293 | void dragRecipientEntered(IWindow window) { |
| 294 | if (DEBUG_DRAG) { |
| 295 | Slog.d(TAG_WM, "Drag into new candidate view @ " + window.asBinder()); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void dragRecipientExited(IWindow window) { |
| 300 | if (DEBUG_DRAG) { |
| 301 | Slog.d(TAG_WM, "Drag from old candidate view @ " + window.asBinder()); |
| 302 | } |
| 303 | } |
| 304 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 305 | /** |
| 306 | * Sends a message to the Handler managed by DragDropController. |
| 307 | */ |
| 308 | void sendHandlerMessage(int what, Object arg) { |
| 309 | mHandler.obtainMessage(what, arg).sendToTarget(); |
| 310 | } |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 311 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 312 | /** |
| 313 | * Sends a timeout message to the Handler managed by DragDropController. |
| 314 | */ |
| 315 | void sendTimeoutMessage(int what, Object arg) { |
| 316 | mHandler.removeMessages(what, arg); |
| 317 | final Message msg = mHandler.obtainMessage(what, arg); |
| 318 | mHandler.sendMessageDelayed(msg, DRAG_TIMEOUT_MS); |
| 319 | } |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 320 | |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 321 | /** |
| 322 | * Notifies the current drag state is closed. |
| 323 | */ |
| 324 | void onDragStateClosedLocked(DragState dragState) { |
| 325 | if (mDragState != dragState) { |
| 326 | Slog.wtf(TAG_WM, "Unknown drag state is closed"); |
| 327 | return; |
| 328 | } |
| 329 | mDragState = null; |
| 330 | } |
| 331 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 332 | private class DragHandler extends Handler { |
| 333 | /** |
| 334 | * Lock for window manager. |
| 335 | */ |
| 336 | private final WindowManagerService mService; |
| 337 | |
| 338 | DragHandler(WindowManagerService service, Looper looper) { |
| 339 | super(looper); |
| 340 | mService = service; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void handleMessage(Message msg) { |
| 345 | switch (msg.what) { |
| 346 | case MSG_DRAG_START_TIMEOUT: { |
| 347 | IBinder win = (IBinder) msg.obj; |
| 348 | if (DEBUG_DRAG) { |
| 349 | Slog.w(TAG_WM, "Timeout starting drag by win " + win); |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 350 | } |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 351 | synchronized (mService.mWindowMap) { |
| 352 | // !!! TODO: ANR the app that has failed to start the drag in time |
| 353 | if (mDragState != null) { |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 354 | mDragState.closeLocked(); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | break; |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 358 | } |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 359 | |
| 360 | case MSG_DRAG_END_TIMEOUT: { |
| 361 | IBinder win = (IBinder) msg.obj; |
| 362 | if (DEBUG_DRAG) { |
| 363 | Slog.w(TAG_WM, "Timeout ending drag to win " + win); |
| 364 | } |
| 365 | synchronized (mService.mWindowMap) { |
| 366 | // !!! TODO: ANR the drag-receiving app |
| 367 | if (mDragState != null) { |
| 368 | mDragState.mDragResult = false; |
| 369 | mDragState.endDragLocked(); |
| 370 | } |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | case MSG_TEAR_DOWN_DRAG_AND_DROP_INPUT: { |
| 376 | if (DEBUG_DRAG) |
| 377 | Slog.d(TAG_WM, "Drag ending; tearing down input channel"); |
| 378 | DragState.InputInterceptor interceptor = (DragState.InputInterceptor) msg.obj; |
| 379 | if (interceptor != null) { |
| 380 | synchronized (mService.mWindowMap) { |
| 381 | interceptor.tearDown(); |
| 382 | } |
| 383 | } |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | case MSG_ANIMATION_END: { |
| 388 | synchronized (mService.mWindowMap) { |
| 389 | if (mDragState == null) { |
| 390 | Slog.wtf(TAG_WM, "mDragState unexpectedly became null while " + |
| 391 | "plyaing animation"); |
| 392 | return; |
| 393 | } |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 394 | mDragState.closeLocked(); |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | } |
Daichi Hirono | df5cf62 | 2017-09-11 14:59:26 +0900 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |