blob: 9cba533310075b20a07a900166b8f07e982052ba [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;
21import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
22import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
23
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090024import android.annotation.NonNull;
Daichi Hironodf5cf622017-09-11 14:59:26 +090025import android.content.ClipData;
Robert Carre625fcf2017-09-01 12:36:28 -070026import android.graphics.PixelFormat;
Daichi Hironodf5cf622017-09-11 14:59:26 +090027import android.os.Binder;
Daichi Hirono58e25e12017-10-25 15:48:08 +090028import android.os.Handler;
Daichi Hironodf5cf622017-09-11 14:59:26 +090029import android.os.IBinder;
Daichi Hirono58e25e12017-10-25 15:48:08 +090030import android.os.Looper;
Daichi Hironodf5cf622017-09-11 14:59:26 +090031import android.os.Message;
32import android.util.Slog;
33import android.view.Display;
34import android.view.IWindow;
35import android.view.Surface;
Daichi Hironodf5cf622017-09-11 14:59:26 +090036import android.view.SurfaceControl;
37import android.view.SurfaceSession;
38import android.view.View;
Adrian Roose99bc052017-11-20 17:55:31 +010039
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090040import com.android.internal.util.Preconditions;
Daichi Hirono768012e2017-10-30 10:05:37 +090041import com.android.server.input.InputWindowHandle;
Adrian Roose99bc052017-11-20 17:55:31 +010042import com.android.server.wm.WindowManagerInternal.IDragDropCallback;
Daichi Hironobb28efb2017-11-21 15:11:01 +090043import java.util.concurrent.atomic.AtomicReference;
Daichi Hironodf5cf622017-09-11 14:59:26 +090044
45/**
46 * Managing drag and drop operations initiated by View#startDragAndDrop.
47 */
48class DragDropController {
49 private static final float DRAG_SHADOW_ALPHA_TRANSPARENT = .7071f;
50 private static final long DRAG_TIMEOUT_MS = 5000;
Daichi Hirono58e25e12017-10-25 15:48:08 +090051
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 Hirono3bae0b02017-10-27 13:05:13 +090058 /**
59 * Drag state per operation.
Daichi Hirono7a5d0072017-10-30 10:07:24 +090060 * 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 Hirono3bae0b02017-10-27 13:05:13 +090062 * 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 Hirono768012e2017-10-30 10:05:37 +090065 private DragState mDragState;
Daichi Hirono76a26aa2017-09-11 15:13:38 +090066
Daichi Hirono58e25e12017-10-25 15:48:08 +090067 private WindowManagerService mService;
68 private final Handler mHandler;
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090069
Daichi Hirono7a5d0072017-10-30 10:07:24 +090070 /**
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090071 * Callback which is used to sync drag state with the vendor-specific code.
72 */
Daichi Hironobb28efb2017-11-21 15:11:01 +090073 @NonNull private AtomicReference<IDragDropCallback> mCallback = new AtomicReference<>(
74 new IDragDropCallback() {});
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090075
Daichi Hirono76a26aa2017-09-11 15:13:38 +090076 boolean dragDropActiveLocked() {
77 return mDragState != null;
78 }
Daichi Hironodf5cf622017-09-11 14:59:26 +090079
Daichi Hirono768012e2017-10-30 10:05:37 +090080 InputWindowHandle getInputWindowHandleLocked() {
81 return mDragState.getInputWindowHandle();
82 }
83
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090084 void registerCallback(IDragDropCallback callback) {
85 Preconditions.checkNotNull(callback);
Daichi Hironobb28efb2017-11-21 15:11:01 +090086 mCallback.set(callback);
Daichi Hirono3c6c95e2017-09-13 12:23:57 +090087 }
88
Daichi Hirono58e25e12017-10-25 15:48:08 +090089 DragDropController(WindowManagerService service, Looper looper) {
90 mService = service;
91 mHandler = new DragHandler(service, looper);
92 }
93
Daichi Hirono768012e2017-10-30 10:05:37 +090094 void sendDragStartedIfNeededLocked(WindowState window) {
95 mDragState.sendDragStartedIfNeededLocked(window);
96 }
97
Daichi Hirono58e25e12017-10-25 15:48:08 +090098 IBinder prepareDrag(SurfaceSession session, int callerPid,
Daichi Hironodf5cf622017-09-11 14:59:26 +090099 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 Hironobb28efb2017-11-21 15:11:01 +0900106 synchronized (mService.mWindowMap) {
107 if (dragDropActiveLocked()) {
108 Slog.w(TAG_WM, "Drag already in progress");
109 return null;
Daichi Hironodf5cf622017-09-11 14:59:26 +0900110 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900111
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 Hironodf5cf622017-09-11 14:59:26 +0900143 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900144 }
145
Daichi Hirono58e25e12017-10-25 15:48:08 +0900146 boolean performDrag(IWindow window, IBinder dragToken,
Daichi Hironodf5cf622017-09-11 14:59:26 +0900147 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 Hironobb28efb2017-11-21 15:11:01 +0900153 if (!mCallback.get().prePerformDrag(window, dragToken, touchSource, touchX, touchY, thumbCenterX,
154 thumbCenterY, data)) {
155 return false;
156 }
157 try {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900158 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 Hironodf5cf622017-09-11 14:59:26 +0900227 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900228 return true; // success!
229 } finally {
230 mCallback.get().postPerformDrag();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900231 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900232 }
233
Daichi Hirono58e25e12017-10-25 15:48:08 +0900234 void reportDropResult(IWindow window, boolean consumed) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900235 IBinder token = window.asBinder();
236 if (DEBUG_DRAG) {
237 Slog.d(TAG_WM, "Drop result=" + consumed + " reported by " + token);
238 }
239
Daichi Hironobb28efb2017-11-21 15:11:01 +0900240 mCallback.get().preReportDropResult(window, consumed);
241 try {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900242 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 Hironodf5cf622017-09-11 14:59:26 +0900249
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900250 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 Hironodf5cf622017-09-11 14:59:26 +0900255
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900256 // 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 Hironodf5cf622017-09-11 14:59:26 +0900265
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900266 mDragState.mDragResult = consumed;
267 mDragState.endDragLocked();
268 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900269 } finally {
270 mCallback.get().postReportDropResult();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900271 }
272 }
273
Daichi Hirono58e25e12017-10-25 15:48:08 +0900274 void cancelDragAndDrop(IBinder dragToken) {
Daichi Hironodf5cf622017-09-11 14:59:26 +0900275 if (DEBUG_DRAG) {
276 Slog.d(TAG_WM, "cancelDragAndDrop");
277 }
278
Daichi Hironobb28efb2017-11-21 15:11:01 +0900279 mCallback.get().preCancelDragAndDrop(dragToken);
280 try {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900281 synchronized (mService.mWindowMap) {
282 if (mDragState == null) {
283 Slog.w(TAG_WM, "cancelDragAndDrop() without prepareDrag()");
284 throw new IllegalStateException("cancelDragAndDrop() without prepareDrag()");
285 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900286
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900287 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 Hironodf5cf622017-09-11 14:59:26 +0900293
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900294 mDragState.mDragResult = false;
295 mDragState.cancelDragLocked();
296 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900297 } finally {
298 mCallback.get().postCancelDragAndDrop();
Daichi Hironodf5cf622017-09-11 14:59:26 +0900299 }
300 }
301
Daichi Hirono768012e2017-10-30 10:05:37 +0900302 /**
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 Hironobb28efb2017-11-21 15:11:01 +0900310 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 Hirono768012e2017-10-30 10:05:37 +0900317
Daichi Hironobb28efb2017-11-21 15:11:01 +0900318 if (keepHandling) {
319 mDragState.notifyMoveLocked(newX, newY);
320 } else {
321 mDragState.notifyDropLocked(newX, newY);
Daichi Hirono768012e2017-10-30 10:05:37 +0900322 }
323 }
324 }
325
Daichi Hironodf5cf622017-09-11 14:59:26 +0900326 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 Hirono58e25e12017-10-25 15:48:08 +0900338 /**
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 Hironodf5cf622017-09-11 14:59:26 +0900344
Daichi Hirono58e25e12017-10-25 15:48:08 +0900345 /**
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 Hironodf5cf622017-09-11 14:59:26 +0900353
Daichi Hirono3bae0b02017-10-27 13:05:13 +0900354 /**
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 Hirono58e25e12017-10-25 15:48:08 +0900365 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 Hironodf5cf622017-09-11 14:59:26 +0900383 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900384
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 Hirono58e25e12017-10-25 15:48:08 +0900389 }
390 }
391 break;
Daichi Hironodf5cf622017-09-11 14:59:26 +0900392 }
Daichi Hirono58e25e12017-10-25 15:48:08 +0900393
394 case MSG_DRAG_END_TIMEOUT: {
Daichi Hirono7a5d0072017-10-30 10:07:24 +0900395 final IBinder win = (IBinder) msg.obj;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900396 if (DEBUG_DRAG) {
397 Slog.w(TAG_WM, "Timeout ending drag to win " + win);
398 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900399
400 synchronized (mService.mWindowMap) {
401 // !!! TODO: ANR the drag-receiving app
402 if (mDragState != null) {
403 mDragState.mDragResult = false;
404 mDragState.endDragLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900405 }
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 Hirono7a5d0072017-10-30 10:07:24 +0900413 final DragState.InputInterceptor interceptor =
414 (DragState.InputInterceptor) msg.obj;
415 if (interceptor == null) return;
416 synchronized (mService.mWindowMap) {
417 interceptor.tearDown();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900418 }
419 break;
420 }
421
422 case MSG_ANIMATION_END: {
Daichi Hironobb28efb2017-11-21 15:11:01 +0900423 synchronized (mService.mWindowMap) {
424 if (mDragState == null) {
425 Slog.wtf(TAG_WM, "mDragState unexpectedly became null while " +
426 "plyaing animation");
427 return;
Daichi Hirono58e25e12017-10-25 15:48:08 +0900428 }
Daichi Hironobb28efb2017-11-21 15:11:01 +0900429 mDragState.closeLocked();
Daichi Hirono58e25e12017-10-25 15:48:08 +0900430 }
431 break;
432 }
Daichi Hironodf5cf622017-09-11 14:59:26 +0900433 }
434 }
435 }
436}