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.view.accessibility; |
| 18 | |
Svetoslav | 6b25e72 | 2013-02-28 16:55:54 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 20 | import android.accessibilityservice.AccessibilityServiceInfo; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Svetoslav | 6b25e72 | 2013-02-28 16:55:54 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 23 | import android.content.pm.ServiceInfo; |
| 24 | import android.os.Binder; |
| 25 | import android.os.Handler; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
Svetoslav | 6b25e72 | 2013-02-28 16:55:54 -0800 | [diff] [blame] | 29 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
| 32 | import android.os.SystemClock; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 35 | import android.view.IWindow; |
| 36 | import android.view.View; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
| 40 | import java.util.List; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 41 | import java.util.concurrent.CopyOnWriteArrayList; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 44 | * System level service that serves as an event dispatch for {@link AccessibilityEvent}s, |
| 45 | * and provides facilities for querying the accessibility state of the system. |
| 46 | * Accessibility events are generated when something notable happens in the user interface, |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | * for example an {@link android.app.Activity} starts, the focus or selection of a |
| 48 | * {@link android.view.View} changes etc. Parties interested in handling accessibility |
| 49 | * events implement and register an accessibility service which extends |
| 50 | * {@link android.accessibilityservice.AccessibilityService}. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 51 | * <p> |
| 52 | * To obtain a handle to the accessibility manager do the following: |
| 53 | * </p> |
| 54 | * <p> |
| 55 | * <code> |
Scott Main | b303d83 | 2011-10-12 16:45:18 -0700 | [diff] [blame] | 56 | * <pre>AccessibilityManager accessibilityManager = |
| 57 | * (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);</pre> |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 58 | * </code> |
| 59 | * </p> |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 60 | * |
| 61 | * @see AccessibilityEvent |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 62 | * @see AccessibilityNodeInfo |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 63 | * @see android.accessibilityservice.AccessibilityService |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 64 | * @see Context#getSystemService |
| 65 | * @see Context#ACCESSIBILITY_SERVICE |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 66 | */ |
| 67 | public final class AccessibilityManager { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 68 | private static final boolean DEBUG = false; |
| 69 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 70 | private static final String LOG_TAG = "AccessibilityManager"; |
| 71 | |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 72 | /** @hide */ |
| 73 | public static final int STATE_FLAG_ACCESSIBILITY_ENABLED = 0x00000001; |
| 74 | |
| 75 | /** @hide */ |
| 76 | public static final int STATE_FLAG_TOUCH_EXPLORATION_ENABLED = 0x00000002; |
| 77 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 78 | static final Object sInstanceSync = new Object(); |
| 79 | |
| 80 | private static AccessibilityManager sInstance; |
| 81 | |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 82 | private static final int DO_SET_STATE = 10; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 83 | |
| 84 | final IAccessibilityManager mService; |
| 85 | |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 86 | final int mUserId; |
| 87 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 88 | final Handler mHandler; |
| 89 | |
| 90 | boolean mIsEnabled; |
| 91 | |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 92 | boolean mIsTouchExplorationEnabled; |
| 93 | |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 94 | private final CopyOnWriteArrayList<AccessibilityStateChangeListener> |
| 95 | mAccessibilityStateChangeListeners = new CopyOnWriteArrayList< |
| 96 | AccessibilityStateChangeListener>(); |
| 97 | |
| 98 | private final CopyOnWriteArrayList<TouchExplorationStateChangeListener> |
| 99 | mTouchExplorationStateChangeListeners = new CopyOnWriteArrayList< |
| 100 | TouchExplorationStateChangeListener>(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 103 | * Listener for the system accessibility state. To listen for changes to the |
| 104 | * accessibility state on the device, implement this interface and register |
| 105 | * it with the system by calling {@link #addAccessibilityStateChangeListener}. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 106 | */ |
| 107 | public interface AccessibilityStateChangeListener { |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 108 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 109 | /** |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 110 | * Called when the accessibility enabled state changes. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 111 | * |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 112 | * @param enabled Whether accessibility is enabled. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 113 | */ |
| 114 | public void onAccessibilityStateChanged(boolean enabled); |
| 115 | } |
| 116 | |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Listener for the system touch exploration state. To listen for changes to |
| 119 | * the touch exploration state on the device, implement this interface and |
| 120 | * register it with the system by calling |
| 121 | * {@link #addTouchExplorationStateChangeListener}. |
| 122 | */ |
| 123 | public interface TouchExplorationStateChangeListener { |
| 124 | |
| 125 | /** |
| 126 | * Called when the touch exploration enabled state changes. |
| 127 | * |
| 128 | * @param enabled Whether touch exploration is enabled. |
| 129 | */ |
| 130 | public void onTouchExplorationStateChanged(boolean enabled); |
| 131 | } |
| 132 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 133 | final IAccessibilityManagerClient.Stub mClient = new IAccessibilityManagerClient.Stub() { |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 134 | public void setState(int state) { |
| 135 | mHandler.obtainMessage(DO_SET_STATE, state, 0).sendToTarget(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | |
| 139 | class MyHandler extends Handler { |
| 140 | |
| 141 | MyHandler(Looper mainLooper) { |
| 142 | super(mainLooper); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void handleMessage(Message message) { |
| 147 | switch (message.what) { |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 148 | case DO_SET_STATE : |
| 149 | setState(message.arg1); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 150 | return; |
| 151 | default : |
| 152 | Log.w(LOG_TAG, "Unknown message type: " + message.what); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get an AccessibilityManager instance (create one if necessary). |
| 159 | * |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 160 | * @param context Context in which this manager operates. |
| 161 | * |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 162 | * @hide |
| 163 | */ |
| 164 | public static AccessibilityManager getInstance(Context context) { |
| 165 | synchronized (sInstanceSync) { |
| 166 | if (sInstance == null) { |
Svetoslav | 6b25e72 | 2013-02-28 16:55:54 -0800 | [diff] [blame] | 167 | final int userId; |
| 168 | if (Binder.getCallingUid() == Process.SYSTEM_UID |
| 169 | || context.checkCallingOrSelfPermission( |
| 170 | Manifest.permission.INTERACT_ACROSS_USERS) |
| 171 | == PackageManager.PERMISSION_GRANTED |
| 172 | || context.checkCallingOrSelfPermission( |
| 173 | Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 174 | == PackageManager.PERMISSION_GRANTED) { |
| 175 | userId = UserHandle.USER_CURRENT; |
| 176 | } else { |
| 177 | userId = UserHandle.myUserId(); |
| 178 | } |
| 179 | IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE); |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 180 | IAccessibilityManager service = iBinder == null |
| 181 | ? null : IAccessibilityManager.Stub.asInterface(iBinder); |
Svetoslav | 6b25e72 | 2013-02-28 16:55:54 -0800 | [diff] [blame] | 182 | sInstance = new AccessibilityManager(context, service, userId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | return sInstance; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Create an instance. |
| 190 | * |
| 191 | * @param context A {@link Context}. |
Svetoslav Ganov | af7adab | 2010-04-16 18:07:48 -0700 | [diff] [blame] | 192 | * @param service An interface to the backing service. |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 193 | * @param userId User id under which to run. |
Svetoslav Ganov | af7adab | 2010-04-16 18:07:48 -0700 | [diff] [blame] | 194 | * |
| 195 | * @hide |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 196 | */ |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 197 | public AccessibilityManager(Context context, IAccessibilityManager service, int userId) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 198 | mHandler = new MyHandler(context.getMainLooper()); |
Svetoslav Ganov | af7adab | 2010-04-16 18:07:48 -0700 | [diff] [blame] | 199 | mService = service; |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 200 | mUserId = userId; |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 201 | if (mService == null) { |
| 202 | mIsEnabled = false; |
| 203 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 204 | try { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 205 | if (mService != null) { |
| 206 | final int stateFlags = mService.addClient(mClient, userId); |
| 207 | setState(stateFlags); |
| 208 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 209 | } catch (RemoteException re) { |
| 210 | Log.e(LOG_TAG, "AccessibilityManagerService is dead", re); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 215 | * Returns if the accessibility in the system is enabled. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 216 | * |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 217 | * @return True if accessibility is enabled, false otherwise. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 218 | */ |
| 219 | public boolean isEnabled() { |
| 220 | synchronized (mHandler) { |
| 221 | return mIsEnabled; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Svetoslav Ganov | 35bfede | 2011-07-14 17:57:06 -0700 | [diff] [blame] | 226 | * Returns if the touch exploration in the system is enabled. |
| 227 | * |
| 228 | * @return True if touch exploration is enabled, false otherwise. |
| 229 | */ |
| 230 | public boolean isTouchExplorationEnabled() { |
| 231 | synchronized (mHandler) { |
| 232 | return mIsTouchExplorationEnabled; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
Svetoslav Ganov | af7adab | 2010-04-16 18:07:48 -0700 | [diff] [blame] | 237 | * Returns the client interface this instance registers in |
| 238 | * the centralized accessibility manager service. |
| 239 | * |
| 240 | * @return The client. |
| 241 | * |
| 242 | * @hide |
| 243 | */ |
| 244 | public IAccessibilityManagerClient getClient() { |
Jim Miller | 4dfecf5 | 2011-06-30 15:45:35 -0700 | [diff] [blame] | 245 | return (IAccessibilityManagerClient) mClient.asBinder(); |
Svetoslav Ganov | af7adab | 2010-04-16 18:07:48 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 249 | * Sends an {@link AccessibilityEvent}. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 250 | * |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 251 | * @param event The event to send. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 252 | * |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 253 | * @throws IllegalStateException if accessibility is not enabled. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 254 | * |
| 255 | * <strong>Note:</strong> The preferred mechanism for sending custom accessibility |
| 256 | * events is through calling |
| 257 | * {@link android.view.ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)} |
| 258 | * instead of this method to allow predecessors to augment/filter events sent by |
| 259 | * their descendants. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 260 | */ |
| 261 | public void sendAccessibilityEvent(AccessibilityEvent event) { |
| 262 | if (!mIsEnabled) { |
| 263 | throw new IllegalStateException("Accessibility off. Did you forget to check that?"); |
| 264 | } |
| 265 | boolean doRecycle = false; |
| 266 | try { |
| 267 | event.setEventTime(SystemClock.uptimeMillis()); |
| 268 | // it is possible that this manager is in the same process as the service but |
| 269 | // client using it is called through Binder from another process. Example: MMS |
| 270 | // app adds a SMS notification and the NotificationManagerService calls this method |
| 271 | long identityToken = Binder.clearCallingIdentity(); |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 272 | doRecycle = mService.sendAccessibilityEvent(event, mUserId); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 273 | Binder.restoreCallingIdentity(identityToken); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 274 | if (DEBUG) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 275 | Log.i(LOG_TAG, event + " sent"); |
| 276 | } |
| 277 | } catch (RemoteException re) { |
| 278 | Log.e(LOG_TAG, "Error during sending " + event + " ", re); |
| 279 | } finally { |
| 280 | if (doRecycle) { |
| 281 | event.recycle(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 287 | * Requests feedback interruption from all accessibility services. |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 288 | */ |
| 289 | public void interrupt() { |
| 290 | if (!mIsEnabled) { |
| 291 | throw new IllegalStateException("Accessibility off. Did you forget to check that?"); |
| 292 | } |
| 293 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 294 | mService.interrupt(mUserId); |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 295 | if (DEBUG) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 296 | Log.i(LOG_TAG, "Requested interrupt from all services"); |
| 297 | } |
| 298 | } catch (RemoteException re) { |
| 299 | Log.e(LOG_TAG, "Error while requesting interrupt from all services. ", re); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Returns the {@link ServiceInfo}s of the installed accessibility services. |
| 305 | * |
| 306 | * @return An unmodifiable list with {@link ServiceInfo}s. |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 307 | * |
| 308 | * @deprecated Use {@link #getInstalledAccessibilityServiceList()} |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 309 | */ |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 310 | @Deprecated |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 311 | public List<ServiceInfo> getAccessibilityServiceList() { |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 312 | List<AccessibilityServiceInfo> infos = getInstalledAccessibilityServiceList(); |
| 313 | List<ServiceInfo> services = new ArrayList<ServiceInfo>(); |
| 314 | final int infoCount = infos.size(); |
| 315 | for (int i = 0; i < infoCount; i++) { |
| 316 | AccessibilityServiceInfo info = infos.get(i); |
| 317 | services.add(info.getResolveInfo().serviceInfo); |
| 318 | } |
| 319 | return Collections.unmodifiableList(services); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Returns the {@link AccessibilityServiceInfo}s of the installed accessibility services. |
| 324 | * |
| 325 | * @return An unmodifiable list with {@link AccessibilityServiceInfo}s. |
| 326 | */ |
| 327 | public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList() { |
| 328 | List<AccessibilityServiceInfo> services = null; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 329 | try { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 330 | if (mService != null) { |
| 331 | services = mService.getInstalledAccessibilityServiceList(mUserId); |
| 332 | if (DEBUG) { |
| 333 | Log.i(LOG_TAG, "Installed AccessibilityServices " + services); |
| 334 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 335 | } |
| 336 | } catch (RemoteException re) { |
| 337 | Log.e(LOG_TAG, "Error while obtaining the installed AccessibilityServices. ", re); |
| 338 | } |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 339 | return services != null ? Collections.unmodifiableList(services) : Collections.EMPTY_LIST; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 343 | * Returns the {@link AccessibilityServiceInfo}s of the enabled accessibility services |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 344 | * for a given feedback type. |
| 345 | * |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 346 | * @param feedbackTypeFlags The feedback type flags. |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 347 | * @return An unmodifiable list with {@link AccessibilityServiceInfo}s. |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 348 | * |
| 349 | * @see AccessibilityServiceInfo#FEEDBACK_AUDIBLE |
| 350 | * @see AccessibilityServiceInfo#FEEDBACK_GENERIC |
| 351 | * @see AccessibilityServiceInfo#FEEDBACK_HAPTIC |
| 352 | * @see AccessibilityServiceInfo#FEEDBACK_SPOKEN |
| 353 | * @see AccessibilityServiceInfo#FEEDBACK_VISUAL |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 354 | */ |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 355 | public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList( |
| 356 | int feedbackTypeFlags) { |
Svetoslav Ganov | cc4053e | 2011-05-23 13:37:44 -0700 | [diff] [blame] | 357 | List<AccessibilityServiceInfo> services = null; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 358 | try { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 359 | if (mService != null) { |
| 360 | services = mService.getEnabledAccessibilityServiceList(feedbackTypeFlags, mUserId); |
| 361 | if (DEBUG) { |
| 362 | Log.i(LOG_TAG, "Installed AccessibilityServices " + services); |
| 363 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 364 | } |
| 365 | } catch (RemoteException re) { |
| 366 | Log.e(LOG_TAG, "Error while obtaining the installed AccessibilityServices. ", re); |
| 367 | } |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 368 | return services != null ? Collections.unmodifiableList(services) : Collections.EMPTY_LIST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 369 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
Svetoslav Ganov | 38e8b4e | 2011-06-29 20:00:53 -0700 | [diff] [blame] | 372 | * Registers an {@link AccessibilityStateChangeListener} for changes in |
| 373 | * the global accessibility state of the system. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 374 | * |
| 375 | * @param listener The listener. |
| 376 | * @return True if successfully registered. |
| 377 | */ |
| 378 | public boolean addAccessibilityStateChangeListener( |
| 379 | AccessibilityStateChangeListener listener) { |
| 380 | return mAccessibilityStateChangeListeners.add(listener); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Unregisters an {@link AccessibilityStateChangeListener}. |
| 385 | * |
| 386 | * @param listener The listener. |
| 387 | * @return True if successfully unregistered. |
| 388 | */ |
| 389 | public boolean removeAccessibilityStateChangeListener( |
| 390 | AccessibilityStateChangeListener listener) { |
| 391 | return mAccessibilityStateChangeListeners.remove(listener); |
| 392 | } |
| 393 | |
| 394 | /** |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 395 | * Registers a {@link TouchExplorationStateChangeListener} for changes in |
| 396 | * the global touch exploration state of the system. |
| 397 | * |
| 398 | * @param listener The listener. |
| 399 | * @return True if successfully registered. |
| 400 | */ |
| 401 | public boolean addTouchExplorationStateChangeListener( |
| 402 | TouchExplorationStateChangeListener listener) { |
| 403 | return mTouchExplorationStateChangeListeners.add(listener); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Unregisters a {@link TouchExplorationStateChangeListener}. |
| 408 | * |
| 409 | * @param listener The listener. |
| 410 | * @return True if successfully unregistered. |
| 411 | */ |
| 412 | public boolean removeTouchExplorationStateChangeListener( |
| 413 | TouchExplorationStateChangeListener listener) { |
| 414 | return mTouchExplorationStateChangeListeners.remove(listener); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Sets the current state and notifies listeners, if necessary. |
Svetoslav Ganov | 00aabf7 | 2011-07-21 11:35:03 -0700 | [diff] [blame] | 419 | * |
| 420 | * @param stateFlags The state flags. |
| 421 | */ |
| 422 | private void setState(int stateFlags) { |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 423 | final boolean enabled = (stateFlags & STATE_FLAG_ACCESSIBILITY_ENABLED) != 0; |
| 424 | final boolean touchExplorationEnabled = |
| 425 | (stateFlags & STATE_FLAG_TOUCH_EXPLORATION_ENABLED) != 0; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 426 | synchronized (mHandler) { |
Alan Viverette | 7e361d2 | 2013-10-09 17:20:43 -0700 | [diff] [blame] | 427 | final boolean wasEnabled = mIsEnabled; |
| 428 | final boolean wasTouchExplorationEnabled = mIsTouchExplorationEnabled; |
| 429 | |
| 430 | // Ensure listeners get current state from isZzzEnabled() calls. |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 431 | mIsEnabled = enabled; |
| 432 | mIsTouchExplorationEnabled = touchExplorationEnabled; |
| 433 | |
Alan Viverette | 7e361d2 | 2013-10-09 17:20:43 -0700 | [diff] [blame] | 434 | if (wasEnabled != enabled) { |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 435 | notifyAccessibilityStateChangedLh(); |
| 436 | } |
| 437 | |
Alan Viverette | 7e361d2 | 2013-10-09 17:20:43 -0700 | [diff] [blame] | 438 | if (wasTouchExplorationEnabled != touchExplorationEnabled) { |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 439 | notifyTouchExplorationStateChangedLh(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Notifies the registered {@link AccessibilityStateChangeListener}s. |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 446 | * <p> |
| 447 | * The caller must be locked on {@link #mHandler}. |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 448 | */ |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 449 | private void notifyAccessibilityStateChangedLh() { |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 450 | final int listenerCount = mAccessibilityStateChangeListeners.size(); |
| 451 | for (int i = 0; i < listenerCount; i++) { |
| 452 | mAccessibilityStateChangeListeners.get(i).onAccessibilityStateChanged(mIsEnabled); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
Alan Viverette | 5baeb9a | 2013-10-09 14:42:05 -0700 | [diff] [blame] | 457 | * Notifies the registered {@link TouchExplorationStateChangeListener}s. |
| 458 | * <p> |
| 459 | * The caller must be locked on {@link #mHandler}. |
| 460 | */ |
| 461 | private void notifyTouchExplorationStateChangedLh() { |
| 462 | final int listenerCount = mTouchExplorationStateChangeListeners.size(); |
| 463 | for (int i = 0; i < listenerCount; i++) { |
| 464 | mTouchExplorationStateChangeListeners.get(i) |
| 465 | .onTouchExplorationStateChanged(mIsTouchExplorationEnabled); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 470 | * Adds an accessibility interaction connection interface for a given window. |
| 471 | * @param windowToken The window token to which a connection is added. |
| 472 | * @param connection The connection. |
| 473 | * |
| 474 | * @hide |
| 475 | */ |
| 476 | public int addAccessibilityInteractionConnection(IWindow windowToken, |
| 477 | IAccessibilityInteractionConnection connection) { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 478 | if (mService == null) { |
| 479 | return View.NO_ID; |
| 480 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 481 | try { |
Svetoslav Ganov | 58d37b5 | 2012-09-18 12:04:19 -0700 | [diff] [blame] | 482 | return mService.addAccessibilityInteractionConnection(windowToken, connection, mUserId); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 483 | } catch (RemoteException re) { |
| 484 | Log.e(LOG_TAG, "Error while adding an accessibility interaction connection. ", re); |
| 485 | } |
| 486 | return View.NO_ID; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Removed an accessibility interaction connection interface for a given window. |
| 491 | * @param windowToken The window token to which a connection is removed. |
| 492 | * |
| 493 | * @hide |
| 494 | */ |
| 495 | public void removeAccessibilityInteractionConnection(IWindow windowToken) { |
| 496 | try { |
Amith Yamasani | 9158825 | 2013-11-22 08:25:26 -0800 | [diff] [blame] | 497 | if (mService != null) { |
| 498 | mService.removeAccessibilityInteractionConnection(windowToken); |
| 499 | } |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 500 | } catch (RemoteException re) { |
| 501 | Log.e(LOG_TAG, "Error while removing an accessibility interaction connection. ", re); |
| 502 | } |
| 503 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 504 | } |