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 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 24 | |
| 25 | import android.content.ClipData; |
| 26 | import android.content.ClipDescription; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 27 | import android.graphics.Matrix; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 28 | import android.graphics.Point; |
Vladislav Kaznacheev | c7ad0a0 | 2015-03-19 10:13:31 -0700 | [diff] [blame] | 29 | import android.graphics.Rect; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 30 | import android.graphics.Region; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.Message; |
| 33 | import android.os.Process; |
| 34 | import android.os.RemoteException; |
| 35 | import android.util.Slog; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 36 | import android.view.Display; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 37 | import android.view.DragEvent; |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 38 | import android.view.DropPermissionHolder; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 39 | import android.view.InputChannel; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 40 | import android.view.SurfaceControl; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 41 | import android.view.View; |
| 42 | import android.view.WindowManager; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 43 | import android.view.animation.AlphaAnimation; |
| 44 | import android.view.animation.Animation; |
| 45 | import android.view.animation.AnimationSet; |
| 46 | import android.view.animation.DecelerateInterpolator; |
| 47 | import android.view.animation.Interpolator; |
| 48 | import android.view.animation.ScaleAnimation; |
| 49 | import android.view.animation.Transformation; |
| 50 | import android.view.animation.TranslateAnimation; |
| 51 | |
| 52 | import com.android.server.input.InputApplicationHandle; |
| 53 | import com.android.server.input.InputWindowHandle; |
| 54 | import com.android.server.wm.WindowManagerService.DragInputEventReceiver; |
| 55 | import com.android.server.wm.WindowManagerService.H; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 56 | |
| 57 | import java.util.ArrayList; |
| 58 | |
| 59 | /** |
| 60 | * Drag/drop state |
| 61 | */ |
| 62 | class DragState { |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 63 | private static final long ANIMATION_DURATION_MS = 500; |
| 64 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 65 | final WindowManagerService mService; |
| 66 | IBinder mToken; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 67 | SurfaceControl mSurfaceControl; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 68 | int mFlags; |
| 69 | IBinder mLocalWin; |
Vladislav Kaznacheev | c748241 | 2015-11-05 14:03:07 -0800 | [diff] [blame] | 70 | int mPid; |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 71 | int mUid; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 72 | ClipData mData; |
| 73 | ClipDescription mDataDescription; |
| 74 | boolean mDragResult; |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 75 | float mOriginalAlpha; |
| 76 | float mOriginalX, mOriginalY; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 77 | float mCurrentX, mCurrentY; |
| 78 | float mThumbOffsetX, mThumbOffsetY; |
| 79 | InputChannel mServerChannel, mClientChannel; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 80 | DragInputEventReceiver mInputEventReceiver; |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 81 | InputApplicationHandle mDragApplicationHandle; |
| 82 | InputWindowHandle mDragWindowHandle; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 83 | WindowState mTargetWindow; |
| 84 | ArrayList<WindowState> mNotifiedWindows; |
| 85 | boolean mDragInProgress; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 86 | Display mDisplay; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 87 | |
| 88 | private final Region mTmpRegion = new Region(); |
Vladislav Kaznacheev | c7ad0a0 | 2015-03-19 10:13:31 -0700 | [diff] [blame] | 89 | private final Rect mTmpRect = new Rect(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 90 | |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 91 | private Animation mAnimation; |
| 92 | final Transformation mTransformation = new Transformation(); |
| 93 | private final Interpolator mCubicEaseOutInterpolator = new DecelerateInterpolator(1.5f); |
| 94 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 95 | DragState(WindowManagerService service, IBinder token, SurfaceControl surface, |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 96 | int flags, IBinder localWin) { |
| 97 | mService = service; |
| 98 | mToken = token; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 99 | mSurfaceControl = surface; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 100 | mFlags = flags; |
| 101 | mLocalWin = localWin; |
| 102 | mNotifiedWindows = new ArrayList<WindowState>(); |
| 103 | } |
| 104 | |
| 105 | void reset() { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 106 | if (mSurfaceControl != null) { |
| 107 | mSurfaceControl.destroy(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 108 | } |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 109 | mSurfaceControl = null; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 110 | mFlags = 0; |
| 111 | mLocalWin = null; |
| 112 | mToken = null; |
| 113 | mData = null; |
| 114 | mThumbOffsetX = mThumbOffsetY = 0; |
| 115 | mNotifiedWindows = null; |
| 116 | } |
| 117 | |
Craig Mautner | 3486b96 | 2012-08-27 11:31:24 -0700 | [diff] [blame] | 118 | /** |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 119 | * @param display The Display that the window being dragged is on. |
Craig Mautner | 3486b96 | 2012-08-27 11:31:24 -0700 | [diff] [blame] | 120 | */ |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 121 | void register(Display display) { |
| 122 | mDisplay = display; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 123 | if (DEBUG_DRAG) Slog.d(TAG_WM, "registering drag input channel"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 124 | if (mClientChannel != null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 125 | Slog.e(TAG_WM, "Duplicate register of drag input channel"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 126 | } else { |
| 127 | InputChannel[] channels = InputChannel.openInputChannelPair("drag"); |
| 128 | mServerChannel = channels[0]; |
| 129 | mClientChannel = channels[1]; |
| 130 | mService.mInputManager.registerInputChannel(mServerChannel, null); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 131 | mInputEventReceiver = mService.new DragInputEventReceiver(mClientChannel, |
| 132 | mService.mH.getLooper()); |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 133 | |
| 134 | mDragApplicationHandle = new InputApplicationHandle(null); |
| 135 | mDragApplicationHandle.name = "drag"; |
| 136 | mDragApplicationHandle.dispatchingTimeoutNanos = |
| 137 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 138 | |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 139 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 140 | mDisplay.getDisplayId()); |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 141 | mDragWindowHandle.name = "drag"; |
| 142 | mDragWindowHandle.inputChannel = mServerChannel; |
| 143 | mDragWindowHandle.layer = getDragLayerLw(); |
| 144 | mDragWindowHandle.layoutParamsFlags = 0; |
| 145 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 146 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 147 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 148 | mDragWindowHandle.visible = true; |
| 149 | mDragWindowHandle.canReceiveKeys = false; |
| 150 | mDragWindowHandle.hasFocus = true; |
| 151 | mDragWindowHandle.hasWallpaper = false; |
| 152 | mDragWindowHandle.paused = false; |
| 153 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 154 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 155 | mDragWindowHandle.inputFeatures = 0; |
| 156 | mDragWindowHandle.scaleFactor = 1.0f; |
| 157 | |
| 158 | // The drag window cannot receive new touches. |
| 159 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 160 | |
| 161 | // The drag window covers the entire display |
| 162 | mDragWindowHandle.frameLeft = 0; |
| 163 | mDragWindowHandle.frameTop = 0; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 164 | Point p = new Point(); |
| 165 | mDisplay.getRealSize(p); |
| 166 | mDragWindowHandle.frameRight = p.x; |
| 167 | mDragWindowHandle.frameBottom = p.y; |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 168 | |
| 169 | // Pause rotations before a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 170 | if (DEBUG_ORIENTATION) { |
| 171 | Slog.d(TAG_WM, "Pausing rotation during drag"); |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 172 | } |
| 173 | mService.pauseRotationLocked(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | void unregister() { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 178 | if (DEBUG_DRAG) Slog.d(TAG_WM, "unregistering drag input channel"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 179 | if (mClientChannel == null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 180 | Slog.e(TAG_WM, "Unregister of nonexistent drag input channel"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 181 | } else { |
| 182 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 183 | mInputEventReceiver.dispose(); |
| 184 | mInputEventReceiver = null; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 185 | mClientChannel.dispose(); |
| 186 | mServerChannel.dispose(); |
| 187 | mClientChannel = null; |
| 188 | mServerChannel = null; |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 189 | |
| 190 | mDragWindowHandle = null; |
| 191 | mDragApplicationHandle = null; |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 192 | |
| 193 | // Resume rotations after a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 194 | if (DEBUG_ORIENTATION) { |
| 195 | Slog.d(TAG_WM, "Resuming rotation after drag"); |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 196 | } |
| 197 | mService.resumeRotationLocked(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | int getDragLayerLw() { |
| 202 | return mService.mPolicy.windowTypeToLayerLw(WindowManager.LayoutParams.TYPE_DRAG) |
| 203 | * WindowManagerService.TYPE_LAYER_MULTIPLIER |
| 204 | + WindowManagerService.TYPE_LAYER_OFFSET; |
| 205 | } |
| 206 | |
| 207 | /* call out to each visible window/session informing it about the drag |
| 208 | */ |
| 209 | void broadcastDragStartedLw(final float touchX, final float touchY) { |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 210 | mOriginalX = mCurrentX = touchX; |
| 211 | mOriginalY = mCurrentY = touchY; |
| 212 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 213 | // Cache a base-class instance of the clip metadata so that parceling |
| 214 | // works correctly in calling out to the apps. |
| 215 | mDataDescription = (mData != null) ? mData.getDescription() : null; |
| 216 | mNotifiedWindows.clear(); |
| 217 | mDragInProgress = true; |
| 218 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 219 | if (DEBUG_DRAG) { |
| 220 | Slog.d(TAG_WM, "broadcasting DRAG_STARTED at (" + touchX + ", " + touchY + ")"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Craig Mautner | 5c0e78c | 2012-09-12 16:45:36 -0700 | [diff] [blame] | 223 | final WindowList windows = mService.getWindowListLocked(mDisplay); |
Craig Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 224 | if (windows != null) { |
| 225 | final int N = windows.size(); |
| 226 | for (int i = 0; i < N; i++) { |
| 227 | sendDragStartedLw(windows.get(i), touchX, touchY, mDataDescription); |
| 228 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /* helper - send a caller-provided event, presumed to be DRAG_STARTED, if the |
| 233 | * designated window is potentially a drop recipient. There are race situations |
| 234 | * around DRAG_ENDED broadcast, so we make sure that once we've declared that |
| 235 | * the drag has ended, we never send out another DRAG_STARTED for this drag action. |
| 236 | * |
| 237 | * This method clones the 'event' parameter if it's being delivered to the same |
| 238 | * process, so it's safe for the caller to call recycle() on the event afterwards. |
| 239 | */ |
| 240 | private void sendDragStartedLw(WindowState newWin, float touchX, float touchY, |
| 241 | ClipDescription desc) { |
| 242 | // Don't actually send the event if the drag is supposed to be pinned |
| 243 | // to the originating window but 'newWin' is not that window. |
| 244 | if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) { |
| 245 | final IBinder winBinder = newWin.mClient.asBinder(); |
| 246 | if (winBinder != mLocalWin) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 247 | if (DEBUG_DRAG) { |
| 248 | Slog.d(TAG_WM, "Not dispatching local DRAG_STARTED to " + newWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 249 | } |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (mDragInProgress && newWin.isPotentialDragTarget()) { |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 255 | DragEvent event = obtainDragEvent(newWin, DragEvent.ACTION_DRAG_STARTED, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 256 | touchX, touchY, null, desc, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 257 | try { |
| 258 | newWin.mClient.dispatchDragEvent(event); |
| 259 | // track each window that we've notified that the drag is starting |
| 260 | mNotifiedWindows.add(newWin); |
| 261 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 262 | Slog.w(TAG_WM, "Unable to drag-start window " + newWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 263 | } finally { |
| 264 | // if the callee was local, the dispatch has already recycled the event |
| 265 | if (Process.myPid() != newWin.mSession.mPid) { |
| 266 | event.recycle(); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* helper - construct and send a DRAG_STARTED event only if the window has not |
| 273 | * previously been notified, i.e. it became visible after the drag operation |
| 274 | * was begun. This is a rare case. |
| 275 | */ |
| 276 | void sendDragStartedIfNeededLw(WindowState newWin) { |
| 277 | if (mDragInProgress) { |
| 278 | // If we have sent the drag-started, we needn't do so again |
| 279 | for (WindowState ws : mNotifiedWindows) { |
| 280 | if (ws == newWin) { |
| 281 | return; |
| 282 | } |
| 283 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 284 | if (DEBUG_DRAG) { |
| 285 | Slog.d(TAG_WM, "need to send DRAG_STARTED to new window " + newWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 286 | } |
| 287 | sendDragStartedLw(newWin, mCurrentX, mCurrentY, mDataDescription); |
| 288 | } |
| 289 | } |
| 290 | |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 291 | private void broadcastDragEndedLw() { |
Vladislav Kaznacheev | 68dd2e8 | 2015-10-22 17:06:40 -0700 | [diff] [blame] | 292 | final int myPid = Process.myPid(); |
| 293 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 294 | if (DEBUG_DRAG) { |
| 295 | Slog.d(TAG_WM, "broadcasting DRAG_ENDED"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 296 | } |
Vladislav Kaznacheev | 68dd2e8 | 2015-10-22 17:06:40 -0700 | [diff] [blame] | 297 | for (WindowState ws : mNotifiedWindows) { |
Vladislav Kaznacheev | c748241 | 2015-11-05 14:03:07 -0800 | [diff] [blame] | 298 | float x = 0; |
| 299 | float y = 0; |
| 300 | if (!mDragResult && (ws.mSession.mPid == mPid)) { |
| 301 | // Report unconsumed drop location back to the app that started the drag. |
| 302 | x = mCurrentX; |
| 303 | y = mCurrentY; |
| 304 | } |
Vladislav Kaznacheev | 68dd2e8 | 2015-10-22 17:06:40 -0700 | [diff] [blame] | 305 | DragEvent evt = DragEvent.obtain(DragEvent.ACTION_DRAG_ENDED, |
Vladislav Kaznacheev | c748241 | 2015-11-05 14:03:07 -0800 | [diff] [blame] | 306 | x, y, null, null, null, null, mDragResult); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 307 | try { |
| 308 | ws.mClient.dispatchDragEvent(evt); |
| 309 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 310 | Slog.w(TAG_WM, "Unable to drag-end window " + ws); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 311 | } |
Vladislav Kaznacheev | 68dd2e8 | 2015-10-22 17:06:40 -0700 | [diff] [blame] | 312 | // if the current window is in the same process, |
| 313 | // the dispatch has already recycled the event |
| 314 | if (myPid != ws.mSession.mPid) { |
| 315 | evt.recycle(); |
| 316 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 317 | } |
| 318 | mNotifiedWindows.clear(); |
| 319 | mDragInProgress = false; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void endDragLw() { |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 323 | if (mAnimation != null) { |
| 324 | return; |
| 325 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 326 | if (!mDragResult) { |
| 327 | mAnimation = createReturnAnimationLocked(); |
| 328 | mService.scheduleAnimationLocked(); |
| 329 | return; // Will call cleanUpDragLw when the animation is done. |
| 330 | } |
| 331 | cleanUpDragLw(); |
| 332 | } |
| 333 | |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 334 | void cancelDragLw() { |
| 335 | if (mAnimation != null) { |
| 336 | return; |
| 337 | } |
| 338 | mAnimation = createCancelAnimationLocked(); |
| 339 | mService.scheduleAnimationLocked(); |
| 340 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 341 | |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 342 | private void cleanUpDragLw() { |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 343 | broadcastDragEndedLw(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 344 | |
| 345 | // stop intercepting input |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 346 | unregister(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 347 | |
| 348 | // free our resources and drop all the object references |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 349 | reset(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 350 | mService.mDragState = null; |
Chong Zhang | 8e89b31 | 2015-09-09 15:09:30 -0700 | [diff] [blame] | 351 | |
| 352 | mService.mInputMonitor.updateInputWindowsLw(true /*force*/); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void notifyMoveLw(float x, float y) { |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 356 | if (mAnimation != null) { |
| 357 | return; |
| 358 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 359 | mCurrentX = x; |
| 360 | mCurrentY = y; |
| 361 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 362 | final int myPid = Process.myPid(); |
| 363 | |
| 364 | // Move the surface to the given touch |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 365 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
| 366 | TAG_WM, ">>> OPEN TRANSACTION notifyMoveLw"); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 367 | SurfaceControl.openTransaction(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 368 | try { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 369 | mSurfaceControl.setPosition(x - mThumbOffsetX, y - mThumbOffsetY); |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 370 | if (SHOW_TRANSACTIONS) Slog.i(TAG_WM, " DRAG " |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 371 | + mSurfaceControl + ": pos=(" + |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 372 | (int)(x - mThumbOffsetX) + "," + (int)(y - mThumbOffsetY) + ")"); |
| 373 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 374 | SurfaceControl.closeTransaction(); |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 375 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
| 376 | TAG_WM, "<<< CLOSE TRANSACTION notifyMoveLw"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // Tell the affected window |
| 380 | WindowState touchedWin = getTouchedWinAtPointLw(x, y); |
| 381 | if (touchedWin == null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 382 | if (DEBUG_DRAG) Slog.d(TAG_WM, "No touched win at x=" + x + " y=" + y); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0) { |
| 386 | final IBinder touchedBinder = touchedWin.mClient.asBinder(); |
| 387 | if (touchedBinder != mLocalWin) { |
| 388 | // This drag is pinned only to the originating window, but the drag |
| 389 | // point is outside that window. Pretend it's over empty space. |
| 390 | touchedWin = null; |
| 391 | } |
| 392 | } |
| 393 | try { |
| 394 | // have we dragged over a new window? |
| 395 | if ((touchedWin != mTargetWindow) && (mTargetWindow != null)) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 396 | if (DEBUG_DRAG) { |
| 397 | Slog.d(TAG_WM, "sending DRAG_EXITED to " + mTargetWindow); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 398 | } |
| 399 | // force DRAG_EXITED_EVENT if appropriate |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 400 | DragEvent evt = obtainDragEvent(mTargetWindow, DragEvent.ACTION_DRAG_EXITED, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 401 | x, y, null, null, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 402 | mTargetWindow.mClient.dispatchDragEvent(evt); |
| 403 | if (myPid != mTargetWindow.mSession.mPid) { |
| 404 | evt.recycle(); |
| 405 | } |
| 406 | } |
| 407 | if (touchedWin != null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 408 | if (false && DEBUG_DRAG) { |
| 409 | Slog.d(TAG_WM, "sending DRAG_LOCATION to " + touchedWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 410 | } |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 411 | DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DRAG_LOCATION, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 412 | x, y, null, null, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 413 | touchedWin.mClient.dispatchDragEvent(evt); |
| 414 | if (myPid != touchedWin.mSession.mPid) { |
| 415 | evt.recycle(); |
| 416 | } |
| 417 | } |
| 418 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 419 | Slog.w(TAG_WM, "can't send drag notification to windows"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 420 | } |
| 421 | mTargetWindow = touchedWin; |
| 422 | } |
| 423 | |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 424 | WindowState getDropTargetWinLw(float x, float y) { |
| 425 | return getTouchedWinAtPointLw(x, y); |
| 426 | } |
| 427 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 428 | // Tell the drop target about the data. Returns 'true' if we can immediately |
| 429 | // dispatch the global drag-ended message, 'false' if we need to wait for a |
| 430 | // result from the recipient. |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 431 | boolean notifyDropLw(WindowState touchedWin, DropPermissionHolder dropPermissionHolder, |
| 432 | float x, float y) { |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 433 | if (mAnimation != null) { |
| 434 | return false; |
| 435 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 436 | mCurrentX = x; |
| 437 | mCurrentY = y; |
| 438 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 439 | if (touchedWin == null) { |
| 440 | // "drop" outside a valid window -- no recipient to apply a |
| 441 | // timeout to, and we can send the drag-ended message immediately. |
| 442 | mDragResult = false; |
| 443 | return true; |
| 444 | } |
| 445 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 446 | if (DEBUG_DRAG) { |
| 447 | Slog.d(TAG_WM, "sending DROP to " + touchedWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 448 | } |
| 449 | final int myPid = Process.myPid(); |
| 450 | final IBinder token = touchedWin.mClient.asBinder(); |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 451 | DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DROP, x, y, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 452 | null, null, mData, dropPermissionHolder, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 453 | try { |
| 454 | touchedWin.mClient.dispatchDragEvent(evt); |
| 455 | |
| 456 | // 5 second timeout for this window to respond to the drop |
| 457 | mService.mH.removeMessages(H.DRAG_END_TIMEOUT, token); |
| 458 | Message msg = mService.mH.obtainMessage(H.DRAG_END_TIMEOUT, token); |
| 459 | mService.mH.sendMessageDelayed(msg, 5000); |
| 460 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 461 | Slog.w(TAG_WM, "can't send drop notification to win " + touchedWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 462 | return true; |
| 463 | } finally { |
| 464 | if (myPid != touchedWin.mSession.mPid) { |
| 465 | evt.recycle(); |
| 466 | } |
| 467 | } |
| 468 | mToken = token; |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | // Find the visible, touch-deliverable window under the given point |
| 473 | private WindowState getTouchedWinAtPointLw(float xf, float yf) { |
| 474 | WindowState touchedWin = null; |
| 475 | final int x = (int) xf; |
| 476 | final int y = (int) yf; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 477 | |
Craig Mautner | 5c0e78c | 2012-09-12 16:45:36 -0700 | [diff] [blame] | 478 | final WindowList windows = mService.getWindowListLocked(mDisplay); |
Craig Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 479 | if (windows == null) { |
| 480 | return null; |
| 481 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 482 | final int N = windows.size(); |
| 483 | for (int i = N - 1; i >= 0; i--) { |
| 484 | WindowState child = windows.get(i); |
| 485 | final int flags = child.mAttrs.flags; |
| 486 | if (!child.isVisibleLw()) { |
| 487 | // not visible == don't tell about drags |
| 488 | continue; |
| 489 | } |
| 490 | if ((flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0) { |
| 491 | // not touchable == don't tell about drags |
| 492 | continue; |
| 493 | } |
| 494 | |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 495 | child.getVisibleBounds(mTmpRect); |
Vladislav Kaznacheev | c7ad0a0 | 2015-03-19 10:13:31 -0700 | [diff] [blame] | 496 | if (!mTmpRect.contains(x, y)) { |
| 497 | // outside of this window's activity stack == don't tell about drags |
| 498 | continue; |
| 499 | } |
| 500 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 501 | child.getTouchableRegion(mTmpRegion); |
| 502 | |
| 503 | final int touchFlags = flags & |
| 504 | (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 505 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); |
| 506 | if (mTmpRegion.contains(x, y) || touchFlags == 0) { |
| 507 | // Found it |
| 508 | touchedWin = child; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return touchedWin; |
| 514 | } |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 515 | |
| 516 | private static DragEvent obtainDragEvent(WindowState win, int action, |
| 517 | float x, float y, Object localState, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 518 | ClipDescription description, ClipData data, |
| 519 | DropPermissionHolder dropPermissionHolder, |
| 520 | boolean result) { |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 521 | float winX = x - win.mFrame.left; |
| 522 | float winY = y - win.mFrame.top; |
| 523 | if (win.mEnforceSizeCompat) { |
| 524 | winX *= win.mGlobalScale; |
| 525 | winY *= win.mGlobalScale; |
| 526 | } |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 527 | return DragEvent.obtain(action, winX, winY, localState, description, data, |
| 528 | dropPermissionHolder, result); |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 529 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 530 | |
| 531 | boolean stepAnimationLocked(long currentTimeMs) { |
| 532 | if (mAnimation == null) { |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | mTransformation.clear(); |
| 537 | if (!mAnimation.getTransformation(currentTimeMs, mTransformation)) { |
| 538 | cleanUpDragLw(); |
| 539 | return false; |
| 540 | } |
| 541 | |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 542 | mTransformation.getMatrix().postTranslate( |
| 543 | mCurrentX - mThumbOffsetX, mCurrentY - mThumbOffsetY); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 544 | final float tmpFloats[] = mService.mTmpFloats; |
| 545 | mTransformation.getMatrix().getValues(tmpFloats); |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 546 | mSurfaceControl.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 547 | mSurfaceControl.setAlpha(mTransformation.getAlpha()); |
| 548 | mSurfaceControl.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y], |
| 549 | tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]); |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | private Animation createReturnAnimationLocked() { |
| 554 | final AnimationSet set = new AnimationSet(false); |
| 555 | set.addAnimation(new TranslateAnimation( |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 556 | 0, mOriginalX - mCurrentX, 0, mOriginalY - mCurrentY)); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 557 | set.addAnimation(new AlphaAnimation(mOriginalAlpha, mOriginalAlpha / 2)); |
| 558 | set.setDuration(ANIMATION_DURATION_MS); |
| 559 | set.setInterpolator(mCubicEaseOutInterpolator); |
| 560 | set.initialize(0, 0, 0, 0); |
| 561 | set.start(); // Will start on the first call to getTransformation. |
| 562 | return set; |
| 563 | } |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 564 | |
| 565 | private Animation createCancelAnimationLocked() { |
| 566 | final AnimationSet set = new AnimationSet(false); |
| 567 | set.addAnimation(new ScaleAnimation(1, 0, 1, 0, mThumbOffsetX, mThumbOffsetY)); |
| 568 | set.addAnimation(new AlphaAnimation(mOriginalAlpha, 0)); |
| 569 | set.setDuration(ANIMATION_DURATION_MS); |
| 570 | set.setInterpolator(mCubicEaseOutInterpolator); |
| 571 | set.initialize(0, 0, 0, 0); |
| 572 | set.start(); // Will start on the first call to getTransformation. |
| 573 | return set; |
| 574 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 575 | } |