AccessibilityService.java revision 4e2a762eae1f6981d32e6098a95498865ad7f795
1/* 2 * Copyright (C) 2009 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.accessibilityservice; 18 19import com.android.internal.os.HandlerCaller; 20 21import android.app.Service; 22import android.content.Intent; 23import android.os.IBinder; 24import android.os.Message; 25import android.os.RemoteException; 26import android.util.Log; 27import android.view.accessibility.AccessibilityEvent; 28import android.view.accessibility.AccessibilityNodeInfo; 29 30/** 31 * An accessibility service runs in the background and receives callbacks by the system 32 * when {@link AccessibilityEvent}s are fired. Such events denote some state transition 33 * in the user interface, for example, the focus has changed, a button has been clicked, 34 * etc. Such a service can optionally request the capability for querying the content 35 * of the active window. Development of an accessibility service requires extends this 36 * class and implements its abstract methods. 37 * <p> 38 * <strong>Lifecycle</strong> 39 * </p> 40 * <p> 41 * The lifecycle of an accessibility service is managed exclusively by the system and 42 * follows the established service life cycle. Additionally, starting or stopping an 43 * accessibility service is triggered exclusively by an explicit user action through 44 * enabling or disabling it in the device settings. After the system binds to a service it 45 * calls {@link AccessibilityService#onServiceConnected()}. This method can be 46 * overriden by clients that want to perform post binding setup. 47 * </p> 48 * <p> 49 * <strong>Declaration</strong> 50 * </p> 51 * <p> 52 * An accessibility is declared as any other service in an AndroidManifest.xml but it 53 * must also specify that it handles the "android.accessibilityservice.AccessibilityService" 54 * {@link android.content.Intent}. Failure to declare this intent will cause the system to 55 * ignore the accessibility service. Following is an example declaration: 56 * </p> 57 * <p> 58 * <code> 59 * <pre> 60 * <service android:name=".MyAccessibilityService"> 61 * <intent-filter> 62 * <action android:name="android.accessibilityservice.AccessibilityService" /> 63 * </intent-filter> 64 * . . . 65 * </service> 66 * </pre> 67 * </code> 68 * </p> 69 * <p> 70 * <strong>Configuration</strong> 71 * </p> 72 * <p> 73 * An accessibility service can be configured to receive specific types of accessibility events, 74 * listen only to specific packages, get events from each type only once in a given time frame, 75 * retrieve window content, specify a settings activity, etc. 76 * </p> 77 * <p> 78 * There are two approaches for configuring an accessibility service: 79 * </p> 80 * <ul> 81 * <li> 82 * Providing a {@link #SERVICE_META_DATA meta-data} entry in the manifest when declaring 83 * the service. A service declaration with a meta-data tag is presented below: 84 * <p> 85 * <code> 86 * <pre> 87 * <service android:name=".MyAccessibilityService"> 88 * <intent-filter> 89 * <action android:name="android.accessibilityservice.AccessibilityService" /> 90 * </intent-filter> 91 * <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" /> 92 * </service> 93 * </pre> 94 * </code> 95 * </p> 96 * <p> 97 * <strong>Note:</strong>This approach enables setting all properties. 98 * </p> 99 * <p> 100 * For more details refer to {@link #SERVICE_META_DATA} and 101 * <code><{@link android.R.styleable#AccessibilityService accessibility-service}></code>.. 102 * </p> 103 * </li> 104 * <li> 105 * Calling {@link AccessibilityService#setServiceInfo(AccessibilityServiceInfo)}. Note 106 * that this method can be called any time to dynamically change the service configuration. 107 * <p> 108 * <strong>Note:</strong> This approach enables setting only dynamically configurable properties: 109 * {@link AccessibilityServiceInfo#eventTypes}, 110 * {@link AccessibilityServiceInfo#feedbackType}, 111 * {@link AccessibilityServiceInfo#flags}, 112 * {@link AccessibilityServiceInfo#notificationTimeout}, 113 * {@link AccessibilityServiceInfo#packageNames} 114 * </p> 115 * <p> 116 * For more details refer to {@link AccessibilityServiceInfo}. 117 * </p> 118 * </li> 119 * </ul> 120 * <p> 121 * <strong>Retrieving window content</strong> 122 * </p> 123 * <p> 124 * An service can specify in its declaration that it can retrieve the active window 125 * content which is represented as a tree of {@link AccessibilityNodeInfo}. Note that 126 * declaring this capability requires that the service declares its configuration via 127 * an XML resource referenced by {@link #SERVICE_META_DATA}. 128 * </p> 129 * <p> 130 * For security purposes an accessibility service can retrieve only the content of the 131 * currently active window. The currently active window is defined as the window from 132 * which was fired the last event of the following types: 133 * {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}, 134 * {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}, 135 * {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}, 136 * In other words, the last window that was shown or the last window that the user has touched 137 * during touch exploration. 138 * </p> 139 * <p> 140 * The entry point for retrieving window content is through calling 141 * {@link AccessibilityEvent#getSource() AccessibilityEvent.getSource()} of the last received 142 * event of the above types or a previous event from the same window 143 * (see {@link AccessibilityEvent#getWindowId() AccessibilityEvent.getWindowId()}). Invoking 144 * this method will return an {@link AccessibilityNodeInfo} that can be used to traverse the 145 * window content which represented as a tree of such objects. 146 * </p> 147 * <p> 148 * <strong>Note</strong>An accessibility service may have requested to be notified for 149 * a subset of the event types, thus be unaware that the active window has changed. Therefore 150 * accessibility service that would like to retrieve window content should: 151 * <ul> 152 * <li> 153 * Register for all event types with no notification timeout and keep track for the active 154 * window by calling {@link AccessibilityEvent#getWindowId()} of the last received event and 155 * compare this with the {@link AccessibilityNodeInfo#getWindowId()} before calling retrieval 156 * methods on the latter. 157 * </li> 158 * <li> 159 * Prepare that a retrieval method on {@link AccessibilityNodeInfo} may fail since the 160 * active window has changed and the service did not get the accessibility event yet. Note 161 * that it is possible to have a retrieval method failing event adopting the strategy 162 * specified in the previous bullet because the accessibility event dispatch is asynchronous 163 * and crosses process boundaries. 164 * </li> 165 * </ul> 166 * </p> 167 * <p> 168 * <b>Notification strategy</b> 169 * </p> 170 * <p> 171 * For each feedback type only one accessibility service is notified. Services are notified 172 * in the order of registration. Hence, if two services are registered for the same 173 * feedback type in the same package the first one wins. It is possible however, to 174 * register a service as the default one for a given feedback type. In such a case this 175 * service is invoked if no other service was interested in the event. In other words, default 176 * services do not compete with other services and are notified last regardless of the 177 * registration order. This enables "generic" accessibility services that work reasonably 178 * well with most applications to coexist with "polished" ones that are targeted for 179 * specific applications. 180 * </p> 181 * <p> 182 * <b>Event types</b> 183 * </p> 184 * {@link AccessibilityEvent#TYPE_VIEW_CLICKED} 185 * {@link AccessibilityEvent#TYPE_VIEW_LONG_CLICKED} 186 * {@link AccessibilityEvent#TYPE_VIEW_FOCUSED} 187 * {@link AccessibilityEvent#TYPE_VIEW_SELECTED} 188 * {@link AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED} 189 * {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED} 190 * {@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED} 191 * {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START} 192 * {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END} 193 * {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER} 194 * {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT} 195 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} 196 * {@link AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED} 197 * {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED} 198 * <p> 199 * <b>Feedback types</b> 200 * <p> 201 * {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE} 202 * {@link AccessibilityServiceInfo#FEEDBACK_HAPTIC} 203 * {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE} 204 * {@link AccessibilityServiceInfo#FEEDBACK_VISUAL} 205 * {@link AccessibilityServiceInfo#FEEDBACK_GENERIC} 206 * 207 * @see AccessibilityEvent 208 * @see AccessibilityServiceInfo 209 * @see android.view.accessibility.AccessibilityManager 210 * 211 * <strong>Note:</strong> The event notification timeout is useful to avoid propagating 212 * events to the client too frequently since this is accomplished via an expensive 213 * interprocess call. One can think of the timeout as a criteria to determine when 214 * event generation has settled down. 215 */ 216public abstract class AccessibilityService extends Service { 217 /** 218 * The {@link Intent} that must be declared as handled by the service. 219 */ 220 public static final String SERVICE_INTERFACE = 221 "android.accessibilityservice.AccessibilityService"; 222 223 /** 224 * Name under which an AccessibilityService component publishes information 225 * about itself. This meta-data must reference an XML resource containing an 226 * <code><{@link android.R.styleable#AccessibilityService accessibility-service}></code> 227 * tag. This is a a sample XML file configuring an accessibility service: 228 * <p> 229 * <code> 230 * <pre> 231 * <accessibility-service 232 * android:accessibilityEventTypes="typeViewClicked|typeViewFocused" 233 * android:packageNames="foo.bar, foo.baz" 234 * android:accessibilityFeedbackType="feedbackSpoken" 235 * android:notificationTimeout="100" 236 * android:accessibilityFlags="flagDefault" 237 * android:settingsActivity="foo.bar.TestBackActivity" 238 * android:canRetrieveWindowContent="true" 239 * . . . 240 * /> 241 * </pre> 242 * </code> 243 * </p> 244 */ 245 public static final String SERVICE_META_DATA = "android.accessibilityservice"; 246 247 private static final String LOG_TAG = "AccessibilityService"; 248 249 private AccessibilityServiceInfo mInfo; 250 251 IAccessibilityServiceConnection mConnection; 252 253 /** 254 * Callback for {@link android.view.accessibility.AccessibilityEvent}s. 255 * 256 * @param event An event. 257 */ 258 public abstract void onAccessibilityEvent(AccessibilityEvent event); 259 260 /** 261 * Callback for interrupting the accessibility feedback. 262 */ 263 public abstract void onInterrupt(); 264 265 /** 266 * This method is a part of the {@link AccessibilityService} lifecycle and is 267 * called after the system has successfully bound to the service. If is 268 * convenient to use this method for setting the {@link AccessibilityServiceInfo}. 269 * 270 * @see AccessibilityServiceInfo 271 * @see #setServiceInfo(AccessibilityServiceInfo) 272 */ 273 protected void onServiceConnected() { 274 275 } 276 277 /** 278 * Sets the {@link AccessibilityServiceInfo} that describes this service. 279 * <p> 280 * Note: You can call this method any time but the info will be picked up after 281 * the system has bound to this service and when this method is called thereafter. 282 * 283 * @param info The info. 284 */ 285 public final void setServiceInfo(AccessibilityServiceInfo info) { 286 mInfo = info; 287 sendServiceInfo(); 288 } 289 290 /** 291 * Sets the {@link AccessibilityServiceInfo} for this service if the latter is 292 * properly set and there is an {@link IAccessibilityServiceConnection} to the 293 * AccessibilityManagerService. 294 */ 295 private void sendServiceInfo() { 296 if (mInfo != null && mConnection != null) { 297 try { 298 mConnection.setServiceInfo(mInfo); 299 } catch (RemoteException re) { 300 Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re); 301 } 302 } 303 } 304 305 /** 306 * Implement to return the implementation of the internal accessibility 307 * service interface. 308 */ 309 @Override 310 public final IBinder onBind(Intent intent) { 311 return new IEventListenerWrapper(this); 312 } 313 314 /** 315 * Implements the internal {@link IEventListener} interface to convert 316 * incoming calls to it back to calls on an {@link AccessibilityService}. 317 */ 318 class IEventListenerWrapper extends IEventListener.Stub 319 implements HandlerCaller.Callback { 320 321 private static final int DO_SET_SET_CONNECTION = 10; 322 private static final int DO_ON_INTERRUPT = 20; 323 private static final int DO_ON_ACCESSIBILITY_EVENT = 30; 324 325 private final HandlerCaller mCaller; 326 327 private final AccessibilityService mTarget; 328 329 public IEventListenerWrapper(AccessibilityService context) { 330 mTarget = context; 331 mCaller = new HandlerCaller(context, this); 332 } 333 334 public void setConnection(IAccessibilityServiceConnection connection) { 335 Message message = mCaller.obtainMessageO(DO_SET_SET_CONNECTION, connection); 336 mCaller.sendMessage(message); 337 } 338 339 public void onInterrupt() { 340 Message message = mCaller.obtainMessage(DO_ON_INTERRUPT); 341 mCaller.sendMessage(message); 342 } 343 344 public void onAccessibilityEvent(AccessibilityEvent event) { 345 Message message = mCaller.obtainMessageO(DO_ON_ACCESSIBILITY_EVENT, event); 346 mCaller.sendMessage(message); 347 } 348 349 public void executeMessage(Message message) { 350 switch (message.what) { 351 case DO_ON_ACCESSIBILITY_EVENT : 352 AccessibilityEvent event = (AccessibilityEvent) message.obj; 353 if (event != null) { 354 mTarget.onAccessibilityEvent(event); 355 event.recycle(); 356 } 357 return; 358 case DO_ON_INTERRUPT : 359 mTarget.onInterrupt(); 360 return; 361 case DO_SET_SET_CONNECTION : 362 mConnection = ((IAccessibilityServiceConnection) message.obj); 363 mTarget.onServiceConnected(); 364 return; 365 default : 366 Log.w(LOG_TAG, "Unknown message type " + message.what); 367 } 368 } 369 } 370} 371