1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.view.accessibility;
18
19import android.os.Build;
20import android.view.accessibility.AccessibilityEvent;
21import android.view.accessibility.AccessibilityRecord;
22
23/**
24 * Helper for accessing features in {@link AccessibilityEvent}.
25 */
26public final class AccessibilityEventCompat {
27    /**
28     * Represents the event of a hover enter over a {@link android.view.View}.
29     * @deprecated Use {@link  AccessibilityEvent#TYPE_VIEW_HOVER_ENTER} directly.
30     */
31    @Deprecated
32    public static final int TYPE_VIEW_HOVER_ENTER = AccessibilityEvent.TYPE_VIEW_HOVER_ENTER;
33
34    /**
35     * Represents the event of a hover exit over a {@link android.view.View}.
36     * @deprecated Use {@link  AccessibilityEvent#TYPE_VIEW_HOVER_EXIT} directly.
37     */
38    @Deprecated
39    public static final int TYPE_VIEW_HOVER_EXIT = AccessibilityEvent.TYPE_VIEW_HOVER_EXIT;
40
41    /**
42     * Represents the event of starting a touch exploration gesture.
43     * @deprecated Use {@link  AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START} directly.
44     */
45    @Deprecated
46    public static final int TYPE_TOUCH_EXPLORATION_GESTURE_START =
47            AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START;
48
49    /**
50     * Represents the event of ending a touch exploration gesture.
51     * @deprecated Use {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END} directly.
52     */
53    @Deprecated
54    public static final int TYPE_TOUCH_EXPLORATION_GESTURE_END =
55            AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END;
56
57    /**
58     * Represents the event of changing the content of a window.
59     * @deprecated Use {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} directly.
60     */
61    @Deprecated
62    public static final int TYPE_WINDOW_CONTENT_CHANGED =
63            AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
64
65    /**
66     * Represents the event of scrolling a view.
67     * @deprecated Use {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} directly.
68     */
69    @Deprecated
70    public static final int TYPE_VIEW_SCROLLED = AccessibilityEvent.TYPE_VIEW_SCROLLED;
71
72    /**
73     * Represents the event of changing the selection in an {@link android.widget.EditText}.
74     * @deprecated Use {@link AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED} directly.
75     */
76    @Deprecated
77    public static final int TYPE_VIEW_TEXT_SELECTION_CHANGED =
78            AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
79
80    /**
81     * Represents the event of an application making an announcement.
82     */
83    public static final int TYPE_ANNOUNCEMENT = 0x00004000;
84
85    /**
86     * Represents the event of gaining accessibility focus.
87     */
88    public static final int TYPE_VIEW_ACCESSIBILITY_FOCUSED = 0x00008000;
89
90    /**
91     * Represents the event of clearing accessibility focus.
92     */
93    public static final int TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED = 0x00010000;
94
95    /**
96     * Represents the event of traversing the text of a view at a given movement granularity.
97     */
98    public static final int TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY = 0x00020000;
99
100    /**
101     * Represents the event of beginning gesture detection.
102     */
103    public static final int TYPE_GESTURE_DETECTION_START = 0x00040000;
104
105    /**
106     * Represents the event of ending gesture detection.
107     */
108    public static final int TYPE_GESTURE_DETECTION_END = 0x00080000;
109
110    /**
111     * Represents the event of the user starting to touch the screen.
112     */
113    public static final int TYPE_TOUCH_INTERACTION_START = 0x00100000;
114
115    /**
116     * Represents the event of the user ending to touch the screen.
117     */
118    public static final int TYPE_TOUCH_INTERACTION_END = 0x00200000;
119
120    /**
121     * Represents the event change in the windows shown on the screen.
122     */
123    public static final int TYPE_WINDOWS_CHANGED = 0x00400000;
124
125    /**
126     * Represents the event of a context click on a {@link android.view.View}.
127     */
128    public static final int TYPE_VIEW_CONTEXT_CLICKED = 0x00800000;
129
130    /**
131     * Represents the event of the assistant currently reading the users screen context.
132     */
133    public static final int TYPE_ASSIST_READING_CONTEXT = 0x01000000;
134
135    /**
136     * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
137     * The type of change is not defined.
138     */
139    public static final int CONTENT_CHANGE_TYPE_UNDEFINED = 0x00000000;
140
141    /**
142     * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
143     * A node in the subtree rooted at the source node was added or removed.
144     */
145    public static final int CONTENT_CHANGE_TYPE_SUBTREE = 0x00000001;
146
147    /**
148     * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
149     * The node's text changed.
150     */
151    public static final int CONTENT_CHANGE_TYPE_TEXT = 0x00000002;
152
153    /**
154     * Change type for {@link #TYPE_WINDOW_CONTENT_CHANGED} event:
155     * The node's content description changed.
156     */
157    public static final int CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION = 0x00000004;
158
159    /**
160     * Mask for {@link AccessibilityEvent} all types.
161     *
162     * @see AccessibilityEvent#TYPE_VIEW_CLICKED
163     * @see AccessibilityEvent#TYPE_VIEW_LONG_CLICKED
164     * @see AccessibilityEvent#TYPE_VIEW_SELECTED
165     * @see AccessibilityEvent#TYPE_VIEW_FOCUSED
166     * @see AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED
167     * @see AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED
168     * @see AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED
169     * @see AccessibilityEvent#TYPE_VIEW_HOVER_ENTER
170     * @see AccessibilityEvent#TYPE_VIEW_HOVER_EXIT
171     * @see AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START
172     * @see AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END
173     * @see AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED
174     * @see AccessibilityEvent#TYPE_VIEW_SCROLLED
175     * @see AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED
176     * @see #TYPE_ANNOUNCEMENT
177     * @see #TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
178     * @see #TYPE_GESTURE_DETECTION_START
179     * @see #TYPE_GESTURE_DETECTION_END
180     * @see #TYPE_TOUCH_INTERACTION_START
181     * @see #TYPE_TOUCH_INTERACTION_END
182     * @see #TYPE_WINDOWS_CHANGED
183     * @see #TYPE_VIEW_CONTEXT_CLICKED
184     * @see #TYPE_ASSIST_READING_CONTEXT
185     */
186    public static final int TYPES_ALL_MASK = 0xFFFFFFFF;
187
188    /*
189     * Hide constructor from clients.
190     */
191    private AccessibilityEventCompat() {
192
193    }
194
195    /**
196     * Gets the number of records contained in the event.
197     *
198     * @return The number of records.
199     *
200     * @deprecated Use {@link AccessibilityEvent#getRecordCount()} directly.
201     */
202    @Deprecated
203    public static int getRecordCount(AccessibilityEvent event) {
204        return event.getRecordCount();
205    }
206
207    /**
208     * Appends an {@link android.view.accessibility.AccessibilityRecord} to the end of
209     * event records.
210     *
211     * @param record The record to append.
212     *
213     * @throws IllegalStateException If called from an AccessibilityService.
214     *
215     * @deprecated Use {@link AccessibilityEvent#appendRecord(AccessibilityRecord)} directly.
216     */
217    @Deprecated
218    public static void appendRecord(AccessibilityEvent event, AccessibilityRecordCompat record) {
219        event.appendRecord((AccessibilityRecord) record.getImpl());
220    }
221
222    /**
223     * Gets the record at a given index.
224     *
225     * @param index The index.
226     * @return The record at the specified index.
227     *
228     * @deprecated Use {@link AccessibilityEvent#getRecord(int)} directly.
229     */
230    @Deprecated
231    public static AccessibilityRecordCompat getRecord(AccessibilityEvent event, int index) {
232        return new AccessibilityRecordCompat(event.getRecord(index));
233    }
234
235    /**
236     * Creates an {@link AccessibilityRecordCompat} from an {@link AccessibilityEvent}
237     * that can be used to manipulate the event properties defined in
238     * {@link android.view.accessibility.AccessibilityRecord}.
239     * <p>
240     * <strong>Note:</strong> Do not call {@link AccessibilityRecordCompat#recycle()} on the
241     * returned {@link AccessibilityRecordCompat}. Call {@link AccessibilityEvent#recycle()}
242     * in case you want to recycle the event.
243     * </p>
244     *
245     * @param event The from which to create a record.
246     * @return An {@link AccessibilityRecordCompat}.
247     *
248     * @deprecated Use the {@link AccessibilityEvent} directly as {@link AccessibilityRecord}.
249     */
250    @Deprecated
251    public static AccessibilityRecordCompat asRecord(AccessibilityEvent event) {
252        return new AccessibilityRecordCompat(event);
253    }
254
255    /**
256     * Sets the bit mask of node tree changes signaled by an
257     * {@link #TYPE_WINDOW_CONTENT_CHANGED} event.
258     *
259     * @param changeTypes The bit mask of change types.
260     * @throws IllegalStateException If called from an AccessibilityService.
261     * @see #getContentChangeTypes(AccessibilityEvent)
262     */
263    public static void setContentChangeTypes(AccessibilityEvent event, int changeTypes) {
264        if (Build.VERSION.SDK_INT >= 19) {
265            event.setContentChangeTypes(changeTypes);
266        }
267    }
268
269    /**
270     * Gets the bit mask of change types signaled by an
271     * {@link #TYPE_WINDOW_CONTENT_CHANGED} event. A single event may represent
272     * multiple change types.
273     *
274     * @return The bit mask of change types. One or more of:
275     *         <ul>
276     *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION}
277     *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_SUBTREE}
278     *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_TEXT}
279     *         <li>{@link AccessibilityEvent#CONTENT_CHANGE_TYPE_UNDEFINED}
280     *         </ul>
281     */
282    public static int getContentChangeTypes(AccessibilityEvent event) {
283        if (Build.VERSION.SDK_INT >= 19) {
284            return event.getContentChangeTypes();
285        } else {
286            return 0;
287        }
288    }
289
290    /**
291     * Sets the movement granularity that was traversed.
292     *
293     * @param granularity The granularity.
294     *
295     * @throws IllegalStateException If called from an AccessibilityService.
296     */
297    public static void setMovementGranularity(AccessibilityEvent event, int granularity) {
298        if (Build.VERSION.SDK_INT >= 16) {
299            event.setMovementGranularity(granularity);
300        }
301    }
302
303    /**
304     * Gets the movement granularity that was traversed.
305     *
306     * @return The granularity.
307     */
308    public static int getMovementGranularity(AccessibilityEvent event) {
309        if (Build.VERSION.SDK_INT >= 16) {
310            return event.getMovementGranularity();
311        } else {
312            return 0;
313        }
314    }
315
316    /**
317     * Sets the performed action that triggered this event.
318     * <p>
319     * Valid actions are defined in {@link AccessibilityNodeInfoCompat}:
320     * <ul>
321     * <li>{@link AccessibilityNodeInfoCompat#ACTION_ACCESSIBILITY_FOCUS}
322     * <li>{@link AccessibilityNodeInfoCompat#ACTION_CLEAR_ACCESSIBILITY_FOCUS}
323     * <li>{@link AccessibilityNodeInfoCompat#ACTION_CLEAR_FOCUS}
324     * <li>{@link AccessibilityNodeInfoCompat#ACTION_CLEAR_SELECTION}
325     * <li>{@link AccessibilityNodeInfoCompat#ACTION_CLICK}
326     * <li>etc.
327     * </ul>
328     *
329     * @param action The action.
330     * @throws IllegalStateException If called from an AccessibilityService.
331     * @see AccessibilityNodeInfoCompat#performAction(int)
332     */
333    public static void setAction(AccessibilityEvent event, int action) {
334        if (Build.VERSION.SDK_INT >= 16) {
335            event.setAction(action);
336        }
337    }
338
339    /**
340     * Gets the performed action that triggered this event.
341     *
342     * @return The action.
343     */
344    public static int getAction(AccessibilityEvent event) {
345        if (Build.VERSION.SDK_INT >= 16) {
346            return event.getAction();
347        } else {
348            return 0;
349        }
350    }
351}
352