AccessibilityNodeProviderCompat.java revision f3ed7c56e6c409d27c60f7d74c026906593c21d4
1/*
2 * Copyright (C) 2012 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.os.Bundle;
21import android.view.View;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Helper for accessing {@link android.view.accessibility.AccessibilityNodeProvider}
28 * introduced after API level 4 in a backwards compatible fashion.
29 */
30public class AccessibilityNodeProviderCompat {
31
32    interface AccessibilityNodeProviderImpl {
33        public Object newAccessibilityNodeProviderBridge(AccessibilityNodeProviderCompat compat);
34    }
35
36    static class AccessibilityNodeProviderStubImpl implements AccessibilityNodeProviderImpl {
37        @Override
38        public Object newAccessibilityNodeProviderBridge(AccessibilityNodeProviderCompat compat) {
39            return null;
40        }
41    }
42
43    static class AccessibilityNodeProviderJellyBeanImpl extends AccessibilityNodeProviderStubImpl {
44        @Override
45        public Object newAccessibilityNodeProviderBridge(
46                final AccessibilityNodeProviderCompat compat) {
47            return AccessibilityNodeProviderCompatJellyBean.newAccessibilityNodeProviderBridge(
48                    new AccessibilityNodeProviderCompatJellyBean.AccessibilityNodeInfoBridge() {
49                        @Override
50                        public boolean performAction(int virtualViewId, int action,
51                                Bundle arguments) {
52                            return compat.performAction(virtualViewId, action, arguments);
53                        }
54
55                        @Override
56                        public List<Object> findAccessibilityNodeInfosByText(
57                                            String text, int virtualViewId) {
58                            List<AccessibilityNodeInfoCompat> compatInfos =
59                                compat.findAccessibilityNodeInfosByText(text, virtualViewId);
60                            List<Object> infos = new ArrayList<Object>();
61                            final int infoCount = compatInfos.size();
62                            for (int i = 0; i < infoCount; i++) {
63                                AccessibilityNodeInfoCompat infoCompat = compatInfos.get(i);
64                                infos.add(infoCompat.getInfo());
65                            }
66                            return infos;
67                        }
68
69                        @Override
70                        public Object createAccessibilityNodeInfo(
71                                int virtualViewId) {
72                            return compat.createAccessibilityNodeInfo(virtualViewId).getInfo();
73                        }
74                    });
75        }
76    }
77
78    private static final AccessibilityNodeProviderImpl IMPL;
79
80    private final Object mProvider;
81
82    static {
83        if (Build.VERSION.SDK_INT >= 16) { // JellyBean
84            IMPL = new AccessibilityNodeProviderJellyBeanImpl();
85        } else {
86            IMPL = new AccessibilityNodeProviderStubImpl();
87        }
88    }
89
90    /**
91     * Creates a new instance.
92     */
93    public AccessibilityNodeProviderCompat() {
94        mProvider = IMPL.newAccessibilityNodeProviderBridge(this);
95    }
96
97    /**
98     * Creates a new instance wrapping an
99     * {@link android.view.accessibility.AccessibilityNodeProvider}.
100     *
101     * @param provider The provider.
102     */
103    public AccessibilityNodeProviderCompat(Object provider) {
104        mProvider = provider;
105    }
106
107    /**
108     * @return The wrapped {@link android.view.accessibility.AccessibilityNodeProvider}.
109     */
110    public Object getProvider() {
111        return mProvider;
112    }
113
114    /**
115     * Returns an {@link AccessibilityNodeInfoCompat} representing a virtual view,
116     * i.e. a descendant of the host View, with the given <code>virtualViewId</code>
117     * or the host View itself if <code>virtualViewId</code> equals to {@link View#NO_ID}.
118     * <p>
119     * A virtual descendant is an imaginary View that is reported as a part of the view
120     * hierarchy for accessibility purposes. This enables custom views that draw complex
121     * content to report them selves as a tree of virtual views, thus conveying their
122     * logical structure.
123     * </p>
124     * <p>
125     * The implementer is responsible for obtaining an accessibility node info from the
126     * pool of reusable instances and setting the desired properties of the node info
127     * before returning it.
128     * </p>
129     *
130     * @param virtualViewId A client defined virtual view id.
131     * @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant
132     *     or the host View.
133     *
134     * @see AccessibilityNodeInfoCompat
135     */
136    public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) {
137        return null;
138    }
139
140    /**
141     * Performs an accessibility action on a virtual view, i.e. a descendant of the
142     * host View, with the given <code>virtualViewId</code> or the host View itself
143     * if <code>virtualViewId</code> equals to {@link View#NO_ID}.
144     *
145     * @param virtualViewId A client defined virtual view id.
146     * @param action The action to perform.
147     * @param arguments Optional arguments.
148     * @return True if the action was performed.
149     *
150     * @see #createAccessibilityNodeInfo(int)
151     * @see AccessibilityNodeInfoCompat
152     */
153    public boolean performAction(int virtualViewId, int action, Bundle arguments) {
154        return false;
155    }
156
157    /**
158     * Finds {@link AccessibilityNodeInfoCompat}s by text. The match is case insensitive
159     * containment. The search is relative to the virtual view, i.e. a descendant of the
160     * host View, with the given <code>virtualViewId</code> or the host View itself
161     * <code>virtualViewId</code> equals to {@link View#NO_ID}.
162     *
163     * @param virtualViewId A client defined virtual view id which defined
164     *     the root of the tree in which to perform the search.
165     * @param text The searched text.
166     * @return A list of node info.
167     *
168     * @see #createAccessibilityNodeInfo(int)
169     * @see AccessibilityNodeInfoCompat
170     */
171    public List<AccessibilityNodeInfoCompat> findAccessibilityNodeInfosByText(String text,
172            int virtualViewId) {
173        return null;
174    }
175}
176