FileDocCategorySizeDatePackage
AccessibilityService.javaAPI DocAndroid 5.1 API34052Thu Mar 12 22:22:08 GMT 2015android.accessibilityservice

AccessibilityService

public abstract class AccessibilityService extends android.app.Service
An accessibility service runs in the background and receives callbacks by the system when {@link AccessibilityEvent}s are fired. Such events denote some state transition in the user interface, for example, the focus has changed, a button has been clicked, etc. Such a service can optionally request the capability for querying the content of the active window. Development of an accessibility service requires extending this class and implementing its abstract methods.

Developer Guides

For more information about creating AccessibilityServices, read the Accessibility developer guide.

Lifecycle

The lifecycle of an accessibility service is managed exclusively by the system and follows the established service life cycle. Additionally, starting or stopping an accessibility service is triggered exclusively by an explicit user action through enabling or disabling it in the device settings. After the system binds to a service it calls {@link AccessibilityService#onServiceConnected()}. This method can be overriden by clients that want to perform post binding setup.

Declaration

An accessibility is declared as any other service in an AndroidManifest.xml but it must also specify that it handles the "android.accessibilityservice.AccessibilityService" {@link android.content.Intent}. Failure to declare this intent will cause the system to ignore the accessibility service. Additionally an accessibility service must request the {@link android.Manifest.permission#BIND_ACCESSIBILITY_SERVICE} permission to ensure that only the system can bind to it. Failure to declare this intent will cause the system to ignore the accessibility service. Following is an example declaration:

 <service android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
. . .
</service>

Configuration

An accessibility service can be configured to receive specific types of accessibility events, listen only to specific packages, get events from each type only once in a given time frame, retrieve window content, specify a settings activity, etc.

There are two approaches for configuring an accessibility service:

  • Providing a {@link #SERVICE_META_DATA meta-data} entry in the manifest when declaring the service. A service declaration with a meta-data tag is presented below:
     <service android:name=".MyAccessibilityService">
    <intent-filter>
    <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />
    </service>

    Note: This approach enables setting all properties.

    For more details refer to {@link #SERVICE_META_DATA} and <{@link android.R.styleable#AccessibilityService accessibility-service}>.

  • Calling {@link AccessibilityService#setServiceInfo(AccessibilityServiceInfo)}. Note that this method can be called any time to dynamically change the service configuration.

    Note: This approach enables setting only dynamically configurable properties: {@link AccessibilityServiceInfo#eventTypes}, {@link AccessibilityServiceInfo#feedbackType}, {@link AccessibilityServiceInfo#flags}, {@link AccessibilityServiceInfo#notificationTimeout}, {@link AccessibilityServiceInfo#packageNames}

    For more details refer to {@link AccessibilityServiceInfo}.

Retrieving window content

A service can specify in its declaration that it can retrieve the active window content which is represented as a tree of {@link AccessibilityNodeInfo}. Note that declaring this capability requires that the service declares its configuration via an XML resource referenced by {@link #SERVICE_META_DATA}.

For security purposes an accessibility service can retrieve only the content of the currently active window. The currently active window is defined as the window from which was fired the last event of the following types: {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}, {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}, {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}, In other words, the last window that was shown or the last window that the user has touched during touch exploration.

The entry point for retrieving window content is through calling {@link AccessibilityEvent#getSource() AccessibilityEvent.getSource()} of the last received event of the above types or a previous event from the same window (see {@link AccessibilityEvent#getWindowId() AccessibilityEvent.getWindowId()}). Invoking this method will return an {@link AccessibilityNodeInfo} that can be used to traverse the window content which represented as a tree of such objects.

Note An accessibility service may have requested to be notified for a subset of the event types, thus be unaware that the active window has changed. Therefore accessibility service that would like to retrieve window content should:

  • Register for all event types with no notification timeout and keep track for the active window by calling {@link AccessibilityEvent#getWindowId()} of the last received event and compare this with the {@link AccessibilityNodeInfo#getWindowId()} before calling retrieval methods on the latter.
  • Prepare that a retrieval method on {@link AccessibilityNodeInfo} may fail since the active window has changed and the service did not get the accessibility event yet. Note that it is possible to have a retrieval method failing even adopting the strategy specified in the previous bullet because the accessibility event dispatch is asynchronous and crosses process boundaries.

Notification strategy

For each feedback type only one accessibility service is notified. Services are notified in the order of registration. Hence, if two services are registered for the same feedback type in the same package the first one wins. It is possible however, to register a service as the default one for a given feedback type. In such a case this service is invoked if no other service was interested in the event. In other words, default services do not compete with other services and are notified last regardless of the registration order. This enables "generic" accessibility services that work reasonably well with most applications to coexist with "polished" ones that are targeted for specific applications.

Note: The event notification timeout is useful to avoid propagating events to the client too frequently since this is accomplished via an expensive interprocess call. One can think of the timeout as a criteria to determine when event generation has settled down.

Event types

  • {@link AccessibilityEvent#TYPE_VIEW_CLICKED}
  • {@link AccessibilityEvent#TYPE_VIEW_LONG_CLICKED}
  • {@link AccessibilityEvent#TYPE_VIEW_FOCUSED}
  • {@link AccessibilityEvent#TYPE_VIEW_SELECTED}
  • {@link AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED}
  • {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}
  • {@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED}
  • {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START}
  • {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END}
  • {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}
  • {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}
  • {@link AccessibilityEvent#TYPE_VIEW_SCROLLED}
  • {@link AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED}
  • {@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED}
  • {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
  • {@link AccessibilityEvent#TYPE_GESTURE_DETECTION_START}
  • {@link AccessibilityEvent#TYPE_GESTURE_DETECTION_END}
  • {@link AccessibilityEvent#TYPE_TOUCH_INTERACTION_START}
  • {@link AccessibilityEvent#TYPE_TOUCH_INTERACTION_END}
  • {@link AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUSED}
  • {@link AccessibilityEvent#TYPE_WINDOWS_CHANGED}
  • {@link AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED}

Feedback types

  • {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE}
  • {@link AccessibilityServiceInfo#FEEDBACK_HAPTIC}
  • {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE}
  • {@link AccessibilityServiceInfo#FEEDBACK_VISUAL}
  • {@link AccessibilityServiceInfo#FEEDBACK_GENERIC}
  • {@link AccessibilityServiceInfo#FEEDBACK_BRAILLE}
see
AccessibilityEvent
see
AccessibilityServiceInfo
see
android.view.accessibility.AccessibilityManager

Fields Summary
public static final int
GESTURE_SWIPE_UP
The user has performed a swipe up gesture on the touch screen.
public static final int
GESTURE_SWIPE_DOWN
The user has performed a swipe down gesture on the touch screen.
public static final int
GESTURE_SWIPE_LEFT
The user has performed a swipe left gesture on the touch screen.
public static final int
GESTURE_SWIPE_RIGHT
The user has performed a swipe right gesture on the touch screen.
public static final int
GESTURE_SWIPE_LEFT_AND_RIGHT
The user has performed a swipe left and right gesture on the touch screen.
public static final int
GESTURE_SWIPE_RIGHT_AND_LEFT
The user has performed a swipe right and left gesture on the touch screen.
public static final int
GESTURE_SWIPE_UP_AND_DOWN
The user has performed a swipe up and down gesture on the touch screen.
public static final int
GESTURE_SWIPE_DOWN_AND_UP
The user has performed a swipe down and up gesture on the touch screen.
public static final int
GESTURE_SWIPE_LEFT_AND_UP
The user has performed a left and up gesture on the touch screen.
public static final int
GESTURE_SWIPE_LEFT_AND_DOWN
The user has performed a left and down gesture on the touch screen.
public static final int
GESTURE_SWIPE_RIGHT_AND_UP
The user has performed a right and up gesture on the touch screen.
public static final int
GESTURE_SWIPE_RIGHT_AND_DOWN
The user has performed a right and down gesture on the touch screen.
public static final int
GESTURE_SWIPE_UP_AND_LEFT
The user has performed an up and left gesture on the touch screen.
public static final int
GESTURE_SWIPE_UP_AND_RIGHT
The user has performed an up and right gesture on the touch screen.
public static final int
GESTURE_SWIPE_DOWN_AND_LEFT
The user has performed an down and left gesture on the touch screen.
public static final int
GESTURE_SWIPE_DOWN_AND_RIGHT
The user has performed an down and right gesture on the touch screen.
public static final String
SERVICE_INTERFACE
The {@link Intent} that must be declared as handled by the service.
public static final String
SERVICE_META_DATA
Name under which an AccessibilityService component publishes information about itself. This meta-data must reference an XML resource containing an <{@link android.R.styleable#AccessibilityService accessibility-service}> tag. This is a a sample XML file configuring an accessibility service:
 <accessibility-service
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:packageNames="foo.bar, foo.baz"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:accessibilityFlags="flagDefault"
android:settingsActivity="foo.bar.TestBackActivity"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:canRequestEnhancedWebAccessibility="true"
. . .
/>
public static final int
GLOBAL_ACTION_BACK
Action to go back.
public static final int
GLOBAL_ACTION_HOME
Action to go home.
public static final int
GLOBAL_ACTION_RECENTS
Action to open the recent apps.
public static final int
GLOBAL_ACTION_NOTIFICATIONS
Action to open the notifications.
public static final int
GLOBAL_ACTION_QUICK_SETTINGS
Action to open the quick settings.
public static final int
GLOBAL_ACTION_POWER_DIALOG
Action to open the power long-press dialog.
private static final String
LOG_TAG
private int
mConnectionId
private AccessibilityServiceInfo
mInfo
private android.os.IBinder
mWindowToken
private android.view.WindowManager
mWindowManager
Constructors Summary
Methods Summary
public android.view.accessibility.AccessibilityNodeInfofindFocus(int focus)
Find the view that has the specified focus type. The search is performed across all windows.

Note: In order to access the windows your service has to declare the capability to retrieve window content by setting the {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} property in its meta-data. For details refer to {@link #SERVICE_META_DATA}. Also the service has to opt-in to retrieve the interactive windows by setting the {@link AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS} flag.Otherwise, the search will be performed only in the active window.

param
focus The focus to find. One of {@link AccessibilityNodeInfo#FOCUS_INPUT} or {@link AccessibilityNodeInfo#FOCUS_ACCESSIBILITY}.
return
The node info of the focused view or null.
see
AccessibilityNodeInfo#FOCUS_INPUT
see
AccessibilityNodeInfo#FOCUS_ACCESSIBILITY

        return AccessibilityInteractionClient.getInstance().findFocus(mConnectionId,
                AccessibilityNodeInfo.ANY_WINDOW_ID, AccessibilityNodeInfo.ROOT_NODE_ID, focus);
    
public android.view.accessibility.AccessibilityNodeInfogetRootInActiveWindow()
Gets the root node in the currently active window if this service can retrieve window content. The active window is the one that the user is currently touching or the window with input focus, if the user is not touching any window.

Note: In order to access the root node your service has to declare the capability to retrieve window content by setting the {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} property in its meta-data. For details refer to {@link #SERVICE_META_DATA}.

return
The root node if this service can retrieve window content.

        return AccessibilityInteractionClient.getInstance().getRootInActiveWindow(mConnectionId);
    
public final AccessibilityServiceInfogetServiceInfo()
Gets the an {@link AccessibilityServiceInfo} describing this {@link AccessibilityService}. This method is useful if one wants to change some of the dynamically configurable properties at runtime.

return
The accessibility service info.
see
AccessibilityServiceInfo

        IAccessibilityServiceConnection connection =
            AccessibilityInteractionClient.getInstance().getConnection(mConnectionId);
        if (connection != null) {
            try {
                return connection.getServiceInfo();
            } catch (RemoteException re) {
                Log.w(LOG_TAG, "Error while getting AccessibilityServiceInfo", re);
            }
        }
        return null;
    
public java.lang.ObjectgetSystemService(java.lang.String name)

        if (getBaseContext() == null) {
            throw new IllegalStateException(
                    "System services not available to Activities before onCreate()");
        }

        // Guarantee that we always return the same window manager instance.
        if (WINDOW_SERVICE.equals(name)) {
            if (mWindowManager == null) {
                mWindowManager = (WindowManager) getBaseContext().getSystemService(name);
            }
            return mWindowManager;
        }
        return super.getSystemService(name);
    
public java.util.ListgetWindows()
Gets the windows on the screen. This method returns only the windows that a sighted user can interact with, as opposed to all windows. For example, if there is a modal dialog shown and the user cannot touch anything behind it, then only the modal window will be reported (assuming it is the top one). For convenience the returned windows are ordered in a descending layer order, which is the windows that are higher in the Z-order are reported first. Since the user can always interact with the window that has input focus by typing, the focused window is always returned (even if covered by a modal window).

Note: In order to access the windows your service has to declare the capability to retrieve window content by setting the {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} property in its meta-data. For details refer to {@link #SERVICE_META_DATA}. Also the service has to opt-in to retrieve the interactive windows by setting the {@link AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS} flag.

return
The windows if there are windows and the service is can retrieve them, otherwise an empty list.

        return AccessibilityInteractionClient.getInstance().getWindows(mConnectionId);
    
public abstract voidonAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)
Callback for {@link android.view.accessibility.AccessibilityEvent}s.

param
event An event.

public final android.os.IBinderonBind(android.content.Intent intent)
Implement to return the implementation of the internal accessibility service interface.

        return new IAccessibilityServiceClientWrapper(this, getMainLooper(), new Callbacks() {
            @Override
            public void onServiceConnected() {
                AccessibilityService.this.onServiceConnected();
            }

            @Override
            public void onInterrupt() {
                AccessibilityService.this.onInterrupt();
            }

            @Override
            public void onAccessibilityEvent(AccessibilityEvent event) {
                AccessibilityService.this.onAccessibilityEvent(event);
            }

            @Override
            public void init(int connectionId, IBinder windowToken) {
                mConnectionId = connectionId;
                mWindowToken = windowToken;

                // The client may have already obtained the window manager, so
                // update the default token on whatever manager we gave them.
                final WindowManagerImpl wm = (WindowManagerImpl) getSystemService(WINDOW_SERVICE);
                wm.setDefaultToken(windowToken);
            }

            @Override
            public boolean onGesture(int gestureId) {
                return AccessibilityService.this.onGesture(gestureId);
            }

            @Override
            public boolean onKeyEvent(KeyEvent event) {
                return AccessibilityService.this.onKeyEvent(event);
            }
        });
    
protected booleanonGesture(int gestureId)
Called by the system when the user performs a specific gesture on the touch screen. Note: To receive gestures an accessibility service must request that the device is in touch exploration mode by setting the {@link android.accessibilityservice.AccessibilityServiceInfo#FLAG_REQUEST_TOUCH_EXPLORATION_MODE} flag.

param
gestureId The unique id of the performed gesture.
return
Whether the gesture was handled.
see
#GESTURE_SWIPE_UP
see
#GESTURE_SWIPE_UP_AND_LEFT
see
#GESTURE_SWIPE_UP_AND_DOWN
see
#GESTURE_SWIPE_UP_AND_RIGHT
see
#GESTURE_SWIPE_DOWN
see
#GESTURE_SWIPE_DOWN_AND_LEFT
see
#GESTURE_SWIPE_DOWN_AND_UP
see
#GESTURE_SWIPE_DOWN_AND_RIGHT
see
#GESTURE_SWIPE_LEFT
see
#GESTURE_SWIPE_LEFT_AND_UP
see
#GESTURE_SWIPE_LEFT_AND_RIGHT
see
#GESTURE_SWIPE_LEFT_AND_DOWN
see
#GESTURE_SWIPE_RIGHT
see
#GESTURE_SWIPE_RIGHT_AND_UP
see
#GESTURE_SWIPE_RIGHT_AND_LEFT
see
#GESTURE_SWIPE_RIGHT_AND_DOWN

        return false;
    
public abstract voidonInterrupt()
Callback for interrupting the accessibility feedback.

protected booleanonKeyEvent(android.view.KeyEvent event)
Callback that allows an accessibility service to observe the key events before they are passed to the rest of the system. This means that the events are first delivered here before they are passed to the device policy, the input method, or applications.

Note: It is important that key events are handled in such a way that the event stream that would be passed to the rest of the system is well-formed. For example, handling the down event but not the up event and vice versa would generate an inconsistent event stream.

Note: The key events delivered in this method are copies and modifying them will have no effect on the events that will be passed to the system. This method is intended to perform purely filtering functionality.

param
event The event to be processed.
return
If true then the event will be consumed and not delivered to applications, otherwise it will be delivered as usual.

        return false;
    
protected voidonServiceConnected()
This method is a part of the {@link AccessibilityService} lifecycle and is called after the system has successfully bound to the service. If is convenient to use this method for setting the {@link AccessibilityServiceInfo}.

see
AccessibilityServiceInfo
see
#setServiceInfo(AccessibilityServiceInfo)


                 
        

               
       

                                               
       

    
public final booleanperformGlobalAction(int action)
Performs a global action. Such an action can be performed at any moment regardless of the current application or user location in that application. For example going back, going home, opening recents, etc.

param
action The action to perform.
return
Whether the action was successfully performed.
see
#GLOBAL_ACTION_BACK
see
#GLOBAL_ACTION_HOME
see
#GLOBAL_ACTION_NOTIFICATIONS
see
#GLOBAL_ACTION_RECENTS

        IAccessibilityServiceConnection connection =
            AccessibilityInteractionClient.getInstance().getConnection(mConnectionId);
        if (connection != null) {
            try {
                return connection.performGlobalAction(action);
            } catch (RemoteException re) {
                Log.w(LOG_TAG, "Error while calling performGlobalAction", re);
            }
        }
        return false;
    
private voidsendServiceInfo()
Sets the {@link AccessibilityServiceInfo} for this service if the latter is properly set and there is an {@link IAccessibilityServiceConnection} to the AccessibilityManagerService.

        IAccessibilityServiceConnection connection =
            AccessibilityInteractionClient.getInstance().getConnection(mConnectionId);
        if (mInfo != null && connection != null) {
            try {
                connection.setServiceInfo(mInfo);
                mInfo = null;
                AccessibilityInteractionClient.getInstance().clearCache();
            } catch (RemoteException re) {
                Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re);
            }
        }
    
public final voidsetServiceInfo(AccessibilityServiceInfo info)
Sets the {@link AccessibilityServiceInfo} that describes this service.

Note: You can call this method any time but the info will be picked up after the system has bound to this service and when this method is called thereafter.

param
info The info.

        mInfo = info;
        sendServiceInfo();