blob: f2e7485895fe97aea72da2c603d9db28e306912d [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
19import com.android.server.wm.WindowManagerService.H;
20
21import android.content.ClipData;
22import android.content.ClipDescription;
23import android.graphics.Region;
24import android.os.IBinder;
25import android.os.Message;
26import android.os.Process;
27import android.os.RemoteException;
28import android.util.Slog;
29import android.view.DragEvent;
30import android.view.InputChannel;
31import android.view.InputQueue;
32import android.view.Surface;
33import android.view.View;
34import android.view.WindowManager;
35import android.view.WindowManagerPolicy;
36
37import java.util.ArrayList;
38
39/**
40 * Drag/drop state
41 */
42class DragState {
43 final WindowManagerService mService;
44 IBinder mToken;
45 Surface mSurface;
46 int mFlags;
47 IBinder mLocalWin;
48 ClipData mData;
49 ClipDescription mDataDescription;
50 boolean mDragResult;
51 float mCurrentX, mCurrentY;
52 float mThumbOffsetX, mThumbOffsetY;
53 InputChannel mServerChannel, mClientChannel;
Jeff Brownea426552011-07-18 16:53:48 -070054 InputApplicationHandle mDragApplicationHandle;
55 InputWindowHandle mDragWindowHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080056 WindowState mTargetWindow;
57 ArrayList<WindowState> mNotifiedWindows;
58 boolean mDragInProgress;
59
60 private final Region mTmpRegion = new Region();
61
62 DragState(WindowManagerService service, IBinder token, Surface surface,
63 int flags, IBinder localWin) {
64 mService = service;
65 mToken = token;
66 mSurface = surface;
67 mFlags = flags;
68 mLocalWin = localWin;
69 mNotifiedWindows = new ArrayList<WindowState>();
70 }
71
72 void reset() {
73 if (mSurface != null) {
74 mSurface.destroy();
75 }
76 mSurface = null;
77 mFlags = 0;
78 mLocalWin = null;
79 mToken = null;
80 mData = null;
81 mThumbOffsetX = mThumbOffsetY = 0;
82 mNotifiedWindows = null;
83 }
84
85 void register() {
86 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "registering drag input channel");
87 if (mClientChannel != null) {
88 Slog.e(WindowManagerService.TAG, "Duplicate register of drag input channel");
89 } else {
90 InputChannel[] channels = InputChannel.openInputChannelPair("drag");
91 mServerChannel = channels[0];
92 mClientChannel = channels[1];
93 mService.mInputManager.registerInputChannel(mServerChannel, null);
94 InputQueue.registerInputChannel(mClientChannel, mService.mDragInputHandler,
95 mService.mH.getLooper().getQueue());
Jeff Brownea426552011-07-18 16:53:48 -070096
97 mDragApplicationHandle = new InputApplicationHandle(null);
98 mDragApplicationHandle.name = "drag";
99 mDragApplicationHandle.dispatchingTimeoutNanos =
100 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
101
102 mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null);
103 mDragWindowHandle.name = "drag";
104 mDragWindowHandle.inputChannel = mServerChannel;
105 mDragWindowHandle.layer = getDragLayerLw();
106 mDragWindowHandle.layoutParamsFlags = 0;
107 mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG;
108 mDragWindowHandle.dispatchingTimeoutNanos =
109 WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
110 mDragWindowHandle.visible = true;
111 mDragWindowHandle.canReceiveKeys = false;
112 mDragWindowHandle.hasFocus = true;
113 mDragWindowHandle.hasWallpaper = false;
114 mDragWindowHandle.paused = false;
115 mDragWindowHandle.ownerPid = Process.myPid();
116 mDragWindowHandle.ownerUid = Process.myUid();
117 mDragWindowHandle.inputFeatures = 0;
118 mDragWindowHandle.scaleFactor = 1.0f;
119
120 // The drag window cannot receive new touches.
121 mDragWindowHandle.touchableRegion.setEmpty();
122
123 // The drag window covers the entire display
124 mDragWindowHandle.frameLeft = 0;
125 mDragWindowHandle.frameTop = 0;
Jeff Brownbc68a592011-07-25 12:58:12 -0700126 mDragWindowHandle.frameRight = mService.mCurDisplayWidth;
127 mDragWindowHandle.frameBottom = mService.mCurDisplayHeight;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700128
129 // Pause rotations before a drag.
130 if (WindowManagerService.DEBUG_ORIENTATION) {
131 Slog.d(WindowManagerService.TAG, "Pausing rotation during drag");
132 }
133 mService.pauseRotationLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800134 }
135 }
136
137 void unregister() {
138 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "unregistering drag input channel");
139 if (mClientChannel == null) {
140 Slog.e(WindowManagerService.TAG, "Unregister of nonexistent drag input channel");
141 } else {
142 mService.mInputManager.unregisterInputChannel(mServerChannel);
143 InputQueue.unregisterInputChannel(mClientChannel);
144 mClientChannel.dispose();
145 mServerChannel.dispose();
146 mClientChannel = null;
147 mServerChannel = null;
Jeff Browncc4f7db2011-08-30 20:34:48 -0700148
149 mDragWindowHandle = null;
150 mDragApplicationHandle = null;
Jeff Brown01a98dd2011-09-20 15:08:29 -0700151
152 // Resume rotations after a drag.
153 if (WindowManagerService.DEBUG_ORIENTATION) {
154 Slog.d(WindowManagerService.TAG, "Resuming rotation after drag");
155 }
156 mService.resumeRotationLocked();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800157 }
158 }
159
160 int getDragLayerLw() {
161 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_DRAG)
162 * WindowManagerService.TYPE_LAYER_MULTIPLIER
163 + WindowManagerService.TYPE_LAYER_OFFSET;
164 }
165
166 /* call out to each visible window/session informing it about the drag
167 */
168 void broadcastDragStartedLw(final float touchX, final float touchY) {
169 // Cache a base-class instance of the clip metadata so that parceling
170 // works correctly in calling out to the apps.
171 mDataDescription = (mData != null) ? mData.getDescription() : null;
172 mNotifiedWindows.clear();
173 mDragInProgress = true;
174
175 if (WindowManagerService.DEBUG_DRAG) {
176 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_STARTED at (" + touchX + ", " + touchY + ")");
177 }
178
179 final int N = mService.mWindows.size();
180 for (int i = 0; i < N; i++) {
181 sendDragStartedLw(mService.mWindows.get(i), touchX, touchY, mDataDescription);
182 }
183 }
184
185 /* helper - send a caller-provided event, presumed to be DRAG_STARTED, if the
186 * designated window is potentially a drop recipient. There are race situations
187 * around DRAG_ENDED broadcast, so we make sure that once we've declared that
188 * the drag has ended, we never send out another DRAG_STARTED for this drag action.
189 *
190 * This method clones the 'event' parameter if it's being delivered to the same
191 * process, so it's safe for the caller to call recycle() on the event afterwards.
192 */
193 private void sendDragStartedLw(WindowState newWin, float touchX, float touchY,
194 ClipDescription desc) {
195 // Don't actually send the event if the drag is supposed to be pinned
196 // to the originating window but 'newWin' is not that window.
197 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
198 final IBinder winBinder = newWin.mClient.asBinder();
199 if (winBinder != mLocalWin) {
200 if (WindowManagerService.DEBUG_DRAG) {
201 Slog.d(WindowManagerService.TAG, "Not dispatching local DRAG_STARTED to " + newWin);
202 }
203 return;
204 }
205 }
206
207 if (mDragInProgress && newWin.isPotentialDragTarget()) {
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700208 DragEvent event = obtainDragEvent(newWin, DragEvent.ACTION_DRAG_STARTED,
209 touchX, touchY, null, desc, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800210 try {
211 newWin.mClient.dispatchDragEvent(event);
212 // track each window that we've notified that the drag is starting
213 mNotifiedWindows.add(newWin);
214 } catch (RemoteException e) {
215 Slog.w(WindowManagerService.TAG, "Unable to drag-start window " + newWin);
216 } finally {
217 // if the callee was local, the dispatch has already recycled the event
218 if (Process.myPid() != newWin.mSession.mPid) {
219 event.recycle();
220 }
221 }
222 }
223 }
224
225 /* helper - construct and send a DRAG_STARTED event only if the window has not
226 * previously been notified, i.e. it became visible after the drag operation
227 * was begun. This is a rare case.
228 */
229 void sendDragStartedIfNeededLw(WindowState newWin) {
230 if (mDragInProgress) {
231 // If we have sent the drag-started, we needn't do so again
232 for (WindowState ws : mNotifiedWindows) {
233 if (ws == newWin) {
234 return;
235 }
236 }
237 if (WindowManagerService.DEBUG_DRAG) {
238 Slog.d(WindowManagerService.TAG, "need to send DRAG_STARTED to new window " + newWin);
239 }
240 sendDragStartedLw(newWin, mCurrentX, mCurrentY, mDataDescription);
241 }
242 }
243
244 void broadcastDragEndedLw() {
245 if (WindowManagerService.DEBUG_DRAG) {
246 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_ENDED");
247 }
248 DragEvent evt = DragEvent.obtain(DragEvent.ACTION_DRAG_ENDED,
249 0, 0, null, null, null, mDragResult);
250 for (WindowState ws: mNotifiedWindows) {
251 try {
252 ws.mClient.dispatchDragEvent(evt);
253 } catch (RemoteException e) {
254 Slog.w(WindowManagerService.TAG, "Unable to drag-end window " + ws);
255 }
256 }
257 mNotifiedWindows.clear();
258 mDragInProgress = false;
259 evt.recycle();
260 }
261
262 void endDragLw() {
263 mService.mDragState.broadcastDragEndedLw();
264
265 // stop intercepting input
266 mService.mDragState.unregister();
267 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
268
269 // free our resources and drop all the object references
270 mService.mDragState.reset();
271 mService.mDragState = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800272 }
273
274 void notifyMoveLw(float x, float y) {
275 final int myPid = Process.myPid();
276
277 // Move the surface to the given touch
278 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, ">>> OPEN TRANSACTION notifyMoveLw");
279 Surface.openTransaction();
280 try {
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700281 mSurface.setPosition(x - mThumbOffsetX, y - mThumbOffsetY);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800282 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, " DRAG "
283 + mSurface + ": pos=(" +
284 (int)(x - mThumbOffsetX) + "," + (int)(y - mThumbOffsetY) + ")");
285 } finally {
286 Surface.closeTransaction();
287 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "<<< CLOSE TRANSACTION notifyMoveLw");
288 }
289
290 // Tell the affected window
291 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
292 if (touchedWin == null) {
293 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "No touched win at x=" + x + " y=" + y);
294 return;
295 }
296 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
297 final IBinder touchedBinder = touchedWin.mClient.asBinder();
298 if (touchedBinder != mLocalWin) {
299 // This drag is pinned only to the originating window, but the drag
300 // point is outside that window. Pretend it's over empty space.
301 touchedWin = null;
302 }
303 }
304 try {
305 // have we dragged over a new window?
306 if ((touchedWin != mTargetWindow) && (mTargetWindow != null)) {
307 if (WindowManagerService.DEBUG_DRAG) {
308 Slog.d(WindowManagerService.TAG, "sending DRAG_EXITED to " + mTargetWindow);
309 }
310 // force DRAG_EXITED_EVENT if appropriate
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700311 DragEvent evt = obtainDragEvent(mTargetWindow, DragEvent.ACTION_DRAG_EXITED,
312 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800313 mTargetWindow.mClient.dispatchDragEvent(evt);
314 if (myPid != mTargetWindow.mSession.mPid) {
315 evt.recycle();
316 }
317 }
318 if (touchedWin != null) {
319 if (false && WindowManagerService.DEBUG_DRAG) {
320 Slog.d(WindowManagerService.TAG, "sending DRAG_LOCATION to " + touchedWin);
321 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700322 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DRAG_LOCATION,
323 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800324 touchedWin.mClient.dispatchDragEvent(evt);
325 if (myPid != touchedWin.mSession.mPid) {
326 evt.recycle();
327 }
328 }
329 } catch (RemoteException e) {
330 Slog.w(WindowManagerService.TAG, "can't send drag notification to windows");
331 }
332 mTargetWindow = touchedWin;
333 }
334
335 // Tell the drop target about the data. Returns 'true' if we can immediately
336 // dispatch the global drag-ended message, 'false' if we need to wait for a
337 // result from the recipient.
338 boolean notifyDropLw(float x, float y) {
339 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
340 if (touchedWin == null) {
341 // "drop" outside a valid window -- no recipient to apply a
342 // timeout to, and we can send the drag-ended message immediately.
343 mDragResult = false;
344 return true;
345 }
346
347 if (WindowManagerService.DEBUG_DRAG) {
348 Slog.d(WindowManagerService.TAG, "sending DROP to " + touchedWin);
349 }
350 final int myPid = Process.myPid();
351 final IBinder token = touchedWin.mClient.asBinder();
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700352 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DROP, x, y,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800353 null, null, mData, false);
354 try {
355 touchedWin.mClient.dispatchDragEvent(evt);
356
357 // 5 second timeout for this window to respond to the drop
358 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, token);
359 Message msg = mService.mH.obtainMessage(H.DRAG_END_TIMEOUT, token);
360 mService.mH.sendMessageDelayed(msg, 5000);
361 } catch (RemoteException e) {
362 Slog.w(WindowManagerService.TAG, "can't send drop notification to win " + touchedWin);
363 return true;
364 } finally {
365 if (myPid != touchedWin.mSession.mPid) {
366 evt.recycle();
367 }
368 }
369 mToken = token;
370 return false;
371 }
372
373 // Find the visible, touch-deliverable window under the given point
374 private WindowState getTouchedWinAtPointLw(float xf, float yf) {
375 WindowState touchedWin = null;
376 final int x = (int) xf;
377 final int y = (int) yf;
378 final ArrayList<WindowState> windows = mService.mWindows;
379 final int N = windows.size();
380 for (int i = N - 1; i >= 0; i--) {
381 WindowState child = windows.get(i);
382 final int flags = child.mAttrs.flags;
383 if (!child.isVisibleLw()) {
384 // not visible == don't tell about drags
385 continue;
386 }
387 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
388 // not touchable == don't tell about drags
389 continue;
390 }
391
392 child.getTouchableRegion(mTmpRegion);
393
394 final int touchFlags = flags &
395 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
396 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
397 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
398 // Found it
399 touchedWin = child;
400 break;
401 }
402 }
403
404 return touchedWin;
405 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700406
407 private static DragEvent obtainDragEvent(WindowState win, int action,
408 float x, float y, Object localState,
409 ClipDescription description, ClipData data, boolean result) {
410 float winX = x - win.mFrame.left;
411 float winY = y - win.mFrame.top;
412 if (win.mEnforceSizeCompat) {
413 winX *= win.mGlobalScale;
414 winY *= win.mGlobalScale;
415 }
416 return DragEvent.obtain(action, winX, winY, localState, description, data, result);
417 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800418}