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 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 19 | import static com.android.server.wm.DragDropController.MSG_ANIMATION_END; |
| 20 | import static com.android.server.wm.DragDropController.MSG_DRAG_END_TIMEOUT; |
| 21 | import static com.android.server.wm.DragDropController.MSG_TEAR_DOWN_DRAG_AND_DROP_INPUT; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DRAG; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; |
| 25 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 26 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 27 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 28 | import android.animation.Animator; |
| 29 | import android.animation.PropertyValuesHolder; |
| 30 | import android.animation.ValueAnimator; |
| 31 | import android.annotation.Nullable; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 32 | import android.content.ClipData; |
| 33 | import android.content.ClipDescription; |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 34 | import android.content.Context; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 35 | import android.graphics.Point; |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 36 | import android.hardware.input.InputManager; |
Yorke Lee | 0e85247 | 2016-06-15 10:03:18 -0700 | [diff] [blame] | 37 | import android.os.Build; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 38 | import android.os.IBinder; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 39 | import android.os.Process; |
| 40 | import android.os.RemoteException; |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 41 | import android.os.ServiceManager; |
| 42 | import android.os.UserHandle; |
| 43 | import android.os.UserManager; |
| 44 | import android.os.IUserManager; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 45 | import android.util.Slog; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 46 | import android.view.Display; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 47 | import android.view.DragEvent; |
| 48 | import android.view.InputChannel; |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 49 | import android.view.InputDevice; |
| 50 | import android.view.PointerIcon; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 51 | import android.view.SurfaceControl; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 52 | import android.view.View; |
| 53 | import android.view.WindowManager; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 54 | import android.view.animation.DecelerateInterpolator; |
| 55 | import android.view.animation.Interpolator; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 56 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 57 | import com.android.internal.view.IDragAndDropPermissions; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 58 | import com.android.server.input.InputApplicationHandle; |
| 59 | import com.android.server.input.InputWindowHandle; |
Vladislav Kaznacheev | 9149d2b | 2015-12-15 12:16:28 -0800 | [diff] [blame] | 60 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 61 | import java.util.ArrayList; |
| 62 | |
| 63 | /** |
| 64 | * Drag/drop state |
| 65 | */ |
| 66 | class DragState { |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 67 | private static final long MIN_ANIMATION_DURATION_MS = 195; |
| 68 | private static final long MAX_ANIMATION_DURATION_MS = 375; |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 69 | |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 70 | private static final int DRAG_FLAGS_URI_ACCESS = View.DRAG_FLAG_GLOBAL_URI_READ | |
| 71 | View.DRAG_FLAG_GLOBAL_URI_WRITE; |
| 72 | |
| 73 | private static final int DRAG_FLAGS_URI_PERMISSIONS = DRAG_FLAGS_URI_ACCESS | |
| 74 | View.DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION | |
| 75 | View.DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION; |
| 76 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 77 | // Property names for animations |
| 78 | private static final String ANIMATED_PROPERTY_X = "x"; |
| 79 | private static final String ANIMATED_PROPERTY_Y = "y"; |
| 80 | private static final String ANIMATED_PROPERTY_ALPHA = "alpha"; |
| 81 | private static final String ANIMATED_PROPERTY_SCALE = "scale"; |
| 82 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 83 | final WindowManagerService mService; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 84 | final DragDropController mDragDropController; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 85 | IBinder mToken; |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 86 | /** |
| 87 | * Do not use the variable from the out of animation thread while mAnimator is not null. |
| 88 | */ |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 89 | SurfaceControl mSurfaceControl; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 90 | int mFlags; |
| 91 | IBinder mLocalWin; |
Vladislav Kaznacheev | c748241 | 2015-11-05 14:03:07 -0800 | [diff] [blame] | 92 | int mPid; |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 93 | int mUid; |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 94 | int mSourceUserId; |
| 95 | boolean mCrossProfileCopyAllowed; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 96 | ClipData mData; |
| 97 | ClipDescription mDataDescription; |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 98 | int mTouchSource; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 99 | boolean mDragResult; |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 100 | float mOriginalAlpha; |
| 101 | float mOriginalX, mOriginalY; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 102 | float mCurrentX, mCurrentY; |
| 103 | float mThumbOffsetX, mThumbOffsetY; |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 104 | InputInterceptor mInputInterceptor; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 105 | WindowState mTargetWindow; |
| 106 | ArrayList<WindowState> mNotifiedWindows; |
| 107 | boolean mDragInProgress; |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 108 | /** |
| 109 | * Whether if animation is completed. Needs to be volatile to update from the animation thread |
| 110 | * without having a WM lock. |
| 111 | */ |
| 112 | volatile boolean mAnimationCompleted = false; |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 113 | DisplayContent mDisplayContent; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 114 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 115 | @Nullable private ValueAnimator mAnimator; |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 116 | private final Interpolator mCubicEaseOutInterpolator = new DecelerateInterpolator(1.5f); |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 117 | private Point mDisplaySize = new Point(); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 118 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 119 | DragState(WindowManagerService service, IBinder token, SurfaceControl surface, |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 120 | int flags, IBinder localWin) { |
| 121 | mService = service; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 122 | mDragDropController = service.mDragDropController; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 123 | mToken = token; |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 124 | mSurfaceControl = surface; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 125 | mFlags = flags; |
| 126 | mLocalWin = localWin; |
| 127 | mNotifiedWindows = new ArrayList<WindowState>(); |
| 128 | } |
| 129 | |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 130 | /** |
| 131 | * After calling this, DragDropController#onDragStateClosedLocked is invoked, which causes |
| 132 | * DragDropController#mDragState becomes null. |
| 133 | */ |
| 134 | void closeLocked() { |
| 135 | // Unregister the input interceptor. |
| 136 | if (mInputInterceptor != null) { |
| 137 | if (DEBUG_DRAG) |
| 138 | Slog.d(TAG_WM, "unregistering drag input channel"); |
| 139 | |
| 140 | // Input channel should be disposed on the thread where the input is being handled. |
| 141 | mDragDropController.sendHandlerMessage( |
| 142 | MSG_TEAR_DOWN_DRAG_AND_DROP_INPUT, mInputInterceptor); |
| 143 | mInputInterceptor = null; |
| 144 | mService.mInputMonitor.updateInputWindowsLw(true /*force*/); |
| 145 | } |
| 146 | |
| 147 | // Send drag end broadcast if drag start has been sent. |
| 148 | if (mDragInProgress) { |
| 149 | final int myPid = Process.myPid(); |
| 150 | |
| 151 | if (DEBUG_DRAG) { |
| 152 | Slog.d(TAG_WM, "broadcasting DRAG_ENDED"); |
| 153 | } |
| 154 | for (WindowState ws : mNotifiedWindows) { |
| 155 | float x = 0; |
| 156 | float y = 0; |
| 157 | if (!mDragResult && (ws.mSession.mPid == mPid)) { |
| 158 | // Report unconsumed drop location back to the app that started the drag. |
| 159 | x = mCurrentX; |
| 160 | y = mCurrentY; |
| 161 | } |
| 162 | DragEvent evt = DragEvent.obtain(DragEvent.ACTION_DRAG_ENDED, |
| 163 | x, y, null, null, null, null, mDragResult); |
| 164 | try { |
| 165 | ws.mClient.dispatchDragEvent(evt); |
| 166 | } catch (RemoteException e) { |
| 167 | Slog.w(TAG_WM, "Unable to drag-end window " + ws); |
| 168 | } |
| 169 | // if the current window is in the same process, |
| 170 | // the dispatch has already recycled the event |
| 171 | if (myPid != ws.mSession.mPid) { |
| 172 | evt.recycle(); |
| 173 | } |
| 174 | } |
| 175 | mNotifiedWindows.clear(); |
| 176 | mDragInProgress = false; |
| 177 | } |
| 178 | |
| 179 | // Take the cursor back if it has been changed. |
| 180 | if (isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 181 | mService.restorePointerIconLocked(mDisplayContent, mCurrentX, mCurrentY); |
| 182 | mTouchSource = 0; |
| 183 | } |
| 184 | |
| 185 | // Clear the internal variables. |
| 186 | if (mSurfaceControl != null) { |
| 187 | mSurfaceControl.destroy(); |
| 188 | mSurfaceControl = null; |
| 189 | } |
| 190 | if (mAnimator != null && !mAnimationCompleted) { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 191 | Slog.wtf(TAG_WM, |
| 192 | "Unexpectedly destroying mSurfaceControl while animation is running"); |
| 193 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 194 | mFlags = 0; |
| 195 | mLocalWin = null; |
| 196 | mToken = null; |
| 197 | mData = null; |
| 198 | mThumbOffsetX = mThumbOffsetY = 0; |
| 199 | mNotifiedWindows = null; |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 200 | |
| 201 | // Notifies the controller that the drag state is closed. |
| 202 | mDragDropController.onDragStateClosedLocked(this); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 205 | class InputInterceptor { |
| 206 | InputChannel mServerChannel, mClientChannel; |
| 207 | DragInputEventReceiver mInputEventReceiver; |
| 208 | InputApplicationHandle mDragApplicationHandle; |
| 209 | InputWindowHandle mDragWindowHandle; |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 210 | |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 211 | InputInterceptor(Display display) { |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 212 | InputChannel[] channels = InputChannel.openInputChannelPair("drag"); |
| 213 | mServerChannel = channels[0]; |
| 214 | mClientChannel = channels[1]; |
| 215 | mService.mInputManager.registerInputChannel(mServerChannel, null); |
Daichi Hirono | 52cb215 | 2017-09-11 15:29:42 +0900 | [diff] [blame] | 216 | mInputEventReceiver = new DragInputEventReceiver(mClientChannel, |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 217 | mService.mH.getLooper(), mDragDropController); |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 218 | |
| 219 | mDragApplicationHandle = new InputApplicationHandle(null); |
| 220 | mDragApplicationHandle.name = "drag"; |
| 221 | mDragApplicationHandle.dispatchingTimeoutNanos = |
| 222 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 223 | |
Vladislav Kaznacheev | 3787de1 | 2016-12-21 10:36:35 -0800 | [diff] [blame] | 224 | mDragWindowHandle = new InputWindowHandle(mDragApplicationHandle, null, null, |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 225 | display.getDisplayId()); |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 226 | mDragWindowHandle.name = "drag"; |
| 227 | mDragWindowHandle.inputChannel = mServerChannel; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 228 | mDragWindowHandle.layer = getDragLayerLocked(); |
Jeff Brown | ea42655 | 2011-07-18 16:53:48 -0700 | [diff] [blame] | 229 | mDragWindowHandle.layoutParamsFlags = 0; |
| 230 | mDragWindowHandle.layoutParamsType = WindowManager.LayoutParams.TYPE_DRAG; |
| 231 | mDragWindowHandle.dispatchingTimeoutNanos = |
| 232 | WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS; |
| 233 | mDragWindowHandle.visible = true; |
| 234 | mDragWindowHandle.canReceiveKeys = false; |
| 235 | mDragWindowHandle.hasFocus = true; |
| 236 | mDragWindowHandle.hasWallpaper = false; |
| 237 | mDragWindowHandle.paused = false; |
| 238 | mDragWindowHandle.ownerPid = Process.myPid(); |
| 239 | mDragWindowHandle.ownerUid = Process.myUid(); |
| 240 | mDragWindowHandle.inputFeatures = 0; |
| 241 | mDragWindowHandle.scaleFactor = 1.0f; |
| 242 | |
| 243 | // The drag window cannot receive new touches. |
| 244 | mDragWindowHandle.touchableRegion.setEmpty(); |
| 245 | |
| 246 | // The drag window covers the entire display |
| 247 | mDragWindowHandle.frameLeft = 0; |
| 248 | mDragWindowHandle.frameTop = 0; |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 249 | mDragWindowHandle.frameRight = mDisplaySize.x; |
| 250 | mDragWindowHandle.frameBottom = mDisplaySize.y; |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 251 | |
| 252 | // Pause rotations before a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 253 | if (DEBUG_ORIENTATION) { |
| 254 | Slog.d(TAG_WM, "Pausing rotation during drag"); |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 255 | } |
| 256 | mService.pauseRotationLocked(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 257 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 258 | |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 259 | void tearDown() { |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 260 | mService.mInputManager.unregisterInputChannel(mServerChannel); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 261 | mInputEventReceiver.dispose(); |
| 262 | mInputEventReceiver = null; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 263 | mClientChannel.dispose(); |
| 264 | mServerChannel.dispose(); |
| 265 | mClientChannel = null; |
| 266 | mServerChannel = null; |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 267 | |
| 268 | mDragWindowHandle = null; |
| 269 | mDragApplicationHandle = null; |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 270 | |
| 271 | // Resume rotations after a drag. |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 272 | if (DEBUG_ORIENTATION) { |
| 273 | Slog.d(TAG_WM, "Resuming rotation after drag"); |
Jeff Brown | 01a98dd | 2011-09-20 15:08:29 -0700 | [diff] [blame] | 274 | } |
| 275 | mService.resumeRotationLocked(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 279 | InputChannel getInputChannel() { |
| 280 | return mInputInterceptor == null ? null : mInputInterceptor.mServerChannel; |
| 281 | } |
| 282 | |
| 283 | InputWindowHandle getInputWindowHandle() { |
| 284 | return mInputInterceptor == null ? null : mInputInterceptor.mDragWindowHandle; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @param display The Display that the window being dragged is on. |
| 289 | */ |
| 290 | void register(Display display) { |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 291 | display.getRealSize(mDisplaySize); |
Vladislav Kaznacheev | 64b103a | 2016-08-10 11:49:08 -0700 | [diff] [blame] | 292 | if (DEBUG_DRAG) Slog.d(TAG_WM, "registering drag input channel"); |
| 293 | if (mInputInterceptor != null) { |
| 294 | Slog.e(TAG_WM, "Duplicate register of drag input channel"); |
| 295 | } else { |
| 296 | mInputInterceptor = new InputInterceptor(display); |
| 297 | mService.mInputMonitor.updateInputWindowsLw(true /*force*/); |
| 298 | } |
| 299 | } |
| 300 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 301 | int getDragLayerLocked() { |
Wale Ogunwale | 5cd907d | 2017-01-26 14:14:08 -0800 | [diff] [blame] | 302 | return mService.mPolicy.getWindowLayerFromTypeLw(WindowManager.LayoutParams.TYPE_DRAG) |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 303 | * WindowManagerService.TYPE_LAYER_MULTIPLIER |
| 304 | + WindowManagerService.TYPE_LAYER_OFFSET; |
| 305 | } |
| 306 | |
| 307 | /* call out to each visible window/session informing it about the drag |
| 308 | */ |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 309 | void broadcastDragStartedLocked(final float touchX, final float touchY) { |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 310 | mOriginalX = mCurrentX = touchX; |
| 311 | mOriginalY = mCurrentY = touchY; |
| 312 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 313 | // Cache a base-class instance of the clip metadata so that parceling |
| 314 | // works correctly in calling out to the apps. |
| 315 | mDataDescription = (mData != null) ? mData.getDescription() : null; |
| 316 | mNotifiedWindows.clear(); |
| 317 | mDragInProgress = true; |
| 318 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 319 | mSourceUserId = UserHandle.getUserId(mUid); |
| 320 | |
| 321 | final IUserManager userManager = |
| 322 | (IUserManager) ServiceManager.getService(Context.USER_SERVICE); |
| 323 | try { |
| 324 | mCrossProfileCopyAllowed = !userManager.getUserRestrictions(mSourceUserId).getBoolean( |
| 325 | UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE); |
| 326 | } catch (RemoteException e) { |
| 327 | Slog.e(TAG_WM, "Remote Exception calling UserManager: " + e); |
| 328 | mCrossProfileCopyAllowed = false; |
| 329 | } |
| 330 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 331 | if (DEBUG_DRAG) { |
| 332 | Slog.d(TAG_WM, "broadcasting DRAG_STARTED at (" + touchX + ", " + touchY + ")"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 335 | mDisplayContent.forAllWindows(w -> { |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 336 | sendDragStartedLocked(w, touchX, touchY, mDataDescription); |
Wale Ogunwale | f4ebe2e | 2016-11-09 13:24:43 -0800 | [diff] [blame] | 337 | }, false /* traverseTopToBottom */ ); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 340 | /* helper - send a ACTION_DRAG_STARTED event, if the |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 341 | * designated window is potentially a drop recipient. There are race situations |
| 342 | * around DRAG_ENDED broadcast, so we make sure that once we've declared that |
| 343 | * the drag has ended, we never send out another DRAG_STARTED for this drag action. |
| 344 | * |
| 345 | * This method clones the 'event' parameter if it's being delivered to the same |
| 346 | * process, so it's safe for the caller to call recycle() on the event afterwards. |
| 347 | */ |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 348 | private void sendDragStartedLocked(WindowState newWin, float touchX, float touchY, |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 349 | ClipDescription desc) { |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 350 | if (mDragInProgress && isValidDropTarget(newWin)) { |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 351 | DragEvent event = obtainDragEvent(newWin, DragEvent.ACTION_DRAG_STARTED, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 352 | touchX, touchY, null, desc, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 353 | try { |
| 354 | newWin.mClient.dispatchDragEvent(event); |
| 355 | // track each window that we've notified that the drag is starting |
| 356 | mNotifiedWindows.add(newWin); |
| 357 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 358 | Slog.w(TAG_WM, "Unable to drag-start window " + newWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 359 | } finally { |
| 360 | // if the callee was local, the dispatch has already recycled the event |
| 361 | if (Process.myPid() != newWin.mSession.mPid) { |
| 362 | event.recycle(); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 368 | private boolean isValidDropTarget(WindowState targetWin) { |
| 369 | if (targetWin == null) { |
| 370 | return false; |
| 371 | } |
| 372 | if (!targetWin.isPotentialDragTarget()) { |
| 373 | return false; |
| 374 | } |
Yorke Lee | 0e85247 | 2016-06-15 10:03:18 -0700 | [diff] [blame] | 375 | if ((mFlags & View.DRAG_FLAG_GLOBAL) == 0 || !targetWindowSupportsGlobalDrag(targetWin)) { |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 376 | // Drag is limited to the current window. |
| 377 | if (mLocalWin != targetWin.mClient.asBinder()) { |
| 378 | return false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return mCrossProfileCopyAllowed || |
| 383 | mSourceUserId == UserHandle.getUserId(targetWin.getOwningUid()); |
| 384 | } |
| 385 | |
Yorke Lee | 0e85247 | 2016-06-15 10:03:18 -0700 | [diff] [blame] | 386 | private boolean targetWindowSupportsGlobalDrag(WindowState targetWin) { |
| 387 | // Global drags are limited to system windows, and windows for apps that are targeting N and |
| 388 | // above. |
| 389 | return targetWin.mAppToken == null |
Wale Ogunwale | 72919d2 | 2016-12-08 18:58:50 -0800 | [diff] [blame] | 390 | || targetWin.mAppToken.mTargetSdk >= Build.VERSION_CODES.N; |
Yorke Lee | 0e85247 | 2016-06-15 10:03:18 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 393 | /* helper - send a ACTION_DRAG_STARTED event only if the window has not |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 394 | * previously been notified, i.e. it became visible after the drag operation |
| 395 | * was begun. This is a rare case. |
| 396 | */ |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 397 | void sendDragStartedIfNeededLocked(WindowState newWin) { |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 398 | if (mDragInProgress) { |
| 399 | // If we have sent the drag-started, we needn't do so again |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 400 | if (isWindowNotified(newWin)) { |
| 401 | return; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 402 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 403 | if (DEBUG_DRAG) { |
| 404 | 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] | 405 | } |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 406 | sendDragStartedLocked(newWin, mCurrentX, mCurrentY, mDataDescription); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 410 | private boolean isWindowNotified(WindowState newWin) { |
| 411 | for (WindowState ws : mNotifiedWindows) { |
| 412 | if (ws == newWin) { |
| 413 | return true; |
| 414 | } |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 419 | void endDragLocked() { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 420 | if (mAnimator != null) { |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 421 | return; |
| 422 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 423 | if (!mDragResult) { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 424 | mAnimator = createReturnAnimationLocked(); |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 425 | return; // Will call closeLocked() when the animation is done. |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 426 | } |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 427 | closeLocked(); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 430 | void cancelDragLocked() { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 431 | if (mAnimator != null) { |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 434 | if (!mDragInProgress) { |
| 435 | // This can happen if an app invokes Session#cancelDragAndDrop before |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 436 | // Session#performDrag. Reset the drag state without playing the cancel animation |
| 437 | // because H.DRAG_START_TIMEOUT may be sent to WindowManagerService, which will cause |
| 438 | // DragState#reset() while playing the cancel animation. |
| 439 | closeLocked(); |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | mAnimator = createCancelAnimationLocked(); |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 443 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 444 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 445 | void notifyMoveLocked(float x, float y) { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 446 | if (mAnimator != null) { |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 447 | return; |
| 448 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 449 | mCurrentX = x; |
| 450 | mCurrentY = y; |
| 451 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 452 | // Move the surface to the given touch |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 453 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 454 | TAG_WM, ">>> OPEN TRANSACTION notifyMoveLocked"); |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 455 | mService.openSurfaceTransaction(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 456 | try { |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 457 | mSurfaceControl.setPosition(x - mThumbOffsetX, y - mThumbOffsetY); |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 458 | if (SHOW_TRANSACTIONS) Slog.i(TAG_WM, " DRAG " |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 459 | + mSurfaceControl + ": pos=(" + |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 460 | (int)(x - mThumbOffsetX) + "," + (int)(y - mThumbOffsetY) + ")"); |
| 461 | } finally { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 462 | mService.closeSurfaceTransaction("notifyMoveLw"); |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 463 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i( |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 464 | TAG_WM, "<<< CLOSE TRANSACTION notifyMoveLocked"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 465 | } |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 466 | notifyLocationLocked(x, y); |
Vladislav Kaznacheev | d0ed893 | 2016-01-15 15:37:05 -0800 | [diff] [blame] | 467 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 468 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 469 | void notifyLocationLocked(float x, float y) { |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 470 | // Tell the affected window |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 471 | WindowState touchedWin = mDisplayContent.getTouchableWinAtPointLocked(x, y); |
Vladislav Kaznacheev | 598d40d | 2016-04-19 15:24:52 -0700 | [diff] [blame] | 472 | if (touchedWin != null && !isWindowNotified(touchedWin)) { |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 473 | // The drag point is over a window which was not notified about a drag start. |
| 474 | // Pretend it's over empty space. |
| 475 | touchedWin = null; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 476 | } |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 477 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 478 | try { |
Vladislav Kaznacheev | d0ed893 | 2016-01-15 15:37:05 -0800 | [diff] [blame] | 479 | final int myPid = Process.myPid(); |
| 480 | |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 481 | // have we dragged over a new window? |
| 482 | if ((touchedWin != mTargetWindow) && (mTargetWindow != null)) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 483 | if (DEBUG_DRAG) { |
| 484 | Slog.d(TAG_WM, "sending DRAG_EXITED to " + mTargetWindow); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 485 | } |
| 486 | // force DRAG_EXITED_EVENT if appropriate |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 487 | DragEvent evt = obtainDragEvent(mTargetWindow, DragEvent.ACTION_DRAG_EXITED, |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame] | 488 | 0, 0, null, null, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 489 | mTargetWindow.mClient.dispatchDragEvent(evt); |
| 490 | if (myPid != mTargetWindow.mSession.mPid) { |
| 491 | evt.recycle(); |
| 492 | } |
| 493 | } |
| 494 | if (touchedWin != null) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 495 | if (false && DEBUG_DRAG) { |
| 496 | Slog.d(TAG_WM, "sending DRAG_LOCATION to " + touchedWin); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 497 | } |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 498 | DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DRAG_LOCATION, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 499 | x, y, null, null, null, null, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 500 | touchedWin.mClient.dispatchDragEvent(evt); |
| 501 | if (myPid != touchedWin.mSession.mPid) { |
| 502 | evt.recycle(); |
| 503 | } |
| 504 | } |
| 505 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 506 | Slog.w(TAG_WM, "can't send drag notification to windows"); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 507 | } |
| 508 | mTargetWindow = touchedWin; |
| 509 | } |
| 510 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 511 | /** |
| 512 | * Finds the drop target and tells it about the data. If the drop event is not sent to the |
| 513 | * target, invokes {@code endDragLocked} immediately. |
| 514 | */ |
| 515 | void notifyDropLocked(float x, float y) { |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 516 | if (mAnimator != null) { |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 517 | return; |
Vladislav Kaznacheev | a56b2b9 | 2015-11-20 18:30:21 -0800 | [diff] [blame] | 518 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 519 | mCurrentX = x; |
| 520 | mCurrentY = y; |
| 521 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 522 | final WindowState touchedWin = mDisplayContent.getTouchableWinAtPointLocked(x, y); |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 523 | |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 524 | if (!isWindowNotified(touchedWin)) { |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 525 | // "drop" outside a valid window -- no recipient to apply a |
| 526 | // timeout to, and we can send the drag-ended message immediately. |
| 527 | mDragResult = false; |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 528 | endDragLocked(); |
| 529 | return; |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 532 | if (DEBUG_DRAG) Slog.d(TAG_WM, "sending DROP to " + touchedWin); |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 533 | |
| 534 | final int targetUserId = UserHandle.getUserId(touchedWin.getOwningUid()); |
| 535 | |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 536 | final DragAndDropPermissionsHandler dragAndDropPermissions; |
| 537 | if ((mFlags & View.DRAG_FLAG_GLOBAL) != 0 && (mFlags & DRAG_FLAGS_URI_ACCESS) != 0) { |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 538 | dragAndDropPermissions = new DragAndDropPermissionsHandler( |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 539 | mData, |
| 540 | mUid, |
| 541 | touchedWin.getOwningPackage(), |
| 542 | mFlags & DRAG_FLAGS_URI_PERMISSIONS, |
| 543 | mSourceUserId, |
| 544 | targetUserId); |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 545 | } else { |
| 546 | dragAndDropPermissions = null; |
Vladislav Kaznacheev | 5d6bdeb | 2016-02-12 17:07:20 -0800 | [diff] [blame] | 547 | } |
| 548 | if (mSourceUserId != targetUserId){ |
Vladislav Kaznacheev | da2f194 | 2016-02-11 18:58:41 -0800 | [diff] [blame] | 549 | mData.fixUris(mSourceUserId); |
| 550 | } |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 551 | final int myPid = Process.myPid(); |
| 552 | final IBinder token = touchedWin.mClient.asBinder(); |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 553 | final DragEvent evt = obtainDragEvent(touchedWin, DragEvent.ACTION_DROP, x, y, |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 554 | null, null, mData, dragAndDropPermissions, false); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 555 | try { |
| 556 | touchedWin.mClient.dispatchDragEvent(evt); |
| 557 | |
| 558 | // 5 second timeout for this window to respond to the drop |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 559 | mDragDropController.sendTimeoutMessage(MSG_DRAG_END_TIMEOUT, token); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 560 | } catch (RemoteException e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 561 | Slog.w(TAG_WM, "can't send drop notification to win " + touchedWin); |
Daichi Hirono | 768012e | 2017-10-30 10:05:37 +0900 | [diff] [blame] | 562 | endDragLocked(); |
Dianne Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 563 | } finally { |
| 564 | if (myPid != touchedWin.mSession.mPid) { |
| 565 | evt.recycle(); |
| 566 | } |
| 567 | } |
| 568 | mToken = token; |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 569 | } |
| 570 | |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 571 | private static DragEvent obtainDragEvent(WindowState win, int action, |
| 572 | float x, float y, Object localState, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 573 | ClipDescription description, ClipData data, |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 574 | IDragAndDropPermissions dragAndDropPermissions, |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 575 | boolean result) { |
Vladislav Kaznacheev | 989b58a | 2016-02-10 12:19:33 -0800 | [diff] [blame] | 576 | final float winX = win.translateToWindowX(x); |
| 577 | final float winY = win.translateToWindowY(y); |
Vladislav Kaznacheev | ede5f54 | 2015-07-15 18:04:04 -0700 | [diff] [blame] | 578 | return DragEvent.obtain(action, winX, winY, localState, description, data, |
Vladislav Kaznacheev | 377c328 | 2016-04-20 14:22:23 -0700 | [diff] [blame] | 579 | dragAndDropPermissions, result); |
Dianne Hackborn | ffb3d93 | 2011-05-17 17:44:51 -0700 | [diff] [blame] | 580 | } |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 581 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 582 | private ValueAnimator createReturnAnimationLocked() { |
| 583 | final ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder( |
| 584 | PropertyValuesHolder.ofFloat( |
| 585 | ANIMATED_PROPERTY_X, mCurrentX - mThumbOffsetX, |
| 586 | mOriginalX - mThumbOffsetX), |
| 587 | PropertyValuesHolder.ofFloat( |
| 588 | ANIMATED_PROPERTY_Y, mCurrentY - mThumbOffsetY, |
| 589 | mOriginalY - mThumbOffsetY), |
| 590 | PropertyValuesHolder.ofFloat(ANIMATED_PROPERTY_SCALE, 1, 1), |
| 591 | PropertyValuesHolder.ofFloat( |
| 592 | ANIMATED_PROPERTY_ALPHA, mOriginalAlpha, mOriginalAlpha / 2)); |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 593 | |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 594 | final float translateX = mOriginalX - mCurrentX; |
| 595 | final float translateY = mOriginalY - mCurrentY; |
Vladislav Kaznacheev | b3665f1 | 2016-11-18 15:06:08 -0800 | [diff] [blame] | 596 | // Adjust the duration to the travel distance. |
| 597 | final double travelDistance = Math.sqrt(translateX * translateX + translateY * translateY); |
| 598 | final double displayDiagonal = |
| 599 | Math.sqrt(mDisplaySize.x * mDisplaySize.x + mDisplaySize.y * mDisplaySize.y); |
| 600 | final long duration = MIN_ANIMATION_DURATION_MS + (long) (travelDistance / displayDiagonal |
| 601 | * (MAX_ANIMATION_DURATION_MS - MIN_ANIMATION_DURATION_MS)); |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 602 | final AnimationListener listener = new AnimationListener(); |
| 603 | animator.setDuration(duration); |
| 604 | animator.setInterpolator(mCubicEaseOutInterpolator); |
| 605 | animator.addListener(listener); |
| 606 | animator.addUpdateListener(listener); |
| 607 | |
| 608 | mService.mAnimationHandler.post(() -> animator.start()); |
| 609 | return animator; |
Vladislav Kaznacheev | 8b6f38c | 2015-11-19 16:00:28 -0800 | [diff] [blame] | 610 | } |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 611 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 612 | private ValueAnimator createCancelAnimationLocked() { |
| 613 | final ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder( |
| 614 | PropertyValuesHolder.ofFloat( |
| 615 | ANIMATED_PROPERTY_X, mCurrentX - mThumbOffsetX, mCurrentX), |
| 616 | PropertyValuesHolder.ofFloat( |
| 617 | ANIMATED_PROPERTY_Y, mCurrentY - mThumbOffsetY, mCurrentY), |
| 618 | PropertyValuesHolder.ofFloat(ANIMATED_PROPERTY_SCALE, 1, 0), |
| 619 | PropertyValuesHolder.ofFloat(ANIMATED_PROPERTY_ALPHA, mOriginalAlpha, 0)); |
| 620 | final AnimationListener listener = new AnimationListener(); |
| 621 | animator.setDuration(MIN_ANIMATION_DURATION_MS); |
| 622 | animator.setInterpolator(mCubicEaseOutInterpolator); |
| 623 | animator.addListener(listener); |
| 624 | animator.addUpdateListener(listener); |
| 625 | |
| 626 | mService.mAnimationHandler.post(() -> animator.start()); |
| 627 | return animator; |
Vladislav Kaznacheev | ce2aef9 | 2015-11-20 18:49:59 -0800 | [diff] [blame] | 628 | } |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 629 | |
| 630 | private boolean isFromSource(int source) { |
| 631 | return (mTouchSource & source) == source; |
| 632 | } |
| 633 | |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 634 | void overridePointerIconLocked(int touchSource) { |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 635 | mTouchSource = touchSource; |
| 636 | if (isFromSource(InputDevice.SOURCE_MOUSE)) { |
Michael Wright | f9d9ce77 | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 637 | InputManager.getInstance().setPointerIconType(PointerIcon.TYPE_GRABBING); |
Vladislav Kaznacheev | ba76112 | 2016-01-22 12:09:45 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 640 | |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 641 | private class AnimationListener |
| 642 | implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener { |
| 643 | @Override |
| 644 | public void onAnimationUpdate(ValueAnimator animation) { |
| 645 | try (final SurfaceControl.Transaction transaction = new SurfaceControl.Transaction()) { |
| 646 | transaction.setPosition( |
| 647 | mSurfaceControl, |
| 648 | (float) mAnimator.getAnimatedValue(ANIMATED_PROPERTY_X), |
| 649 | (float) mAnimator.getAnimatedValue(ANIMATED_PROPERTY_Y)); |
| 650 | transaction.setAlpha( |
| 651 | mSurfaceControl, |
| 652 | (float) mAnimator.getAnimatedValue(ANIMATED_PROPERTY_ALPHA)); |
| 653 | transaction.setMatrix( |
| 654 | mSurfaceControl, |
| 655 | (float) mAnimator.getAnimatedValue(ANIMATED_PROPERTY_SCALE), 0, |
| 656 | 0, (float) mAnimator.getAnimatedValue(ANIMATED_PROPERTY_SCALE)); |
| 657 | transaction.apply(); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public void onAnimationStart(Animator animator) {} |
| 663 | |
| 664 | @Override |
| 665 | public void onAnimationCancel(Animator animator) {} |
| 666 | |
| 667 | @Override |
| 668 | public void onAnimationRepeat(Animator animator) {} |
| 669 | |
| 670 | @Override |
| 671 | public void onAnimationEnd(Animator animator) { |
Daichi Hirono | 3bae0b0 | 2017-10-27 13:05:13 +0900 | [diff] [blame] | 672 | mAnimationCompleted = true; |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 673 | // Updating mDragState requires the WM lock so continues it on the out of |
| 674 | // AnimationThread. |
Daichi Hirono | 58e25e1 | 2017-10-25 15:48:08 +0900 | [diff] [blame] | 675 | mDragDropController.sendHandlerMessage(MSG_ANIMATION_END, null); |
Daichi Hirono | 4a700b1 | 2017-10-04 15:49:35 +0900 | [diff] [blame] | 676 | } |
| 677 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 678 | } |