/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.accessibilityservice; import com.android.internal.os.HandlerCaller; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.util.Log; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; /** * 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. *

* An accessibility service extends this class and implements its abstract methods. Such * a service 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}. Following is an example of such a declaration: *

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

*

* The lifecycle of an accessibility service is managed exclusively by the system. Starting * or stopping an accessibility service is triggered 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. *

*

* 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: * *

* An accessibility service can be registered for events in specific packages to provide a * specific type of feedback and is notified with a certain timeout after the last event * of interest has been fired. *

* 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. *

* 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} *

* Feedback types *

* {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE} * {@link AccessibilityServiceInfo#FEEDBACK_HAPTIC} * {@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE} * {@link AccessibilityServiceInfo#FEEDBACK_VISUAL} * {@link AccessibilityServiceInfo#FEEDBACK_GENERIC} * * @see AccessibilityEvent * @see AccessibilityServiceInfo * @see android.view.accessibility.AccessibilityManager * * 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. */ public abstract class AccessibilityService extends Service { /** * The {@link Intent} that must be declared as handled by the service. */ public static final String SERVICE_INTERFACE = "android.accessibilityservice.AccessibilityService"; /** * 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: *

* * <?xml version="1.0" encoding="utf-8"?>
* <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
*   android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
*   android:packageNames="foo.bar, foo.baz"
*   android:accessibilityFeedbackType="feedbackSpoken"
*   android:notificationTimeout="100"
*   android:accessibilityFlags="flagDefault"
*   android:settingsActivity="foo.bar.TestBackActivity"
*   . . .
* /> *
*

*

* Note: A service can retrieve only the content of the active window. * An active window is the source of the most recent event of type * {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START}, * {@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END}, * {@link AccessibilityEvent#TYPE_VIEW_CLICKED}, * {@link AccessibilityEvent#TYPE_VIEW_FOCUSED}, * {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}, * {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}, * {@link AccessibilityEvent#TYPE_VIEW_LONG_CLICKED}, * {@link AccessibilityEvent#TYPE_VIEW_SELECTED}, * {@link AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED}, * {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}. * Therefore the service should: *