blob: 63f306f8524cfc27be2ff2b2345aa1e6fa7d4745 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 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
Jeff Brown4532e612012-04-05 14:27:12 -070019import com.android.server.input.InputApplicationHandle;
20import com.android.server.input.InputWindowHandle;
Jeff Brown32cbc38552011-12-01 14:01:49 -080021import com.android.server.wm.WindowManagerService.DragInputEventReceiver;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080022import com.android.server.wm.WindowManagerService.H;
23
24import android.content.ClipData;
25import android.content.ClipDescription;
Craig Mautner39834192012-09-02 07:47:24 -070026import android.graphics.Point;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080027import android.graphics.Region;
28import android.os.IBinder;
29import android.os.Message;
30import android.os.Process;
31import android.os.RemoteException;
32import android.util.Slog;
Craig Mautner39834192012-09-02 07:47:24 -070033import android.view.Display;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080034import android.view.DragEvent;
35import android.view.InputChannel;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080036import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080038import android.view.View;
39import android.view.WindowManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080040
41import java.util.ArrayList;
42
43/**
44 * Drag/drop state
45 */
46class DragState {
47 final WindowManagerService mService;
48 IBinder mToken;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080049 SurfaceControl mSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080050 int mFlags;
51 IBinder mLocalWin;
52 ClipData mData;
53 ClipDescription mDataDescription;
54 boolean mDragResult;
55 float mCurrentX, mCurrentY;
56 float mThumbOffsetX, mThumbOffsetY;
57 InputChannel mServerChannel, mClientChannel;
Jeff Brown32cbc38552011-12-01 14:01:49 -080058 DragInputEventReceiver mInputEventReceiver;
Jeff Brownea426552011-07-18 16:53:48 -070059 InputApplicationHandle mDragApplicationHandle;
60 InputWindowHandle mDragWindowHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080061 WindowState mTargetWindow;
62 ArrayList<WindowState> mNotifiedWindows;
63 boolean mDragInProgress;
Craig Mautner39834192012-09-02 07:47:24 -070064 Display mDisplay;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065
66 private final Region mTmpRegion = new Region();
67
Mathias Agopian3866f0d2013-02-11 22:08:48 -080068 DragState(WindowManagerService service, IBinder token, SurfaceControl surface,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080069 int flags, IBinder localWin) {
70 mService = service;
71 mToken = token;
72 mSurface = surface;
73 mFlags = flags;
74 mLocalWin = localWin;
75 mNotifiedWindows = new ArrayList<WindowState>();
76 }
77
78 void reset() {
79 if (mSurface != null) {
80 mSurface.destroy();
81 }
82 mSurface = null;
83 mFlags = 0;
84 mLocalWin = null;
85 mToken = null;
86 mData = null;
87 mThumbOffsetX = mThumbOffsetY = 0;
88 mNotifiedWindows = null;
89 }
90
Craig Mautner3486b962012-08-27 11:31:24 -070091 /**
Craig Mautner39834192012-09-02 07:47:24 -070092 * @param display The Display that the window being dragged is on.
Craig Mautner3486b962012-08-27 11:31:24 -070093 */
Craig Mautner39834192012-09-02 07:47:24 -070094 void register(Display display) {
95 mDisplay = display;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "registering drag input channel");
97 if (mClientChannel != null) {
98 Slog.e(WindowManagerService.TAG, "Duplicate register of drag input channel");
99 } else {
100 InputChannel[] channels = InputChannel.openInputChannelPair("drag");
101 mServerChannel = channels[0];
102 mClientChannel = channels[1];
103 mService.mInputManager.registerInputChannel(mServerChannel, null);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800104 mInputEventReceiver = mService.new DragInputEventReceiver(mClientChannel,
105 mService.mH.getLooper());
Jeff Brownea426552011-07-18 16:53:48 -0700106
107 mDragApplicationHandle = new InputApplicationHandle(null);
108 mDragApplicationHandle.name = "drag";
109 mDragApplicationHandle.dispatchingTimeoutNanos =
110 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
111
Craig Mautner59c00972012-07-30 12:10:24 -0700112 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null,
Craig Mautner39834192012-09-02 07:47:24 -0700113 mDisplay.getDisplayId());
Jeff Brownea426552011-07-18 16:53:48 -0700114 mDragWindowHandle.name = "drag";
115 mDragWindowHandle.inputChannel = mServerChannel;
116 mDragWindowHandle.layer = getDragLayerLw();
117 mDragWindowHandle.layoutParamsFlags = 0;
118 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
119 mDragWindowHandle.dispatchingTimeoutNanos =
120 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
121 mDragWindowHandle.visible = true;
122 mDragWindowHandle.canReceiveKeys = false;
123 mDragWindowHandle.hasFocus = true;
124 mDragWindowHandle.hasWallpaper = false;
125 mDragWindowHandle.paused = false;
126 mDragWindowHandle.ownerPid = Process.myPid();
127 mDragWindowHandle.ownerUid = Process.myUid();
128 mDragWindowHandle.inputFeatures = 0;
129 mDragWindowHandle.scaleFactor = 1.0f;
130
131 // The drag window cannot receive new touches.
132 mDragWindowHandle.touchableRegion.setEmpty();
133
134 // The drag window covers the entire display
135 mDragWindowHandle.frameLeft = 0;
136 mDragWindowHandle.frameTop = 0;
Craig Mautner39834192012-09-02 07:47:24 -0700137 Point p = new Point();
138 mDisplay.getRealSize(p);
139 mDragWindowHandle.frameRight = p.x;
140 mDragWindowHandle.frameBottom = p.y;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700141
142 // Pause rotations before a drag.
143 if (WindowManagerService.DEBUG_ORIENTATION) {
144 Slog.d(WindowManagerService.TAG, "Pausing rotation during drag");
145 }
146 mService.pauseRotationLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800147 }
148 }
149
150 void unregister() {
151 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "unregistering drag input channel");
152 if (mClientChannel == null) {
153 Slog.e(WindowManagerService.TAG, "Unregister of nonexistent drag input channel");
154 } else {
155 mService.mInputManager.unregisterInputChannel(mServerChannel);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800156 mInputEventReceiver.dispose();
157 mInputEventReceiver = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800158 mClientChannel.dispose();
159 mServerChannel.dispose();
160 mClientChannel = null;
161 mServerChannel = null;
Jeff Browncc4f7db2011-08-30 20:34:48 -0700162
163 mDragWindowHandle = null;
164 mDragApplicationHandle = null;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700165
166 // Resume rotations after a drag.
167 if (WindowManagerService.DEBUG_ORIENTATION) {
168 Slog.d(WindowManagerService.TAG, "Resuming rotation after drag");
169 }
170 mService.resumeRotationLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800171 }
172 }
173
174 int getDragLayerLw() {
175 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_DRAG)
176 * WindowManagerService.TYPE_LAYER_MULTIPLIER
177 + WindowManagerService.TYPE_LAYER_OFFSET;
178 }
179
180 /* call out to each visible window/session informing it about the drag
181 */
182 void broadcastDragStartedLw(final float touchX, final float touchY) {
183 // Cache a base-class instance of the clip metadata so that parceling
184 // works correctly in calling out to the apps.
185 mDataDescription = (mData != null) ? mData.getDescription() : null;
186 mNotifiedWindows.clear();
187 mDragInProgress = true;
188
189 if (WindowManagerService.DEBUG_DRAG) {
190 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_STARTED at (" + touchX + ", " + touchY + ")");
191 }
192
Craig Mautner5c0e78c2012-09-12 16:45:36 -0700193 final WindowList windows = mService.getWindowListLocked(mDisplay);
Craig Mautner2d5618c2012-10-18 13:55:47 -0700194 if (windows != null) {
195 final int N = windows.size();
196 for (int i = 0; i < N; i++) {
197 sendDragStartedLw(windows.get(i), touchX, touchY, mDataDescription);
198 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800199 }
200 }
201
202 /* helper - send a caller-provided event, presumed to be DRAG_STARTED, if the
203 * designated window is potentially a drop recipient. There are race situations
204 * around DRAG_ENDED broadcast, so we make sure that once we've declared that
205 * the drag has ended, we never send out another DRAG_STARTED for this drag action.
206 *
207 * This method clones the 'event' parameter if it's being delivered to the same
208 * process, so it's safe for the caller to call recycle() on the event afterwards.
209 */
210 private void sendDragStartedLw(WindowState newWin, float touchX, float touchY,
211 ClipDescription desc) {
212 // Don't actually send the event if the drag is supposed to be pinned
213 // to the originating window but 'newWin' is not that window.
214 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
215 final IBinder winBinder = newWin.mClient.asBinder();
216 if (winBinder != mLocalWin) {
217 if (WindowManagerService.DEBUG_DRAG) {
218 Slog.d(WindowManagerService.TAG, "Not dispatching local DRAG_STARTED to " + newWin);
219 }
220 return;
221 }
222 }
223
224 if (mDragInProgress && newWin.isPotentialDragTarget()) {
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700225 DragEvent event = obtainDragEvent(newWin, DragEvent.ACTION_DRAG_STARTED,
226 touchX, touchY, null, desc, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 try {
228 newWin.mClient.dispatchDragEvent(event);
229 // track each window that we've notified that the drag is starting
230 mNotifiedWindows.add(newWin);
231 } catch (RemoteException e) {
232 Slog.w(WindowManagerService.TAG, "Unable to drag-start window " + newWin);
233 } finally {
234 // if the callee was local, the dispatch has already recycled the event
235 if (Process.myPid() != newWin.mSession.mPid) {
236 event.recycle();
237 }
238 }
239 }
240 }
241
242 /* helper - construct and send a DRAG_STARTED event only if the window has not
243 * previously been notified, i.e. it became visible after the drag operation
244 * was begun. This is a rare case.
245 */
246 void sendDragStartedIfNeededLw(WindowState newWin) {
247 if (mDragInProgress) {
248 // If we have sent the drag-started, we needn't do so again
249 for (WindowState ws : mNotifiedWindows) {
250 if (ws == newWin) {
251 return;
252 }
253 }
254 if (WindowManagerService.DEBUG_DRAG) {
255 Slog.d(WindowManagerService.TAG, "need to send DRAG_STARTED to new window " + newWin);
256 }
257 sendDragStartedLw(newWin, mCurrentX, mCurrentY, mDataDescription);
258 }
259 }
260
261 void broadcastDragEndedLw() {
262 if (WindowManagerService.DEBUG_DRAG) {
263 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_ENDED");
264 }
265 DragEvent evt = DragEvent.obtain(DragEvent.ACTION_DRAG_ENDED,
266 0, 0, null, null, null, mDragResult);
267 for (WindowState ws: mNotifiedWindows) {
268 try {
269 ws.mClient.dispatchDragEvent(evt);
270 } catch (RemoteException e) {
271 Slog.w(WindowManagerService.TAG, "Unable to drag-end window " + ws);
272 }
273 }
274 mNotifiedWindows.clear();
275 mDragInProgress = false;
276 evt.recycle();
277 }
278
279 void endDragLw() {
280 mService.mDragState.broadcastDragEndedLw();
281
282 // stop intercepting input
283 mService.mDragState.unregister();
284 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
285
286 // free our resources and drop all the object references
287 mService.mDragState.reset();
288 mService.mDragState = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800289 }
290
291 void notifyMoveLw(float x, float y) {
292 final int myPid = Process.myPid();
293
294 // Move the surface to the given touch
Dianne Hackborn36991742011-10-11 21:35:26 -0700295 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
296 WindowManagerService.TAG, ">>> OPEN TRANSACTION notifyMoveLw");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800297 SurfaceControl.openTransaction();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800298 try {
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700299 mSurface.setPosition(x - mThumbOffsetX, y - mThumbOffsetY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800300 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, " DRAG "
301 + mSurface + ": pos=(" +
302 (int)(x - mThumbOffsetX) + "," + (int)(y - mThumbOffsetY) + ")");
303 } finally {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800304 SurfaceControl.closeTransaction();
Dianne Hackborn36991742011-10-11 21:35:26 -0700305 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(
306 WindowManagerService.TAG, "<<< CLOSE TRANSACTION notifyMoveLw");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800307 }
308
309 // Tell the affected window
310 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
311 if (touchedWin == null) {
312 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "No touched win at x=" + x + " y=" + y);
313 return;
314 }
315 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
316 final IBinder touchedBinder = touchedWin.mClient.asBinder();
317 if (touchedBinder != mLocalWin) {
318 // This drag is pinned only to the originating window, but the drag
319 // point is outside that window. Pretend it's over empty space.
320 touchedWin = null;
321 }
322 }
323 try {
324 // have we dragged over a new window?
325 if ((touchedWin != mTargetWindow) && (mTargetWindow != null)) {
326 if (WindowManagerService.DEBUG_DRAG) {
327 Slog.d(WindowManagerService.TAG, "sending DRAG_EXITED to " + mTargetWindow);
328 }
329 // force DRAG_EXITED_EVENT if appropriate
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700330 DragEvent evt = obtainDragEvent(mTargetWindow, DragEvent.ACTION_DRAG_EXITED,
331 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800332 mTargetWindow.mClient.dispatchDragEvent(evt);
333 if (myPid != mTargetWindow.mSession.mPid) {
334 evt.recycle();
335 }
336 }
337 if (touchedWin != null) {
338 if (false && WindowManagerService.DEBUG_DRAG) {
339 Slog.d(WindowManagerService.TAG, "sending DRAG_LOCATION to " + touchedWin);
340 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700341 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DRAG_LOCATION,
342 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800343 touchedWin.mClient.dispatchDragEvent(evt);
344 if (myPid != touchedWin.mSession.mPid) {
345 evt.recycle();
346 }
347 }
348 } catch (RemoteException e) {
349 Slog.w(WindowManagerService.TAG, "can't send drag notification to windows");
350 }
351 mTargetWindow = touchedWin;
352 }
353
354 // Tell the drop target about the data. Returns 'true' if we can immediately
355 // dispatch the global drag-ended message, 'false' if we need to wait for a
356 // result from the recipient.
357 boolean notifyDropLw(float x, float y) {
358 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
359 if (touchedWin == null) {
360 // "drop" outside a valid window -- no recipient to apply a
361 // timeout to, and we can send the drag-ended message immediately.
362 mDragResult = false;
363 return true;
364 }
365
366 if (WindowManagerService.DEBUG_DRAG) {
367 Slog.d(WindowManagerService.TAG, "sending DROP to " + touchedWin);
368 }
369 final int myPid = Process.myPid();
370 final IBinder token = touchedWin.mClient.asBinder();
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700371 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DROP, x, y,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800372 null, null, mData, false);
373 try {
374 touchedWin.mClient.dispatchDragEvent(evt);
375
376 // 5 second timeout for this window to respond to the drop
377 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, token);
378 Message msg = mService.mH.obtainMessage(H.DRAG_END_TIMEOUT, token);
379 mService.mH.sendMessageDelayed(msg, 5000);
380 } catch (RemoteException e) {
381 Slog.w(WindowManagerService.TAG, "can't send drop notification to win " + touchedWin);
382 return true;
383 } finally {
384 if (myPid != touchedWin.mSession.mPid) {
385 evt.recycle();
386 }
387 }
388 mToken = token;
389 return false;
390 }
391
392 // Find the visible, touch-deliverable window under the given point
393 private WindowState getTouchedWinAtPointLw(float xf, float yf) {
394 WindowState touchedWin = null;
395 final int x = (int) xf;
396 final int y = (int) yf;
Craig Mautner59c00972012-07-30 12:10:24 -0700397
Craig Mautner5c0e78c2012-09-12 16:45:36 -0700398 final WindowList windows = mService.getWindowListLocked(mDisplay);
Craig Mautner2d5618c2012-10-18 13:55:47 -0700399 if (windows == null) {
400 return null;
401 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800402 final int N = windows.size();
403 for (int i = N - 1; i >= 0; i--) {
404 WindowState child = windows.get(i);
405 final int flags = child.mAttrs.flags;
406 if (!child.isVisibleLw()) {
407 // not visible == don't tell about drags
408 continue;
409 }
410 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
411 // not touchable == don't tell about drags
412 continue;
413 }
414
415 child.getTouchableRegion(mTmpRegion);
416
417 final int touchFlags = flags &
418 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
419 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
420 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
421 // Found it
422 touchedWin = child;
423 break;
424 }
425 }
426
427 return touchedWin;
428 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700429
430 private static DragEvent obtainDragEvent(WindowState win, int action,
431 float x, float y, Object localState,
432 ClipDescription description, ClipData data, boolean result) {
433 float winX = x - win.mFrame.left;
434 float winY = y - win.mFrame.top;
435 if (win.mEnforceSizeCompat) {
436 winX *= win.mGlobalScale;
437 winY *= win.mGlobalScale;
438 }
439 return DragEvent.obtain(action, winX, winY, localState, description, data, result);
440 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800441}