svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 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 | |
| 17 | package android.accessibilityservice; |
| 18 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 19 | import android.app.Service; |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 20 | import android.content.Context; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.os.IBinder; |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 23 | import android.os.Looper; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 24 | import android.os.Message; |
| 25 | import android.os.RemoteException; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 26 | import android.os.SystemClock; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 28 | import android.view.KeyEvent; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityInteractionClient; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 31 | import android.view.accessibility.AccessibilityNodeInfo; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 32 | import android.view.accessibility.AccessibilityWindowInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 33 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 34 | import com.android.internal.os.HandlerCaller; |
| 35 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 38 | /** |
| 39 | * An accessibility service runs in the background and receives callbacks by the system |
| 40 | * when {@link AccessibilityEvent}s are fired. Such events denote some state transition |
| 41 | * in the user interface, for example, the focus has changed, a button has been clicked, |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 42 | * etc. Such a service can optionally request the capability for querying the content |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 43 | * of the active window. Development of an accessibility service requires extending this |
| 44 | * class and implementing its abstract methods. |
Joe Fernandez | e1302ed | 2012-02-06 14:30:15 -0800 | [diff] [blame] | 45 | * |
| 46 | * <div class="special reference"> |
| 47 | * <h3>Developer Guides</h3> |
| 48 | * <p>For more information about creating AccessibilityServices, read the |
| 49 | * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a> |
| 50 | * developer guide.</p> |
| 51 | * </div> |
| 52 | * |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 53 | * <h3>Lifecycle</h3> |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 54 | * <p> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 55 | * The lifecycle of an accessibility service is managed exclusively by the system and |
| 56 | * follows the established service life cycle. Additionally, starting or stopping an |
| 57 | * accessibility service is triggered exclusively by an explicit user action through |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 58 | * enabling or disabling it in the device settings. After the system binds to a service it |
| 59 | * calls {@link AccessibilityService#onServiceConnected()}. This method can be |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 60 | * overriden by clients that want to perform post binding setup. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 61 | * </p> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 62 | * <h3>Declaration</h3> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 63 | * <p> |
| 64 | * An accessibility is declared as any other service in an AndroidManifest.xml but it |
| 65 | * must also specify that it handles the "android.accessibilityservice.AccessibilityService" |
| 66 | * {@link android.content.Intent}. Failure to declare this intent will cause the system to |
Dianne Hackborn | 636fd52 | 2012-06-05 17:38:50 -0700 | [diff] [blame] | 67 | * ignore the accessibility service. Additionally an accessibility service must request the |
| 68 | * {@link android.Manifest.permission#BIND_ACCESSIBILITY_SERVICE} permission to ensure |
| 69 | * that only the system |
Svetoslav Ganov | 53e184d | 2012-05-16 15:57:10 -0700 | [diff] [blame] | 70 | * can bind to it. Failure to declare this intent will cause the system to ignore the |
| 71 | * accessibility service. Following is an example declaration: |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 72 | * </p> |
Svetoslav Ganov | 53e184d | 2012-05-16 15:57:10 -0700 | [diff] [blame] | 73 | * <pre> <service android:name=".MyAccessibilityService" |
Scott Main | 53b0fda | 2012-08-06 12:50:48 -0700 | [diff] [blame] | 74 | * android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 75 | * <intent-filter> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 76 | * <action android:name="android.accessibilityservice.AccessibilityService" /> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 77 | * </intent-filter> |
| 78 | * . . . |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 79 | * </service></pre> |
| 80 | * <h3>Configuration</h3> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 81 | * <p> |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 82 | * An accessibility service can be configured to receive specific types of accessibility events, |
| 83 | * listen only to specific packages, get events from each type only once in a given time frame, |
| 84 | * retrieve window content, specify a settings activity, etc. |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 85 | * </p> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 86 | * <p> |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 87 | * There are two approaches for configuring an accessibility service: |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 88 | * </p> |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 89 | * <ul> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 90 | * <li> |
| 91 | * Providing a {@link #SERVICE_META_DATA meta-data} entry in the manifest when declaring |
| 92 | * the service. A service declaration with a meta-data tag is presented below: |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 93 | * <pre> <service android:name=".MyAccessibilityService"> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 94 | * <intent-filter> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 95 | * <action android:name="android.accessibilityservice.AccessibilityService" /> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 96 | * </intent-filter> |
| 97 | * <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" /> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 98 | * </service></pre> |
| 99 | * <p class="note"> |
| 100 | * <strong>Note:</strong> This approach enables setting all properties. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 101 | * </p> |
| 102 | * <p> |
| 103 | * For more details refer to {@link #SERVICE_META_DATA} and |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 104 | * <code><{@link android.R.styleable#AccessibilityService accessibility-service}></code>. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 105 | * </p> |
| 106 | * </li> |
| 107 | * <li> |
| 108 | * Calling {@link AccessibilityService#setServiceInfo(AccessibilityServiceInfo)}. Note |
| 109 | * that this method can be called any time to dynamically change the service configuration. |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 110 | * <p class="note"> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 111 | * <strong>Note:</strong> This approach enables setting only dynamically configurable properties: |
| 112 | * {@link AccessibilityServiceInfo#eventTypes}, |
| 113 | * {@link AccessibilityServiceInfo#feedbackType}, |
| 114 | * {@link AccessibilityServiceInfo#flags}, |
| 115 | * {@link AccessibilityServiceInfo#notificationTimeout}, |
| 116 | * {@link AccessibilityServiceInfo#packageNames} |
| 117 | * </p> |
| 118 | * <p> |
| 119 | * For more details refer to {@link AccessibilityServiceInfo}. |
| 120 | * </p> |
| 121 | * </li> |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 122 | * </ul> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 123 | * <h3>Retrieving window content</h3> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 124 | * <p> |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 125 | * A service can specify in its declaration that it can retrieve the active window |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 126 | * content which is represented as a tree of {@link AccessibilityNodeInfo}. Note that |
| 127 | * declaring this capability requires that the service declares its configuration via |
| 128 | * an XML resource referenced by {@link #SERVICE_META_DATA}. |
| 129 | * </p> |
| 130 | * <p> |
| 131 | * For security purposes an accessibility service can retrieve only the content of the |
| 132 | * currently active window. The currently active window is defined as the window from |
| 133 | * which was fired the last event of the following types: |
Svetoslav Ganov | 4e2a762 | 2011-07-26 20:08:46 -0700 | [diff] [blame] | 134 | * {@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}, |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 135 | * {@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}, |
| 136 | * {@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}, |
Svetoslav Ganov | 4e2a762 | 2011-07-26 20:08:46 -0700 | [diff] [blame] | 137 | * In other words, the last window that was shown or the last window that the user has touched |
| 138 | * during touch exploration. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 139 | * </p> |
| 140 | * <p> |
| 141 | * The entry point for retrieving window content is through calling |
| 142 | * {@link AccessibilityEvent#getSource() AccessibilityEvent.getSource()} of the last received |
| 143 | * event of the above types or a previous event from the same window |
| 144 | * (see {@link AccessibilityEvent#getWindowId() AccessibilityEvent.getWindowId()}). Invoking |
| 145 | * this method will return an {@link AccessibilityNodeInfo} that can be used to traverse the |
| 146 | * window content which represented as a tree of such objects. |
| 147 | * </p> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 148 | * <p class="note"> |
| 149 | * <strong>Note</strong> An accessibility service may have requested to be notified for |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 150 | * a subset of the event types, thus be unaware that the active window has changed. Therefore |
| 151 | * accessibility service that would like to retrieve window content should: |
| 152 | * <ul> |
| 153 | * <li> |
| 154 | * Register for all event types with no notification timeout and keep track for the active |
| 155 | * window by calling {@link AccessibilityEvent#getWindowId()} of the last received event and |
| 156 | * compare this with the {@link AccessibilityNodeInfo#getWindowId()} before calling retrieval |
| 157 | * methods on the latter. |
| 158 | * </li> |
| 159 | * <li> |
| 160 | * Prepare that a retrieval method on {@link AccessibilityNodeInfo} may fail since the |
| 161 | * active window has changed and the service did not get the accessibility event yet. Note |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 162 | * that it is possible to have a retrieval method failing even adopting the strategy |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 163 | * specified in the previous bullet because the accessibility event dispatch is asynchronous |
| 164 | * and crosses process boundaries. |
| 165 | * </li> |
| 166 | * </ul> |
| 167 | * </p> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 168 | * <h3>Notification strategy</h3> |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 169 | * <p> |
| 170 | * For each feedback type only one accessibility service is notified. Services are notified |
| 171 | * in the order of registration. Hence, if two services are registered for the same |
| 172 | * feedback type in the same package the first one wins. It is possible however, to |
| 173 | * register a service as the default one for a given feedback type. In such a case this |
| 174 | * service is invoked if no other service was interested in the event. In other words, default |
| 175 | * services do not compete with other services and are notified last regardless of the |
| 176 | * registration order. This enables "generic" accessibility services that work reasonably |
| 177 | * well with most applications to coexist with "polished" ones that are targeted for |
| 178 | * specific applications. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 179 | * </p> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 180 | * <p class="note"> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 181 | * <strong>Note:</strong> The event notification timeout is useful to avoid propagating |
| 182 | * events to the client too frequently since this is accomplished via an expensive |
| 183 | * interprocess call. One can think of the timeout as a criteria to determine when |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 184 | * event generation has settled down.</p> |
| 185 | * <h3>Event types</h3> |
| 186 | * <ul> |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 187 | * <li>{@link AccessibilityEvent#TYPE_VIEW_CLICKED}</li> |
| 188 | * <li>{@link AccessibilityEvent#TYPE_VIEW_LONG_CLICKED}</li> |
| 189 | * <li>{@link AccessibilityEvent#TYPE_VIEW_FOCUSED}</li> |
| 190 | * <li>{@link AccessibilityEvent#TYPE_VIEW_SELECTED}</li> |
| 191 | * <li>{@link AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED}</li> |
| 192 | * <li>{@link AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED}</li> |
| 193 | * <li>{@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED}</li> |
| 194 | * <li>{@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START}</li> |
| 195 | * <li>{@link AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END}</li> |
| 196 | * <li>{@link AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}</li> |
| 197 | * <li>{@link AccessibilityEvent#TYPE_VIEW_HOVER_EXIT}</li> |
| 198 | * <li>{@link AccessibilityEvent#TYPE_VIEW_SCROLLED}</li> |
| 199 | * <li>{@link AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED}</li> |
| 200 | * <li>{@link AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED}</li> |
| 201 | * <li>{@link AccessibilityEvent#TYPE_ANNOUNCEMENT}</li> |
| 202 | * <li>{@link AccessibilityEvent#TYPE_GESTURE_DETECTION_START}</li> |
| 203 | * <li>{@link AccessibilityEvent#TYPE_GESTURE_DETECTION_END}</li> |
| 204 | * <li>{@link AccessibilityEvent#TYPE_TOUCH_INTERACTION_START}</li> |
| 205 | * <li>{@link AccessibilityEvent#TYPE_TOUCH_INTERACTION_END}</li> |
| 206 | * <li>{@link AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUSED}</li> |
| 207 | * <li>{@link AccessibilityEvent#TYPE_WINDOWS_CHANGED}</li> |
| 208 | * <li>{@link AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED}</li> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 209 | * </ul> |
| 210 | * <h3>Feedback types</h3> |
| 211 | * <ul> |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 212 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE}</li> |
| 213 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_HAPTIC}</li> |
| 214 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_AUDIBLE}</li> |
| 215 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_VISUAL}</li> |
| 216 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_GENERIC}</li> |
| 217 | * <li>{@link AccessibilityServiceInfo#FEEDBACK_BRAILLE}</li> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 218 | * </ul> |
| 219 | * @see AccessibilityEvent |
| 220 | * @see AccessibilityServiceInfo |
| 221 | * @see android.view.accessibility.AccessibilityManager |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 222 | */ |
| 223 | public abstract class AccessibilityService extends Service { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * The user has performed a swipe up gesture on the touch screen. |
| 227 | */ |
| 228 | public static final int GESTURE_SWIPE_UP = 1; |
| 229 | |
| 230 | /** |
| 231 | * The user has performed a swipe down gesture on the touch screen. |
| 232 | */ |
| 233 | public static final int GESTURE_SWIPE_DOWN = 2; |
| 234 | |
| 235 | /** |
| 236 | * The user has performed a swipe left gesture on the touch screen. |
| 237 | */ |
| 238 | public static final int GESTURE_SWIPE_LEFT = 3; |
| 239 | |
| 240 | /** |
| 241 | * The user has performed a swipe right gesture on the touch screen. |
| 242 | */ |
| 243 | public static final int GESTURE_SWIPE_RIGHT = 4; |
| 244 | |
| 245 | /** |
| 246 | * The user has performed a swipe left and right gesture on the touch screen. |
| 247 | */ |
| 248 | public static final int GESTURE_SWIPE_LEFT_AND_RIGHT = 5; |
| 249 | |
| 250 | /** |
| 251 | * The user has performed a swipe right and left gesture on the touch screen. |
| 252 | */ |
| 253 | public static final int GESTURE_SWIPE_RIGHT_AND_LEFT = 6; |
| 254 | |
| 255 | /** |
| 256 | * The user has performed a swipe up and down gesture on the touch screen. |
| 257 | */ |
| 258 | public static final int GESTURE_SWIPE_UP_AND_DOWN = 7; |
| 259 | |
| 260 | /** |
| 261 | * The user has performed a swipe down and up gesture on the touch screen. |
| 262 | */ |
| 263 | public static final int GESTURE_SWIPE_DOWN_AND_UP = 8; |
| 264 | |
| 265 | /** |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 266 | * The user has performed a left and up gesture on the touch screen. |
| 267 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 268 | public static final int GESTURE_SWIPE_LEFT_AND_UP = 9; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * The user has performed a left and down gesture on the touch screen. |
| 272 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 273 | public static final int GESTURE_SWIPE_LEFT_AND_DOWN = 10; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * The user has performed a right and up gesture on the touch screen. |
| 277 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 278 | public static final int GESTURE_SWIPE_RIGHT_AND_UP = 11; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * The user has performed a right and down gesture on the touch screen. |
| 282 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 283 | public static final int GESTURE_SWIPE_RIGHT_AND_DOWN = 12; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * The user has performed an up and left gesture on the touch screen. |
| 287 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 288 | public static final int GESTURE_SWIPE_UP_AND_LEFT = 13; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * The user has performed an up and right gesture on the touch screen. |
| 292 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 293 | public static final int GESTURE_SWIPE_UP_AND_RIGHT = 14; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * The user has performed an down and left gesture on the touch screen. |
| 297 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 298 | public static final int GESTURE_SWIPE_DOWN_AND_LEFT = 15; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * The user has performed an down and right gesture on the touch screen. |
| 302 | */ |
Svetoslav Ganov | 65d98ad | 2012-05-02 17:57:06 -0700 | [diff] [blame] | 303 | public static final int GESTURE_SWIPE_DOWN_AND_RIGHT = 16; |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 304 | |
| 305 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 306 | * The {@link Intent} that must be declared as handled by the service. |
| 307 | */ |
| 308 | public static final String SERVICE_INTERFACE = |
| 309 | "android.accessibilityservice.AccessibilityService"; |
| 310 | |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 311 | /** |
| 312 | * Name under which an AccessibilityService component publishes information |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 313 | * about itself. This meta-data must reference an XML resource containing an |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 314 | * <code><{@link android.R.styleable#AccessibilityService accessibility-service}></code> |
| 315 | * tag. This is a a sample XML file configuring an accessibility service: |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 316 | * <pre> <accessibility-service |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 317 | * android:accessibilityEventTypes="typeViewClicked|typeViewFocused" |
| 318 | * android:packageNames="foo.bar, foo.baz" |
| 319 | * android:accessibilityFeedbackType="feedbackSpoken" |
| 320 | * android:notificationTimeout="100" |
| 321 | * android:accessibilityFlags="flagDefault" |
| 322 | * android:settingsActivity="foo.bar.TestBackActivity" |
| 323 | * android:canRetrieveWindowContent="true" |
Svetoslav | 688a697 | 2013-04-16 18:55:38 -0700 | [diff] [blame] | 324 | * android:canRequestTouchExplorationMode="true" |
| 325 | * android:canRequestEnhancedWebAccessibility="true" |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 326 | * . . . |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 327 | * /></pre> |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 328 | */ |
| 329 | public static final String SERVICE_META_DATA = "android.accessibilityservice"; |
| 330 | |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Action to go back. |
| 333 | */ |
| 334 | public static final int GLOBAL_ACTION_BACK = 1; |
| 335 | |
| 336 | /** |
| 337 | * Action to go home. |
| 338 | */ |
| 339 | public static final int GLOBAL_ACTION_HOME = 2; |
| 340 | |
| 341 | /** |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 342 | * Action to open the recent apps. |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 343 | */ |
| 344 | public static final int GLOBAL_ACTION_RECENTS = 3; |
| 345 | |
| 346 | /** |
| 347 | * Action to open the notifications. |
| 348 | */ |
| 349 | public static final int GLOBAL_ACTION_NOTIFICATIONS = 4; |
| 350 | |
Svetoslav Ganov | e20a177 | 2012-09-25 16:07:46 -0700 | [diff] [blame] | 351 | /** |
| 352 | * Action to open the quick settings. |
| 353 | */ |
| 354 | public static final int GLOBAL_ACTION_QUICK_SETTINGS = 5; |
| 355 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 356 | private static final String LOG_TAG = "AccessibilityService"; |
| 357 | |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 358 | /** |
| 359 | * @hide |
| 360 | */ |
| 361 | public interface Callbacks { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 362 | public void onAccessibilityEvent(AccessibilityEvent event); |
| 363 | public void onInterrupt(); |
| 364 | public void onServiceConnected(); |
| 365 | public void onSetConnectionId(int connectionId); |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 366 | public boolean onGesture(int gestureId); |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 367 | public boolean onKeyEvent(KeyEvent event); |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 368 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 369 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 370 | private int mConnectionId; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 371 | |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 372 | private AccessibilityServiceInfo mInfo; |
| 373 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 374 | /** |
| 375 | * Callback for {@link android.view.accessibility.AccessibilityEvent}s. |
| 376 | * |
| 377 | * @param event An event. |
| 378 | */ |
| 379 | public abstract void onAccessibilityEvent(AccessibilityEvent event); |
| 380 | |
| 381 | /** |
| 382 | * Callback for interrupting the accessibility feedback. |
| 383 | */ |
| 384 | public abstract void onInterrupt(); |
| 385 | |
| 386 | /** |
| 387 | * This method is a part of the {@link AccessibilityService} lifecycle and is |
| 388 | * called after the system has successfully bound to the service. If is |
| 389 | * convenient to use this method for setting the {@link AccessibilityServiceInfo}. |
| 390 | * |
| 391 | * @see AccessibilityServiceInfo |
| 392 | * @see #setServiceInfo(AccessibilityServiceInfo) |
| 393 | */ |
| 394 | protected void onServiceConnected() { |
| 395 | |
| 396 | } |
| 397 | |
| 398 | /** |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 399 | * Called by the system when the user performs a specific gesture on the |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 400 | * touch screen. |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 401 | * |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 402 | * <strong>Note:</strong> To receive gestures an accessibility service must |
| 403 | * request that the device is in touch exploration mode by setting the |
Svetoslav Ganov | 7b1e0c7 | 2012-05-13 11:57:29 -0700 | [diff] [blame] | 404 | * {@link android.accessibilityservice.AccessibilityServiceInfo#FLAG_REQUEST_TOUCH_EXPLORATION_MODE} |
Svetoslav Ganov | e4abc51 | 2012-05-09 11:02:38 -0700 | [diff] [blame] | 405 | * flag. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 406 | * |
| 407 | * @param gestureId The unique id of the performed gesture. |
| 408 | * |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 409 | * @return Whether the gesture was handled. |
| 410 | * |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 411 | * @see #GESTURE_SWIPE_UP |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 412 | * @see #GESTURE_SWIPE_UP_AND_LEFT |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 413 | * @see #GESTURE_SWIPE_UP_AND_DOWN |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 414 | * @see #GESTURE_SWIPE_UP_AND_RIGHT |
| 415 | * @see #GESTURE_SWIPE_DOWN |
| 416 | * @see #GESTURE_SWIPE_DOWN_AND_LEFT |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 417 | * @see #GESTURE_SWIPE_DOWN_AND_UP |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 418 | * @see #GESTURE_SWIPE_DOWN_AND_RIGHT |
| 419 | * @see #GESTURE_SWIPE_LEFT |
| 420 | * @see #GESTURE_SWIPE_LEFT_AND_UP |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 421 | * @see #GESTURE_SWIPE_LEFT_AND_RIGHT |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 422 | * @see #GESTURE_SWIPE_LEFT_AND_DOWN |
| 423 | * @see #GESTURE_SWIPE_RIGHT |
| 424 | * @see #GESTURE_SWIPE_RIGHT_AND_UP |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 425 | * @see #GESTURE_SWIPE_RIGHT_AND_LEFT |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 426 | * @see #GESTURE_SWIPE_RIGHT_AND_DOWN |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 427 | */ |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 428 | protected boolean onGesture(int gestureId) { |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 429 | return false; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 433 | * Callback that allows an accessibility service to observe the key events |
| 434 | * before they are passed to the rest of the system. This means that the events |
| 435 | * are first delivered here before they are passed to the device policy, the |
| 436 | * input method, or applications. |
| 437 | * <p> |
| 438 | * <strong>Note:</strong> It is important that key events are handled in such |
| 439 | * a way that the event stream that would be passed to the rest of the system |
| 440 | * is well-formed. For example, handling the down event but not the up event |
| 441 | * and vice versa would generate an inconsistent event stream. |
| 442 | * </p> |
| 443 | * <p> |
| 444 | * <strong>Note:</strong> The key events delivered in this method are copies |
| 445 | * and modifying them will have no effect on the events that will be passed |
| 446 | * to the system. This method is intended to perform purely filtering |
| 447 | * functionality. |
| 448 | * <p> |
| 449 | * |
| 450 | * @param event The event to be processed. |
| 451 | * @return If true then the event will be consumed and not delivered to |
| 452 | * applications, otherwise it will be delivered as usual. |
| 453 | */ |
| 454 | protected boolean onKeyEvent(KeyEvent event) { |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | /** |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 459 | * Gets the windows on the screen. This method returns only the windows |
| 460 | * that a sighted user can interact with, as opposed to all windows. |
| 461 | * For example, if there is a modal dialog shown and the user cannot touch |
| 462 | * anything behind it, then only the modal window will be reported |
| 463 | * (assuming it is the top one). For convenience the returned windows |
| 464 | * are ordered in a descending layer order, which is the windows that |
| 465 | * are higher in the Z-order are reported first. |
| 466 | * <p> |
| 467 | * <strong>Note:</strong> In order to access the windows your service has |
| 468 | * to declare the capability to retrieve window content by setting the |
| 469 | * {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} |
| 470 | * property in its meta-data. For details refer to {@link #SERVICE_META_DATA}. |
| 471 | * Also the service has to opt-in to retrieve the interactive windows by |
| 472 | * setting the {@link AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS} |
| 473 | * flag. |
| 474 | * </p> |
| 475 | * |
| 476 | * @return The windows if there are windows and the service is can retrieve |
| 477 | * them, otherwise an empty list. |
| 478 | */ |
| 479 | public List<AccessibilityWindowInfo> getWindows() { |
| 480 | return AccessibilityInteractionClient.getInstance().getWindows(mConnectionId); |
| 481 | } |
| 482 | |
| 483 | /** |
Svetoslav Ganov | 0846e29 | 2012-04-18 18:47:13 -0700 | [diff] [blame] | 484 | * Gets the root node in the currently active window if this service |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 485 | * can retrieve window content. The active window is the one that the user |
| 486 | * is currently touching or the window with input focus, if the user is not |
| 487 | * touching any window. |
| 488 | * <p> |
| 489 | * <strong>Note:</strong> In order to access the root node your service has |
| 490 | * to declare the capability to retrieve window content by setting the |
| 491 | * {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} |
| 492 | * property in its meta-data. For details refer to {@link #SERVICE_META_DATA}. |
| 493 | * </p> |
Svetoslav Ganov | 0846e29 | 2012-04-18 18:47:13 -0700 | [diff] [blame] | 494 | * |
| 495 | * @return The root node if this service can retrieve window content. |
| 496 | */ |
| 497 | public AccessibilityNodeInfo getRootInActiveWindow() { |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 498 | return AccessibilityInteractionClient.getInstance().getRootInActiveWindow(mConnectionId); |
Svetoslav Ganov | 0846e29 | 2012-04-18 18:47:13 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 502 | * Performs a global action. Such an action can be performed |
| 503 | * at any moment regardless of the current application or user |
| 504 | * location in that application. For example going back, going |
| 505 | * home, opening recents, etc. |
| 506 | * |
| 507 | * @param action The action to perform. |
| 508 | * @return Whether the action was successfully performed. |
| 509 | * |
| 510 | * @see #GLOBAL_ACTION_BACK |
| 511 | * @see #GLOBAL_ACTION_HOME |
| 512 | * @see #GLOBAL_ACTION_NOTIFICATIONS |
| 513 | * @see #GLOBAL_ACTION_RECENTS |
| 514 | */ |
| 515 | public final boolean performGlobalAction(int action) { |
| 516 | IAccessibilityServiceConnection connection = |
| 517 | AccessibilityInteractionClient.getInstance().getConnection(mConnectionId); |
| 518 | if (connection != null) { |
| 519 | try { |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 520 | return connection.performGlobalAction(action); |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 521 | } catch (RemoteException re) { |
| 522 | Log.w(LOG_TAG, "Error while calling performGlobalAction", re); |
| 523 | } |
| 524 | } |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | /** |
Svetoslav | 1e0d4af | 2014-04-10 17:41:29 -0700 | [diff] [blame] | 529 | * Find the view that has the specified focus type. The search is performed |
| 530 | * across all windows. |
| 531 | * <p> |
| 532 | * <strong>Note:</strong> In order to access the windows your service has |
| 533 | * to declare the capability to retrieve window content by setting the |
| 534 | * {@link android.R.styleable#AccessibilityService_canRetrieveWindowContent} |
| 535 | * property in its meta-data. For details refer to {@link #SERVICE_META_DATA}. |
| 536 | * Also the service has to opt-in to retrieve the interactive windows by |
| 537 | * setting the {@link AccessibilityServiceInfo#FLAG_RETRIEVE_INTERACTIVE_WINDOWS} |
| 538 | * flag.Otherwise, the search will be performed only in the active window. |
| 539 | * </p> |
| 540 | * |
| 541 | * @param focus The focus to find. One of {@link AccessibilityNodeInfo#FOCUS_INPUT} or |
| 542 | * {@link AccessibilityNodeInfo#FOCUS_ACCESSIBILITY}. |
| 543 | * @return The node info of the focused view or null. |
| 544 | * |
| 545 | * @see AccessibilityNodeInfo#FOCUS_INPUT |
| 546 | * @see AccessibilityNodeInfo#FOCUS_ACCESSIBILITY |
| 547 | */ |
| 548 | public AccessibilityNodeInfo findFocus(int focus) { |
| 549 | return AccessibilityInteractionClient.getInstance().findFocus(mConnectionId, |
| 550 | AccessibilityNodeInfo.ANY_WINDOW_ID, AccessibilityNodeInfo.ROOT_NODE_ID, focus); |
| 551 | } |
| 552 | |
| 553 | /** |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 554 | * Gets the an {@link AccessibilityServiceInfo} describing this |
| 555 | * {@link AccessibilityService}. This method is useful if one wants |
| 556 | * to change some of the dynamically configurable properties at |
| 557 | * runtime. |
| 558 | * |
| 559 | * @return The accessibility service info. |
| 560 | * |
Svetoslav | bbfa585 | 2013-02-11 19:38:12 -0800 | [diff] [blame] | 561 | * @see AccessibilityServiceInfo |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 562 | */ |
| 563 | public final AccessibilityServiceInfo getServiceInfo() { |
| 564 | IAccessibilityServiceConnection connection = |
| 565 | AccessibilityInteractionClient.getInstance().getConnection(mConnectionId); |
| 566 | if (connection != null) { |
| 567 | try { |
| 568 | return connection.getServiceInfo(); |
| 569 | } catch (RemoteException re) { |
| 570 | Log.w(LOG_TAG, "Error while getting AccessibilityServiceInfo", re); |
| 571 | } |
| 572 | } |
| 573 | return null; |
| 574 | } |
| 575 | |
| 576 | /** |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 577 | * Sets the {@link AccessibilityServiceInfo} that describes this service. |
| 578 | * <p> |
| 579 | * Note: You can call this method any time but the info will be picked up after |
| 580 | * the system has bound to this service and when this method is called thereafter. |
| 581 | * |
| 582 | * @param info The info. |
| 583 | */ |
| 584 | public final void setServiceInfo(AccessibilityServiceInfo info) { |
| 585 | mInfo = info; |
| 586 | sendServiceInfo(); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Sets the {@link AccessibilityServiceInfo} for this service if the latter is |
| 591 | * properly set and there is an {@link IAccessibilityServiceConnection} to the |
| 592 | * AccessibilityManagerService. |
| 593 | */ |
| 594 | private void sendServiceInfo() { |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 595 | IAccessibilityServiceConnection connection = |
| 596 | AccessibilityInteractionClient.getInstance().getConnection(mConnectionId); |
| 597 | if (mInfo != null && connection != null) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 598 | try { |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 599 | connection.setServiceInfo(mInfo); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 600 | mInfo = null; |
| 601 | AccessibilityInteractionClient.getInstance().clearCache(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 602 | } catch (RemoteException re) { |
| 603 | Log.w(LOG_TAG, "Error while setting AccessibilityServiceInfo", re); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
Dianne Hackborn | 7f20543 | 2009-07-28 00:13:47 -0700 | [diff] [blame] | 608 | /** |
| 609 | * Implement to return the implementation of the internal accessibility |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 610 | * service interface. |
Dianne Hackborn | 7f20543 | 2009-07-28 00:13:47 -0700 | [diff] [blame] | 611 | */ |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 612 | @Override |
| 613 | public final IBinder onBind(Intent intent) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 614 | return new IAccessibilityServiceClientWrapper(this, getMainLooper(), new Callbacks() { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 615 | @Override |
| 616 | public void onServiceConnected() { |
| 617 | AccessibilityService.this.onServiceConnected(); |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public void onInterrupt() { |
| 622 | AccessibilityService.this.onInterrupt(); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void onAccessibilityEvent(AccessibilityEvent event) { |
| 627 | AccessibilityService.this.onAccessibilityEvent(event); |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void onSetConnectionId( int connectionId) { |
| 632 | mConnectionId = connectionId; |
| 633 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 634 | |
| 635 | @Override |
Svetoslav Ganov | fefd20e | 2012-04-19 21:44:35 -0700 | [diff] [blame] | 636 | public boolean onGesture(int gestureId) { |
| 637 | return AccessibilityService.this.onGesture(gestureId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 638 | } |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 639 | |
| 640 | @Override |
| 641 | public boolean onKeyEvent(KeyEvent event) { |
| 642 | return AccessibilityService.this.onKeyEvent(event); |
| 643 | } |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 644 | }); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 648 | * Implements the internal {@link IAccessibilityServiceClient} interface to convert |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 649 | * incoming calls to it back to calls on an {@link AccessibilityService}. |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 650 | * |
| 651 | * @hide |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 652 | */ |
Svetoslav Ganov | 80943d8 | 2013-01-02 10:25:37 -0800 | [diff] [blame] | 653 | public static class IAccessibilityServiceClientWrapper extends IAccessibilityServiceClient.Stub |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 654 | implements HandlerCaller.Callback { |
| 655 | |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 656 | static final int NO_ID = -1; |
| 657 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 658 | private static final int DO_SET_SET_CONNECTION = 1; |
| 659 | private static final int DO_ON_INTERRUPT = 2; |
| 660 | private static final int DO_ON_ACCESSIBILITY_EVENT = 3; |
| 661 | private static final int DO_ON_GESTURE = 4; |
| 662 | private static final int DO_CLEAR_ACCESSIBILITY_CACHE = 5; |
| 663 | private static final int DO_ON_KEY_EVENT = 6; |
| 664 | private static final int DO_ON_WINDOWS_CHANGED = 7; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 665 | |
| 666 | private final HandlerCaller mCaller; |
| 667 | |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 668 | private final Callbacks mCallback; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 669 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 670 | private int mConnectionId; |
| 671 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 672 | public IAccessibilityServiceClientWrapper(Context context, Looper looper, |
| 673 | Callbacks callback) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 674 | mCallback = callback; |
Mita Yun | ed218c7 | 2012-12-06 17:18:25 -0800 | [diff] [blame] | 675 | mCaller = new HandlerCaller(context, looper, this, true /*asyncHandler*/); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 678 | public void setConnection(IAccessibilityServiceConnection connection, int connectionId) { |
| 679 | Message message = mCaller.obtainMessageIO(DO_SET_SET_CONNECTION, connectionId, |
| 680 | connection); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 681 | mCaller.sendMessage(message); |
| 682 | } |
| 683 | |
| 684 | public void onInterrupt() { |
| 685 | Message message = mCaller.obtainMessage(DO_ON_INTERRUPT); |
| 686 | mCaller.sendMessage(message); |
| 687 | } |
| 688 | |
| 689 | public void onAccessibilityEvent(AccessibilityEvent event) { |
| 690 | Message message = mCaller.obtainMessageO(DO_ON_ACCESSIBILITY_EVENT, event); |
| 691 | mCaller.sendMessage(message); |
| 692 | } |
| 693 | |
Svetoslav Ganov | 7b1e0c7 | 2012-05-13 11:57:29 -0700 | [diff] [blame] | 694 | public void onGesture(int gestureId) { |
| 695 | Message message = mCaller.obtainMessageI(DO_ON_GESTURE, gestureId); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 696 | mCaller.sendMessage(message); |
| 697 | } |
| 698 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 699 | public void clearAccessibilityCache() { |
| 700 | Message message = mCaller.obtainMessage(DO_CLEAR_ACCESSIBILITY_CACHE); |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 701 | mCaller.sendMessage(message); |
| 702 | } |
| 703 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 704 | @Override |
| 705 | public void onKeyEvent(KeyEvent event, int sequence) { |
| 706 | Message message = mCaller.obtainMessageIO(DO_ON_KEY_EVENT, sequence, event); |
| 707 | mCaller.sendMessage(message); |
| 708 | } |
| 709 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 710 | @Override |
| 711 | public void onWindowsChanged(int[] windowIds) { |
| 712 | Message message = mCaller.obtainMessageO(DO_ON_WINDOWS_CHANGED, windowIds); |
| 713 | mCaller.sendMessage(message); |
| 714 | } |
| 715 | |
| 716 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 717 | public void executeMessage(Message message) { |
| 718 | switch (message.what) { |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 719 | case DO_ON_ACCESSIBILITY_EVENT: { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 720 | AccessibilityEvent event = (AccessibilityEvent) message.obj; |
Dianne Hackborn | 7ed6ee5 | 2009-09-10 18:41:28 -0700 | [diff] [blame] | 721 | if (event != null) { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 722 | AccessibilityInteractionClient.getInstance().onAccessibilityEvent(event); |
| 723 | mCallback.onAccessibilityEvent(event); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 724 | // Make sure the event is recycled. |
| 725 | try { |
| 726 | event.recycle(); |
| 727 | } catch (IllegalStateException ise) { |
| 728 | /* ignore - best effort */ |
| 729 | } |
Charles Chen | 85f6fb7 | 2009-08-28 11:32:23 -0700 | [diff] [blame] | 730 | } |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 731 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 732 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 733 | case DO_ON_INTERRUPT: { |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 734 | mCallback.onInterrupt(); |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 735 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 736 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 737 | case DO_SET_SET_CONNECTION: { |
| 738 | mConnectionId = message.arg1; |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 739 | IAccessibilityServiceConnection connection = |
| 740 | (IAccessibilityServiceConnection) message.obj; |
| 741 | if (connection != null) { |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 742 | AccessibilityInteractionClient.getInstance().addConnection(mConnectionId, |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 743 | connection); |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 744 | mCallback.onSetConnectionId(mConnectionId); |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 745 | mCallback.onServiceConnected(); |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 746 | } else { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 747 | AccessibilityInteractionClient.getInstance().removeConnection( |
| 748 | mConnectionId); |
Svetoslav | f7e5099 | 2013-01-22 18:15:40 -0800 | [diff] [blame] | 749 | AccessibilityInteractionClient.getInstance().clearCache(); |
Svetoslav Ganov | 79311c4 | 2012-01-17 20:24:26 -0800 | [diff] [blame] | 750 | mCallback.onSetConnectionId(AccessibilityInteractionClient.NO_ID); |
Svetoslav Ganov | d116d7c | 2011-11-21 18:41:59 -0800 | [diff] [blame] | 751 | } |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 752 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 753 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 754 | case DO_ON_GESTURE: { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 755 | final int gestureId = message.arg1; |
Svetoslav Ganov | 7b1e0c7 | 2012-05-13 11:57:29 -0700 | [diff] [blame] | 756 | mCallback.onGesture(gestureId); |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 757 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 758 | |
| 759 | case DO_CLEAR_ACCESSIBILITY_CACHE: { |
Svetoslav Ganov | 152e9bb | 2012-10-12 20:15:29 -0700 | [diff] [blame] | 760 | AccessibilityInteractionClient.getInstance().clearCache(); |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 761 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 762 | |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 763 | case DO_ON_KEY_EVENT: { |
| 764 | KeyEvent event = (KeyEvent) message.obj; |
| 765 | try { |
| 766 | IAccessibilityServiceConnection connection = AccessibilityInteractionClient |
| 767 | .getInstance().getConnection(mConnectionId); |
| 768 | if (connection != null) { |
| 769 | final boolean result = mCallback.onKeyEvent(event); |
| 770 | final int sequence = message.arg1; |
| 771 | try { |
| 772 | connection.setOnKeyEventResult(result, sequence); |
| 773 | } catch (RemoteException re) { |
| 774 | /* ignore */ |
| 775 | } |
| 776 | } |
| 777 | } finally { |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 778 | // Make sure the event is recycled. |
| 779 | try { |
| 780 | event.recycle(); |
| 781 | } catch (IllegalStateException ise) { |
| 782 | /* ignore - best effort */ |
| 783 | } |
Svetoslav | c4fccd1 | 2013-04-09 12:58:41 -0700 | [diff] [blame] | 784 | } |
| 785 | } return; |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 786 | |
| 787 | case DO_ON_WINDOWS_CHANGED: { |
| 788 | final int[] windowIds = (int[]) message.obj; |
| 789 | |
| 790 | // Update the cached windows first. |
| 791 | // NOTE: The cache will hold on to the windows so do not recycle. |
| 792 | if (windowIds != null) { |
| 793 | AccessibilityInteractionClient.getInstance().removeWindows(windowIds); |
| 794 | } |
| 795 | |
| 796 | // Let the client know the windows changed. |
| 797 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 798 | AccessibilityEvent.TYPE_WINDOWS_CHANGED); |
| 799 | event.setEventTime(SystemClock.uptimeMillis()); |
| 800 | event.setSealed(true); |
| 801 | |
| 802 | mCallback.onAccessibilityEvent(event); |
| 803 | |
| 804 | // Make sure the event is recycled. |
| 805 | try { |
| 806 | event.recycle(); |
| 807 | } catch (IllegalStateException ise) { |
| 808 | /* ignore - best effort */ |
| 809 | } |
| 810 | } break; |
| 811 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 812 | default : |
| 813 | Log.w(LOG_TAG, "Unknown message type " + message.what); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | } |