blob: 716e4ef2ef8234b9a47640524692471a7e31dc5e [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);
116 if (callingWin == null) {
117 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
170 final SurfaceControl.Transaction transaction =
171 callingWin.getPendingTransaction();
172 transaction.setAlpha(surfaceControl, mDragState.mOriginalAlpha);
173 transaction.setPosition(
174 surfaceControl, touchX - thumbCenterX, touchY - thumbCenterY);
175 transaction.show(surfaceControl);
176 displayContent.reparentToOverlay(transaction, surfaceControl);
177 callingWin.scheduleAnimation();
178
179 if (SHOW_LIGHT_TRANSACTIONS) {
180 Slog.i(TAG_WM, "<<< CLOSE TRANSACTION performDrag");
Daichi Hirono663e9a72017-11-10 13:51:02 +0900181 }
182
183 mDragState.notifyLocationLocked(touchX, touchY);
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900184 } finally {
Daichi Hironoa1fb9be2017-12-18 17:02:54 +0900185 if (surface != null) {
186 surface.release();
187 }
Daichi Hirono663e9a72017-11-10 13:51:02 +0900188 if (mDragState != null && !mDragState.isInProgress()) {
189 mDragState.closeLocked();
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900190 }
191 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900192 }
Daichi Hironofabca092017-12-19 15:02:50 +0900193 return dragToken; // success!
Daichi Hironobb28efb2017-11-21 15:11:01 +0900194 } finally {
195 mCallback.get().postPerformDrag();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900196 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900197 }
198
Daichi Hirono58e25e12017-10-25 15:48:08 +0900199 void reportDropResult(IWindow window, boolean consumed) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900200 IBinder token = window.asBinder();
201 if (DEBUG_DRAG) {
202 Slog.d(TAG_WM, "Drop result=" + consumed + " reported by " + token);
203 }
204
Daichi Hironobb28efb2017-11-21 15:11:01 +0900205 mCallback.get().preReportDropResult(window, consumed);
206 try {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700207 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900208 if (mDragState == null) {
209 // Most likely the drop recipient ANRed and we ended the drag
210 // out from under it. Log the issue and move on.
211 Slog.w(TAG_WM, "Drop result given but no drag in progress");
212 return;
213 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900214
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900215 if (mDragState.mToken != token) {
216 // We're in a drag, but the wrong window has responded.
217 Slog.w(TAG_WM, "Invalid drop-result claim by " + window);
218 throw new IllegalStateException("reportDropResult() by non-recipient");
219 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900220
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900221 // The right window has responded, even if it's no longer around,
222 // so be sure to halt the timeout even if the later WindowState
223 // lookup fails.
224 mHandler.removeMessages(MSG_DRAG_END_TIMEOUT, window.asBinder());
225 WindowState callingWin = mService.windowForClientLocked(null, window, false);
226 if (callingWin == null) {
227 Slog.w(TAG_WM, "Bad result-reporting window " + window);
228 return; // !!! TODO: throw here?
229 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900230
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900231 mDragState.mDragResult = consumed;
232 mDragState.endDragLocked();
233 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900234 } finally {
235 mCallback.get().postReportDropResult();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900236 }
237 }
238
Daichi Hirono58e25e12017-10-25 15:48:08 +0900239 void cancelDragAndDrop(IBinder dragToken) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900240 if (DEBUG_DRAG) {
241 Slog.d(TAG_WM, "cancelDragAndDrop");
242 }
243
Daichi Hironobb28efb2017-11-21 15:11:01 +0900244 mCallback.get().preCancelDragAndDrop(dragToken);
245 try {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700246 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900247 if (mDragState == null) {
248 Slog.w(TAG_WM, "cancelDragAndDrop() without prepareDrag()");
249 throw new IllegalStateException("cancelDragAndDrop() without prepareDrag()");
250 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900251
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900252 if (mDragState.mToken != dragToken) {
253 Slog.w(TAG_WM,
254 "cancelDragAndDrop() does not match prepareDrag()");
255 throw new IllegalStateException(
256 "cancelDragAndDrop() does not match prepareDrag()");
257 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900258
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900259 mDragState.mDragResult = false;
260 mDragState.cancelDragLocked();
261 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900262 } finally {
263 mCallback.get().postCancelDragAndDrop();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900264 }
265 }
266
Daichi Hirono768012e2017-10-30 10:05:37 +0900267 /**
268 * Handles motion events.
269 * @param keepHandling Whether if the drag operation is continuing or this is the last motion
270 * event.
271 * @param newX X coordinate value in dp in the screen coordinate
272 * @param newY Y coordinate value in dp in the screen coordinate
273 */
274 void handleMotionEvent(boolean keepHandling, float newX, float newY) {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700275 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900276 if (!dragDropActiveLocked()) {
277 // The drag has ended but the clean-up message has not been processed by
278 // window manager. Drop events that occur after this until window manager
279 // has a chance to clean-up the input handle.
280 return;
281 }
Daichi Hirono768012e2017-10-30 10:05:37 +0900282
Daichi Hironobb28efb2017-11-21 15:11:01 +0900283 if (keepHandling) {
284 mDragState.notifyMoveLocked(newX, newY);
285 } else {
286 mDragState.notifyDropLocked(newX, newY);
Daichi Hirono768012e2017-10-30 10:05:37 +0900287 }
288 }
289 }
290
Daichi Hironodf5cf622017-09-11 14:59:26 +0900291 void dragRecipientEntered(IWindow window) {
292 if (DEBUG_DRAG) {
293 Slog.d(TAG_WM, "Drag into new candidate view @ " + window.asBinder());
294 }
295 }
296
297 void dragRecipientExited(IWindow window) {
298 if (DEBUG_DRAG) {
299 Slog.d(TAG_WM, "Drag from old candidate view @ " + window.asBinder());
300 }
301 }
302
Daichi Hirono58e25e12017-10-25 15:48:08 +0900303 /**
304 * Sends a message to the Handler managed by DragDropController.
305 */
306 void sendHandlerMessage(int what, Object arg) {
307 mHandler.obtainMessage(what, arg).sendToTarget();
308 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900309
Daichi Hirono58e25e12017-10-25 15:48:08 +0900310 /**
311 * Sends a timeout message to the Handler managed by DragDropController.
312 */
313 void sendTimeoutMessage(int what, Object arg) {
314 mHandler.removeMessages(what, arg);
315 final Message msg = mHandler.obtainMessage(what, arg);
316 mHandler.sendMessageDelayed(msg, DRAG_TIMEOUT_MS);
317 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900318
Daichi Hirono3bae0b02017-10-27 13:05:13 +0900319 /**
320 * Notifies the current drag state is closed.
321 */
322 void onDragStateClosedLocked(DragState dragState) {
323 if (mDragState != dragState) {
324 Slog.wtf(TAG_WM, "Unknown drag state is closed");
325 return;
326 }
327 mDragState = null;
328 }
329
Daichi Hirono58e25e12017-10-25 15:48:08 +0900330 private class DragHandler extends Handler {
331 /**
332 * Lock for window manager.
333 */
334 private final WindowManagerService mService;
335
336 DragHandler(WindowManagerService service, Looper looper) {
337 super(looper);
338 mService = service;
339 }
340
341 @Override
342 public void handleMessage(Message msg) {
343 switch (msg.what) {
Daichi Hirono58e25e12017-10-25 15:48:08 +0900344 case MSG_DRAG_END_TIMEOUT: {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900345 final IBinder win = (IBinder) msg.obj;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900346 if (DEBUG_DRAG) {
347 Slog.w(TAG_WM, "Timeout ending drag to win " + win);
348 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900349
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700350 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900351 // !!! TODO: ANR the drag-receiving app
352 if (mDragState != null) {
353 mDragState.mDragResult = false;
354 mDragState.endDragLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900355 }
356 }
357 break;
358 }
359
360 case MSG_TEAR_DOWN_DRAG_AND_DROP_INPUT: {
361 if (DEBUG_DRAG)
362 Slog.d(TAG_WM, "Drag ending; tearing down input channel");
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900363 final DragState.InputInterceptor interceptor =
364 (DragState.InputInterceptor) msg.obj;
365 if (interceptor == null) return;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700366 synchronized (mService.mGlobalLock) {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900367 interceptor.tearDown();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900368 }
369 break;
370 }
371
372 case MSG_ANIMATION_END: {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700373 synchronized (mService.mGlobalLock) {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900374 if (mDragState == null) {
375 Slog.wtf(TAG_WM, "mDragState unexpectedly became null while " +
376 "plyaing animation");
377 return;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900378 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900379 mDragState.closeLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900380 }
381 break;
382 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900383 }
384 }
385 }
386}