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