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