blob: a394f351b7f3dec37fa30b812551521474432bb6 [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
19import android.content.ClipData;
20import android.content.ClipDescription;
21import android.os.Parcel;
22import android.os.Parcelable;
23
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -070024import com.android.internal.view.IDragAndDropPermissions;
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -080025
Joe Malin32736f02011-01-19 16:14:20 -080026//TODO: Improve Javadoc
27/**
28 * Represents an event that is sent out by the system at various times during a drag and drop
29 * operation. It is a complex data structure that contains several important pieces of data about
30 * the operation and the underlying data.
31 * <p>
32 * View objects that receive a DragEvent call {@link #getAction()}, which returns
33 * an action type that indicates the state of the drag and drop operation. This allows a View
34 * object to react to a change in state by changing its appearance or performing other actions.
35 * For example, a View can react to the {@link #ACTION_DRAG_ENTERED} action type by
36 * by changing one or more colors in its displayed image.
37 * </p>
38 * <p>
39 * During a drag and drop operation, the system displays an image that the user drags. This image
40 * is called a drag shadow. Several action types reflect the position of the drag shadow relative
41 * to the View receiving the event.
42 * </p>
43 * <p>
44 * Most methods return valid data only for certain event actions. This is summarized in the
45 * following table. Each possible {@link #getAction()} value is listed in the first column. The
46 * other columns indicate which method or methods return valid data for that getAction() value:
47 * </p>
48 * <table>
49 * <tr>
50 * <th scope="col">getAction() Value</th>
51 * <th scope="col">getClipDescription()</th>
52 * <th scope="col">getLocalState()</th>
53 * <th scope="col">getX()</th>
54 * <th scope="col">getY()</th>
55 * <th scope="col">getClipData()</th>
56 * <th scope="col">getResult()</th>
57 * </tr>
58 * <tr>
59 * <td>ACTION_DRAG_STARTED</td>
60 * <td style="text-align: center;">X</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;">&nbsp;</td>
65 * <td style="text-align: center;">&nbsp;</td>
66 * </tr>
67 * <tr>
68 * <td>ACTION_DRAG_ENTERED</td>
69 * <td style="text-align: center;">X</td>
70 * <td style="text-align: center;">X</td>
71 * <td style="text-align: center;">&nbsp;</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 * </tr>
76 * <tr>
77 * <td>ACTION_DRAG_LOCATION</td>
78 * <td style="text-align: center;">X</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;">&nbsp;</td>
83 * <td style="text-align: center;">&nbsp;</td>
84 * </tr>
85 * <tr>
86 * <td>ACTION_DRAG_EXITED</td>
87 * <td style="text-align: center;">X</td>
88 * <td style="text-align: center;">X</td>
89 * <td style="text-align: center;">&nbsp;</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 * </tr>
94 * <tr>
95 * <td>ACTION_DROP</td>
96 * <td style="text-align: center;">X</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;">&nbsp;</td>
102 * </tr>
103 * <tr>
104 * <td>ACTION_DRAG_ENDED</td>
Vladislav Kaznacheev3067bc62016-07-11 13:52:22 -0700105 * <td style="text-align: center;">&nbsp;</td>
106 * <td style="text-align: center;">&nbsp;</td>
Joe Malin32736f02011-01-19 16:14:20 -0800107 * <td style="text-align: center;">&nbsp;</td>
108 * <td style="text-align: center;">&nbsp;</td>
109 * <td style="text-align: center;">&nbsp;</td>
110 * <td style="text-align: center;">X</td>
111 * </tr>
112 * </table>
113 * <p>
114 * The {@link android.view.DragEvent#getAction()},
115 * {@link android.view.DragEvent#describeContents()},
116 * {@link android.view.DragEvent#writeToParcel(Parcel,int)}, and
117 * {@link android.view.DragEvent#toString()} methods always return valid data.
118 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -0700119 *
120 * <div class="special reference">
121 * <h3>Developer Guides</h3>
122 * <p>For a guide to implementing drag and drop features, read the
123 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
124 * </div>
Joe Malin32736f02011-01-19 16:14:20 -0800125 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700126public class DragEvent implements Parcelable {
127 private static final boolean TRACK_RECYCLED_LOCATION = false;
128
129 int mAction;
130 float mX, mY;
131 ClipDescription mClipDescription;
132 ClipData mClipData;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700133 IDragAndDropPermissions mDragAndDropPermissions;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700134
Christopher Tate407b4e92010-11-30 17:14:08 -0800135 Object mLocalState;
Chris Tated4533f12010-10-19 15:15:08 -0700136 boolean mDragResult;
Christopher Tatea53146c2010-09-07 11:57:52 -0700137
138 private DragEvent mNext;
139 private RuntimeException mRecycledLocation;
140 private boolean mRecycled;
141
142 private static final int MAX_RECYCLED = 10;
143 private static final Object gRecyclerLock = new Object();
144 private static int gRecyclerUsed = 0;
145 private static DragEvent gRecyclerTop = null;
146
147 /**
Joe Malin32736f02011-01-19 16:14:20 -0800148 * Action constant returned by {@link #getAction()}: Signals the start of a
149 * drag and drop operation. The View should return {@code true} from its
150 * {@link View#onDragEvent(DragEvent) onDragEvent()} handler method or
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800151 * {@link View.OnDragListener#onDrag(View,DragEvent) OnDragListener.onDrag()} listener
Joe Malin32736f02011-01-19 16:14:20 -0800152 * if it can accept a drop. The onDragEvent() or onDrag() methods usually inspect the metadata
153 * from {@link #getClipDescription()} to determine if they can accept the data contained in
154 * this drag. For an operation that doesn't represent data transfer, these methods may
155 * perform other actions to determine whether or not the View should accept the drag.
156 * If the View wants to indicate that it is a valid drop target, it can also react by
157 * changing its appearance.
Christopher Tate855e4c92010-11-19 14:55:12 -0800158 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800159 * A View only receives further drag events if it returns {@code true} in response to
160 * ACTION_DRAG_STARTED.
161 * </p>
162 * @see #ACTION_DRAG_ENDED
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -0700163 * @see #getX()
164 * @see #getY()
Christopher Tatea53146c2010-09-07 11:57:52 -0700165 */
166 public static final int ACTION_DRAG_STARTED = 1;
Christopher Tatea53146c2010-09-07 11:57:52 -0700167
Christopher Tate855e4c92010-11-19 14:55:12 -0800168 /**
Joe Malin32736f02011-01-19 16:14:20 -0800169 * Action constant returned by {@link #getAction()}: Sent to a View after
170 * {@link #ACTION_DRAG_ENTERED} if the drag shadow is still within the View object's bounding
171 * box. The {@link #getX()} and {@link #getY()} methods supply
172 * the X and Y position of of the drag point within the View object's bounding box.
Christopher Tate855e4c92010-11-19 14:55:12 -0800173 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800174 * A View receives an {@link #ACTION_DRAG_ENTERED} event before receiving any
175 * ACTION_DRAG_LOCATION events.
176 * </p>
177 * <p>
178 * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
179 * drag shadow out of the View object's bounding box. If the user moves the drag shadow back
180 * into the View object's bounding box, the View receives an ACTION_DRAG_ENTERED again before
181 * receiving any more ACTION_DRAG_LOCATION events.
182 * </p>
183 * @see #ACTION_DRAG_ENTERED
184 * @see #getX()
185 * @see #getY()
Christopher Tate855e4c92010-11-19 14:55:12 -0800186 */
187 public static final int ACTION_DRAG_LOCATION = 2;
188
189 /**
Joe Malin32736f02011-01-19 16:14:20 -0800190 * Action constant returned by {@link #getAction()}: Signals to a View that the user
191 * has released the drag shadow, and the drag point is within the bounding box of the View.
192 * The View should retrieve the data from the DragEvent by calling {@link #getClipData()}.
193 * The methods {@link #getX()} and {@link #getY()} return the X and Y position of the drop point
194 * within the View object's bounding box.
Christopher Tate855e4c92010-11-19 14:55:12 -0800195 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800196 * The View should return {@code true} from its {@link View#onDragEvent(DragEvent)}
Vladislav Kaznacheev9149d2b2015-12-15 12:16:28 -0800197 * handler or {@link View.OnDragListener#onDrag(View,DragEvent) OnDragListener.onDrag()}
Joe Malin32736f02011-01-19 16:14:20 -0800198 * listener if it accepted the drop, and {@code false} if it ignored the drop.
199 * </p>
200 * <p>
201 * The View can also react to this action by changing its appearance.
202 * </p>
203 * @see #getClipData()
204 * @see #getX()
205 * @see #getY()
Christopher Tate855e4c92010-11-19 14:55:12 -0800206 */
207 public static final int ACTION_DROP = 3;
208
209 /**
Joe Malin32736f02011-01-19 16:14:20 -0800210 * Action constant returned by {@link #getAction()}: Signals to a View that the drag and drop
211 * operation has concluded. A View that changed its appearance during the operation should
212 * return to its usual drawing state in response to this event.
Christopher Tate855e4c92010-11-19 14:55:12 -0800213 * <p>
214 * All views that received an ACTION_DRAG_STARTED event will receive the
Joe Malin32736f02011-01-19 16:14:20 -0800215 * ACTION_DRAG_ENDED event even if they are not currently visible when the drag ends.
216 * </p>
217 * <p>
218 * The View object can call {@link #getResult()} to see the result of the operation.
219 * If a View returned {@code true} in response to {@link #ACTION_DROP}, then
220 * getResult() returns {@code true}, otherwise it returns {@code false}.
221 * </p>
222 * @see #ACTION_DRAG_STARTED
223 * @see #getResult()
Christopher Tate855e4c92010-11-19 14:55:12 -0800224 */
225 public static final int ACTION_DRAG_ENDED = 4;
226
227 /**
Joe Malin32736f02011-01-19 16:14:20 -0800228 * Action constant returned by {@link #getAction()}: Signals to a View that the drag point has
229 * entered the bounding box of the View.
Christopher Tate855e4c92010-11-19 14:55:12 -0800230 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800231 * If the View can accept a drop, it can react to ACTION_DRAG_ENTERED
232 * by changing its appearance in a way that tells the user that the View is the current
233 * drop target.
234 * </p>
235 * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
236 * drag shadow out of the View object's bounding box. If the user moves the drag shadow back
237 * into the View object's bounding box, the View receives an ACTION_DRAG_ENTERED again before
238 * receiving any more ACTION_DRAG_LOCATION events.
239 * </p>
240 * @see #ACTION_DRAG_ENTERED
241 * @see #ACTION_DRAG_LOCATION
Christopher Tate855e4c92010-11-19 14:55:12 -0800242 */
243 public static final int ACTION_DRAG_ENTERED = 5;
244
245 /**
Joe Malin32736f02011-01-19 16:14:20 -0800246 * Action constant returned by {@link #getAction()}: Signals that the user has moved the
247 * drag shadow outside the bounding box of the View.
248 * The View can react by changing its appearance in a way that tells the user that
249 * View is no longer the immediate drop target.
Christopher Tate855e4c92010-11-19 14:55:12 -0800250 * <p>
Joe Malin32736f02011-01-19 16:14:20 -0800251 * After the system sends an ACTION_DRAG_EXITED event to the View, the View receives no more
252 * ACTION_DRAG_LOCATION events until the user drags the drag shadow back over the View.
253 * </p>
254 *
Christopher Tate855e4c92010-11-19 14:55:12 -0800255 */
Joe Malin32736f02011-01-19 16:14:20 -0800256 public static final int ACTION_DRAG_EXITED = 6;
Christopher Tate855e4c92010-11-19 14:55:12 -0800257
Christopher Tate2c095f32010-10-04 14:13:40 -0700258 private DragEvent() {
Christopher Tatea53146c2010-09-07 11:57:52 -0700259 }
260
Chris Tated4533f12010-10-19 15:15:08 -0700261 private void init(int action, float x, float y, ClipDescription description, ClipData data,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700262 IDragAndDropPermissions dragAndDropPermissions, Object localState, boolean result) {
Chris Tatec02c7af2010-10-20 18:55:13 -0700263 mAction = action;
264 mX = x;
265 mY = y;
266 mClipDescription = description;
267 mClipData = data;
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700268 this.mDragAndDropPermissions = dragAndDropPermissions;
Christopher Tate7fb8b562011-01-20 13:46:41 -0800269 mLocalState = localState;
Chris Tated4533f12010-10-19 15:15:08 -0700270 mDragResult = result;
Chris Tatec02c7af2010-10-20 18:55:13 -0700271 }
272
Christopher Tate2c095f32010-10-04 14:13:40 -0700273 static DragEvent obtain() {
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700274 return DragEvent.obtain(0, 0f, 0f, null, null, null, null, false);
Christopher Tatea53146c2010-09-07 11:57:52 -0700275 }
276
Christopher Tate855e4c92010-11-19 14:55:12 -0800277 /** @hide */
Christopher Tate407b4e92010-11-30 17:14:08 -0800278 public static DragEvent obtain(int action, float x, float y, Object localState,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700279 ClipDescription description, ClipData data,
280 IDragAndDropPermissions dragAndDropPermissions, boolean result) {
Christopher Tatea53146c2010-09-07 11:57:52 -0700281 final DragEvent ev;
282 synchronized (gRecyclerLock) {
283 if (gRecyclerTop == null) {
Chris Tatec02c7af2010-10-20 18:55:13 -0700284 ev = new DragEvent();
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700285 ev.init(action, x, y, description, data, dragAndDropPermissions, localState,
286 result);
Chris Tatec02c7af2010-10-20 18:55:13 -0700287 return ev;
Christopher Tatea53146c2010-09-07 11:57:52 -0700288 }
289 ev = gRecyclerTop;
290 gRecyclerTop = ev.mNext;
291 gRecyclerUsed -= 1;
292 }
293 ev.mRecycledLocation = null;
294 ev.mRecycled = false;
295 ev.mNext = null;
296
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700297 ev.init(action, x, y, description, data, dragAndDropPermissions, localState, result);
Christopher Tatea53146c2010-09-07 11:57:52 -0700298
299 return ev;
300 }
301
Christopher Tate855e4c92010-11-19 14:55:12 -0800302 /** @hide */
Christopher Tate2c095f32010-10-04 14:13:40 -0700303 public static DragEvent obtain(DragEvent source) {
Christopher Tate407b4e92010-11-30 17:14:08 -0800304 return obtain(source.mAction, source.mX, source.mY, source.mLocalState,
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700305 source.mClipDescription, source.mClipData, source.mDragAndDropPermissions,
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700306 source.mDragResult);
Christopher Tate2c095f32010-10-04 14:13:40 -0700307 }
308
Christopher Tate855e4c92010-11-19 14:55:12 -0800309 /**
310 * Inspect the action value of this event.
Joe Malin32736f02011-01-19 16:14:20 -0800311 * @return One of the following action constants, in the order in which they usually occur
312 * during a drag and drop operation:
313 * <ul>
314 * <li>{@link #ACTION_DRAG_STARTED}</li>
315 * <li>{@link #ACTION_DRAG_ENTERED}</li>
316 * <li>{@link #ACTION_DRAG_LOCATION}</li>
317 * <li>{@link #ACTION_DROP}</li>
318 * <li>{@link #ACTION_DRAG_EXITED}</li>
319 * <li>{@link #ACTION_DRAG_ENDED}</li>
320 * </ul>
Christopher Tate855e4c92010-11-19 14:55:12 -0800321 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700322 public int getAction() {
323 return mAction;
324 }
325
Christopher Tate855e4c92010-11-19 14:55:12 -0800326 /**
Joe Malin32736f02011-01-19 16:14:20 -0800327 * 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 -0700328 * {@link #ACTION_DRAG_STARTED}, {@link #ACTION_DRAG_LOCATION} or {@link #ACTION_DROP}.
329 * @return The current drag point's X coordinate
Christopher Tate855e4c92010-11-19 14:55:12 -0800330 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700331 public float getX() {
332 return mX;
333 }
334
Christopher Tate855e4c92010-11-19 14:55:12 -0800335 /**
Vladislav Kaznacheevc2449702016-05-16 12:57:15 -0700336 * Gets the Y coordinate of the drag point. The value is only valid if the event action is
337 * {@link #ACTION_DRAG_STARTED}, {@link #ACTION_DRAG_LOCATION} or {@link #ACTION_DROP}.
Joe Malin32736f02011-01-19 16:14:20 -0800338 * @return The current drag point's Y coordinate
Christopher Tate855e4c92010-11-19 14:55:12 -0800339 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700340 public float getY() {
341 return mY;
342 }
343
Christopher Tate855e4c92010-11-19 14:55:12 -0800344 /**
Joe Malin32736f02011-01-19 16:14:20 -0800345 * Returns the {@link android.content.ClipData} object sent to the system as part of the call
346 * to
347 * {@link android.view.View#startDrag(ClipData,View.DragShadowBuilder,Object,int) startDrag()}.
348 * This method only returns valid data if the event action is {@link #ACTION_DROP}.
349 * @return The ClipData sent to the system by startDrag().
Christopher Tate855e4c92010-11-19 14:55:12 -0800350 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700351 public ClipData getClipData() {
352 return mClipData;
353 }
354
Christopher Tate855e4c92010-11-19 14:55:12 -0800355 /**
Joe Malin32736f02011-01-19 16:14:20 -0800356 * Returns the {@link android.content.ClipDescription} object contained in the
357 * {@link android.content.ClipData} object sent to the system as part of the call to
358 * {@link android.view.View#startDrag(ClipData,View.DragShadowBuilder,Object,int) startDrag()}.
359 * The drag handler or listener for a View can use the metadata in this object to decide if the
360 * View can accept the dragged View object's data.
361 * <p>
Vladislav Kaznacheev3067bc62016-07-11 13:52:22 -0700362 * This method returns valid data for all event actions except for {@link #ACTION_DRAG_ENDED}.
Joe Malin32736f02011-01-19 16:14:20 -0800363 * @return The ClipDescription that was part of the ClipData sent to the system by startDrag().
Christopher Tate855e4c92010-11-19 14:55:12 -0800364 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700365 public ClipDescription getClipDescription() {
366 return mClipDescription;
367 }
368
Vladislav Kaznacheevb23a7572015-12-18 12:23:43 -0800369 /** @hide */
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700370 public IDragAndDropPermissions getDragAndDropPermissions() {
371 return mDragAndDropPermissions;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700372 }
373
374 /**
Joe Malin32736f02011-01-19 16:14:20 -0800375 * Returns the local state object sent to the system as part of the call to
376 * {@link android.view.View#startDrag(ClipData,View.DragShadowBuilder,Object,int) startDrag()}.
377 * The object is intended to provide local information about the drag and drop operation. For
378 * example, it can indicate whether the drag and drop operation is a copy or a move.
379 * <p>
Vladislav Kaznacheev048f3bf2016-09-08 16:37:23 -0700380 * The local state is available only to views in the activity which has started the drag
381 * operation. In all other activities this method will return null
382 * </p>
383 * <p>
Vladislav Kaznacheev3067bc62016-07-11 13:52:22 -0700384 * This method returns valid data for all event actions except for {@link #ACTION_DRAG_ENDED}.
Joe Malin32736f02011-01-19 16:14:20 -0800385 * </p>
386 * @return The local state object sent to the system by startDrag().
Christopher Tate407b4e92010-11-30 17:14:08 -0800387 */
388 public Object getLocalState() {
389 return mLocalState;
390 }
391
392 /**
Joe Malin32736f02011-01-19 16:14:20 -0800393 * <p>
394 * Returns an indication of the result of the drag and drop operation.
395 * This method only returns valid data if the action type is {@link #ACTION_DRAG_ENDED}.
396 * The return value depends on what happens after the user releases the drag shadow.
397 * </p>
398 * <p>
399 * If the user releases the drag shadow on a View that can accept a drop, the system sends an
400 * {@link #ACTION_DROP} event to the View object's drag event listener. If the listener
401 * returns {@code true}, then getResult() will return {@code true}.
402 * If the listener returns {@code false}, then getResult() returns {@code false}.
403 * </p>
404 * <p>
405 * Notice that getResult() also returns {@code false} if no {@link #ACTION_DROP} is sent. This
406 * happens, for example, when the user releases the drag shadow over an area outside of the
407 * application. In this case, the system sends out {@link #ACTION_DRAG_ENDED} for the current
408 * operation, but never sends out {@link #ACTION_DROP}.
409 * </p>
410 * @return {@code true} if a drag event listener returned {@code true} in response to
411 * {@link #ACTION_DROP}. If the system did not send {@link #ACTION_DROP} before
412 * {@link #ACTION_DRAG_ENDED}, or if the listener returned {@code false} in response to
413 * {@link #ACTION_DROP}, then {@code false} is returned.
Christopher Tate855e4c92010-11-19 14:55:12 -0800414 */
Chris Tated4533f12010-10-19 15:15:08 -0700415 public boolean getResult() {
416 return mDragResult;
417 }
418
Christopher Tatea53146c2010-09-07 11:57:52 -0700419 /**
420 * Recycle the DragEvent, to be re-used by a later caller. After calling
421 * this function you must never touch the event again.
Christopher Tate855e4c92010-11-19 14:55:12 -0800422 *
423 * @hide
Christopher Tatea53146c2010-09-07 11:57:52 -0700424 */
425 public final void recycle() {
426 // Ensure recycle is only called once!
427 if (TRACK_RECYCLED_LOCATION) {
428 if (mRecycledLocation != null) {
429 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
430 }
431 mRecycledLocation = new RuntimeException("Last recycled here");
432 } else {
433 if (mRecycled) {
434 throw new RuntimeException(toString() + " recycled twice!");
435 }
436 mRecycled = true;
437 }
438
439 mClipData = null;
440 mClipDescription = null;
Christopher Tate407b4e92010-11-30 17:14:08 -0800441 mLocalState = null;
Christopher Tatea53146c2010-09-07 11:57:52 -0700442
443 synchronized (gRecyclerLock) {
444 if (gRecyclerUsed < MAX_RECYCLED) {
445 gRecyclerUsed++;
446 mNext = gRecyclerTop;
447 gRecyclerTop = this;
448 }
449 }
450 }
451
Joe Malin32736f02011-01-19 16:14:20 -0800452 /**
453 * Returns a string containing a concise, human-readable representation of this DragEvent
454 * object.
455 * @return A string representation of the DragEvent object.
456 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700457 @Override
458 public String toString() {
459 return "DragEvent{" + Integer.toHexString(System.identityHashCode(this))
460 + " action=" + mAction + " @ (" + mX + ", " + mY + ") desc=" + mClipDescription
Christopher Tatef01af752011-01-19 16:22:07 -0800461 + " data=" + mClipData + " local=" + mLocalState + " result=" + mDragResult
Christopher Tatea53146c2010-09-07 11:57:52 -0700462 + "}";
463 }
464
465 /* Parcelable interface */
466
Joe Malin32736f02011-01-19 16:14:20 -0800467 /**
468 * Returns information about the {@link android.os.Parcel} representation of this DragEvent
469 * object.
470 * @return Information about the {@link android.os.Parcel} representation.
471 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700472 public int describeContents() {
473 return 0;
474 }
475
Joe Malin32736f02011-01-19 16:14:20 -0800476 /**
477 * Creates a {@link android.os.Parcel} object from this DragEvent object.
478 * @param dest A {@link android.os.Parcel} object in which to put the DragEvent object.
479 * @param flags Flags to store in the Parcel.
480 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700481 public void writeToParcel(Parcel dest, int flags) {
482 dest.writeInt(mAction);
483 dest.writeFloat(mX);
484 dest.writeFloat(mY);
Chris Tated4533f12010-10-19 15:15:08 -0700485 dest.writeInt(mDragResult ? 1 : 0);
Christopher Tatea53146c2010-09-07 11:57:52 -0700486 if (mClipData == null) {
487 dest.writeInt(0);
488 } else {
489 dest.writeInt(1);
490 mClipData.writeToParcel(dest, flags);
491 }
492 if (mClipDescription == null) {
493 dest.writeInt(0);
494 } else {
495 dest.writeInt(1);
496 mClipDescription.writeToParcel(dest, flags);
497 }
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700498 if (mDragAndDropPermissions == null) {
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700499 dest.writeInt(0);
500 } else {
501 dest.writeInt(1);
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700502 dest.writeStrongBinder(mDragAndDropPermissions.asBinder());
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700503 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700504 }
505
Joe Malin32736f02011-01-19 16:14:20 -0800506 /**
507 * A container for creating a DragEvent from a Parcel.
508 */
Christopher Tatea53146c2010-09-07 11:57:52 -0700509 public static final Parcelable.Creator<DragEvent> CREATOR =
510 new Parcelable.Creator<DragEvent>() {
511 public DragEvent createFromParcel(Parcel in) {
512 DragEvent event = DragEvent.obtain();
513 event.mAction = in.readInt();
514 event.mX = in.readFloat();
515 event.mY = in.readFloat();
Chris Tated4533f12010-10-19 15:15:08 -0700516 event.mDragResult = (in.readInt() != 0);
Christopher Tatea53146c2010-09-07 11:57:52 -0700517 if (in.readInt() != 0) {
518 event.mClipData = ClipData.CREATOR.createFromParcel(in);
519 }
520 if (in.readInt() != 0) {
521 event.mClipDescription = ClipDescription.CREATOR.createFromParcel(in);
522 }
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700523 if (in.readInt() != 0) {
Vladislav Kaznacheev377c3282016-04-20 14:22:23 -0700524 event.mDragAndDropPermissions =
525 IDragAndDropPermissions.Stub.asInterface(in.readStrongBinder());;
Vladislav Kaznacheevede5f542015-07-15 18:04:04 -0700526 }
Christopher Tatea53146c2010-09-07 11:57:52 -0700527 return event;
528 }
529
530 public DragEvent[] newArray(int size) {
531 return new DragEvent[size];
532 }
533 };
534}