AccessibilityNodeProviderCompat.java revision d16944cf0ad052b56562a56886fed92afbb9b09f
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 // TODO: Update the conditional to use SDK_INT when we have an SDK version set. 84 // (tracked by bug:5947249) 85 if (Build.VERSION.CODENAME.equals("JellyBean")) { // JellyBean 86 IMPL = new AccessibilityNodeProviderJellyBeanImpl(); 87 } else { 88 IMPL = new AccessibilityNodeProviderStubImpl(); 89 } 90 } 91 92 /** 93 * Creates a new instance. 94 */ 95 public AccessibilityNodeProviderCompat() { 96 mProvider = IMPL.newAccessibilityNodeProviderBridge(this); 97 } 98 99 /** 100 * Creates a new instance wrapping an 101 * {@link android.view.accessibility.AccessibilityNodeProvider}. 102 * 103 * @param provider The provider. 104 */ 105 public AccessibilityNodeProviderCompat(Object provider) { 106 mProvider = provider; 107 } 108 109 /** 110 * @return The wrapped {@link android.view.accessibility.AccessibilityNodeProvider}. 111 */ 112 public Object getProvider() { 113 return mProvider; 114 } 115 116 /** 117 * Returns an {@link AccessibilityNodeInfoCompat} representing a virtual view, 118 * i.e. a descendant of the host View, with the given <code>virtualViewId</code> 119 * or the host View itself if <code>virtualViewId</code> equals to {@link View#NO_ID}. 120 * <p> 121 * A virtual descendant is an imaginary View that is reported as a part of the view 122 * hierarchy for accessibility purposes. This enables custom views that draw complex 123 * content to report them selves as a tree of virtual views, thus conveying their 124 * logical structure. 125 * </p> 126 * <p> 127 * The implementer is responsible for obtaining an accessibility node info from the 128 * pool of reusable instances and setting the desired properties of the node info 129 * before returning it. 130 * </p> 131 * 132 * @param virtualViewId A client defined virtual view id. 133 * @return A populated {@link AccessibilityNodeInfoCompat} for a virtual descendant 134 * or the host View. 135 * 136 * @see AccessibilityNodeInfoCompat 137 */ 138 public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) { 139 return null; 140 } 141 142 /** 143 * Performs an accessibility action on a virtual view, i.e. a descendant of the 144 * host View, with the given <code>virtualViewId</code> or the host View itself 145 * if <code>virtualViewId</code> equals to {@link View#NO_ID}. 146 * 147 * @param virtualViewId A client defined virtual view id. 148 * @param action The action to perform. 149 * @param arguments Optional arguments. 150 * @return True if the action was performed. 151 * 152 * @see #createAccessibilityNodeInfo(int) 153 * @see AccessibilityNodeInfoCompat 154 */ 155 public boolean performAction(int virtualViewId, int action, Bundle arguments) { 156 return false; 157 } 158 159 /** 160 * Finds {@link AccessibilityNodeInfoCompat}s by text. The match is case insensitive 161 * containment. The search is relative to the virtual view, i.e. a descendant of the 162 * host View, with the given <code>virtualViewId</code> or the host View itself 163 * <code>virtualViewId</code> equals to {@link View#NO_ID}. 164 * 165 * @param virtualViewId A client defined virtual view id which defined 166 * the root of the tree in which to perform the search. 167 * @param text The searched text. 168 * @return A list of node info. 169 * 170 * @see #createAccessibilityNodeInfo(int) 171 * @see AccessibilityNodeInfoCompat 172 */ 173 public List<AccessibilityNodeInfoCompat> findAccessibilityNodeInfosByText(String text, 174 int virtualViewId) { 175 return null; 176 } 177} 178