AccessibilityNodeProviderCompat.java revision b1d4cb28db8b08974b651665bf4d1a7926c17b9c
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 final AccessibilityNodeInfoCompat compatInfo = compat 73 .createAccessibilityNodeInfo(virtualViewId); 74 if (compatInfo == null) { 75 return null; 76 } else { 77 return compatInfo.getInfo(); 78 } 79 } 80 81 @Override 82 public Object findAccessibilityFocus(int virtualViewId) { 83 final AccessibilityNodeInfoCompat compatInfo = compat 84 .findAccessibilityFocus(virtualViewId); 85 if (compatInfo == null) { 86 return null; 87 } else { 88 return compatInfo.getInfo(); 89 } 90 } 91 92 @Override 93 public Object accessibilityFocusSearch(int direction, 94 int virtualViewId) { 95 final AccessibilityNodeInfoCompat compatInfo = compat 96 .accessibilityFocusSearch(direction, virtualViewId); 97 if (compatInfo == null) { 98 return null; 99 } else { 100 return compatInfo.getInfo(); 101 } 102 } 103 }); 104 } 105 } 106 107 private static final AccessibilityNodeProviderImpl IMPL; 108 109 private final Object mProvider; 110 111 static { 112 if (Build.VERSION.SDK_INT >= 16) { // JellyBean 113 IMPL = new AccessibilityNodeProviderJellyBeanImpl(); 114 } else { 115 IMPL = new AccessibilityNodeProviderStubImpl(); 116 } 117 } 118 119 /** 120 * Creates a new instance. 121 */ 122 public AccessibilityNodeProviderCompat() { 123 mProvider = IMPL.newAccessibilityNodeProviderBridge(this); 124 } 125 126 /** 127 * Creates a new instance wrapping an 128 * {@link android.view.accessibility.AccessibilityNodeProvider}. 129 * 130 * @param provider The provider. 131 */ 132 public AccessibilityNodeProviderCompat(Object provider) { 133 mProvider = provider; 134 } 135 136 /** 137 * @return The wrapped {@link android.view.accessibility.AccessibilityNodeProvider}. 138 */ 139 public Object getProvider() { 140 return mProvider; 141 } 142 143 /** 144 * Returns an {@link AccessibilityNodeInfoCompat} representing a virtual view, 145 * i.e. a descendant of the host View, with the given <code>virtualViewId</code> 146 * or the host View itself if <code>virtualViewId</code> equals to {@link View#NO_ID}. 147 * <p> 148 * A virtual descendant is an imaginary View that is reported as a part of the view 149 * hierarchy for accessibility purposes. This enables custom views that draw complex 150 * content to report them selves as a tree of virtual views, thus conveying their 151 * logical structure. 152 * </p> 153 * <p> 154 * The implementer is responsible for obtaining an accessibility node info from the 155 * pool of reusable instances and setting the desired properties of the node info 156 * before returning it. 157 * </p> 158 * 159 * @param virtualViewId A client defined virtual view id. 160 * @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant 161 * or the host View. 162 * 163 * @see AccessibilityNodeInfoCompat 164 */ 165 public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) { 166 return null; 167 } 168 169 /** 170 * Performs an accessibility action on a virtual view, i.e. a descendant of the 171 * host View, with the given <code>virtualViewId</code> or the host View itself 172 * if <code>virtualViewId</code> equals to {@link View#NO_ID}. 173 * 174 * @param virtualViewId A client defined virtual view id. 175 * @param action The action to perform. 176 * @param arguments Optional arguments. 177 * @return True if the action was performed. 178 * 179 * @see #createAccessibilityNodeInfo(int) 180 * @see AccessibilityNodeInfoCompat 181 */ 182 public boolean performAction(int virtualViewId, int action, Bundle arguments) { 183 return false; 184 } 185 186 /** 187 * Finds {@link AccessibilityNodeInfoCompat}s by text. The match is case insensitive 188 * containment. The search is relative to the virtual view, i.e. a descendant of the 189 * host View, with the given <code>virtualViewId</code> or the host View itself 190 * <code>virtualViewId</code> equals to {@link View#NO_ID}. 191 * 192 * @param virtualViewId A client defined virtual view id which defined 193 * the root of the tree in which to perform the search. 194 * @param text The searched text. 195 * @return A list of node info. 196 * 197 * @see #createAccessibilityNodeInfo(int) 198 * @see AccessibilityNodeInfoCompat 199 */ 200 public List<AccessibilityNodeInfoCompat> findAccessibilityNodeInfosByText(String text, 201 int virtualViewId) { 202 return null; 203 } 204 205 /** 206 * Finds the accessibility focused {@link AccessibilityNodeInfoCompat}. The search 207 * is relative to the virtual view, i.e. a descendant of the host View, with the 208 * given <code>virtualViewId</code> or the host View itself 209 * <code>virtualViewId</code> equals to {@link View#NO_ID}. 210 * 211 * <strong>Note:</strong> Normally the system is responsible to transparently find 212 * accessibility focused view starting from a given root but for virtual view 213 * hierarchies it is a responsibility of this provider's implementor to find 214 * the accessibility focused virtual view. 215 * 216 * @param virtualViewId A client defined virtual view id which defined 217 * the root of the tree in which to perform the search. 218 * @return A list of node info. 219 * 220 * @see #createAccessibilityNodeInfo(int) 221 * @see AccessibilityNodeInfoCompat 222 */ 223 public AccessibilityNodeInfoCompat findAccessibilityFocus(int virtualViewId) { 224 return null; 225 } 226 227 /** 228 * Finds {@link AccessibilityNodeInfoCompat} to take accessibility focus in the 229 * given <code>direction</code>. The search is relative to the virtual view, i.e. 230 * a descendant of the host View, with the given <code>virtualViewId</code> or 231 * the host View itself <code>virtualViewId</code> equals to {@link View#NO_ID}. 232 * 233 * <strong>Note:</strong> Normally the system is responsible to transparently find 234 * the next view to take accessibility focus but for virtual view hierarchies 235 * it is a responsibility of this provider's implementor to compute the next 236 * focusable. 237 * 238 * @param direction The direction in which to search for a focus candidate. 239 * Values are 240 * {@link View#ACCESSIBILITY_FOCUS_FORWARD}, 241 * {@link View#ACCESSIBILITY_FOCUS_BACKWARD}, 242 * {@link View#ACCESSIBILITY_FOCUS_UP}, 243 * {@link View#ACCESSIBILITY_FOCUS_DOWN}, 244 * {@link View#ACCESSIBILITY_FOCUS_LEFT}, 245 * {@link View#ACCESSIBILITY_FOCUS_RIGHT}. 246 * @param virtualViewId A client defined virtual view id which defined 247 * the root of the tree in which to perform the search. 248 * @return A list of node info. 249 * 250 * @see #createAccessibilityNodeInfo(int) 251 * @see AccessibilityNodeInfoCompat 252 */ 253 public AccessibilityNodeInfoCompat accessibilityFocusSearch(int direction, int virtualViewId) { 254 return null; 255 } 256} 257