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