blob: e6225030853c2fb3a8c7e8624b9fac7141c6984e [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;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800128 }
129 }
130
131 void unregister() {
132 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "unregistering drag input channel");
133 if (mClientChannel == null) {
134 Slog.e(WindowManagerService.TAG, "Unregister of nonexistent drag input channel");
135 } else {
136 mService.mInputManager.unregisterInputChannel(mServerChannel);
137 InputQueue.unregisterInputChannel(mClientChannel);
138 mClientChannel.dispose();
139 mServerChannel.dispose();
140 mClientChannel = null;
141 mServerChannel = null;
Jeff Browncc4f7db2011-08-30 20:34:48 -0700142
143 mDragWindowHandle = null;
144 mDragApplicationHandle = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800145 }
146 }
147
148 int getDragLayerLw() {
149 return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_DRAG)
150 * WindowManagerService.TYPE_LAYER_MULTIPLIER
151 + WindowManagerService.TYPE_LAYER_OFFSET;
152 }
153
154 /* call out to each visible window/session informing it about the drag
155 */
156 void broadcastDragStartedLw(final float touchX, final float touchY) {
157 // Cache a base-class instance of the clip metadata so that parceling
158 // works correctly in calling out to the apps.
159 mDataDescription = (mData != null) ? mData.getDescription() : null;
160 mNotifiedWindows.clear();
161 mDragInProgress = true;
162
163 if (WindowManagerService.DEBUG_DRAG) {
164 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_STARTED at (" + touchX + ", " + touchY + ")");
165 }
166
167 final int N = mService.mWindows.size();
168 for (int i = 0; i < N; i++) {
169 sendDragStartedLw(mService.mWindows.get(i), touchX, touchY, mDataDescription);
170 }
171 }
172
173 /* helper - send a caller-provided event, presumed to be DRAG_STARTED, if the
174 * designated window is potentially a drop recipient. There are race situations
175 * around DRAG_ENDED broadcast, so we make sure that once we've declared that
176 * the drag has ended, we never send out another DRAG_STARTED for this drag action.
177 *
178 * This method clones the 'event' parameter if it's being delivered to the same
179 * process, so it's safe for the caller to call recycle() on the event afterwards.
180 */
181 private void sendDragStartedLw(WindowState newWin, float touchX, float touchY,
182 ClipDescription desc) {
183 // Don't actually send the event if the drag is supposed to be pinned
184 // to the originating window but 'newWin' is not that window.
185 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
186 final IBinder winBinder = newWin.mClient.asBinder();
187 if (winBinder != mLocalWin) {
188 if (WindowManagerService.DEBUG_DRAG) {
189 Slog.d(WindowManagerService.TAG, "Not dispatching local DRAG_STARTED to " + newWin);
190 }
191 return;
192 }
193 }
194
195 if (mDragInProgress && newWin.isPotentialDragTarget()) {
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700196 DragEvent event = obtainDragEvent(newWin, DragEvent.ACTION_DRAG_STARTED,
197 touchX, touchY, null, desc, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800198 try {
199 newWin.mClient.dispatchDragEvent(event);
200 // track each window that we've notified that the drag is starting
201 mNotifiedWindows.add(newWin);
202 } catch (RemoteException e) {
203 Slog.w(WindowManagerService.TAG, "Unable to drag-start window " + newWin);
204 } finally {
205 // if the callee was local, the dispatch has already recycled the event
206 if (Process.myPid() != newWin.mSession.mPid) {
207 event.recycle();
208 }
209 }
210 }
211 }
212
213 /* helper - construct and send a DRAG_STARTED event only if the window has not
214 * previously been notified, i.e. it became visible after the drag operation
215 * was begun. This is a rare case.
216 */
217 void sendDragStartedIfNeededLw(WindowState newWin) {
218 if (mDragInProgress) {
219 // If we have sent the drag-started, we needn't do so again
220 for (WindowState ws : mNotifiedWindows) {
221 if (ws == newWin) {
222 return;
223 }
224 }
225 if (WindowManagerService.DEBUG_DRAG) {
226 Slog.d(WindowManagerService.TAG, "need to send DRAG_STARTED to new window " + newWin);
227 }
228 sendDragStartedLw(newWin, mCurrentX, mCurrentY, mDataDescription);
229 }
230 }
231
232 void broadcastDragEndedLw() {
233 if (WindowManagerService.DEBUG_DRAG) {
234 Slog.d(WindowManagerService.TAG, "broadcasting DRAG_ENDED");
235 }
236 DragEvent evt = DragEvent.obtain(DragEvent.ACTION_DRAG_ENDED,
237 0, 0, null, null, null, mDragResult);
238 for (WindowState ws: mNotifiedWindows) {
239 try {
240 ws.mClient.dispatchDragEvent(evt);
241 } catch (RemoteException e) {
242 Slog.w(WindowManagerService.TAG, "Unable to drag-end window " + ws);
243 }
244 }
245 mNotifiedWindows.clear();
246 mDragInProgress = false;
247 evt.recycle();
248 }
249
250 void endDragLw() {
251 mService.mDragState.broadcastDragEndedLw();
252
253 // stop intercepting input
254 mService.mDragState.unregister();
255 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
256
257 // free our resources and drop all the object references
258 mService.mDragState.reset();
259 mService.mDragState = null;
260
261 if (WindowManagerService.DEBUG_ORIENTATION) Slog.d(WindowManagerService.TAG, "Performing post-drag rotation");
262 boolean changed = mService.setRotationUncheckedLocked(
263 WindowManagerPolicy.USE_LAST_ROTATION, 0, false);
264 if (changed) {
265 mService.mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
266 }
267 }
268
269 void notifyMoveLw(float x, float y) {
270 final int myPid = Process.myPid();
271
272 // Move the surface to the given touch
273 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, ">>> OPEN TRANSACTION notifyMoveLw");
274 Surface.openTransaction();
275 try {
276 mSurface.setPosition((int)(x - mThumbOffsetX), (int)(y - mThumbOffsetY));
277 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, " DRAG "
278 + mSurface + ": pos=(" +
279 (int)(x - mThumbOffsetX) + "," + (int)(y - mThumbOffsetY) + ")");
280 } finally {
281 Surface.closeTransaction();
282 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "<<< CLOSE TRANSACTION notifyMoveLw");
283 }
284
285 // Tell the affected window
286 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
287 if (touchedWin == null) {
288 if (WindowManagerService.DEBUG_DRAG) Slog.d(WindowManagerService.TAG, "No touched win at x=" + x + " y=" + y);
289 return;
290 }
291 if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) {
292 final IBinder touchedBinder = touchedWin.mClient.asBinder();
293 if (touchedBinder != mLocalWin) {
294 // This drag is pinned only to the originating window, but the drag
295 // point is outside that window. Pretend it's over empty space.
296 touchedWin = null;
297 }
298 }
299 try {
300 // have we dragged over a new window?
301 if ((touchedWin != mTargetWindow) && (mTargetWindow != null)) {
302 if (WindowManagerService.DEBUG_DRAG) {
303 Slog.d(WindowManagerService.TAG, "sending DRAG_EXITED to " + mTargetWindow);
304 }
305 // force DRAG_EXITED_EVENT if appropriate
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700306 DragEvent evt = obtainDragEvent(mTargetWindow, DragEvent.ACTION_DRAG_EXITED,
307 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800308 mTargetWindow.mClient.dispatchDragEvent(evt);
309 if (myPid != mTargetWindow.mSession.mPid) {
310 evt.recycle();
311 }
312 }
313 if (touchedWin != null) {
314 if (false && WindowManagerService.DEBUG_DRAG) {
315 Slog.d(WindowManagerService.TAG, "sending DRAG_LOCATION to " + touchedWin);
316 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700317 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DRAG_LOCATION,
318 x, y, null, null, null, false);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800319 touchedWin.mClient.dispatchDragEvent(evt);
320 if (myPid != touchedWin.mSession.mPid) {
321 evt.recycle();
322 }
323 }
324 } catch (RemoteException e) {
325 Slog.w(WindowManagerService.TAG, "can't send drag notification to windows");
326 }
327 mTargetWindow = touchedWin;
328 }
329
330 // Tell the drop target about the data. Returns 'true' if we can immediately
331 // dispatch the global drag-ended message, 'false' if we need to wait for a
332 // result from the recipient.
333 boolean notifyDropLw(float x, float y) {
334 WindowState touchedWin = getTouchedWinAtPointLw(x, y);
335 if (touchedWin == null) {
336 // "drop" outside a valid window -- no recipient to apply a
337 // timeout to, and we can send the drag-ended message immediately.
338 mDragResult = false;
339 return true;
340 }
341
342 if (WindowManagerService.DEBUG_DRAG) {
343 Slog.d(WindowManagerService.TAG, "sending DROP to " + touchedWin);
344 }
345 final int myPid = Process.myPid();
346 final IBinder token = touchedWin.mClient.asBinder();
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700347 DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DROP, x, y,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800348 null, null, mData, false);
349 try {
350 touchedWin.mClient.dispatchDragEvent(evt);
351
352 // 5 second timeout for this window to respond to the drop
353 mService.mH.removeMessages(H.DRAG_END_TIMEOUT, token);
354 Message msg = mService.mH.obtainMessage(H.DRAG_END_TIMEOUT, token);
355 mService.mH.sendMessageDelayed(msg, 5000);
356 } catch (RemoteException e) {
357 Slog.w(WindowManagerService.TAG, "can't send drop notification to win " + touchedWin);
358 return true;
359 } finally {
360 if (myPid != touchedWin.mSession.mPid) {
361 evt.recycle();
362 }
363 }
364 mToken = token;
365 return false;
366 }
367
368 // Find the visible, touch-deliverable window under the given point
369 private WindowState getTouchedWinAtPointLw(float xf, float yf) {
370 WindowState touchedWin = null;
371 final int x = (int) xf;
372 final int y = (int) yf;
373 final ArrayList<WindowState> windows = mService.mWindows;
374 final int N = windows.size();
375 for (int i = N - 1; i >= 0; i--) {
376 WindowState child = windows.get(i);
377 final int flags = child.mAttrs.flags;
378 if (!child.isVisibleLw()) {
379 // not visible == don't tell about drags
380 continue;
381 }
382 if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) {
383 // not touchable == don't tell about drags
384 continue;
385 }
386
387 child.getTouchableRegion(mTmpRegion);
388
389 final int touchFlags = flags &
390 (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
391 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
392 if (mTmpRegion.contains(x, y) || touchFlags == 0) {
393 // Found it
394 touchedWin = child;
395 break;
396 }
397 }
398
399 return touchedWin;
400 }
Dianne Hackbornffb3d932011-05-17 17:44:51 -0700401
402 private static DragEvent obtainDragEvent(WindowState win, int action,
403 float x, float y, Object localState,
404 ClipDescription description, ClipData data, boolean result) {
405 float winX = x - win.mFrame.left;
406 float winY = y - win.mFrame.top;
407 if (win.mEnforceSizeCompat) {
408 winX *= win.mGlobalScale;
409 winY *= win.mGlobalScale;
410 }
411 return DragEvent.obtain(action, winX, winY, localState, description, data, result);
412 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800413}