blob: d5f403f8562123c8d7914da486b7e9fe4ee4fbe3 [file] [log] [blame]
Daichi Hironodf5cf622017-09-11 14:59:26 +09001/*
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
17package com.android.server.wm;
18
Daichi Hironodf5cf622017-09-11 14:59:26 +090019import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG;
20import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Daichi Hironodf5cf622017-09-11 14:59:26 +090021import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
22
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090023import android.annotation.NonNull;
Daichi Hironodf5cf622017-09-11 14:59:26 +090024import android.content.ClipData;
25import android.os.Binder;
Daichi Hirono58e25e12017-10-25 15:48:08 +090026import android.os.Handler;
Daichi Hironodf5cf622017-09-11 14:59:26 +090027import android.os.IBinder;
Daichi Hirono58e25e12017-10-25 15:48:08 +090028import android.os.Looper;
Daichi Hironodf5cf622017-09-11 14:59:26 +090029import android.os.Message;
30import android.util.Slog;
31import android.view.Display;
32import android.view.IWindow;
Daichi Hironodf5cf622017-09-11 14:59:26 +090033import android.view.SurfaceControl;
34import android.view.SurfaceSession;
35import android.view.View;
Adrian Roose99bc052017-11-20 17:55:31 +010036
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090037import com.android.internal.util.Preconditions;
Adrian Roose99bc052017-11-20 17:55:31 +010038import com.android.server.wm.WindowManagerInternal.IDragDropCallback;
Garfield Tand427c622018-11-30 13:00:04 -080039
Daichi Hironobb28efb2017-11-21 15:11:01 +090040import java.util.concurrent.atomic.AtomicReference;
Daichi Hironodf5cf622017-09-11 14:59:26 +090041
42/**
43 * Managing drag and drop operations initiated by View#startDragAndDrop.
44 */
45class DragDropController {
46 private static final float DRAG_SHADOW_ALPHA_TRANSPARENT = .7071f;
47 private static final long DRAG_TIMEOUT_MS = 5000;
Daichi Hirono58e25e12017-10-25 15:48:08 +090048
49 // Messages for Handler.
Daichi Hironofabca092017-12-19 15:02:50 +090050 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 Hirono58e25e12017-10-25 15:48:08 +090053
Daichi Hirono3bae0b02017-10-27 13:05:13 +090054 /**
55 * Drag state per operation.
Daichi Hirono7a5d0072017-10-30 10:07:24 +090056 * 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 Hirono3bae0b02017-10-27 13:05:13 +090058 * 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 Hirono768012e2017-10-30 10:05:37 +090061 private DragState mDragState;
Daichi Hirono76a26aa2017-09-11 15:13:38 +090062
Daichi Hirono58e25e12017-10-25 15:48:08 +090063 private WindowManagerService mService;
64 private final Handler mHandler;
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090065
Daichi Hirono7a5d0072017-10-30 10:07:24 +090066 /**
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090067 * Callback which is used to sync drag state with the vendor-specific code.
68 */
Daichi Hironobb28efb2017-11-21 15:11:01 +090069 @NonNull private AtomicReference<IDragDropCallback> mCallback = new AtomicReference<>(
70 new IDragDropCallback() {});
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090071
Daichi Hirono76a26aa2017-09-11 15:13:38 +090072 boolean dragDropActiveLocked() {
Garfield Tand427c622018-11-30 13:00:04 -080073 return mDragState != null && !mDragState.isClosing();
Daichi Hirono76a26aa2017-09-11 15:13:38 +090074 }
Daichi Hironodf5cf622017-09-11 14:59:26 +090075
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090076 void registerCallback(IDragDropCallback callback) {
77 Preconditions.checkNotNull(callback);
Daichi Hironobb28efb2017-11-21 15:11:01 +090078 mCallback.set(callback);
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090079 }
80
Daichi Hirono58e25e12017-10-25 15:48:08 +090081 DragDropController(WindowManagerService service, Looper looper) {
82 mService = service;
83 mHandler = new DragHandler(service, looper);
84 }
85
Daichi Hirono768012e2017-10-30 10:05:37 +090086 void sendDragStartedIfNeededLocked(WindowState window) {
87 mDragState.sendDragStartedIfNeededLocked(window);
88 }
89
Daichi Hironofabca092017-12-19 15:02:50 +090090 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 Hironodf5cf622017-09-11 14:59:26 +090093 if (DEBUG_DRAG) {
Daichi Hironofabca092017-12-19 15:02:50 +090094 Slog.d(TAG_WM, "perform drag: win=" + window + " surface=" + surface + " flags=" +
95 Integer.toHexString(flags) + " data=" + data);
Daichi Hironodf5cf622017-09-11 14:59:26 +090096 }
97
Daichi Hironofabca092017-12-19 15:02:50 +090098 final IBinder dragToken = new Binder();
Daichi Hirono663e9a72017-11-10 13:51:02 +090099 final boolean callbackResult = mCallback.get().prePerformDrag(window, dragToken,
100 touchSource, touchX, touchY, thumbCenterX, thumbCenterY, data);
Daichi Hironobb28efb2017-11-21 15:11:01 +0900101 try {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700102 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900103 try {
Daichi Hirono663e9a72017-11-10 13:51:02 +0900104 if (!callbackResult) {
Daichi Hironofabca092017-12-19 15:02:50 +0900105 Slog.w(TAG_WM, "IDragDropCallback rejects the performDrag request");
106 return null;
Daichi Hirono663e9a72017-11-10 13:51:02 +0900107 }
108
Daichi Hironofabca092017-12-19 15:02:50 +0900109 if (dragDropActiveLocked()) {
110 Slog.w(TAG_WM, "Drag already in progress");
111 return null;
112 }
Daichi Hirono663e9a72017-11-10 13:51:02 +0900113
114 final WindowState callingWin = mService.windowForClientLocked(
115 null, window, false);
Arthur Hungb62e5002019-08-29 12:28:07 +0800116 if (callingWin == null || callingWin.cantReceiveTouchInput()) {
Daichi Hirono663e9a72017-11-10 13:51:02 +0900117 Slog.w(TAG_WM, "Bad requesting window " + window);
Daichi Hironofabca092017-12-19 15:02:50 +0900118 return null; // !!! TODO: throw here?
Daichi Hirono663e9a72017-11-10 13:51:02 +0900119 }
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 Hironofabca092017-12-19 15:02:50 +0900131 // !!! TODO(multi-display): support other displays
132
Daichi Hirono663e9a72017-11-10 13:51:02 +0900133 final DisplayContent displayContent = callingWin.getDisplayContent();
134 if (displayContent == null) {
135 Slog.w(TAG_WM, "display content is null");
Daichi Hironofabca092017-12-19 15:02:50 +0900136 return null;
Daichi Hirono663e9a72017-11-10 13:51:02 +0900137 }
138
Daichi Hironofabca092017-12-19 15:02:50 +0900139 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 Hsu654a6f92018-07-13 22:59:36 +0800149 mDragState.mDisplayContent = displayContent;
Daichi Hironofabca092017-12-19 15:02:50 +0900150
Daichi Hirono663e9a72017-11-10 13:51:02 +0900151 final Display display = displayContent.getDisplay();
Daichi Hirono4a545172018-02-01 10:59:48 +0900152 if (!mCallback.get().registerInputChannel(
153 mDragState, display, mService.mInputManager,
154 callingWin.mInputChannel)) {
Daichi Hirono663e9a72017-11-10 13:51:02 +0900155 Slog.e(TAG_WM, "Unable to transfer touch focus");
Daichi Hironofabca092017-12-19 15:02:50 +0900156 return null;
Daichi Hirono663e9a72017-11-10 13:51:02 +0900157 }
158
Daichi Hirono663e9a72017-11-10 13:51:02 +0900159 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 Hironoa1fb9be2017-12-18 17:02:54 +0900169
Arthur Hungb62e5002019-08-29 12:28:07 +0800170 final SurfaceControl.Transaction transaction = mDragState.mTransaction;
Daichi Hironoa1fb9be2017-12-18 17:02:54 +0900171 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 Hirono663e9a72017-11-10 13:51:02 +0900180 }
181
182 mDragState.notifyLocationLocked(touchX, touchY);
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900183 } finally {
Daichi Hironoa1fb9be2017-12-18 17:02:54 +0900184 if (surface != null) {
185 surface.release();
186 }
Daichi Hirono663e9a72017-11-10 13:51:02 +0900187 if (mDragState != null && !mDragState.isInProgress()) {
188 mDragState.closeLocked();
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900189 }
190 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900191 }
Daichi Hironofabca092017-12-19 15:02:50 +0900192 return dragToken; // success!
Daichi Hironobb28efb2017-11-21 15:11:01 +0900193 } finally {
194 mCallback.get().postPerformDrag();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900195 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900196 }
197
Daichi Hirono58e25e12017-10-25 15:48:08 +0900198 void reportDropResult(IWindow window, boolean consumed) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900199 IBinder token = window.asBinder();
200 if (DEBUG_DRAG) {
201 Slog.d(TAG_WM, "Drop result=" + consumed + " reported by " + token);
202 }
203
Daichi Hironobb28efb2017-11-21 15:11:01 +0900204 mCallback.get().preReportDropResult(window, consumed);
205 try {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700206 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900207 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 Hironodf5cf622017-09-11 14:59:26 +0900213
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900214 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 Hironodf5cf622017-09-11 14:59:26 +0900219
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900220 // 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 Hironodf5cf622017-09-11 14:59:26 +0900229
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900230 mDragState.mDragResult = consumed;
231 mDragState.endDragLocked();
232 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900233 } finally {
234 mCallback.get().postReportDropResult();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900235 }
236 }
237
Daichi Hirono3e9d5102019-04-15 15:58:11 +0900238 void cancelDragAndDrop(IBinder dragToken, boolean skipAnimation) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900239 if (DEBUG_DRAG) {
240 Slog.d(TAG_WM, "cancelDragAndDrop");
241 }
242
Daichi Hironobb28efb2017-11-21 15:11:01 +0900243 mCallback.get().preCancelDragAndDrop(dragToken);
244 try {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700245 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900246 if (mDragState == null) {
247 Slog.w(TAG_WM, "cancelDragAndDrop() without prepareDrag()");
248 throw new IllegalStateException("cancelDragAndDrop() without prepareDrag()");
249 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900250
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900251 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 Hironodf5cf622017-09-11 14:59:26 +0900257
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900258 mDragState.mDragResult = false;
Daichi Hirono3e9d5102019-04-15 15:58:11 +0900259 mDragState.cancelDragLocked(skipAnimation);
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900260 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900261 } finally {
262 mCallback.get().postCancelDragAndDrop();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900263 }
264 }
265
Daichi Hirono768012e2017-10-30 10:05:37 +0900266 /**
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 Ogunwaledb485de2018-10-29 09:47:07 -0700274 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900275 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 Hirono768012e2017-10-30 10:05:37 +0900281
Daichi Hironobb28efb2017-11-21 15:11:01 +0900282 if (keepHandling) {
283 mDragState.notifyMoveLocked(newX, newY);
284 } else {
285 mDragState.notifyDropLocked(newX, newY);
Daichi Hirono768012e2017-10-30 10:05:37 +0900286 }
287 }
288 }
289
Daichi Hironodf5cf622017-09-11 14:59:26 +0900290 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 Hirono58e25e12017-10-25 15:48:08 +0900302 /**
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 Hironodf5cf622017-09-11 14:59:26 +0900308
Daichi Hirono58e25e12017-10-25 15:48:08 +0900309 /**
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 Hironodf5cf622017-09-11 14:59:26 +0900317
Daichi Hirono3bae0b02017-10-27 13:05:13 +0900318 /**
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 Hirono58e25e12017-10-25 15:48:08 +0900329 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 Hirono58e25e12017-10-25 15:48:08 +0900343 case MSG_DRAG_END_TIMEOUT: {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900344 final IBinder win = (IBinder) msg.obj;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900345 if (DEBUG_DRAG) {
346 Slog.w(TAG_WM, "Timeout ending drag to win " + win);
347 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900348
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700349 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900350 // !!! TODO: ANR the drag-receiving app
351 if (mDragState != null) {
352 mDragState.mDragResult = false;
353 mDragState.endDragLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900354 }
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 Hirono7a5d0072017-10-30 10:07:24 +0900362 final DragState.InputInterceptor interceptor =
363 (DragState.InputInterceptor) msg.obj;
364 if (interceptor == null) return;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700365 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900366 interceptor.tearDown();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900367 }
368 break;
369 }
370
371 case MSG_ANIMATION_END: {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700372 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900373 if (mDragState == null) {
374 Slog.wtf(TAG_WM, "mDragState unexpectedly became null while " +
375 "plyaing animation");
376 return;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900377 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900378 mDragState.closeLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900379 }
380 break;
381 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900382 }
383 }
384 }
385}