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