AccessibilityEventCompat.java revision 9648c538bac4f04145c118cc41168d1d7a536312
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;
21
22/**
23 * Helper for accessing newer features in AccessibilityEvent.
24 */
25public class AccessibilityEventCompat {
26
27    static interface AccessibilityEventVersionImpl {
28        public int getRecordCount(AccessibilityEvent event);
29        public void appendRecord(AccessibilityEvent event, Object record);
30        public Object getRecord(AccessibilityEvent event, int index);
31    }
32
33    static class AccessibilityEventStubImpl implements AccessibilityEventVersionImpl {
34
35        public void appendRecord(AccessibilityEvent event, Object record) {
36
37        }
38
39        public Object getRecord(AccessibilityEvent event, int index) {
40            return null;
41        }
42
43        public int getRecordCount(AccessibilityEvent event) {
44            return 0;
45        }
46    }
47
48    static class AccessibilityEventIcsImpl extends AccessibilityEventStubImpl {
49
50        @Override
51        public void appendRecord(AccessibilityEvent event, Object record) {
52            AccessibilityEventCompatIcs.appendRecord(event, record);
53        }
54
55        @Override
56        public Object getRecord(AccessibilityEvent event, int index) {
57            return AccessibilityEventCompatIcs.getRecord(event, index);
58        }
59
60        @Override
61        public int getRecordCount(AccessibilityEvent event) {
62            return AccessibilityEventCompatIcs.getRecordCount(event);
63        }
64    }
65
66    private final static AccessibilityEventVersionImpl IMPL;
67
68    static {
69        if (Build.VERSION.SDK_INT >= 14) { // ICS
70            IMPL = new AccessibilityEventIcsImpl();
71        } else {
72            IMPL = new AccessibilityEventStubImpl();
73        }
74    }
75
76    /**
77     * Represents the event of a hover enter over a {@link android.view.View}.
78     */
79    public static final int TYPE_VIEW_HOVER_ENTER = 0x00000080;
80
81    /**
82     * Represents the event of a hover exit over a {@link android.view.View}.
83     */
84    public static final int TYPE_VIEW_HOVER_EXIT = 0x00000100;
85
86    /**
87     * Represents the event of starting a touch exploration gesture.
88     */
89    public static final int TYPE_TOUCH_EXPLORATION_GESTURE_START = 0x00000200;
90
91    /**
92     * Represents the event of ending a touch exploration gesture.
93     */
94    public static final int TYPE_TOUCH_EXPLORATION_GESTURE_END = 0x00000400;
95
96    /**
97     * Represents the event of changing the content of a window.
98     */
99    public static final int TYPE_WINDOW_CONTENT_CHANGED = 0x00000800;
100
101    /**
102     * Represents the event of scrolling a view.
103     */
104    public static final int TYPE_VIEW_SCROLLED = 0x00001000;
105
106    /**
107     * Represents the event of changing the selection in an {@link android.widget.EditText}.
108     */
109    public static final int TYPE_VIEW_TEXT_SELECTION_CHANGED = 0x00002000;
110
111    /*
112     * Hide constructor from clients.
113     */
114    private AccessibilityEventCompat() {
115
116    }
117
118    /**
119     * Gets the number of records contained in the event.
120     *
121     * @return The number of records.
122     */
123    public static int getRecordCount(AccessibilityEvent event) {
124        return IMPL.getRecordCount(event);
125    }
126
127    /**
128     * Appends an {@link android.view.accessibility.AccessibilityRecord} to the end of
129     * event records.
130     *
131     * @param record The record to append.
132     *
133     * @throws IllegalStateException If called from an AccessibilityService.
134     */
135    public static void appendRecord(AccessibilityEvent event, AccessibilityRecordCompat record) {
136        IMPL.appendRecord(event, record.getImpl());
137    }
138
139    /**
140     * Gets the record at a given index.
141     *
142     * @param index The index.
143     * @return The record at the specified index.
144     */
145    public static AccessibilityRecordCompat getRecord(AccessibilityEvent event, int index) {
146        return new AccessibilityRecordCompat(IMPL.getRecord(event, index));
147    }
148}
149