OSDN Git Service

c6563fd259478068a320a051e804ed02143756a8
[android-x86/frameworks-base.git] / core / java / android / view / accessibility / AccessibilityEvent.java
1 /*
2  * Copyright (C) 2009 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 android.view.accessibility;
18
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.text.TextUtils;
22 import android.util.Pools.SynchronizedPool;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28  * <p>
29  * This class represents accessibility events that are sent by the system when
30  * something notable happens in the user interface. For example, when a
31  * {@link android.widget.Button} is clicked, a {@link android.view.View} is focused, etc.
32  * </p>
33  * <p>
34  * An accessibility event is fired by an individual view which populates the event with
35  * data for its state and requests from its parent to send the event to interested
36  * parties. The parent can optionally add an {@link AccessibilityRecord} for itself before
37  * dispatching a similar request to its parent. A parent can also choose not to respect the
38  * request for sending an event. The accessibility event is sent by the topmost view in the
39  * view tree. Therefore, an {@link android.accessibilityservice.AccessibilityService} can
40  * explore all records in an accessibility event to obtain more information about the
41  * context in which the event was fired.
42  * </p>
43  * <p>
44  * The main purpose of an accessibility event is to expose enough information for an
45  * {@link android.accessibilityservice.AccessibilityService} to provide meaningful feedback
46  * to the user. Sometimes however, an accessibility service may need more contextual
47  * information then the one in the event pay-load. In such cases the service can obtain
48  * the event source which is an {@link AccessibilityNodeInfo} (snapshot of a View state)
49  * which can be used for exploring the window content. Note that the privilege for accessing
50  * an event's source, thus the window content, has to be explicitly requested. For more
51  * details refer to {@link android.accessibilityservice.AccessibilityService}. If an
52  * accessibility service has not requested to retrieve the window content the event will
53  * not contain reference to its source. Also for events of type
54  * {@link #TYPE_NOTIFICATION_STATE_CHANGED} the source is never available.
55  * </p>
56  * <p>
57  * This class represents various semantically different accessibility event
58  * types. Each event type has an associated set of related properties. In other
59  * words, each event type is characterized via a subset of the properties exposed
60  * by this class. For each event type there is a corresponding constant defined
61  * in this class. Follows a specification of the event types and their associated properties:
62  * </p>
63  * <div class="special reference">
64  * <h3>Developer Guides</h3>
65  * <p>For more information about creating and processing AccessibilityEvents, read the
66  * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
67  * developer guide.</p>
68  * </div>
69  * <p>
70  * <b>VIEW TYPES</b></br>
71  * </p>
72  * <p>
73  * <b>View clicked</b> - represents the event of clicking on a {@link android.view.View}
74  * like {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc.</br>
75  * <em>Type:</em>{@link #TYPE_VIEW_CLICKED}</br>
76  * <em>Properties:</em></br>
77  * <ul>
78  *   <li>{@link #getEventType()} - The type of the event.</li>
79  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
80  *   <li>{@link #getClassName()} - The class name of the source.</li>
81  *   <li>{@link #getPackageName()} - The package name of the source.</li>
82  *   <li>{@link #getEventTime()}  - The event time.</li>
83  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
84  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
85  *   <li>{@link #isPassword()} - Whether the source is password.</li>
86  *   <li>{@link #isChecked()} - Whether the source is checked.</li>
87  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
88  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
89  *       (without descendants of AdapterView).</li>
90  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
91  *       (without descendants of AdapterView).</li>
92  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
93  *       inclusive (for descendants of AdapterView).</li>
94  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
95  *       inclusive (for descendants of AdapterView).</li>
96  *   <li>{@link #getItemCount()} - The total items of the source
97  *       (for descendants of AdapterView).</li>
98  * </ul>
99  * </p>
100  * <p>
101  * <b>View long clicked</b> - represents the event of long clicking on a {@link android.view.View}
102  * like {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc </br>
103  * <em>Type:</em>{@link #TYPE_VIEW_LONG_CLICKED}</br>
104  * <em>Properties:</em></br>
105  * <ul>
106  *   <li>{@link #getEventType()} - The type of the event.</li>
107  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
108  *   <li>{@link #getClassName()} - The class name of the source.</li>
109  *   <li>{@link #getPackageName()} - The package name of the source.</li>
110  *   <li>{@link #getEventTime()}  - The event time.</li>
111  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
112  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
113  *   <li>{@link #isPassword()} - Whether the source is password.</li>
114  *   <li>{@link #isChecked()} - Whether the source is checked.</li>
115  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
116  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
117  *       (without descendants of AdapterView).</li>
118  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
119  *       (without descendants of AdapterView).</li>
120  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
121  *       inclusive (for descendants of AdapterView).</li>
122  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
123  *       inclusive (for descendants of AdapterView).</li>
124  *   <li>{@link #getItemCount()} - The total items of the source
125  *       (for descendants of AdapterView).</li>
126  * </ul>
127  * </p>
128  * <p>
129  * <b>View selected</b> - represents the event of selecting an item usually in
130  * the context of an {@link android.widget.AdapterView}.</br>
131  * <em>Type:</em> {@link #TYPE_VIEW_SELECTED}</br>
132  * <em>Properties:</em></br>
133  * <ul>
134  *   <li>{@link #getEventType()} - The type of the event.</li>
135  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
136  *   <li>{@link #getClassName()} - The class name of the source.</li>
137  *   <li>{@link #getPackageName()} - The package name of the source.</li>
138  *   <li>{@link #getEventTime()}  - The event time.</li>
139  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
140  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
141  *   <li>{@link #isPassword()} - Whether the source is password.</li>
142  *   <li>{@link #isChecked()} - Whether the source is checked.</li>
143  *   <li>{@link #getItemCount()} - The number of selectable items of the source.</li>
144  *   <li>{@link #getCurrentItemIndex()} - The currently selected item index.</li>
145  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
146  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
147  *       (without descendants of AdapterView).</li>
148  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
149  *       (without descendants of AdapterView).</li>
150  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
151  *       inclusive (for descendants of AdapterView).</li>
152  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
153  *       inclusive (for descendants of AdapterView).</li>
154  *   <li>{@link #getItemCount()} - The total items of the source
155  *       (for descendants of AdapterView).</li>
156  * </ul>
157  * </p>
158  * <p>
159  * <b>View focused</b> - represents the event of focusing a
160  * {@link android.view.View}.</br>
161  * <em>Type:</em> {@link #TYPE_VIEW_FOCUSED}</br>
162  * <em>Properties:</em></br>
163  * <ul>
164  *   <li>{@link #getEventType()} - The type of the event.</li>
165  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
166  *   <li>{@link #getClassName()} - The class name of the source.</li>
167  *   <li>{@link #getPackageName()} - The package name of the source.</li>
168  *   <li>{@link #getEventTime()}  - The event time.</li>
169  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
170  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
171  *   <li>{@link #isPassword()} - Whether the source is password.</li>
172  *   <li>{@link #isChecked()} - Whether the source is checked.</li>
173  *   <li>{@link #getItemCount()} - The number of focusable items on the screen.</li>
174  *   <li>{@link #getCurrentItemIndex()} - The currently focused item index.</li>
175  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
176  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
177  *       (without descendants of AdapterView).</li>
178  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
179  *       (without descendants of AdapterView).</li>
180  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
181  *       inclusive (for descendants of AdapterView).</li>
182  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
183  *       inclusive (for descendants of AdapterView).</li>
184  *   <li>{@link #getItemCount()} - The total items of the source
185  *       (for descendants of AdapterView).</li>
186  * </ul>
187  * </p>
188  * <p>
189  * <b>View text changed</b> - represents the event of changing the text of an
190  * {@link android.widget.EditText}.</br>
191  * <em>Type:</em> {@link #TYPE_VIEW_TEXT_CHANGED}</br>
192  * <em>Properties:</em></br>
193  * <ul>
194  *   <li>{@link #getEventType()} - The type of the event.</li>
195  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
196  *   <li>{@link #getClassName()} - The class name of the source.</li>
197  *   <li>{@link #getPackageName()} - The package name of the source.</li>
198  *   <li>{@link #getEventTime()}  - The event time.</li>
199  *   <li>{@link #getText()} - The text of the source.</li>
200  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
201  *   <li>{@link #isPassword()} - Whether the source is password.</li>
202  *   <li>{@link #isChecked()} - Whether the source is checked.</li>
203  *   <li>{@link #getFromIndex()} - The text change start index.</li>
204  *   <li>{@link #getAddedCount()} - The number of added characters.</li>
205  *   <li>{@link #getRemovedCount()} - The number of removed characters.</li>
206  *   <li>{@link #getBeforeText()} - The text of the source before the change.</li>
207  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
208  * </ul>
209  * </p>
210  * <p>
211  * <b>View text selection changed</b> - represents the event of changing the text
212  * selection of an {@link android.widget.EditText}.</br>
213  * <em>Type:</em> {@link #TYPE_VIEW_TEXT_SELECTION_CHANGED} </br>
214  * <em>Properties:</em></br>
215  * <ul>
216  *   <li>{@link #getEventType()} - The type of the event.</li>
217  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
218  *   <li>{@link #getClassName()} - The class name of the source.</li>
219  *   <li>{@link #getPackageName()} - The package name of the source.</li>
220  *   <li>{@link #getEventTime()}  - The event time.</li>
221  *   <li>{@link #getText()} - The text of the source.</li>
222  *   <li>{@link #isPassword()} - Whether the source is password.</li>
223  *   <li>{@link #getFromIndex()} - The selection start index.</li>
224  *   <li>{@link #getToIndex()} - The selection end index.</li>
225  *   <li>{@link #getItemCount()} - The length of the source text.</li>
226  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
227  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
228  * </ul>
229  * </p>
230  * <b>View text traversed at movement granularity</b> - represents the event of traversing the
231  * text of a view at a given granularity. For example, moving to the next word.</br>
232  * <em>Type:</em> {@link #TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY} </br>
233  * <em>Properties:</em></br>
234  * <ul>
235  *   <li>{@link #getEventType()} - The type of the event.</li>
236  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
237  *   <li>{@link #getClassName()} - The class name of the source.</li>
238  *   <li>{@link #getPackageName()} - The package name of the source.</li>
239  *   <li>{@link #getEventTime()}  - The event time.</li>
240  *   <li>{@link #getMovementGranularity()} - Sets the granularity at which a view's text
241  *       was traversed.</li>
242  *   <li>{@link #getText()} -  The text of the source's sub-tree.</li>
243  *   <li>{@link #getFromIndex()} - The start the text that was skipped over in this movement.
244  *       This is the starting point when moving forward through the text, but not when moving
245  *       back.</li>
246  *   <li>{@link #getToIndex()} - The end of the text that was skipped over in this movement.
247  *       This is the ending point when moving forward through the text, but not when moving
248  *       back.</li>
249  *   <li>{@link #isPassword()} - Whether the source is password.</li>
250  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
251  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
252  *   <li>{@link #getMovementGranularity()} - Sets the granularity at which a view's text
253  *       was traversed.</li>
254  *   <li>{@link #getAction()} - Gets traversal action which specifies the direction.</li>
255  * </ul>
256  * </p>
257  * <p>
258  * <b>View scrolled</b> - represents the event of scrolling a view. If
259  * the source is a descendant of {@link android.widget.AdapterView} the
260  * scroll is reported in terms of visible items - the first visible item,
261  * the last visible item, and the total items - because the the source
262  * is unaware of its pixel size since its adapter is responsible for
263  * creating views. In all other cases the scroll is reported as the current
264  * scroll on the X and Y axis respectively plus the height of the source in
265  * pixels.</br>
266  * <em>Type:</em> {@link #TYPE_VIEW_SCROLLED}</br>
267  * <em>Properties:</em></br>
268  * <ul>
269  *   <li>{@link #getEventType()} - The type of the event.</li>
270  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
271  *   <li>{@link #getClassName()} - The class name of the source.</li>
272  *   <li>{@link #getPackageName()} - The package name of the source.</li>
273  *   <li>{@link #getEventTime()}  - The event time.</li>
274  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
275  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
276  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
277  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
278  *       (without descendants of AdapterView).</li>
279  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
280  *       (without descendants of AdapterView).</li>
281  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
282  *       inclusive (for descendants of AdapterView).</li>
283  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
284  *       inclusive (for descendants of AdapterView).</li>
285  *   <li>{@link #getItemCount()} - The total items of the source
286  *       (for descendants of AdapterView).</li>
287  * </ul>
288  * <em>Note:</em> This event type is not dispatched to descendants though
289  * {@link android.view.View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
290  * View.dispatchPopulateAccessibilityEvent(AccessibilityEvent)}, hence the event
291  * source {@link android.view.View} and the sub-tree rooted at it will not receive
292  * calls to {@link android.view.View#onPopulateAccessibilityEvent(AccessibilityEvent)
293  * View.onPopulateAccessibilityEvent(AccessibilityEvent)}. The preferred way to add
294  * text content to such events is by setting the
295  * {@link android.R.styleable#View_contentDescription contentDescription} of the source
296  * view.</br>
297  * </p>
298  * <p>
299  * <b>TRANSITION TYPES</b></br>
300  * </p>
301  * <p>
302  * <b>Window state changed</b> - represents the event of opening a
303  * {@link android.widget.PopupWindow}, {@link android.view.Menu},
304  * {@link android.app.Dialog}, etc.</br>
305  * <em>Type:</em> {@link #TYPE_WINDOW_STATE_CHANGED}</br>
306  * <em>Properties:</em></br>
307  * <ul>
308  *   <li>{@link #getEventType()} - The type of the event.</li>
309  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
310  *   <li>{@link #getClassName()} - The class name of the source.</li>
311  *   <li>{@link #getPackageName()} - The package name of the source.</li>
312  *   <li>{@link #getEventTime()}  - The event time.</li>
313  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
314  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
315  * </ul>
316  * </p>
317  * <p>
318  * <b>Window content changed</b> - represents the event of change in the
319  * content of a window. This change can be adding/removing view, changing
320  * a view size, etc.</br>
321  * </p>
322  * <p>
323  * <strong>Note:</strong> This event is fired only for the window source of the
324  * last accessibility event different from {@link #TYPE_NOTIFICATION_STATE_CHANGED}
325  * and its purpose is to notify clients that the content of the user interaction
326  * window has changed.</br>
327  * <em>Type:</em> {@link #TYPE_WINDOW_CONTENT_CHANGED}</br>
328  * <em>Properties:</em></br>
329  * <ul>
330  *   <li>{@link #getEventType()} - The type of the event.</li>
331  *   <li>{@link #getContentChangeTypes()} - The type of content changes.</li>
332  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
333  *   <li>{@link #getClassName()} - The class name of the source.</li>
334  *   <li>{@link #getPackageName()} - The package name of the source.</li>
335  *   <li>{@link #getEventTime()}  - The event time.</li>
336  * </ul>
337  * <em>Note:</em> This event type is not dispatched to descendants though
338  * {@link android.view.View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
339  * View.dispatchPopulateAccessibilityEvent(AccessibilityEvent)}, hence the event
340  * source {@link android.view.View} and the sub-tree rooted at it will not receive
341  * calls to {@link android.view.View#onPopulateAccessibilityEvent(AccessibilityEvent)
342  * View.onPopulateAccessibilityEvent(AccessibilityEvent)}. The preferred way to add
343  * text content to such events is by setting the
344  * {@link android.R.styleable#View_contentDescription contentDescription} of the source
345  * view.</br>
346  * </p>
347  * <p>
348  * <b>Windows changed</b> - represents the event of changes in the windows shown on
349  * the screen such as a window appeared, a window disappeared, a window size changed,
350  * a window layer changed, etc.</br>
351  * <em>Type:</em> {@link #TYPE_WINDOWS_CHANGED}</br>
352  * <em>Properties:</em></br>
353  * <ul>
354  *   <li>{@link #getEventType()} - The type of the event.</li>
355  *   <li>{@link #getEventTime()} - The event time.</li>
356  * </ul>
357  * <em>Note:</em> You can retrieve the {@link AccessibilityWindowInfo} for the window
358  * source of the event via {@link AccessibilityEvent#getSource()} to get the source
359  * node on which then call {@link AccessibilityNodeInfo#getWindow()
360  * AccessibilityNodeInfo.getWindow()} to get the window. Also all windows on the screen can
361  * be retrieved by a call to {@link android.accessibilityservice.AccessibilityService#getWindows()
362  * android.accessibilityservice.AccessibilityService.getWindows()}.
363  * </p>
364  * <p>
365  * <b>NOTIFICATION TYPES</b></br>
366  * </p>
367  * <p>
368  * <b>Notification state changed</b> - represents the event showing a transient piece of information
369  * to the user. This information may be a {@link android.app.Notification} or
370  * {@link android.widget.Toast}.</br>
371  * <em>Type:</em> {@link #TYPE_NOTIFICATION_STATE_CHANGED}</br>
372  * <em>Properties:</em></br>
373  * <ul>
374  *   <li>{@link #getEventType()} - The type of the event.</li>
375  *   <li>{@link #getClassName()} - The class name of the source.</li>
376  *   <li>{@link #getPackageName()} - The package name of the source.</li>
377  *   <li>{@link #getEventTime()}  - The event time.</li>
378  *   <li>{@link #getParcelableData()} - The posted {@link android.app.Notification}, if
379  *   applicable.</li>
380  *   <li>{@link #getText()} - Displayed text of the {@link android.widget.Toast}, if applicable,
381  *   or may contain text from the {@link android.app.Notification}, although
382  *   {@link #getParcelableData()} is a richer set of data for {@link android.app.Notification}.</li>
383  * </ul>
384  * </p>
385  * <p>
386  * <b>EXPLORATION TYPES</b></br>
387  * </p>
388  * <p>
389  * <b>View hover enter</b> - represents the event of beginning to hover
390  * over a {@link android.view.View}. The hover may be generated via
391  * exploring the screen by touch or via a pointing device.</br>
392  * <em>Type:</em> {@link #TYPE_VIEW_HOVER_ENTER}</br>
393  * <em>Properties:</em></br>
394  * <ul>
395  *   <li>{@link #getEventType()} - The type of the event.</li>
396  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
397  *   <li>{@link #getClassName()} - The class name of the source.</li>
398  *   <li>{@link #getPackageName()} - The package name of the source.</li>
399  *   <li>{@link #getEventTime()}  - The event time.</li>
400  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
401  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
402  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
403  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
404  *       (without descendants of AdapterView).</li>
405  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
406  *       (without descendants of AdapterView).</li>
407  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
408  *       inclusive (for descendants of AdapterView).</li>
409  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
410  *       inclusive (for descendants of AdapterView).</li>
411  *   <li>{@link #getItemCount()} - The total items of the source
412  *       (for descendants of AdapterView).</li>
413  * </ul>
414  * </p>
415  * <b>View hover exit</b> - represents the event of stopping to hover
416  * over a {@link android.view.View}. The hover may be generated via
417  * exploring the screen by touch or via a pointing device.</br>
418  * <em>Type:</em> {@link #TYPE_VIEW_HOVER_EXIT}</br>
419  * <em>Properties:</em></br>
420  * <ul>
421  *   <li>{@link #getEventType()} - The type of the event.</li>
422  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
423  *   <li>{@link #getClassName()} - The class name of the source.</li>
424  *   <li>{@link #getPackageName()} - The package name of the source.</li>
425  *   <li>{@link #getEventTime()}  - The event time.</li>
426  *   <li>{@link #getText()} - The text of the source's sub-tree.</li>
427  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
428  *   <li>{@link #getContentDescription()} - The content description of the source.</li>
429  *   <li>{@link #getScrollX()} - The offset of the source left edge in pixels
430  *       (without descendants of AdapterView).</li>
431  *   <li>{@link #getScrollY()} - The offset of the source top edge in pixels
432  *       (without descendants of AdapterView).</li>
433  *   <li>{@link #getFromIndex()} - The zero based index of the first visible item of the source,
434  *       inclusive (for descendants of AdapterView).</li>
435  *   <li>{@link #getToIndex()} - The zero based index of the last visible item of the source,
436  *       inclusive (for descendants of AdapterView).</li>
437  *   <li>{@link #getItemCount()} - The total items of the source
438  *       (for descendants of AdapterView).</li>
439  * </ul>
440  * </p>
441  * <p>
442  * <b>Touch interaction start</b> - represents the event of starting a touch
443  * interaction, which is the user starts touching the screen.</br>
444  * <em>Type:</em> {@link #TYPE_TOUCH_INTERACTION_START}</br>
445  * <em>Properties:</em></br>
446  * <ul>
447  *   <li>{@link #getEventType()} - The type of the event.</li>
448  * </ul>
449  * <em>Note:</em> This event is fired only by the system and is not passed to the
450  * view tree to be populated.</br>
451  * </p>
452  * <p>
453  * <b>Touch interaction end</b> - represents the event of ending a touch
454  * interaction, which is the user stops touching the screen.</br>
455  * <em>Type:</em> {@link #TYPE_TOUCH_INTERACTION_END}</br>
456  * <em>Properties:</em></br>
457  * <ul>
458  *   <li>{@link #getEventType()} - The type of the event.</li>
459  * </ul>
460  * <em>Note:</em> This event is fired only by the system and is not passed to the
461  * view tree to be populated.</br>
462  * </p>
463  * <p>
464  * <b>Touch exploration gesture start</b> - represents the event of starting a touch
465  * exploring gesture.</br>
466  * <em>Type:</em> {@link #TYPE_TOUCH_EXPLORATION_GESTURE_START}</br>
467  * <em>Properties:</em></br>
468  * <ul>
469  *   <li>{@link #getEventType()} - The type of the event.</li>
470  * </ul>
471  * <em>Note:</em> This event is fired only by the system and is not passed to the
472  * view tree to be populated.</br>
473  * </p>
474  * <p>
475  * <b>Touch exploration gesture end</b> - represents the event of ending a touch
476  * exploring gesture.</br>
477  * <em>Type:</em> {@link #TYPE_TOUCH_EXPLORATION_GESTURE_END}</br>
478  * <em>Properties:</em></br>
479  * <ul>
480  *   <li>{@link #getEventType()} - The type of the event.</li>
481  * </ul>
482  * <em>Note:</em> This event is fired only by the system and is not passed to the
483  * view tree to be populated.</br>
484  * </p>
485  * <p>
486  * <b>Touch gesture detection start</b> - represents the event of starting a user
487  * gesture detection.</br>
488  * <em>Type:</em> {@link #TYPE_GESTURE_DETECTION_START}</br>
489  * <em>Properties:</em></br>
490  * <ul>
491  *   <li>{@link #getEventType()} - The type of the event.</li>
492  * </ul>
493  * <em>Note:</em> This event is fired only by the system and is not passed to the
494  * view tree to be populated.</br>
495  * </p>
496  * <p>
497  * <b>Touch gesture detection end</b> - represents the event of ending a user
498  * gesture detection.</br>
499  * <em>Type:</em> {@link #TYPE_GESTURE_DETECTION_END}</br>
500  * <em>Properties:</em></br>
501  * <ul>
502  *   <li>{@link #getEventType()} - The type of the event.</li>
503  * </ul>
504  * <em>Note:</em> This event is fired only by the system and is not passed to the
505  * view tree to be populated.</br>
506  * </p>
507  * <p>
508  * <b>MISCELLANEOUS TYPES</b></br>
509  * </p>
510  * <p>
511  * <b>Announcement</b> - represents the event of an application making an
512  * announcement. Usually this announcement is related to some sort of a context
513  * change for which none of the events representing UI transitions is a good fit.
514  * For example, announcing a new page in a book.</br>
515  * <em>Type:</em> {@link #TYPE_ANNOUNCEMENT}</br>
516  * <em>Properties:</em></br>
517  * <ul>
518  *   <li>{@link #getEventType()} - The type of the event.</li>
519  *   <li>{@link #getSource()} - The source info (for registered clients).</li>
520  *   <li>{@link #getClassName()} - The class name of the source.</li>
521  *   <li>{@link #getPackageName()} - The package name of the source.</li>
522  *   <li>{@link #getEventTime()}  - The event time.</li>
523  *   <li>{@link #getText()} - The text of the announcement.</li>
524  *   <li>{@link #isEnabled()} - Whether the source is enabled.</li>
525  * </ul>
526  * </p>
527  *
528  * @see android.view.accessibility.AccessibilityManager
529  * @see android.accessibilityservice.AccessibilityService
530  * @see AccessibilityNodeInfo
531  */
532 public final class AccessibilityEvent extends AccessibilityRecord implements Parcelable {
533     private static final boolean DEBUG = false;
534
535     /**
536      * Invalid selection/focus position.
537      *
538      * @see #getCurrentItemIndex()
539      */
540     public static final int INVALID_POSITION = -1;
541
542     /**
543      * Maximum length of the text fields.
544      *
545      * @see #getBeforeText()
546      * @see #getText()
547      * </br>
548      * Note: This constant is no longer needed since there
549      *       is no limit on the length of text that is contained
550      *       in an accessibility event anymore.
551      */
552     @Deprecated
553     public static final int MAX_TEXT_LENGTH = 500;
554
555     /**
556      * Represents the event of clicking on a {@link android.view.View} like
557      * {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc.
558      */
559     public static final int TYPE_VIEW_CLICKED = 0x00000001;
560
561     /**
562      * Represents the event of long clicking on a {@link android.view.View} like
563      * {@link android.widget.Button}, {@link android.widget.CompoundButton}, etc.
564      */
565     public static final int TYPE_VIEW_LONG_CLICKED = 0x00000002;
566
567     /**
568      * Represents the event of selecting an item usually in the context of an
569      * {@link android.widget.AdapterView}.
570      */
571     public static final int TYPE_VIEW_SELECTED = 0x00000004;
572
573     /**
574      * Represents the event of setting input focus of a {@link android.view.View}.
575      */
576     public static final int TYPE_VIEW_FOCUSED = 0x00000008;
577
578     /**
579      * Represents the event of changing the text of an {@link android.widget.EditText}.
580      */
581     public static final int TYPE_VIEW_TEXT_CHANGED = 0x00000010;
582
583     /**
584      * Represents the event of opening a {@link android.widget.PopupWindow},
585      * {@link android.view.Menu}, {@link android.app.Dialog}, etc.
586      */
587     public static final int TYPE_WINDOW_STATE_CHANGED = 0x00000020;
588
589     /**
590      * Represents the event showing a {@link android.app.Notification}.
591      */
592     public static final int TYPE_NOTIFICATION_STATE_CHANGED = 0x00000040;
593
594     /**
595      * Represents the event of a hover enter over a {@link android.view.View}.
596      */
597     public static final int TYPE_VIEW_HOVER_ENTER = 0x00000080;
598
599     /**
600      * Represents the event of a hover exit over a {@link android.view.View}.
601      */
602     public static final int TYPE_VIEW_HOVER_EXIT = 0x00000100;
603
604     /**
605      * Represents the event of starting a touch exploration gesture.
606      */
607     public static final int TYPE_TOUCH_EXPLORATION_GESTURE_START = 0x00000200;
608
609     /**
610      * Represents the event of ending a touch exploration gesture.
611      */
612     public static final int TYPE_TOUCH_EXPLORATION_GESTURE_END = 0x00000400;
613
614     /**
615      * Represents the event of changing the content of a window and more
616      * specifically the sub-tree rooted at the event's source.
617      */
618     public static final int TYPE_WINDOW_CONTENT_CHANGED = 0x00000800;
619
620     /**
621      * Represents the event of scrolling a view. This event type is generally not sent directly.
622      * @see View#onScrollChanged(int, int, int, int)
623      */
624     public static final int TYPE_VIEW_SCROLLED = 0x00001000;
625
626     /**
627      * Represents the event of changing the selection in an {@link android.widget.EditText}.
628      */
629     public static final int TYPE_VIEW_TEXT_SELECTION_CHANGED = 0x00002000;
630
631     /**
632      * Represents the event of an application making an announcement.
633      */
634     public static final int TYPE_ANNOUNCEMENT = 0x00004000;
635
636     /**
637      * Represents the event of gaining accessibility focus.
638      */
639     public static final int TYPE_VIEW_ACCESSIBILITY_FOCUSED = 0x00008000;
640
641     /**
642      * Represents the event of clearing accessibility focus.
643      */
644     public static final int TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED = 0x00010000;
645
646     /**
647      * Represents the event of traversing the text of a view at a given movement granularity.
648      */
649     public static final int TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY = 0x00020000;
650
651     /**
652      * Represents the event of beginning gesture detection.
653      */
654     public static final int TYPE_GESTURE_DETECTION_START = 0x00040000;
655
656     /**
657      * Represents the event of ending gesture detection.
658      */
659     public static final int TYPE_GESTURE_DETECTION_END = 0x00080000;
660
661     /**
662      * Represents the event of the user starting to touch the screen.
663      */
664     public static final int TYPE_TOUCH_INTERACTION_START = 0x00100000;
665
666     /**
667      * Represents the event of the user ending to touch the screen.
668      */
669     public static final int TYPE_TOUCH_INTERACTION_END = 0x00200000;
670
671     /**
672      * Represents the event change in the windows shown on the screen.
673      */
674     public static final int TYPE_WINDOWS_CHANGED = 0x00400000;
675
676     /**
677      * Represents the event of a context click on a {@link android.view.View}.
678      */
679     public static final int TYPE_VIEW_CONTEXT_CLICKED = 0x00800000;
680
681     /**
682      * Represents the event of the assistant currently reading the users screen context.
683      */
684     public static final int TYPE_ASSIST_READING_CONTEXT = 0x01000000;
685
686     /**
687      * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
688      * The type of change is not defined.
689      */
690     public static final int CONTENT_CHANGE_TYPE_UNDEFINED = 0x00000000;
691
692     /**
693      * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
694      * A node in the subtree rooted at the source node was added or removed.
695      */
696     public static final int CONTENT_CHANGE_TYPE_SUBTREE = 0x00000001;
697
698     /**
699      * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
700      * The node's text changed.
701      */
702     public static final int CONTENT_CHANGE_TYPE_TEXT = 0x00000002;
703
704     /**
705      * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
706      * The node's content description changed.
707      */
708     public static final int CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION = 0x00000004;
709
710     /**
711      * Mask for {@link AccessibilityEvent} all types.
712      *
713      * @see #TYPE_VIEW_CLICKED
714      * @see #TYPE_VIEW_LONG_CLICKED
715      * @see #TYPE_VIEW_SELECTED
716      * @see #TYPE_VIEW_FOCUSED
717      * @see #TYPE_VIEW_TEXT_CHANGED
718      * @see #TYPE_WINDOW_STATE_CHANGED
719      * @see #TYPE_NOTIFICATION_STATE_CHANGED
720      * @see #TYPE_VIEW_HOVER_ENTER
721      * @see #TYPE_VIEW_HOVER_EXIT
722      * @see #TYPE_TOUCH_EXPLORATION_GESTURE_START
723      * @see #TYPE_TOUCH_EXPLORATION_GESTURE_END
724      * @see #TYPE_WINDOW_CONTENT_CHANGED
725      * @see #TYPE_VIEW_SCROLLED
726      * @see #TYPE_VIEW_TEXT_SELECTION_CHANGED
727      * @see #TYPE_ANNOUNCEMENT
728      * @see #TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
729      * @see #TYPE_GESTURE_DETECTION_START
730      * @see #TYPE_GESTURE_DETECTION_END
731      * @see #TYPE_TOUCH_INTERACTION_START
732      * @see #TYPE_TOUCH_INTERACTION_END
733      * @see #TYPE_WINDOWS_CHANGED
734      * @see #TYPE_VIEW_CONTEXT_CLICKED
735      */
736     public static final int TYPES_ALL_MASK = 0xFFFFFFFF;
737
738     private static final int MAX_POOL_SIZE = 10;
739     private static final SynchronizedPool<AccessibilityEvent> sPool =
740             new SynchronizedPool<AccessibilityEvent>(MAX_POOL_SIZE);
741
742     private int mEventType;
743     private CharSequence mPackageName;
744     private long mEventTime;
745     int mMovementGranularity;
746     int mAction;
747     int mContentChangeTypes;
748
749     private ArrayList<AccessibilityRecord> mRecords;
750
751     /*
752      * Hide constructor from clients.
753      */
754     private AccessibilityEvent() {
755     }
756
757     /**
758      * Initialize an event from another one.
759      *
760      * @param event The event to initialize from.
761      */
762     void init(AccessibilityEvent event) {
763         super.init(event);
764         mEventType = event.mEventType;
765         mMovementGranularity = event.mMovementGranularity;
766         mAction = event.mAction;
767         mContentChangeTypes = event.mContentChangeTypes;
768         mEventTime = event.mEventTime;
769         mPackageName = event.mPackageName;
770     }
771
772     /**
773      * Sets if this instance is sealed.
774      *
775      * @param sealed Whether is sealed.
776      *
777      * @hide
778      */
779     @Override
780     public void setSealed(boolean sealed) {
781         super.setSealed(sealed);
782         final List<AccessibilityRecord> records = mRecords;
783         if (records != null) {
784             final int recordCount = records.size();
785             for (int i = 0; i < recordCount; i++) {
786                 AccessibilityRecord record = records.get(i);
787                 record.setSealed(sealed);
788             }
789         }
790     }
791
792     /**
793      * Gets the number of records contained in the event.
794      *
795      * @return The number of records.
796      */
797     public int getRecordCount() {
798         return mRecords == null ? 0 : mRecords.size();
799     }
800
801     /**
802      * Appends an {@link AccessibilityRecord} to the end of event records.
803      *
804      * @param record The record to append.
805      *
806      * @throws IllegalStateException If called from an AccessibilityService.
807      */
808     public void appendRecord(AccessibilityRecord record) {
809         enforceNotSealed();
810         if (mRecords == null) {
811             mRecords = new ArrayList<AccessibilityRecord>();
812         }
813         mRecords.add(record);
814     }
815
816     /**
817      * Gets the record at a given index.
818      *
819      * @param index The index.
820      * @return The record at the specified index.
821      */
822     public AccessibilityRecord getRecord(int index) {
823         if (mRecords == null) {
824             throw new IndexOutOfBoundsException("Invalid index " + index + ", size is 0");
825         }
826         return mRecords.get(index);
827     }
828
829     /**
830      * Gets the event type.
831      *
832      * @return The event type.
833      */
834     public int getEventType() {
835         return mEventType;
836     }
837
838     /**
839      * Gets the bit mask of change types signaled by an
840      * {@link #TYPE_WINDOW_CONTENT_CHANGED} event. A single event may represent
841      * multiple change types.
842      *
843      * @return The bit mask of change types. One or more of:
844      *         <ul>
845      *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION}
846      *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_SUBTREE}
847      *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_TEXT}
848      *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_UNDEFINED}
849      *         </ul>
850      */
851     public int getContentChangeTypes() {
852         return mContentChangeTypes;
853     }
854
855     /**
856      * Sets the bit mask of node tree changes signaled by an
857      * {@link #TYPE_WINDOW_CONTENT_CHANGED} event.
858      *
859      * @param changeTypes The bit mask of change types.
860      * @throws IllegalStateException If called from an AccessibilityService.
861      * @see #getContentChangeTypes()
862      */
863     public void setContentChangeTypes(int changeTypes) {
864         enforceNotSealed();
865         mContentChangeTypes = changeTypes;
866     }
867
868     /**
869      * Sets the event type.
870      *
871      * @param eventType The event type.
872      *
873      * @throws IllegalStateException If called from an AccessibilityService.
874      */
875     public void setEventType(int eventType) {
876         enforceNotSealed();
877         mEventType = eventType;
878     }
879
880     /**
881      * Gets the time in which this event was sent.
882      *
883      * @return The event time.
884      */
885     public long getEventTime() {
886         return mEventTime;
887     }
888
889     /**
890      * Sets the time in which this event was sent.
891      *
892      * @param eventTime The event time.
893      *
894      * @throws IllegalStateException If called from an AccessibilityService.
895      */
896     public void setEventTime(long eventTime) {
897         enforceNotSealed();
898         mEventTime = eventTime;
899     }
900
901     /**
902      * Gets the package name of the source.
903      *
904      * @return The package name.
905      */
906     public CharSequence getPackageName() {
907         return mPackageName;
908     }
909
910     /**
911      * Sets the package name of the source.
912      *
913      * @param packageName The package name.
914      *
915      * @throws IllegalStateException If called from an AccessibilityService.
916      */
917     public void setPackageName(CharSequence packageName) {
918         enforceNotSealed();
919         mPackageName = packageName;
920     }
921
922     /**
923      * Sets the movement granularity that was traversed.
924      *
925      * @param granularity The granularity.
926      *
927      * @throws IllegalStateException If called from an AccessibilityService.
928      */
929     public void setMovementGranularity(int granularity) {
930         enforceNotSealed();
931         mMovementGranularity = granularity;
932     }
933
934     /**
935      * Gets the movement granularity that was traversed.
936      *
937      * @return The granularity.
938      */
939     public int getMovementGranularity() {
940         return mMovementGranularity;
941     }
942
943     /**
944      * Sets the performed action that triggered this event.
945      * <p>
946      * Valid actions are defined in {@link AccessibilityNodeInfo}:
947      * <ul>
948      * <li>{@link AccessibilityNodeInfo#ACTION_ACCESSIBILITY_FOCUS}
949      * <li>{@link AccessibilityNodeInfo#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
950      * <li>{@link AccessibilityNodeInfo#ACTION_CLEAR_FOCUS}
951      * <li>{@link AccessibilityNodeInfo#ACTION_CLEAR_SELECTION}
952      * <li>{@link AccessibilityNodeInfo#ACTION_CLICK}
953      * <li>etc.
954      * </ul>
955      *
956      * @param action The action.
957      * @throws IllegalStateException If called from an AccessibilityService.
958      * @see AccessibilityNodeInfo#performAction(int)
959      */
960     public void setAction(int action) {
961         enforceNotSealed();
962         mAction = action;
963     }
964
965     /**
966      * Gets the performed action that triggered this event.
967      *
968      * @return The action.
969      */
970     public int getAction() {
971         return mAction;
972     }
973
974     /**
975      * Returns a cached instance if such is available or a new one is
976      * instantiated with its type property set.
977      *
978      * @param eventType The event type.
979      * @return An instance.
980      */
981     public static AccessibilityEvent obtain(int eventType) {
982         AccessibilityEvent event = AccessibilityEvent.obtain();
983         event.setEventType(eventType);
984         return event;
985     }
986
987     /**
988      * Returns a cached instance if such is available or a new one is
989      * created. The returned instance is initialized from the given
990      * <code>event</code>.
991      *
992      * @param event The other event.
993      * @return An instance.
994      */
995     public static AccessibilityEvent obtain(AccessibilityEvent event) {
996         AccessibilityEvent eventClone = AccessibilityEvent.obtain();
997         eventClone.init(event);
998
999         if (event.mRecords != null) {
1000             final int recordCount = event.mRecords.size();
1001             eventClone.mRecords = new ArrayList<AccessibilityRecord>(recordCount);
1002             for (int i = 0; i < recordCount; i++) {
1003                 final AccessibilityRecord record = event.mRecords.get(i);
1004                 final AccessibilityRecord recordClone = AccessibilityRecord.obtain(record);
1005                 eventClone.mRecords.add(recordClone);
1006             }
1007         }
1008
1009         return eventClone;
1010     }
1011
1012     /**
1013      * Returns a cached instance if such is available or a new one is
1014      * instantiated.
1015      *
1016      * @return An instance.
1017      */
1018     public static AccessibilityEvent obtain() {
1019         AccessibilityEvent event = sPool.acquire();
1020         return (event != null) ? event : new AccessibilityEvent();
1021     }
1022
1023     /**
1024      * Recycles an instance back to be reused.
1025      * <p>
1026      *   <b>Note: You must not touch the object after calling this function.</b>
1027      * </p>
1028      *
1029      * @throws IllegalStateException If the event is already recycled.
1030      */
1031     @Override
1032     public void recycle() {
1033         clear();
1034         sPool.release(this);
1035     }
1036
1037     /**
1038      * Clears the state of this instance.
1039      *
1040      * @hide
1041      */
1042     @Override
1043     protected void clear() {
1044         super.clear();
1045         mEventType = 0;
1046         mMovementGranularity = 0;
1047         mAction = 0;
1048         mContentChangeTypes = 0;
1049         mPackageName = null;
1050         mEventTime = 0;
1051         if (mRecords != null) {
1052             while (!mRecords.isEmpty()) {
1053                 AccessibilityRecord record = mRecords.remove(0);
1054                 record.recycle();
1055             }
1056         }
1057     }
1058
1059     /**
1060      * Creates a new instance from a {@link Parcel}.
1061      *
1062      * @param parcel A parcel containing the state of a {@link AccessibilityEvent}.
1063      */
1064     public void initFromParcel(Parcel parcel) {
1065         mSealed = (parcel.readInt() == 1);
1066         mEventType = parcel.readInt();
1067         mMovementGranularity = parcel.readInt();
1068         mAction = parcel.readInt();
1069         mContentChangeTypes = parcel.readInt();
1070         mPackageName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1071         mEventTime = parcel.readLong();
1072         mConnectionId = parcel.readInt();
1073         readAccessibilityRecordFromParcel(this, parcel);
1074
1075         // Read the records.
1076         final int recordCount = parcel.readInt();
1077         if (recordCount > 0) {
1078             mRecords = new ArrayList<AccessibilityRecord>(recordCount);
1079             for (int i = 0; i < recordCount; i++) {
1080                 AccessibilityRecord record = AccessibilityRecord.obtain();
1081                 readAccessibilityRecordFromParcel(record, parcel);
1082                 record.mConnectionId = mConnectionId;
1083                 mRecords.add(record);
1084             }
1085         }
1086     }
1087
1088     /**
1089      * Reads an {@link AccessibilityRecord} from a parcel.
1090      *
1091      * @param record The record to initialize.
1092      * @param parcel The parcel to read from.
1093      */
1094     private void readAccessibilityRecordFromParcel(AccessibilityRecord record,
1095             Parcel parcel) {
1096         record.mBooleanProperties = parcel.readInt();
1097         record.mCurrentItemIndex = parcel.readInt();
1098         record.mItemCount = parcel.readInt();
1099         record.mFromIndex = parcel.readInt();
1100         record.mToIndex = parcel.readInt();
1101         record.mScrollX = parcel.readInt();
1102         record.mScrollY =  parcel.readInt();
1103         record.mMaxScrollX = parcel.readInt();
1104         record.mMaxScrollY =  parcel.readInt();
1105         record.mAddedCount = parcel.readInt();
1106         record.mRemovedCount = parcel.readInt();
1107         record.mClassName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1108         record.mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1109         record.mBeforeText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1110         record.mParcelableData = parcel.readParcelable(null);
1111         parcel.readList(record.mText, null);
1112         record.mSourceWindowId = parcel.readInt();
1113         record.mSourceNode = parcel.readParcelable(null);
1114         record.mSealed = (parcel.readInt() == 1);
1115     }
1116
1117     /**
1118      * {@inheritDoc}
1119      */
1120     public void writeToParcel(Parcel parcel, int flags) {
1121         parcel.writeInt(isSealed() ? 1 : 0);
1122         parcel.writeInt(mEventType);
1123         parcel.writeInt(mMovementGranularity);
1124         parcel.writeInt(mAction);
1125         parcel.writeInt(mContentChangeTypes);
1126         TextUtils.writeToParcel(mPackageName, parcel, 0);
1127         parcel.writeLong(mEventTime);
1128         parcel.writeInt(mConnectionId);
1129         writeAccessibilityRecordToParcel(this, parcel, flags);
1130
1131         // Write the records.
1132         final int recordCount = getRecordCount();
1133         parcel.writeInt(recordCount);
1134         for (int i = 0; i < recordCount; i++) {
1135             AccessibilityRecord record = mRecords.get(i);
1136             writeAccessibilityRecordToParcel(record, parcel, flags);
1137         }
1138     }
1139
1140     /**
1141      * Writes an {@link AccessibilityRecord} to a parcel.
1142      *
1143      * @param record The record to write.
1144      * @param parcel The parcel to which to write.
1145      */
1146     private void writeAccessibilityRecordToParcel(AccessibilityRecord record, Parcel parcel,
1147             int flags) {
1148         parcel.writeInt(record.mBooleanProperties);
1149         parcel.writeInt(record.mCurrentItemIndex);
1150         parcel.writeInt(record.mItemCount);
1151         parcel.writeInt(record.mFromIndex);
1152         parcel.writeInt(record.mToIndex);
1153         parcel.writeInt(record.mScrollX);
1154         parcel.writeInt(record.mScrollY);
1155         parcel.writeInt(record.mMaxScrollX);
1156         parcel.writeInt(record.mMaxScrollY);
1157         parcel.writeInt(record.mAddedCount);
1158         parcel.writeInt(record.mRemovedCount);
1159         TextUtils.writeToParcel(record.mClassName, parcel, flags);
1160         TextUtils.writeToParcel(record.mContentDescription, parcel, flags);
1161         TextUtils.writeToParcel(record.mBeforeText, parcel, flags);
1162         parcel.writeParcelable(record.mParcelableData, flags);
1163         parcel.writeList(record.mText);
1164         parcel.writeInt(record.mSourceWindowId);
1165         // create copy of the node here because the node would be recycled just after it is written
1166         // to parcel
1167         parcel.writeParcelable(record.mSourceNode != null ?
1168                 AccessibilityNodeInfo.obtain(record.mSourceNode) : null, flags);
1169         parcel.writeInt(record.mSealed ? 1 : 0);
1170     }
1171
1172     /**
1173      * {@inheritDoc}
1174      */
1175     public int describeContents() {
1176         return 0;
1177     }
1178
1179     @Override
1180     public String toString() {
1181         StringBuilder builder = new StringBuilder();
1182         builder.append("EventType: ").append(eventTypeToString(mEventType));
1183         builder.append("; EventTime: ").append(mEventTime);
1184         builder.append("; PackageName: ").append(mPackageName);
1185         builder.append("; MovementGranularity: ").append(mMovementGranularity);
1186         builder.append("; Action: ").append(mAction);
1187         builder.append(super.toString());
1188         if (DEBUG) {
1189             builder.append("\n");
1190             builder.append("; ContentChangeTypes: ").append(mContentChangeTypes);
1191             builder.append("; sourceWindowId: ").append(mSourceWindowId);
1192             if (mSourceNode != null) {
1193                 builder.append("; mSourceNodeId: ").append(mSourceNode.getSourceNodeId());
1194             }
1195             for (int i = 0; i < getRecordCount(); i++) {
1196                 final AccessibilityRecord record = getRecord(i);
1197                 builder.append("  Record ");
1198                 builder.append(i);
1199                 builder.append(":");
1200                 builder.append(" [ ClassName: " + record.mClassName);
1201                 builder.append("; Text: " + record.mText);
1202                 builder.append("; ContentDescription: " + record.mContentDescription);
1203                 builder.append("; ItemCount: " + record.mItemCount);
1204                 builder.append("; CurrentItemIndex: " + record.mCurrentItemIndex);
1205                 builder.append("; IsEnabled: " + record.isEnabled());
1206                 builder.append("; IsPassword: " + record.isPassword());
1207                 builder.append("; IsChecked: " + record.isChecked());
1208                 builder.append("; IsFullScreen: " + record.isFullScreen());
1209                 builder.append("; Scrollable: " + record.isScrollable());
1210                 builder.append("; BeforeText: " + record.mBeforeText);
1211                 builder.append("; FromIndex: " + record.mFromIndex);
1212                 builder.append("; ToIndex: " + record.mToIndex);
1213                 builder.append("; ScrollX: " + record.mScrollX);
1214                 builder.append("; ScrollY: " + record.mScrollY);
1215                 builder.append("; AddedCount: " + record.mAddedCount);
1216                 builder.append("; RemovedCount: " + record.mRemovedCount);
1217                 builder.append("; ParcelableData: " + record.mParcelableData);
1218                 builder.append(" ]");
1219                 builder.append("\n");
1220             }
1221         } else {
1222             builder.append("; recordCount: ").append(getRecordCount());
1223         }
1224         return builder.toString();
1225     }
1226
1227     /**
1228      * Returns the string representation of an event type. For example,
1229      * {@link #TYPE_VIEW_CLICKED} is represented by the string TYPE_VIEW_CLICKED.
1230      *
1231      * @param eventType The event type
1232      * @return The string representation.
1233      */
1234     public static String eventTypeToString(int eventType) {
1235         if (eventType == TYPES_ALL_MASK) {
1236             return "TYPES_ALL_MASK";
1237         }
1238         StringBuilder builder = new StringBuilder();
1239         int eventTypeCount = 0;
1240         while (eventType != 0) {
1241             final int eventTypeFlag = 1 << Integer.numberOfTrailingZeros(eventType);
1242             eventType &= ~eventTypeFlag;
1243
1244             if (eventTypeCount > 0) {
1245                 builder.append(", ");
1246             }
1247             builder.append(singleEventTypeToString(eventTypeFlag));
1248
1249             eventTypeCount++;
1250         }
1251         if (eventTypeCount > 1) {
1252             builder.insert(0, '[');
1253             builder.append(']');
1254         }
1255         return builder.toString();
1256     }
1257
1258     private static String singleEventTypeToString(int eventType) {
1259         switch (eventType) {
1260             case TYPE_VIEW_CLICKED: return "TYPE_VIEW_CLICKED";
1261             case TYPE_VIEW_LONG_CLICKED: return "TYPE_VIEW_LONG_CLICKED";
1262             case TYPE_VIEW_SELECTED: return "TYPE_VIEW_SELECTED";
1263             case TYPE_VIEW_FOCUSED: return "TYPE_VIEW_FOCUSED";
1264             case TYPE_VIEW_TEXT_CHANGED: return "TYPE_VIEW_TEXT_CHANGED";
1265             case TYPE_WINDOW_STATE_CHANGED: return "TYPE_WINDOW_STATE_CHANGED";
1266             case TYPE_VIEW_HOVER_ENTER: return "TYPE_VIEW_HOVER_ENTER";
1267             case TYPE_VIEW_HOVER_EXIT: return "TYPE_VIEW_HOVER_EXIT";
1268             case TYPE_NOTIFICATION_STATE_CHANGED: return "TYPE_NOTIFICATION_STATE_CHANGED";
1269             case TYPE_TOUCH_EXPLORATION_GESTURE_START: {
1270                 return "TYPE_TOUCH_EXPLORATION_GESTURE_START";
1271             }
1272             case TYPE_TOUCH_EXPLORATION_GESTURE_END: return "TYPE_TOUCH_EXPLORATION_GESTURE_END";
1273             case TYPE_WINDOW_CONTENT_CHANGED: return "TYPE_WINDOW_CONTENT_CHANGED";
1274             case TYPE_VIEW_TEXT_SELECTION_CHANGED: return "TYPE_VIEW_TEXT_SELECTION_CHANGED";
1275             case TYPE_VIEW_SCROLLED: return "TYPE_VIEW_SCROLLED";
1276             case TYPE_ANNOUNCEMENT: return "TYPE_ANNOUNCEMENT";
1277             case TYPE_VIEW_ACCESSIBILITY_FOCUSED: return "TYPE_VIEW_ACCESSIBILITY_FOCUSED";
1278             case TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
1279                 return "TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED";
1280             }
1281             case TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY: {
1282                 return "TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY";
1283             }
1284             case TYPE_GESTURE_DETECTION_START: return "TYPE_GESTURE_DETECTION_START";
1285             case TYPE_GESTURE_DETECTION_END: return "TYPE_GESTURE_DETECTION_END";
1286             case TYPE_TOUCH_INTERACTION_START: return "TYPE_TOUCH_INTERACTION_START";
1287             case TYPE_TOUCH_INTERACTION_END: return "TYPE_TOUCH_INTERACTION_END";
1288             case TYPE_WINDOWS_CHANGED: return "TYPE_WINDOWS_CHANGED";
1289             case TYPE_VIEW_CONTEXT_CLICKED: return "TYPE_VIEW_CONTEXT_CLICKED";
1290             case TYPE_ASSIST_READING_CONTEXT: return "TYPE_ASSIST_READING_CONTEXT";
1291             default: return Integer.toHexString(eventType);
1292         }
1293     }
1294
1295     /**
1296      * @see Parcelable.Creator
1297      */
1298     public static final Parcelable.Creator<AccessibilityEvent> CREATOR =
1299             new Parcelable.Creator<AccessibilityEvent>() {
1300         public AccessibilityEvent createFromParcel(Parcel parcel) {
1301             AccessibilityEvent event = AccessibilityEvent.obtain();
1302             event.initFromParcel(parcel);
1303             return event;
1304         }
1305
1306         public AccessibilityEvent[] newArray(int size) {
1307             return new AccessibilityEvent[size];
1308         }
1309     };
1310 }