diff options
| author | svetoslavganov <svetoslavganov@google.com> | 2009-05-14 22:28:01 -0700 | 
|---|---|---|
| committer | svetoslavganov <svetoslavganov@google.com> | 2009-05-14 23:47:05 -0700 | 
| commit | 75986cf9bc57ef11ad70f36fb77fbbf5d63af6ec (patch) | |
| tree | 84e1843368037d24f83749d152f818d537267bfa /core/java/android/accessibilityservice | |
| parent | 669ec3a6e47248fee0a3a0f4877b46875eb42140 (diff) | |
| download | frameworks_base-75986cf9bc57ef11ad70f36fb77fbbf5d63af6ec.zip frameworks_base-75986cf9bc57ef11ad70f36fb77fbbf5d63af6ec.tar.gz frameworks_base-75986cf9bc57ef11ad70f36fb77fbbf5d63af6ec.tar.bz2 | |
Accessibility feature - framework changes (replacing 698, 699, 700, 701 and merging with the latest Donut)
Diffstat (limited to 'core/java/android/accessibilityservice')
5 files changed, 453 insertions, 0 deletions
| diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java new file mode 100644 index 0000000..a3456c7 --- /dev/null +++ b/core/java/android/accessibilityservice/AccessibilityService.java @@ -0,0 +1,225 @@ +/* + * 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; + +/** + * 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. + * <p> + * 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: + * <p> + * <code> + * <service android:name=".MyAccessibilityService"><br> + *     <intent-filter><br> + *         <action android:name="android.accessibilityservice.AccessibilityService" /><br> + *     </intent-filter><br> + * </service><br> + * </code> + * <p> + * 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 + * is configured though setting an {@link AccessibilityServiceInfo} by calling + * {@link AccessibilityService#setServiceInfo(AccessibilityServiceInfo)}. You can call this + * method any time to change the service configuration but it is good practice to do that + * in the overriden {@link AccessibilityService#onServiceConnected()}. + * <p> + * 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. + * <p> + * <b>Notification strategy</b> + * <p> + * 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. + * <p> + * <b>Event types</b> + * <p> + * {@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} + *  <p> + *  <b>Feedback types</b> + *  <p> + * {@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"; + +    private static final String LOG_TAG = "AccessibilityService"; + +    private AccessibilityServiceInfo mInfo; + +    IAccessibilityServiceConnection mConnection; + +    /** +     * Callback for {@link android.view.accessibility.AccessibilityEvent}s. +     * +     * @param event An event. +     */ +    public abstract void onAccessibilityEvent(AccessibilityEvent event); + +    /** +     * Callback for interrupting the accessibility feedback. +     */ +    public abstract void onInterrupt(); + +    /** +     * 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) +     */ +    protected void onServiceConnected() { + +    } + +    /** +     * Sets the {@link AccessibilityServiceInfo} that describes this service. +     * <p> +     * 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. +     */ +    public final void setServiceInfo(AccessibilityServiceInfo info) { +        mInfo = info; +        sendServiceInfo(); +    } + +    /** +     * Sets the {@link AccessibilityServiceInfo} for this service if the latter is +     * properly set and there is an {@link IAccessibilityServiceConnection} to the +     * AccessibilityManagerService. +     */ +    private void sendServiceInfo() { +        if (mInfo != null && mConnection != null) { +            try { +                mConnection.setServiceInfo(mInfo); +            } catch (RemoteException re) { +                Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re); +            } +        } +    } + +    @Override +    public final IBinder onBind(Intent intent) { +        return new IEventListenerWrapper(this); +    } + +    /** +     * Implements the internal {@link IEventListener} interface to convert +     * incoming calls to it back to calls on an {@link AccessibilityService}. +     */ +    class IEventListenerWrapper extends IEventListener.Stub +            implements HandlerCaller.Callback { + +        private static final int DO_SET_SET_CONNECTION = 10; +        private static final int DO_ON_INTERRUPT = 20; +        private static final int DO_ON_ACCESSIBILITY_EVENT = 30; + +        private final HandlerCaller mCaller; + +        private AccessibilityService mTarget; + +        public IEventListenerWrapper(AccessibilityService context) { +            mTarget = context; +            mCaller = new HandlerCaller(context, this); +        } + +        public void setConnection(IAccessibilityServiceConnection connection) { +            Message message = mCaller.obtainMessageO(DO_SET_SET_CONNECTION, connection); +            mCaller.sendMessage(message); +        } + +        public void onInterrupt() { +            Message message = mCaller.obtainMessage(DO_ON_INTERRUPT); +            mCaller.sendMessage(message); +        } + +        public void onAccessibilityEvent(AccessibilityEvent event) { +            Message message = mCaller.obtainMessageO(DO_ON_ACCESSIBILITY_EVENT, event); +            mCaller.sendMessage(message); +        } + +        public void executeMessage(Message message) { +            switch (message.what) { +                case DO_ON_ACCESSIBILITY_EVENT : +                    AccessibilityEvent event = (AccessibilityEvent) message.obj; +                    mTarget.onAccessibilityEvent(event); +                    event.recycle(); +                    return; +                case DO_ON_INTERRUPT : +                    mTarget.onInterrupt(); +                    return; +                case DO_SET_SET_CONNECTION : +                    mConnection = ((IAccessibilityServiceConnection) message.obj); +                    mTarget.onServiceConnected(); +                    return; +                default : +                    Log.w(LOG_TAG, "Unknown message type " + message.what); +            } +        } +    } +} diff --git a/core/java/android/accessibilityservice/AccessibilityServiceInfo.aidl b/core/java/android/accessibilityservice/AccessibilityServiceInfo.aidl new file mode 100644 index 0000000..1f5d385 --- /dev/null +++ b/core/java/android/accessibilityservice/AccessibilityServiceInfo.aidl @@ -0,0 +1,19 @@ +/* + * 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; + +parcelable AccessibilityServiceInfo; diff --git a/core/java/android/accessibilityservice/AccessibilityServiceInfo.java b/core/java/android/accessibilityservice/AccessibilityServiceInfo.java new file mode 100644 index 0000000..4761f98 --- /dev/null +++ b/core/java/android/accessibilityservice/AccessibilityServiceInfo.java @@ -0,0 +1,145 @@ +/* + * 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 android.os.Parcel; +import android.os.Parcelable; + +/** + * This class describes an {@link AccessibilityService}. The system + * notifies an {@link AccessibilityService} for + * {@link android.view.accessibility.AccessibilityEvent}s + * according to the information encapsulated in this class. + * + * @see AccessibilityService + * @see android.view.accessibility.AccessibilityEvent + */ +public class AccessibilityServiceInfo implements Parcelable { + +    /** +     * Denotes spoken feedback. +     */ +    public static final int FEEDBACK_SPOKEN = 0x0000001; + +    /** +     * Denotes haptic feedback. +     */ +    public static final int FEEDBACK_HAPTIC =  0x0000002; + +    /** +     * Denotes audible (not spoken) feedback. +     */ +    public static final int FEEDBACK_AUDIBLE = 0x0000004; + +    /** +     * Denotes visual feedback. +     */ +    public static final int FEEDBACK_VISUAL = 0x0000008; + +    /** +     * Denotes generic feedback. +     */ +    public static final int FEEDBACK_GENERIC = 0x0000010; + +    /** +     * If an {@link AccessibilityService} is the default for a given type. +     * Default service is invoked only if no package specific one exists. In case of +     * more than one package specific service only the earlier registered is notified. +     */ +    public static final int DEFAULT = 0x0000001; + +    /** +     * The event types an {@link AccessibilityService} is interested in. +     * +     * @see android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_VIEW_FOCUSED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_VIEW_SELECTED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_ACTIVITY_STARTED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED +     * @see android.view.accessibility.AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED +     */ +    public int eventTypes; + +    /** +     * The package names an {@link AccessibilityService} is interested in. Setting +     * to null is equivalent to all packages.  +     */ +    public String[] packageNames; + +    /** +     * The feedback type an {@link AccessibilityService} provides. +     * +     * @see #FEEDBACK_AUDIBLE +     * @see #FEEDBACK_GENERIC +     * @see #FEEDBACK_HAPTIC +     * @see #FEEDBACK_SPOKEN +     * @see #FEEDBACK_VISUAL +     */ +    public int feedbackType; + +    /** +     * The timeout after the most recent event of a given type before an +     * {@link AccessibilityService} is notified. +     * <p> +     * 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 long notificationTimeout; + +    /** +     * This field represents a set of flags used for configuring an +     * {@link AccessibilityService}. +     * +     * @see #DEFAULT +     */ +    public int flags; + +    public int describeContents() { +        return 0; +    } + +    public void writeToParcel(Parcel parcel, int flags) { +        parcel.writeInt(eventTypes); +        parcel.writeStringArray(packageNames); +        parcel.writeInt(feedbackType); +        parcel.writeLong(notificationTimeout); +        parcel.writeInt(flags); +    } + +    /** +     * @see Parcelable.Creator +     */ +    public static final Parcelable.Creator<AccessibilityServiceInfo> CREATOR = +            new Parcelable.Creator<AccessibilityServiceInfo>() { +        public AccessibilityServiceInfo createFromParcel(Parcel parcel) { +            AccessibilityServiceInfo info = new AccessibilityServiceInfo(); +            info.eventTypes = parcel.readInt(); +            info.packageNames = parcel.readStringArray(); +            info.feedbackType = parcel.readInt(); +            info.notificationTimeout = parcel.readLong(); +            info.flags = parcel.readInt(); +            return info; +        } + +        public AccessibilityServiceInfo[] newArray(int size) { +            return new AccessibilityServiceInfo[size]; +        } +    }; +} diff --git a/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl b/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl new file mode 100644 index 0000000..7157def --- /dev/null +++ b/core/java/android/accessibilityservice/IAccessibilityServiceConnection.aidl @@ -0,0 +1,30 @@ +/* + * 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 android.accessibilityservice.AccessibilityServiceInfo; + +/** + * Interface AccessibilityManagerService#Service implements, and passes to an + * AccessibilityService so it can dynamically configure how the system handles it. + * + * @hide + */ +oneway interface IAccessibilityServiceConnection { + +    void setServiceInfo(in AccessibilityServiceInfo info); +} diff --git a/core/java/android/accessibilityservice/IEventListener.aidl b/core/java/android/accessibilityservice/IEventListener.aidl new file mode 100644 index 0000000..5b849f1 --- /dev/null +++ b/core/java/android/accessibilityservice/IEventListener.aidl @@ -0,0 +1,34 @@ +/* +** Copyright 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 android.accessibilityservice.IAccessibilityServiceConnection; +import android.view.accessibility.AccessibilityEvent; + +/** + * Top-level interface to accessibility service component (implemented in Service). + * + * @hide + */ + oneway interface IEventListener { + +    void setConnection(in IAccessibilityServiceConnection connection); + +    void onAccessibilityEvent(in AccessibilityEvent event); + +    void onInterrupt(); +} | 
