blob: 2a43bcc00daf7fef97d76da7c4053510ed0c7bed [file] [log] [blame]
Christopher Tatea53146c2010-09-07 11:57:52 -07001/*
2 * Copyright (C) 2010 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
17package android.view;
18
Mathew Inwooda570dee2018-08-17 14:56:00 +010019import android.annotation.UnsupportedAppUsage;
Christopher Tatea53146c2010-09-07 11:57:52 -070020import android.content.ClipData;
21import android.content.ClipDescription;
22import android.os.Parcel;
23import android.os.Parcelable;
24
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070025import com.android.internal.view.IDragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080026
Joe Malin32736f02011-01-19 16:14:20 -080027//TODO: Improve Javadoc
28/**
29 * Represents an event that is sent out by the system at various times during a drag and drop
Vadim Tryshevad574312016-09-27 18:33:23 -070030 * operation. It is a data structure that contains several important pieces of data about
Joe Malin32736f02011-01-19 16:14:20 -080031 * the operation and the underlying data.
32 * <p>
33 * View objects that receive a DragEvent call {@link #getAction()}, which returns
34 * an action type that indicates the state of the drag and drop operation. This allows a View
35 * object to react to a change in state by changing its appearance or performing other actions.
36 * For example, a View can react to the {@link #ACTION_DRAG_ENTERED} action type by
37 * by changing one or more colors in its displayed image.
38 * </p>
39 * <p>
40 * During a drag and drop operation, the system displays an image that the user drags. This image
41 * is called a drag shadow. Several action types reflect the position of the drag shadow relative
42 * to the View receiving the event.
43 * </p>
44 * <p>
45 * Most methods return valid data only for certain event actions. This is summarized in the
46 * following table. Each possible {@link #getAction()} value is listed in the first column. The
47 * other columns indicate which method or methods return valid data for that getAction() value:
48 * </p>
49 * <table>
50 * <tr>
51 * <th scope="col">getAction() Value</th>
52 * <th scope="col">getClipDescription()</th>
53 * <th scope="col">getLocalState()</th>
54 * <th scope="col">getX()</th>
55 * <th scope="col">getY()</th>
56 * <th scope="col">getClipData()</th>
57 * <th scope="col">getResult()</th>
58 * </tr>
59 * <tr>
60 * <td>ACTION_DRAG_STARTED</td>
61 * <td style="text-align: center;">X</td>
62 * <td style="text-align: center;">X</td>
63 * <td style="text-align: center;">X</td>
64 * <td style="text-align: center;">X</td>
65 * <td style="text-align: center;">&nbsp;</td>
66 * <td style="text-align: center;">&nbsp;</td>
67 * </tr>
68 * <tr>
69 * <td>ACTION_DRAG_ENTERED</td>
70 * <td style="text-align: center;">X</td>
71 * <td style="text-align: center;">X</td>
72 * <td style="text-align: center;">&nbsp;</td>
73 * <td style="text-align: center;">&nbsp;</td>
74 * <td style="text-align: center;">&nbsp;</td>
75 * <td style="text-align: center;">&nbsp;</td>
76 * </tr>
77 * <tr>
78 * <td>ACTION_DRAG_LOCATION</td>
79 * <td style="text-align: center;">X</td>
80 * <td style="text-align: center;">X</td>
81 * <td style="text-align: center;">X</td>
82 * <td style="text-align: center;">X</td>
83 * <td style="text-align: center;">&nbsp;</td>
84 * <td style="text-align: center;">&nbsp;</td>
85 * </tr>
86 * <tr>
87 * <td>ACTION_DRAG_EXITED</td>
88 * <td style="text-align: center;">X</td>
89 * <td style="text-align: center;">X</td>
90 * <td style="text-align: center;">&nbsp;</td>
91 * <td style="text-align: center;">&nbsp;</td>
92 * <td style="text-align: center;">&nbsp;</td>
93 * <td style="text-align: center;">&nbsp;</td>
94 * </tr>
95 * <tr>
96 * <td>ACTION_DROP</td>
97 * <td style="text-align: center;">X</td>
98 * <td style="text-align: center;">X</td>
99 * <td style="text-align: center;">X</td>
100 * <td style="text-align: center;">X</td>
101 * <td style="text-align: center;">X</td>
102 * <td style="text-align: center;">&nbsp;</td>
103 * </tr>
104 * <tr>
105 * <td>ACTION_DRAG_ENDED</td>
Vladislav Kaznacheev3067bc62016-07-11 13:52:22 -0700106 * <td style="text-align: center;">&nbsp;</td>
Siarhei Vishniakou9d65e0d2017-09-01 20:59:21 -0700107 * <td style="text-align: center;">X</td>
Joe Malin32736f02011-01-19 16:14:20 -0800108 * <td style="text-align: center;">&nbsp;</td>
109 * <td style="text-align: center;">&nbsp;</td>
110 * <td style="text-align: center;">&nbsp;</td>
111 * <td style="text-align: center;">X</td>
112 * </tr>
113 * </table>
114 * <p>
115 * The {@link android.view.DragEvent#getAction()},
Siarhei Vishniakou9d65e0d2017-09-01 20:59:21 -0700116 * {@link android.view.DragEvent#getLocalState()}
Joe Malin32736f02011-01-19 16:14:20 -0800117 * {@link android.view.DragEvent#describeContents()},
118 * {@link android.view.DragEvent#writeToParcel(Parcel,int)}, and
119 * {@link android.view.DragEvent#toString()} methods always return valid data.
120 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -0700121 *
122 * <div class="special reference">
123 * <h3>Developer Guides</h3>
124 * <p>For a guide to implementing drag and drop features, read the
125 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
126 * </div>
Joe Malin32736f02011-01-19 16:14:20 -0800127 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700128public class DragEvent implements Parcelable {
129 private static final boolean TRACK_RECYCLED_LOCATION = false;
130
131 int mAction;
132 float mX, mY;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100133 @UnsupportedAppUsage
Christopher Tatea53146c2010-09-07 11:57:52 -0700134 ClipDescription mClipDescription;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100135 @UnsupportedAppUsage
Christopher Tatea53146c2010-09-07 11:57:52 -0700136 ClipData mClipData;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700137 IDragAndDropPermissions mDragAndDropPermissions;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700138
Christopher Tate407b4e92010-11-30 17:14:08 -0800139 Object mLocalState;
Chris Tated4533f12010-10-19 15:15:08 -0700140 boolean mDragResult;
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -0700141 boolean mEventHandlerWasCalled;
Christopher Tatea53146c2010-09-07 11:57:52 -0700142
143 private DragEvent mNext;
144 private RuntimeException mRecycledLocation;
145 private boolean mRecycled;
146
147 private static final int MAX_RECYCLED = 10;
148 private static final Object gRecyclerLock = new Object();
149 private static int gRecyclerUsed = 0;
150 private static DragEvent gRecyclerTop = null;
151
152 /**
Joe Malin32736f02011-01-19 16:14:20 -0800153 * Action constant returned by {@link #getAction()}: Signals the start of a
154 * drag and drop operation. The View should return {@code true} from its
155 * {@link View#onDragEvent(DragEvent) onDragEvent()} handler method or
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800156 * {@link View.OnDragListener#onDrag(View,DragEvent) OnDragListener.onDrag()} listener
Joe Malin32736f02011-01-19 16:14:20 -0800157 * if it can accept a drop. The onDragEvent() or onDrag() methods usually inspect the metadata
158 * from {@link #getClipDescription()} to determine if they can accept the data contained in
159 * this drag. For an operation that doesn't represent data transfer, these methods may
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700160 * perform other actions to determine whether or not the View should accept the data.
Joe Malin32736f02011-01-19 16:14:20 -0800161 * If the View wants to indicate that it is a valid drop target, it can also react by
162 * changing its appearance.
Christopher Tate855e4c92010-11-19 14:55:12 -0800163 * <p>
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700164 * Views added or becoming visible for the first time during a drag operation receive this
165 * event when they are added or becoming visible.
166 * </p>
167 * <p>
Vadim Tryshevad574312016-09-27 18:33:23 -0700168 * A View only receives further drag events for the drag operation if it returns {@code true}
169 * in response to ACTION_DRAG_STARTED.
Joe Malin32736f02011-01-19 16:14:20 -0800170 * </p>
171 * @see #ACTION_DRAG_ENDED
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -0700172 * @see #getX()
173 * @see #getY()
Christopher Tatea53146c2010-09-07 11:57:52 -0700174 */
175 public static final int ACTION_DRAG_STARTED = 1;
Christopher Tatea53146c2010-09-07 11:57:52 -0700176
Christopher Tate855e4c92010-11-19 14:55:12 -0800177 /**
Joe Malin32736f02011-01-19 16:14:20 -0800178 * Action constant returned by {@link #getAction()}: Sent to a View after
Vadim Tryshevad574312016-09-27 18:33:23 -0700179 * {@link #ACTION_DRAG_ENTERED} while the drag shadow is still within the View object's bounding
180 * box, but not within a descendant view that can accept the data. The {@link #getX()} and
181 * {@link #getY()} methods supply
Joe Malin32736f02011-01-19 16:14:20 -0800182 * the X and Y position of of the drag point within the View object's bounding box.
Christopher Tate855e4c92010-11-19 14:55:12 -0800183 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800184 * A View receives an {@link #ACTION_DRAG_ENTERED} event before receiving any
185 * ACTION_DRAG_LOCATION events.
186 * </p>
187 * <p>
188 * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700189 * drag shadow out of the View object's bounding box or into a descendant view that can accept
190 * the data. If the user moves the drag shadow back into the View object's bounding box or out
191 * of a descendant view that can accept the data, the View receives an ACTION_DRAG_ENTERED again
192 * before receiving any more ACTION_DRAG_LOCATION events.
Joe Malin32736f02011-01-19 16:14:20 -0800193 * </p>
194 * @see #ACTION_DRAG_ENTERED
195 * @see #getX()
196 * @see #getY()
Christopher Tate855e4c92010-11-19 14:55:12 -0800197 */
198 public static final int ACTION_DRAG_LOCATION = 2;
199
200 /**
Joe Malin32736f02011-01-19 16:14:20 -0800201 * Action constant returned by {@link #getAction()}: Signals to a View that the user
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700202 * has released the drag shadow, and the drag point is within the bounding box of the View and
203 * not within a descendant view that can accept the data.
Joe Malin32736f02011-01-19 16:14:20 -0800204 * The View should retrieve the data from the DragEvent by calling {@link #getClipData()}.
205 * The methods {@link #getX()} and {@link #getY()} return the X and Y position of the drop point
206 * within the View object's bounding box.
Christopher Tate855e4c92010-11-19 14:55:12 -0800207 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800208 * The View should return {@code true} from its {@link View#onDragEvent(DragEvent)}
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800209 * handler or {@link View.OnDragListener#onDrag(View,DragEvent) OnDragListener.onDrag()}
Joe Malin32736f02011-01-19 16:14:20 -0800210 * listener if it accepted the drop, and {@code false} if it ignored the drop.
211 * </p>
212 * <p>
213 * The View can also react to this action by changing its appearance.
214 * </p>
215 * @see #getClipData()
216 * @see #getX()
217 * @see #getY()
Christopher Tate855e4c92010-11-19 14:55:12 -0800218 */
219 public static final int ACTION_DROP = 3;
220
221 /**
Joe Malin32736f02011-01-19 16:14:20 -0800222 * Action constant returned by {@link #getAction()}: Signals to a View that the drag and drop
223 * operation has concluded. A View that changed its appearance during the operation should
224 * return to its usual drawing state in response to this event.
Christopher Tate855e4c92010-11-19 14:55:12 -0800225 * <p>
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700226 * All views with listeners that returned boolean <code>true</code> for the ACTION_DRAG_STARTED
227 * event will receive the ACTION_DRAG_ENDED event even if they are not currently visible when
228 * the drag ends. Views removed during the drag operation won't receive the ACTION_DRAG_ENDED
229 * event.
Joe Malin32736f02011-01-19 16:14:20 -0800230 * </p>
231 * <p>
232 * The View object can call {@link #getResult()} to see the result of the operation.
233 * If a View returned {@code true} in response to {@link #ACTION_DROP}, then
234 * getResult() returns {@code true}, otherwise it returns {@code false}.
235 * </p>
236 * @see #ACTION_DRAG_STARTED
237 * @see #getResult()
Christopher Tate855e4c92010-11-19 14:55:12 -0800238 */
239 public static final int ACTION_DRAG_ENDED = 4;
240
241 /**
Joe Malin32736f02011-01-19 16:14:20 -0800242 * Action constant returned by {@link #getAction()}: Signals to a View that the drag point has
243 * entered the bounding box of the View.
Christopher Tate855e4c92010-11-19 14:55:12 -0800244 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800245 * If the View can accept a drop, it can react to ACTION_DRAG_ENTERED
246 * by changing its appearance in a way that tells the user that the View is the current
247 * drop target.
248 * </p>
249 * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700250 * drag shadow out of the View object's bounding box or into a descendant view that can accept
251 * the data. If the user moves the drag shadow back into the View object's bounding box or out
252 * of a descendant view that can accept the data, the View receives an ACTION_DRAG_ENTERED again
253 * before receiving any more ACTION_DRAG_LOCATION events.
Joe Malin32736f02011-01-19 16:14:20 -0800254 * </p>
255 * @see #ACTION_DRAG_ENTERED
256 * @see #ACTION_DRAG_LOCATION
Christopher Tate855e4c92010-11-19 14:55:12 -0800257 */
258 public static final int ACTION_DRAG_ENTERED = 5;
259
260 /**
Joe Malin32736f02011-01-19 16:14:20 -0800261 * Action constant returned by {@link #getAction()}: Signals that the user has moved the
Vadim Tryshevc5109b32016-09-22 18:26:09 -0700262 * drag shadow out of the bounding box of the View or into a descendant view that can accept
263 * the data.
Joe Malin32736f02011-01-19 16:14:20 -0800264 * The View can react by changing its appearance in a way that tells the user that
265 * View is no longer the immediate drop target.
Christopher Tate855e4c92010-11-19 14:55:12 -0800266 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800267 * After the system sends an ACTION_DRAG_EXITED event to the View, the View receives no more
268 * ACTION_DRAG_LOCATION events until the user drags the drag shadow back over the View.
269 * </p>
270 *
Christopher Tate855e4c92010-11-19 14:55:12 -0800271 */
Joe Malin32736f02011-01-19 16:14:20 -0800272 public static final int ACTION_DRAG_EXITED = 6;
Christopher Tate855e4c92010-11-19 14:55:12 -0800273
Christopher Tate2c095f32010-10-04 14:13:40 -0700274 private DragEvent() {
Christopher Tatea53146c2010-09-07 11:57:52 -0700275 }
276
Chris Tated4533f12010-10-19 15:15:08 -0700277 private void init(int action, float x, float y, ClipDescription description, ClipData data,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700278 IDragAndDropPermissions dragAndDropPermissions, Object localState, boolean result) {
Chris Tatec02c7af2010-10-20 18:55:13 -0700279 mAction = action;
280 mX = x;
281 mY = y;
282 mClipDescription = description;
283 mClipData = data;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700284 this.mDragAndDropPermissions = dragAndDropPermissions;
Christopher Tate7fb8b562011-01-20 13:46:41 -0800285 mLocalState = localState;
Chris Tated4533f12010-10-19 15:15:08 -0700286 mDragResult = result;
Chris Tatec02c7af2010-10-20 18:55:13 -0700287 }
288
Christopher Tate2c095f32010-10-04 14:13:40 -0700289 static DragEvent obtain() {
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700290 return DragEvent.obtain(0, 0f, 0f, null, null, null, null, false);
Christopher Tatea53146c2010-09-07 11:57:52 -0700291 }
292
Christopher Tate855e4c92010-11-19 14:55:12 -0800293 /** @hide */
Christopher Tate407b4e92010-11-30 17:14:08 -0800294 public static DragEvent obtain(int action, float x, float y, Object localState,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700295 ClipDescription description, ClipData data,
296 IDragAndDropPermissions dragAndDropPermissions, boolean result) {
Christopher Tatea53146c2010-09-07 11:57:52 -0700297 final DragEvent ev;
298 synchronized (gRecyclerLock) {
299 if (gRecyclerTop == null) {
Chris Tatec02c7af2010-10-20 18:55:13 -0700300 ev = new DragEvent();
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700301 ev.init(action, x, y, description, data, dragAndDropPermissions, localState,
302 result);
Chris Tatec02c7af2010-10-20 18:55:13 -0700303 return ev;
Christopher Tatea53146c2010-09-07 11:57:52 -0700304 }
305 ev = gRecyclerTop;
306 gRecyclerTop = ev.mNext;
307 gRecyclerUsed -= 1;
308 }
309 ev.mRecycledLocation = null;
310 ev.mRecycled = false;
311 ev.mNext = null;
312
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700313 ev.init(action, x, y, description, data, dragAndDropPermissions, localState, result);
Christopher Tatea53146c2010-09-07 11:57:52 -0700314
315 return ev;
316 }
317
Christopher Tate855e4c92010-11-19 14:55:12 -0800318 /** @hide */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100319 @UnsupportedAppUsage
Christopher Tate2c095f32010-10-04 14:13:40 -0700320 public static DragEvent obtain(DragEvent source) {
Christopher Tate407b4e92010-11-30 17:14:08 -0800321 return obtain(source.mAction, source.mX, source.mY, source.mLocalState,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700322 source.mClipDescription, source.mClipData, source.mDragAndDropPermissions,
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700323 source.mDragResult);
Christopher Tate2c095f32010-10-04 14:13:40 -0700324 }
325
Christopher Tate855e4c92010-11-19 14:55:12 -0800326 /**
327 * Inspect the action value of this event.
Joe Malin32736f02011-01-19 16:14:20 -0800328 * @return One of the following action constants, in the order in which they usually occur
329 * during a drag and drop operation:
330 * <ul>
331 * <li>{@link #ACTION_DRAG_STARTED}</li>
332 * <li>{@link #ACTION_DRAG_ENTERED}</li>
333 * <li>{@link #ACTION_DRAG_LOCATION}</li>
334 * <li>{@link #ACTION_DROP}</li>
335 * <li>{@link #ACTION_DRAG_EXITED}</li>
336 * <li>{@link #ACTION_DRAG_ENDED}</li>
337 * </ul>
Christopher Tate855e4c92010-11-19 14:55:12 -0800338 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700339 public int getAction() {
340 return mAction;
341 }
342
Christopher Tate855e4c92010-11-19 14:55:12 -0800343 /**
Joe Malin32736f02011-01-19 16:14:20 -0800344 * Gets the X coordinate of the drag point. The value is only valid if the event action is
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -0700345 * {@link #ACTION_DRAG_STARTED}, {@link #ACTION_DRAG_LOCATION} or {@link #ACTION_DROP}.
346 * @return The current drag point's X coordinate
Christopher Tate855e4c92010-11-19 14:55:12 -0800347 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700348 public float getX() {
349 return mX;
350 }
351
Christopher Tate855e4c92010-11-19 14:55:12 -0800352 /**
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -0700353 * Gets the Y coordinate of the drag point. The value is only valid if the event action is
354 * {@link #ACTION_DRAG_STARTED}, {@link #ACTION_DRAG_LOCATION} or {@link #ACTION_DROP}.
Joe Malin32736f02011-01-19 16:14:20 -0800355 * @return The current drag point's Y coordinate
Christopher Tate855e4c92010-11-19 14:55:12 -0800356 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700357 public float getY() {
358 return mY;
359 }
360
Christopher Tate855e4c92010-11-19 14:55:12 -0800361 /**
Joe Malin32736f02011-01-19 16:14:20 -0800362 * Returns the {@link android.content.ClipData} object sent to the system as part of the call
363 * to
Vadim Tryshevad574312016-09-27 18:33:23 -0700364 * {@link android.view.View#startDragAndDrop(ClipData,View.DragShadowBuilder,Object,int)
365 * startDragAndDrop()}.
Joe Malin32736f02011-01-19 16:14:20 -0800366 * This method only returns valid data if the event action is {@link #ACTION_DROP}.
Vadim Tryshevad574312016-09-27 18:33:23 -0700367 * @return The ClipData sent to the system by startDragAndDrop().
Christopher Tate855e4c92010-11-19 14:55:12 -0800368 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700369 public ClipData getClipData() {
370 return mClipData;
371 }
372
Christopher Tate855e4c92010-11-19 14:55:12 -0800373 /**
Joe Malin32736f02011-01-19 16:14:20 -0800374 * Returns the {@link android.content.ClipDescription} object contained in the
375 * {@link android.content.ClipData} object sent to the system as part of the call to
Vadim Tryshevad574312016-09-27 18:33:23 -0700376 * {@link android.view.View#startDragAndDrop(ClipData,View.DragShadowBuilder,Object,int)
377 * startDragAndDrop()}.
Joe Malin32736f02011-01-19 16:14:20 -0800378 * The drag handler or listener for a View can use the metadata in this object to decide if the
379 * View can accept the dragged View object's data.
380 * <p>
Vladislav Kaznacheev3067bc62016-07-11 13:52:22 -0700381 * This method returns valid data for all event actions except for {@link #ACTION_DRAG_ENDED}.
Vadim Tryshevad574312016-09-27 18:33:23 -0700382 * @return The ClipDescription that was part of the ClipData sent to the system by
383 * startDragAndDrop().
Christopher Tate855e4c92010-11-19 14:55:12 -0800384 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700385 public ClipDescription getClipDescription() {
386 return mClipDescription;
387 }
388
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -0800389 /** @hide */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700390 public IDragAndDropPermissions getDragAndDropPermissions() {
391 return mDragAndDropPermissions;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700392 }
393
394 /**
Joe Malin32736f02011-01-19 16:14:20 -0800395 * Returns the local state object sent to the system as part of the call to
Vadim Tryshevad574312016-09-27 18:33:23 -0700396 * {@link android.view.View#startDragAndDrop(ClipData,View.DragShadowBuilder,Object,int)
397 * startDragAndDrop()}.
Joe Malin32736f02011-01-19 16:14:20 -0800398 * The object is intended to provide local information about the drag and drop operation. For
399 * example, it can indicate whether the drag and drop operation is a copy or a move.
400 * <p>
Vladislav Kaznacheev048f3bf2016-09-08 16:37:23 -0700401 * The local state is available only to views in the activity which has started the drag
402 * operation. In all other activities this method will return null
403 * </p>
404 * <p>
Siarhei Vishniakou9d65e0d2017-09-01 20:59:21 -0700405 * This method returns valid data for all event actions.
Joe Malin32736f02011-01-19 16:14:20 -0800406 * </p>
Vadim Tryshevad574312016-09-27 18:33:23 -0700407 * @return The local state object sent to the system by startDragAndDrop().
Christopher Tate407b4e92010-11-30 17:14:08 -0800408 */
409 public Object getLocalState() {
410 return mLocalState;
411 }
412
413 /**
Joe Malin32736f02011-01-19 16:14:20 -0800414 * <p>
415 * Returns an indication of the result of the drag and drop operation.
416 * This method only returns valid data if the action type is {@link #ACTION_DRAG_ENDED}.
417 * The return value depends on what happens after the user releases the drag shadow.
418 * </p>
419 * <p>
420 * If the user releases the drag shadow on a View that can accept a drop, the system sends an
421 * {@link #ACTION_DROP} event to the View object's drag event listener. If the listener
422 * returns {@code true}, then getResult() will return {@code true}.
423 * If the listener returns {@code false}, then getResult() returns {@code false}.
424 * </p>
425 * <p>
426 * Notice that getResult() also returns {@code false} if no {@link #ACTION_DROP} is sent. This
427 * happens, for example, when the user releases the drag shadow over an area outside of the
428 * application. In this case, the system sends out {@link #ACTION_DRAG_ENDED} for the current
429 * operation, but never sends out {@link #ACTION_DROP}.
430 * </p>
431 * @return {@code true} if a drag event listener returned {@code true} in response to
432 * {@link #ACTION_DROP}. If the system did not send {@link #ACTION_DROP} before
433 * {@link #ACTION_DRAG_ENDED}, or if the listener returned {@code false} in response to
434 * {@link #ACTION_DROP}, then {@code false} is returned.
Christopher Tate855e4c92010-11-19 14:55:12 -0800435 */
Chris Tated4533f12010-10-19 15:15:08 -0700436 public boolean getResult() {
437 return mDragResult;
438 }
439
Christopher Tatea53146c2010-09-07 11:57:52 -0700440 /**
441 * Recycle the DragEvent, to be re-used by a later caller. After calling
442 * this function you must never touch the event again.
Christopher Tate855e4c92010-11-19 14:55:12 -0800443 *
444 * @hide
Christopher Tatea53146c2010-09-07 11:57:52 -0700445 */
446 public final void recycle() {
447 // Ensure recycle is only called once!
448 if (TRACK_RECYCLED_LOCATION) {
449 if (mRecycledLocation != null) {
450 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
451 }
452 mRecycledLocation = new RuntimeException("Last recycled here");
453 } else {
454 if (mRecycled) {
455 throw new RuntimeException(toString() + " recycled twice!");
456 }
457 mRecycled = true;
458 }
459
460 mClipData = null;
461 mClipDescription = null;
Christopher Tate407b4e92010-11-30 17:14:08 -0800462 mLocalState = null;
Vadim Tryshev1edc6daf2016-09-15 16:19:15 -0700463 mEventHandlerWasCalled = false;
Christopher Tatea53146c2010-09-07 11:57:52 -0700464
465 synchronized (gRecyclerLock) {
466 if (gRecyclerUsed < MAX_RECYCLED) {
467 gRecyclerUsed++;
468 mNext = gRecyclerTop;
469 gRecyclerTop = this;
470 }
471 }
472 }
473
Joe Malin32736f02011-01-19 16:14:20 -0800474 /**
475 * Returns a string containing a concise, human-readable representation of this DragEvent
476 * object.
477 * @return A string representation of the DragEvent object.
478 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700479 @Override
480 public String toString() {
481 return "DragEvent{" + Integer.toHexString(System.identityHashCode(this))
482 + " action=" + mAction + " @ (" + mX + ", " + mY + ") desc=" + mClipDescription
Christopher Tatef01af752011-01-19 16:22:07 -0800483 + " data=" + mClipData + " local=" + mLocalState + " result=" + mDragResult
Christopher Tatea53146c2010-09-07 11:57:52 -0700484 + "}";
485 }
486
487 /* Parcelable interface */
488
Joe Malin32736f02011-01-19 16:14:20 -0800489 /**
490 * Returns information about the {@link android.os.Parcel} representation of this DragEvent
491 * object.
492 * @return Information about the {@link android.os.Parcel} representation.
493 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700494 public int describeContents() {
495 return 0;
496 }
497
Joe Malin32736f02011-01-19 16:14:20 -0800498 /**
499 * Creates a {@link android.os.Parcel} object from this DragEvent object.
500 * @param dest A {@link android.os.Parcel} object in which to put the DragEvent object.
501 * @param flags Flags to store in the Parcel.
502 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700503 public void writeToParcel(Parcel dest, int flags) {
504 dest.writeInt(mAction);
505 dest.writeFloat(mX);
506 dest.writeFloat(mY);
Chris Tated4533f12010-10-19 15:15:08 -0700507 dest.writeInt(mDragResult ? 1 : 0);
Christopher Tatea53146c2010-09-07 11:57:52 -0700508 if (mClipData == null) {
509 dest.writeInt(0);
510 } else {
511 dest.writeInt(1);
512 mClipData.writeToParcel(dest, flags);
513 }
514 if (mClipDescription == null) {
515 dest.writeInt(0);
516 } else {
517 dest.writeInt(1);
518 mClipDescription.writeToParcel(dest, flags);
519 }
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700520 if (mDragAndDropPermissions == null) {
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700521 dest.writeInt(0);
522 } else {
523 dest.writeInt(1);
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700524 dest.writeStrongBinder(mDragAndDropPermissions.asBinder());
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700525 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700526 }
527
Joe Malin32736f02011-01-19 16:14:20 -0800528 /**
529 * A container for creating a DragEvent from a Parcel.
530 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700531 public static final @android.annotation.NonNull Parcelable.Creator<DragEvent> CREATOR =
Christopher Tatea53146c2010-09-07 11:57:52 -0700532 new Parcelable.Creator<DragEvent>() {
533 public DragEvent createFromParcel(Parcel in) {
534 DragEvent event = DragEvent.obtain();
535 event.mAction = in.readInt();
536 event.mX = in.readFloat();
537 event.mY = in.readFloat();
Chris Tated4533f12010-10-19 15:15:08 -0700538 event.mDragResult = (in.readInt() != 0);
Christopher Tatea53146c2010-09-07 11:57:52 -0700539 if (in.readInt() != 0) {
540 event.mClipData = ClipData.CREATOR.createFromParcel(in);
541 }
542 if (in.readInt() != 0) {
543 event.mClipDescription = ClipDescription.CREATOR.createFromParcel(in);
544 }
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700545 if (in.readInt() != 0) {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700546 event.mDragAndDropPermissions =
547 IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder());;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700548 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700549 return event;
550 }
551
552 public DragEvent[] newArray(int size) {
553 return new DragEvent[size];
554 }
555 };
556}