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