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