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