2 ** Copyright 2009, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.accessibility;
19 import static android.accessibilityservice.AccessibilityServiceInfo.DEFAULT;
21 import android.Manifest;
22 import android.accessibilityservice.AccessibilityService;
23 import android.accessibilityservice.AccessibilityServiceInfo;
24 import android.accessibilityservice.GestureDescription;
25 import android.accessibilityservice.IAccessibilityServiceClient;
26 import android.accessibilityservice.IAccessibilityServiceConnection;
27 import android.annotation.NonNull;
28 import android.app.AlertDialog;
29 import android.app.PendingIntent;
30 import android.app.StatusBarManager;
31 import android.app.UiAutomation;
32 import android.content.BroadcastReceiver;
33 import android.content.ComponentName;
34 import android.content.ContentResolver;
35 import android.content.Context;
36 import android.content.DialogInterface;
37 import android.content.DialogInterface.OnClickListener;
38 import android.content.Intent;
39 import android.content.IntentFilter;
40 import android.content.ServiceConnection;
41 import android.content.pm.PackageManager;
42 import android.content.pm.ParceledListSlice;
43 import android.content.pm.ResolveInfo;
44 import android.content.pm.ServiceInfo;
45 import android.content.pm.UserInfo;
46 import android.database.ContentObserver;
47 import android.graphics.Point;
48 import android.graphics.Rect;
49 import android.graphics.Region;
50 import android.hardware.display.DisplayManager;
51 import android.hardware.input.InputManager;
52 import android.net.Uri;
53 import android.os.Binder;
54 import android.os.Build;
55 import android.os.Bundle;
56 import android.os.Handler;
57 import android.os.IBinder;
58 import android.os.Looper;
59 import android.os.Message;
60 import android.os.PowerManager;
61 import android.os.Process;
62 import android.os.RemoteCallbackList;
63 import android.os.RemoteException;
64 import android.os.SystemClock;
65 import android.os.UserHandle;
66 import android.os.UserManager;
67 import android.provider.Settings;
68 import android.text.TextUtils;
69 import android.text.TextUtils.SimpleStringSplitter;
70 import android.util.Slog;
71 import android.util.SparseArray;
72 import android.view.Display;
73 import android.view.IWindow;
74 import android.view.InputDevice;
75 import android.view.KeyCharacterMap;
76 import android.view.KeyEvent;
77 import android.view.MagnificationSpec;
78 import android.view.MotionEvent;
79 import android.view.WindowInfo;
80 import android.view.WindowManager;
81 import android.view.WindowManagerInternal;
82 import android.view.accessibility.AccessibilityEvent;
83 import android.view.accessibility.AccessibilityInteractionClient;
84 import android.view.accessibility.AccessibilityManager;
85 import android.view.accessibility.AccessibilityNodeInfo;
86 import android.view.accessibility.AccessibilityWindowInfo;
87 import android.view.accessibility.IAccessibilityInteractionConnection;
88 import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
89 import android.view.accessibility.IAccessibilityManager;
90 import android.view.accessibility.IAccessibilityManagerClient;
92 import com.android.internal.R;
93 import com.android.internal.content.PackageMonitor;
94 import com.android.internal.os.SomeArgs;
95 import com.android.server.LocalServices;
97 import com.android.server.statusbar.StatusBarManagerInternal;
98 import org.xmlpull.v1.XmlPullParserException;
100 import java.io.FileDescriptor;
101 import java.io.IOException;
102 import java.io.PrintWriter;
103 import java.util.ArrayList;
104 import java.util.Arrays;
105 import java.util.Collections;
106 import java.util.HashMap;
107 import java.util.HashSet;
108 import java.util.Iterator;
109 import java.util.List;
110 import java.util.Map;
111 import java.util.Set;
112 import java.util.concurrent.CopyOnWriteArrayList;
115 * This class is instantiated by the system as a system level service and can be
116 * accessed only by the system. The task of this service is to be a centralized
117 * event dispatch for {@link AccessibilityEvent}s generated across all processes
118 * on the device. Events are dispatched to {@link AccessibilityService}s.
120 public class AccessibilityManagerService extends IAccessibilityManager.Stub {
122 private static final boolean DEBUG = false;
124 private static final String LOG_TAG = "AccessibilityManagerService";
126 // TODO: This is arbitrary. When there is time implement this by watching
127 // when that accessibility services are bound.
128 private static final int WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS = 3000;
130 private static final int WAIT_WINDOWS_TIMEOUT_MILLIS = 5000;
132 // TODO: Restructure service initialization so services aren't connected before all of
133 // their capabilities are ready.
134 private static final int WAIT_MOTION_INJECTOR_TIMEOUT_MILLIS = 1000;
136 private static final String FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE =
137 "registerUiTestAutomationService";
139 private static final String TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED =
140 "temporaryEnableAccessibilityStateUntilKeyguardRemoved";
142 private static final String GET_WINDOW_TOKEN = "getWindowToken";
144 private static final ComponentName sFakeAccessibilityServiceComponentName =
145 new ComponentName("foo.bar", "FakeService");
147 private static final String FUNCTION_DUMP = "dump";
149 private static final char COMPONENT_NAME_SEPARATOR = ':';
151 private static final int OWN_PROCESS_ID = android.os.Process.myPid();
153 private static final int WINDOW_ID_UNKNOWN = -1;
155 // Each service has an ID. Also provide one for magnification gesture handling
156 public static final int MAGNIFICATION_GESTURE_HANDLER_ID = 0;
158 private static int sIdCounter = MAGNIFICATION_GESTURE_HANDLER_ID + 1;
160 private static int sNextWindowId;
162 private final Context mContext;
164 private final Object mLock = new Object();
166 private final SimpleStringSplitter mStringColonSplitter =
167 new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
169 private final List<AccessibilityServiceInfo> mEnabledServicesForFeedbackTempList =
172 private final Rect mTempRect = new Rect();
174 private final Rect mTempRect1 = new Rect();
176 private final Point mTempPoint = new Point();
178 private final PackageManager mPackageManager;
180 private final PowerManager mPowerManager;
182 private final WindowManagerInternal mWindowManagerService;
184 private final SecurityPolicy mSecurityPolicy;
186 private final MainHandler mMainHandler;
188 private MagnificationController mMagnificationController;
190 private InteractionBridge mInteractionBridge;
192 private AlertDialog mEnableTouchExplorationDialog;
194 private AccessibilityInputFilter mInputFilter;
196 private boolean mHasInputFilter;
198 private KeyEventDispatcher mKeyEventDispatcher;
200 private MotionEventInjector mMotionEventInjector;
202 private final Set<ComponentName> mTempComponentNameSet = new HashSet<>();
204 private final List<AccessibilityServiceInfo> mTempAccessibilityServiceInfoList =
207 private final RemoteCallbackList<IAccessibilityManagerClient> mGlobalClients =
208 new RemoteCallbackList<>();
210 private final SparseArray<AccessibilityConnectionWrapper> mGlobalInteractionConnections =
213 private final SparseArray<IBinder> mGlobalWindowTokens = new SparseArray<>();
215 private final SparseArray<UserState> mUserStates = new SparseArray<>();
217 private final UserManager mUserManager;
219 private int mCurrentUserId = UserHandle.USER_SYSTEM;
221 //TODO: Remove this hack
222 private boolean mInitialized;
224 private WindowsForAccessibilityCallback mWindowsForAccessibilityCallback;
226 private UserState getCurrentUserStateLocked() {
227 return getUserStateLocked(mCurrentUserId);
231 * Creates a new instance.
233 * @param context A {@link Context} instance.
235 public AccessibilityManagerService(Context context) {
237 mPackageManager = mContext.getPackageManager();
238 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
239 mWindowManagerService = LocalServices.getService(WindowManagerInternal.class);
240 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
241 mSecurityPolicy = new SecurityPolicy();
242 mMainHandler = new MainHandler(mContext.getMainLooper());
243 registerBroadcastReceivers();
244 new AccessibilityContentObserver(mMainHandler).register(
245 context.getContentResolver());
248 private UserState getUserStateLocked(int userId) {
249 UserState state = mUserStates.get(userId);
251 state = new UserState(userId);
252 mUserStates.put(userId, state);
257 private void registerBroadcastReceivers() {
258 PackageMonitor monitor = new PackageMonitor() {
260 public void onSomePackagesChanged() {
261 synchronized (mLock) {
262 // Only the profile parent can install accessibility services.
263 // Therefore we ignore packages from linked profiles.
264 if (getChangingUserId() != mCurrentUserId) {
267 // We will update when the automation service dies.
268 UserState userState = getCurrentUserStateLocked();
269 // We have to reload the installed services since some services may
270 // have different attributes, resolve info (does not support equals),
271 // etc. Remove them then to force reload.
272 userState.mInstalledServices.clear();
273 if (!userState.isUiAutomationSuppressingOtherServices()) {
274 if (readConfigurationForUserStateLocked(userState)) {
275 onUserStateChangedLocked(userState);
282 public void onPackageRemoved(String packageName, int uid) {
283 synchronized (mLock) {
284 final int userId = getChangingUserId();
285 // Only the profile parent can install accessibility services.
286 // Therefore we ignore packages from linked profiles.
287 if (userId != mCurrentUserId) {
290 UserState userState = getUserStateLocked(userId);
291 Iterator<ComponentName> it = userState.mEnabledServices.iterator();
292 while (it.hasNext()) {
293 ComponentName comp = it.next();
294 String compPkg = comp.getPackageName();
295 if (compPkg.equals(packageName)) {
297 // Update the enabled services setting.
298 persistComponentNamesToSettingLocked(
299 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
300 userState.mEnabledServices, userId);
301 // Update the touch exploration granted services setting.
302 userState.mTouchExplorationGrantedServices.remove(comp);
303 persistComponentNamesToSettingLocked(
305 TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
306 userState.mTouchExplorationGrantedServices, userId);
307 // We will update when the automation service dies.
308 if (!userState.isUiAutomationSuppressingOtherServices()) {
309 onUserStateChangedLocked(userState);
318 public boolean onHandleForceStop(Intent intent, String[] packages,
319 int uid, boolean doit) {
320 synchronized (mLock) {
321 final int userId = getChangingUserId();
322 // Only the profile parent can install accessibility services.
323 // Therefore we ignore packages from linked profiles.
324 if (userId != mCurrentUserId) {
327 UserState userState = getUserStateLocked(userId);
328 Iterator<ComponentName> it = userState.mEnabledServices.iterator();
329 while (it.hasNext()) {
330 ComponentName comp = it.next();
331 String compPkg = comp.getPackageName();
332 for (String pkg : packages) {
333 if (compPkg.equals(pkg)) {
338 persistComponentNamesToSettingLocked(
339 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
340 userState.mEnabledServices, userId);
341 // We will update when the automation service dies.
342 if (!userState.isUiAutomationSuppressingOtherServices()) {
343 onUserStateChangedLocked(userState);
354 monitor.register(mContext, null, UserHandle.ALL, true);
356 // user change and unlock
357 IntentFilter intentFilter = new IntentFilter();
358 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
359 intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
360 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
361 intentFilter.addAction(Intent.ACTION_USER_PRESENT);
362 intentFilter.addAction(Intent.ACTION_SETTING_RESTORED);
364 mContext.registerReceiverAsUser(new BroadcastReceiver() {
366 public void onReceive(Context context, Intent intent) {
367 String action = intent.getAction();
368 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
369 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
370 } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
371 unlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
372 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
373 removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
374 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
375 // We will update when the automation service dies.
376 UserState userState = getCurrentUserStateLocked();
377 if (!userState.isUiAutomationSuppressingOtherServices()) {
378 if (readConfigurationForUserStateLocked(userState)) {
379 onUserStateChangedLocked(userState);
382 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
383 final String which = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
384 if (Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(which)) {
385 synchronized (mLock) {
386 restoreEnabledAccessibilityServicesLocked(
387 intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE),
388 intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE));
393 }, UserHandle.ALL, intentFilter, null, null);
397 public int addClient(IAccessibilityManagerClient client, int userId) {
398 synchronized (mLock) {
399 // We treat calls from a profile as if made by its parent as profiles
400 // share the accessibility state of the parent. The call below
401 // performs the current profile parent resolution.
402 final int resolvedUserId = mSecurityPolicy
403 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
404 // If the client is from a process that runs across users such as
405 // the system UI or the system we add it to the global state that
406 // is shared across users.
407 UserState userState = getUserStateLocked(resolvedUserId);
408 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
409 mGlobalClients.register(client);
411 Slog.i(LOG_TAG, "Added global client for pid:" + Binder.getCallingPid());
413 return userState.getClientState();
415 userState.mClients.register(client);
416 // If this client is not for the current user we do not
417 // return a state since it is not for the foreground user.
418 // We will send the state to the client on a user switch.
420 Slog.i(LOG_TAG, "Added user client for pid:" + Binder.getCallingPid()
421 + " and userId:" + mCurrentUserId);
423 return (resolvedUserId == mCurrentUserId) ? userState.getClientState() : 0;
429 public boolean sendAccessibilityEvent(AccessibilityEvent event, int userId) {
430 synchronized (mLock) {
431 // We treat calls from a profile as if made by its parent as profiles
432 // share the accessibility state of the parent. The call below
433 // performs the current profile parent resolution..
434 final int resolvedUserId = mSecurityPolicy
435 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
436 // This method does nothing for a background user.
437 if (resolvedUserId != mCurrentUserId) {
438 return true; // yes, recycle the event
440 if (mSecurityPolicy.canDispatchAccessibilityEventLocked(event)) {
441 mSecurityPolicy.updateActiveAndAccessibilityFocusedWindowLocked(event.getWindowId(),
442 event.getSourceNodeId(), event.getEventType(), event.getAction());
443 mSecurityPolicy.updateEventSourceLocked(event);
444 notifyAccessibilityServicesDelayedLocked(event, false);
445 notifyAccessibilityServicesDelayedLocked(event, true);
447 if (mHasInputFilter && mInputFilter != null) {
448 mMainHandler.obtainMessage(MainHandler.MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER,
449 AccessibilityEvent.obtain(event)).sendToTarget();
453 return (OWN_PROCESS_ID != Binder.getCallingPid());
457 public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList(int userId) {
458 synchronized (mLock) {
459 // We treat calls from a profile as if made by its parent as profiles
460 // share the accessibility state of the parent. The call below
461 // performs the current profile parent resolution.
462 final int resolvedUserId = mSecurityPolicy
463 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
464 // The automation service is a fake one and should not be reported
465 // to clients as being installed - it really is not.
466 UserState userState = getUserStateLocked(resolvedUserId);
467 if (userState.mUiAutomationService != null) {
468 List<AccessibilityServiceInfo> installedServices = new ArrayList<>();
469 installedServices.addAll(userState.mInstalledServices);
470 installedServices.remove(userState.mUiAutomationService.mAccessibilityServiceInfo);
471 return installedServices;
473 return userState.mInstalledServices;
478 public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType,
480 List<AccessibilityServiceInfo> result = null;
481 synchronized (mLock) {
482 // We treat calls from a profile as if made by its parent as profiles
483 // share the accessibility state of the parent. The call below
484 // performs the current profile parent resolution.
485 final int resolvedUserId = mSecurityPolicy
486 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
488 // The automation service can suppress other services.
489 UserState userState = getUserStateLocked(resolvedUserId);
490 if (userState.isUiAutomationSuppressingOtherServices()) {
491 return Collections.emptyList();
494 result = mEnabledServicesForFeedbackTempList;
496 List<Service> services = userState.mBoundServices;
497 while (feedbackType != 0) {
498 final int feedbackTypeBit = (1 << Integer.numberOfTrailingZeros(feedbackType));
499 feedbackType &= ~feedbackTypeBit;
500 final int serviceCount = services.size();
501 for (int i = 0; i < serviceCount; i++) {
502 Service service = services.get(i);
503 // Don't report the UIAutomation (fake service)
504 if (!sFakeAccessibilityServiceComponentName.equals(service.mComponentName)
505 && (service.mFeedbackType & feedbackTypeBit) != 0) {
506 result.add(service.mAccessibilityServiceInfo);
515 public void interrupt(int userId) {
516 CopyOnWriteArrayList<Service> services;
517 synchronized (mLock) {
518 // We treat calls from a profile as if made by its parent as profiles
519 // share the accessibility state of the parent. The call below
520 // performs the current profile parent resolution.
521 final int resolvedUserId = mSecurityPolicy
522 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
523 // This method does nothing for a background user.
524 if (resolvedUserId != mCurrentUserId) {
527 services = getUserStateLocked(resolvedUserId).mBoundServices;
529 for (int i = 0, count = services.size(); i < count; i++) {
530 Service service = services.get(i);
532 service.mServiceInterface.onInterrupt();
533 } catch (RemoteException re) {
534 Slog.e(LOG_TAG, "Error during sending interrupt request to "
535 + service.mService, re);
541 public int addAccessibilityInteractionConnection(IWindow windowToken,
542 IAccessibilityInteractionConnection connection, int userId) throws RemoteException {
543 synchronized (mLock) {
544 // We treat calls from a profile as if made by its parent as profiles
545 // share the accessibility state of the parent. The call below
546 // performs the current profile parent resolution.
547 final int resolvedUserId = mSecurityPolicy
548 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
549 final int windowId = sNextWindowId++;
550 // If the window is from a process that runs across users such as
551 // the system UI or the system we add it to the global state that
552 // is shared across users.
553 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
554 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
555 windowId, connection, UserHandle.USER_ALL);
556 wrapper.linkToDeath();
557 mGlobalInteractionConnections.put(windowId, wrapper);
558 mGlobalWindowTokens.put(windowId, windowToken.asBinder());
560 Slog.i(LOG_TAG, "Added global connection for pid:" + Binder.getCallingPid()
561 + " with windowId: " + windowId + " and token: " + windowToken.asBinder());
564 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
565 windowId, connection, resolvedUserId);
566 wrapper.linkToDeath();
567 UserState userState = getUserStateLocked(resolvedUserId);
568 userState.mInteractionConnections.put(windowId, wrapper);
569 userState.mWindowTokens.put(windowId, windowToken.asBinder());
571 Slog.i(LOG_TAG, "Added user connection for pid:" + Binder.getCallingPid()
572 + " with windowId: " + windowId + " and userId:" + mCurrentUserId
573 + " and token: " + windowToken.asBinder());
581 public void removeAccessibilityInteractionConnection(IWindow window) {
582 synchronized (mLock) {
583 // We treat calls from a profile as if made by its parent as profiles
584 // share the accessibility state of the parent. The call below
585 // performs the current profile parent resolution.
586 mSecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(
587 UserHandle.getCallingUserId());
588 IBinder token = window.asBinder();
589 final int removedWindowId = removeAccessibilityInteractionConnectionInternalLocked(
590 token, mGlobalWindowTokens, mGlobalInteractionConnections);
591 if (removedWindowId >= 0) {
593 Slog.i(LOG_TAG, "Removed global connection for pid:" + Binder.getCallingPid()
594 + " with windowId: " + removedWindowId + " and token: " + window.asBinder());
598 final int userCount = mUserStates.size();
599 for (int i = 0; i < userCount; i++) {
600 UserState userState = mUserStates.valueAt(i);
601 final int removedWindowIdForUser =
602 removeAccessibilityInteractionConnectionInternalLocked(
603 token, userState.mWindowTokens, userState.mInteractionConnections);
604 if (removedWindowIdForUser >= 0) {
606 Slog.i(LOG_TAG, "Removed user connection for pid:" + Binder.getCallingPid()
607 + " with windowId: " + removedWindowIdForUser + " and userId:"
608 + mUserStates.keyAt(i) + " and token: " + window.asBinder());
616 private int removeAccessibilityInteractionConnectionInternalLocked(IBinder windowToken,
617 SparseArray<IBinder> windowTokens,
618 SparseArray<AccessibilityConnectionWrapper> interactionConnections) {
619 final int count = windowTokens.size();
620 for (int i = 0; i < count; i++) {
621 if (windowTokens.valueAt(i) == windowToken) {
622 final int windowId = windowTokens.keyAt(i);
623 windowTokens.removeAt(i);
624 AccessibilityConnectionWrapper wrapper = interactionConnections.get(windowId);
625 wrapper.unlinkToDeath();
626 interactionConnections.remove(windowId);
634 public void registerUiTestAutomationService(IBinder owner,
635 IAccessibilityServiceClient serviceClient,
636 AccessibilityServiceInfo accessibilityServiceInfo,
638 mSecurityPolicy.enforceCallingPermission(Manifest.permission.RETRIEVE_WINDOW_CONTENT,
639 FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE);
641 accessibilityServiceInfo.setComponentName(sFakeAccessibilityServiceComponentName);
643 synchronized (mLock) {
644 UserState userState = getCurrentUserStateLocked();
646 if (userState.mUiAutomationService != null) {
647 throw new IllegalStateException("UiAutomationService " + serviceClient
648 + "already registered!");
652 owner.linkToDeath(userState.mUiAutomationSerivceOnwerDeathRecipient, 0);
653 } catch (RemoteException re) {
654 Slog.e(LOG_TAG, "Couldn't register for the death of a"
655 + " UiTestAutomationService!", re);
659 userState.mUiAutomationServiceOwner = owner;
660 userState.mUiAutomationServiceClient = serviceClient;
661 userState.mUiAutomationFlags = flags;
662 userState.mInstalledServices.add(accessibilityServiceInfo);
663 if ((flags & UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES) == 0) {
664 // Set the temporary state, and use it instead of settings
665 userState.mIsTouchExplorationEnabled = false;
666 userState.mIsEnhancedWebAccessibilityEnabled = false;
667 userState.mIsDisplayMagnificationEnabled = false;
668 userState.mIsAutoclickEnabled = false;
669 userState.mEnabledServices.clear();
671 userState.mEnabledServices.add(sFakeAccessibilityServiceComponentName);
672 userState.mTouchExplorationGrantedServices.add(sFakeAccessibilityServiceComponentName);
674 // Use the new state instead of settings.
675 onUserStateChangedLocked(userState);
680 public void unregisterUiTestAutomationService(IAccessibilityServiceClient serviceClient) {
681 synchronized (mLock) {
682 UserState userState = getCurrentUserStateLocked();
683 // Automation service is not bound, so pretend it died to perform clean up.
684 if (userState.mUiAutomationService != null
685 && serviceClient != null
686 && userState.mUiAutomationService.mServiceInterface != null
687 && userState.mUiAutomationService.mServiceInterface.asBinder()
688 == serviceClient.asBinder()) {
689 userState.mUiAutomationService.binderDied();
691 throw new IllegalStateException("UiAutomationService " + serviceClient
692 + " not registered!");
698 public void temporaryEnableAccessibilityStateUntilKeyguardRemoved(
699 ComponentName service, boolean touchExplorationEnabled) {
700 mSecurityPolicy.enforceCallingPermission(
701 Manifest.permission.TEMPORARY_ENABLE_ACCESSIBILITY,
702 TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED);
703 if (!mWindowManagerService.isKeyguardLocked()) {
706 synchronized (mLock) {
707 // Set the temporary state.
708 UserState userState = getCurrentUserStateLocked();
710 // This is a nop if UI automation is enabled.
711 if (userState.isUiAutomationSuppressingOtherServices()) {
715 userState.mIsTouchExplorationEnabled = touchExplorationEnabled;
716 userState.mIsEnhancedWebAccessibilityEnabled = false;
717 userState.mIsDisplayMagnificationEnabled = false;
718 userState.mIsAutoclickEnabled = false;
719 userState.mEnabledServices.clear();
720 userState.mEnabledServices.add(service);
721 userState.mBindingServices.clear();
722 userState.mTouchExplorationGrantedServices.clear();
723 userState.mTouchExplorationGrantedServices.add(service);
725 // User the current state instead settings.
726 onUserStateChangedLocked(userState);
731 public IBinder getWindowToken(int windowId, int userId) {
732 mSecurityPolicy.enforceCallingPermission(
733 Manifest.permission.RETRIEVE_WINDOW_TOKEN,
735 synchronized (mLock) {
736 // We treat calls from a profile as if made by its parent as profiles
737 // share the accessibility state of the parent. The call below
738 // performs the current profile parent resolution.
739 final int resolvedUserId = mSecurityPolicy
740 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
741 if (resolvedUserId != mCurrentUserId) {
744 if (mSecurityPolicy.findWindowById(windowId) == null) {
747 IBinder token = mGlobalWindowTokens.get(windowId);
751 return getCurrentUserStateLocked().mWindowTokens.get(windowId);
755 boolean onGesture(int gestureId) {
756 synchronized (mLock) {
757 boolean handled = notifyGestureLocked(gestureId, false);
759 handled = notifyGestureLocked(gestureId, true);
765 boolean notifyKeyEvent(KeyEvent event, int policyFlags) {
766 synchronized (mLock) {
767 List<Service> boundServices = getCurrentUserStateLocked().mBoundServices;
768 if (boundServices.isEmpty()) {
771 return getKeyEventDispatcher().notifyKeyEventLocked(event, policyFlags, boundServices);
776 * Called by the MagnificationController when the state of display
777 * magnification changes.
779 * @param region the new magnified region, may be empty if
780 * magnification is not enabled (e.g. scale is 1)
781 * @param scale the new scale
782 * @param centerX the new screen-relative center X coordinate
783 * @param centerY the new screen-relative center Y coordinate
785 void notifyMagnificationChanged(@NonNull Region region,
786 float scale, float centerX, float centerY) {
787 synchronized (mLock) {
788 notifyMagnificationChangedLocked(region, scale, centerX, centerY);
793 * Called by AccessibilityInputFilter when it creates or destroys the motionEventInjector.
794 * Not using a getter because the AccessibilityInputFilter isn't thread-safe
796 * @param motionEventInjector The new value of the motionEventInjector. May be null.
798 void setMotionEventInjector(MotionEventInjector motionEventInjector) {
799 synchronized (mLock) {
800 mMotionEventInjector = motionEventInjector;
801 // We may be waiting on this object being set
807 * Gets a point within the accessibility focused node where we can send down
808 * and up events to perform a click.
810 * @param outPoint The click point to populate.
811 * @return Whether accessibility a click point was found and set.
813 // TODO: (multi-display) Make sure this works for multiple displays.
814 boolean getAccessibilityFocusClickPointInScreen(Point outPoint) {
815 return getInteractionBridgeLocked()
816 .getAccessibilityFocusClickPointInScreenNotLocked(outPoint);
820 * Gets the bounds of a window.
822 * @param outBounds The output to which to write the bounds.
824 boolean getWindowBounds(int windowId, Rect outBounds) {
826 synchronized (mLock) {
827 token = mGlobalWindowTokens.get(windowId);
829 token = getCurrentUserStateLocked().mWindowTokens.get(windowId);
832 mWindowManagerService.getWindowFrame(token, outBounds);
833 if (!outBounds.isEmpty()) {
839 boolean accessibilityFocusOnlyInActiveWindow() {
840 synchronized (mLock) {
841 return mWindowsForAccessibilityCallback == null;
845 int getActiveWindowId() {
846 return mSecurityPolicy.getActiveWindowId();
849 void onTouchInteractionStart() {
850 mSecurityPolicy.onTouchInteractionStart();
853 void onTouchInteractionEnd() {
854 mSecurityPolicy.onTouchInteractionEnd();
857 void onMagnificationStateChanged() {
858 notifyClearAccessibilityCacheLocked();
861 private void switchUser(int userId) {
862 synchronized (mLock) {
863 if (mCurrentUserId == userId && mInitialized) {
867 // Disconnect from services for the old user.
868 UserState oldUserState = getCurrentUserStateLocked();
869 oldUserState.onSwitchToAnotherUser();
871 // Disable the local managers for the old user.
872 if (oldUserState.mClients.getRegisteredCallbackCount() > 0) {
873 mMainHandler.obtainMessage(MainHandler.MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER,
874 oldUserState.mUserId, 0).sendToTarget();
877 // Announce user changes only if more that one exist.
878 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
879 final boolean announceNewUser = userManager.getUsers().size() > 1;
882 mCurrentUserId = userId;
884 UserState userState = getCurrentUserStateLocked();
885 if (userState.mUiAutomationService != null) {
886 // Switching users disables the UI automation service.
887 userState.mUiAutomationService.binderDied();
890 readConfigurationForUserStateLocked(userState);
891 // Even if reading did not yield change, we have to update
892 // the state since the context in which the current user
893 // state was used has changed since it was inactive.
894 onUserStateChangedLocked(userState);
896 if (announceNewUser) {
897 // Schedule announcement of the current user if needed.
898 mMainHandler.sendEmptyMessageDelayed(MainHandler.MSG_ANNOUNCE_NEW_USER_IF_NEEDED,
899 WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS);
904 private void unlockUser(int userId) {
905 synchronized (mLock) {
906 int parentUserId = mSecurityPolicy.resolveProfileParentLocked(userId);
907 if (parentUserId == mCurrentUserId) {
908 UserState userState = getUserStateLocked(mCurrentUserId);
909 onUserStateChangedLocked(userState);
914 private void removeUser(int userId) {
915 synchronized (mLock) {
916 mUserStates.remove(userId);
920 // Called only during settings restore; currently supports only the owner user
921 // TODO: http://b/22388012
922 void restoreEnabledAccessibilityServicesLocked(String oldSetting, String newSetting) {
923 readComponentNamesFromStringLocked(oldSetting, mTempComponentNameSet, false);
924 readComponentNamesFromStringLocked(newSetting, mTempComponentNameSet, true);
926 UserState userState = getUserStateLocked(UserHandle.USER_SYSTEM);
927 userState.mEnabledServices.clear();
928 userState.mEnabledServices.addAll(mTempComponentNameSet);
929 persistComponentNamesToSettingLocked(
930 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
931 userState.mEnabledServices,
932 UserHandle.USER_SYSTEM);
933 onUserStateChangedLocked(userState);
936 private InteractionBridge getInteractionBridgeLocked() {
937 if (mInteractionBridge == null) {
938 mInteractionBridge = new InteractionBridge();
940 return mInteractionBridge;
943 private boolean notifyGestureLocked(int gestureId, boolean isDefault) {
944 // TODO: Now we are giving the gestures to the last enabled
945 // service that can handle them which is the last one
946 // in our list since we write the last enabled as the
947 // last record in the enabled services setting. Ideally,
948 // the user should make the call which service handles
949 // gestures. However, only one service should handle
950 // gestures to avoid user frustration when different
951 // behavior is observed from different combinations of
952 // enabled accessibility services.
953 UserState state = getCurrentUserStateLocked();
954 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
955 Service service = state.mBoundServices.get(i);
956 if (service.mRequestTouchExplorationMode && service.mIsDefault == isDefault) {
957 service.notifyGesture(gestureId);
964 private void notifyClearAccessibilityCacheLocked() {
965 UserState state = getCurrentUserStateLocked();
966 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
967 Service service = state.mBoundServices.get(i);
968 service.notifyClearAccessibilityNodeInfoCache();
972 private void notifyMagnificationChangedLocked(@NonNull Region region,
973 float scale, float centerX, float centerY) {
974 final UserState state = getCurrentUserStateLocked();
975 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
976 final Service service = state.mBoundServices.get(i);
977 service.notifyMagnificationChangedLocked(region, scale, centerX, centerY);
981 private void notifySoftKeyboardShowModeChangedLocked(int showMode) {
982 final UserState state = getCurrentUserStateLocked();
983 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
984 final Service service = state.mBoundServices.get(i);
985 service.notifySoftKeyboardShowModeChangedLocked(showMode);
990 * Removes an AccessibilityInteractionConnection.
992 * @param windowId The id of the window to which the connection is targeted.
993 * @param userId The id of the user owning the connection. UserHandle.USER_ALL
996 private void removeAccessibilityInteractionConnectionLocked(int windowId, int userId) {
997 if (userId == UserHandle.USER_ALL) {
998 mGlobalWindowTokens.remove(windowId);
999 mGlobalInteractionConnections.remove(windowId);
1001 UserState userState = getCurrentUserStateLocked();
1002 userState.mWindowTokens.remove(windowId);
1003 userState.mInteractionConnections.remove(windowId);
1006 Slog.i(LOG_TAG, "Removing interaction connection to windowId: " + windowId);
1010 private boolean readInstalledAccessibilityServiceLocked(UserState userState) {
1011 mTempAccessibilityServiceInfoList.clear();
1013 List<ResolveInfo> installedServices = mPackageManager.queryIntentServicesAsUser(
1014 new Intent(AccessibilityService.SERVICE_INTERFACE),
1015 PackageManager.GET_SERVICES
1016 | PackageManager.GET_META_DATA
1017 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
1018 | PackageManager.MATCH_DIRECT_BOOT_AWARE
1019 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
1022 for (int i = 0, count = installedServices.size(); i < count; i++) {
1023 ResolveInfo resolveInfo = installedServices.get(i);
1024 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
1025 if (!android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE.equals(
1026 serviceInfo.permission)) {
1027 Slog.w(LOG_TAG, "Skipping accessibilty service " + new ComponentName(
1028 serviceInfo.packageName, serviceInfo.name).flattenToShortString()
1029 + ": it does not require the permission "
1030 + android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE);
1033 AccessibilityServiceInfo accessibilityServiceInfo;
1035 accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext);
1036 mTempAccessibilityServiceInfoList.add(accessibilityServiceInfo);
1037 } catch (XmlPullParserException | IOException xppe) {
1038 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", xppe);
1042 if (!mTempAccessibilityServiceInfoList.equals(userState.mInstalledServices)) {
1043 userState.mInstalledServices.clear();
1044 userState.mInstalledServices.addAll(mTempAccessibilityServiceInfoList);
1045 mTempAccessibilityServiceInfoList.clear();
1049 mTempAccessibilityServiceInfoList.clear();
1053 private boolean readEnabledAccessibilityServicesLocked(UserState userState) {
1054 mTempComponentNameSet.clear();
1055 readComponentNamesFromSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
1056 userState.mUserId, mTempComponentNameSet);
1057 if (!mTempComponentNameSet.equals(userState.mEnabledServices)) {
1058 userState.mEnabledServices.clear();
1059 userState.mEnabledServices.addAll(mTempComponentNameSet);
1060 if (userState.mUiAutomationService != null) {
1061 userState.mEnabledServices.add(sFakeAccessibilityServiceComponentName);
1063 mTempComponentNameSet.clear();
1066 mTempComponentNameSet.clear();
1070 private boolean readTouchExplorationGrantedAccessibilityServicesLocked(
1071 UserState userState) {
1072 mTempComponentNameSet.clear();
1073 readComponentNamesFromSettingLocked(
1074 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
1075 userState.mUserId, mTempComponentNameSet);
1076 if (!mTempComponentNameSet.equals(userState.mTouchExplorationGrantedServices)) {
1077 userState.mTouchExplorationGrantedServices.clear();
1078 userState.mTouchExplorationGrantedServices.addAll(mTempComponentNameSet);
1079 mTempComponentNameSet.clear();
1082 mTempComponentNameSet.clear();
1087 * Performs {@link AccessibilityService}s delayed notification. The delay is configurable
1088 * and denotes the period after the last event before notifying the service.
1090 * @param event The event.
1091 * @param isDefault True to notify default listeners, not default services.
1093 private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event,
1094 boolean isDefault) {
1096 UserState state = getCurrentUserStateLocked();
1097 for (int i = 0, count = state.mBoundServices.size(); i < count; i++) {
1098 Service service = state.mBoundServices.get(i);
1100 if (service.mIsDefault == isDefault) {
1101 if (canDispatchEventToServiceLocked(service, event)) {
1102 service.notifyAccessibilityEvent(event);
1106 } catch (IndexOutOfBoundsException oobe) {
1107 // An out of bounds exception can happen if services are going away
1108 // as the for loop is running. If that happens, just bail because
1109 // there are no more services to notify.
1113 private void addServiceLocked(Service service, UserState userState) {
1116 userState.mBoundServices.add(service);
1117 userState.mComponentNameToServiceMap.put(service.mComponentName, service);
1118 } catch (RemoteException re) {
1124 * Removes a service.
1126 * @param service The service.
1128 private void removeServiceLocked(Service service, UserState userState) {
1129 userState.mBoundServices.remove(service);
1130 userState.mComponentNameToServiceMap.remove(service.mComponentName);
1131 service.onRemoved();
1135 * Determines if given event can be dispatched to a service based on the package of the
1136 * event source. Specifically, a service is notified if it is interested in events from the
1139 * @param service The potential receiver.
1140 * @param event The event.
1141 * @return True if the listener should be notified, false otherwise.
1143 private boolean canDispatchEventToServiceLocked(Service service, AccessibilityEvent event) {
1145 if (!service.canReceiveEventsLocked()) {
1149 if (event.getWindowId() != WINDOW_ID_UNKNOWN && !event.isImportantForAccessibility()
1150 && (service.mFetchFlags
1151 & AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) == 0) {
1155 int eventType = event.getEventType();
1156 if ((service.mEventTypes & eventType) != eventType) {
1160 Set<String> packageNames = service.mPackageNames;
1161 String packageName = (event.getPackageName() != null)
1162 ? event.getPackageName().toString() : null;
1164 return (packageNames.isEmpty() || packageNames.contains(packageName));
1167 private void unbindAllServicesLocked(UserState userState) {
1168 List<Service> services = userState.mBoundServices;
1169 for (int i = 0, count = services.size(); i < count; i++) {
1170 Service service = services.get(i);
1171 if (service.unbindLocked()) {
1179 * Populates a set with the {@link ComponentName}s stored in a colon
1180 * separated value setting for a given user.
1182 * @param settingName The setting to parse.
1183 * @param userId The user id.
1184 * @param outComponentNames The output component names.
1186 private void readComponentNamesFromSettingLocked(String settingName, int userId,
1187 Set<ComponentName> outComponentNames) {
1188 String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(),
1189 settingName, userId);
1190 readComponentNamesFromStringLocked(settingValue, outComponentNames, false);
1194 * Populates a set with the {@link ComponentName}s contained in a colon-delimited string.
1196 * @param names The colon-delimited string to parse.
1197 * @param outComponentNames The set of component names to be populated based on
1198 * the contents of the <code>names</code> string.
1199 * @param doMerge If true, the parsed component names will be merged into the output
1200 * set, rather than replacing the set's existing contents entirely.
1202 private void readComponentNamesFromStringLocked(String names,
1203 Set<ComponentName> outComponentNames,
1206 outComponentNames.clear();
1208 if (names != null) {
1209 TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
1210 splitter.setString(names);
1211 while (splitter.hasNext()) {
1212 String str = splitter.next();
1213 if (str == null || str.length() <= 0) {
1216 ComponentName enabledService = ComponentName.unflattenFromString(str);
1217 if (enabledService != null) {
1218 outComponentNames.add(enabledService);
1225 * Persists the component names in the specified setting in a
1226 * colon separated fashion.
1228 * @param settingName The setting name.
1229 * @param componentNames The component names.
1231 private void persistComponentNamesToSettingLocked(String settingName,
1232 Set<ComponentName> componentNames, int userId) {
1233 StringBuilder builder = new StringBuilder();
1234 for (ComponentName componentName : componentNames) {
1235 if (builder.length() > 0) {
1236 builder.append(COMPONENT_NAME_SEPARATOR);
1238 builder.append(componentName.flattenToShortString());
1240 final long identity = Binder.clearCallingIdentity();
1242 Settings.Secure.putStringForUser(mContext.getContentResolver(),
1243 settingName, builder.toString(), userId);
1245 Binder.restoreCallingIdentity(identity);
1249 private void updateServicesLocked(UserState userState) {
1250 Map<ComponentName, Service> componentNameToServiceMap =
1251 userState.mComponentNameToServiceMap;
1252 boolean isUnlockingOrUnlocked = mContext.getSystemService(UserManager.class)
1253 .isUserUnlockingOrUnlocked(userState.mUserId);
1255 for (int i = 0, count = userState.mInstalledServices.size(); i < count; i++) {
1256 AccessibilityServiceInfo installedService = userState.mInstalledServices.get(i);
1257 ComponentName componentName = ComponentName.unflattenFromString(
1258 installedService.getId());
1260 Service service = componentNameToServiceMap.get(componentName);
1262 // Ignore non-encryption-aware services until user is unlocked
1263 if (!isUnlockingOrUnlocked && !installedService.isDirectBootAware()) {
1264 Slog.d(LOG_TAG, "Ignoring non-encryption-aware service " + componentName);
1268 // Wait for the binding if it is in process.
1269 if (userState.mBindingServices.contains(componentName)) {
1272 if (userState.mEnabledServices.contains(componentName)) {
1273 if (service == null) {
1274 service = new Service(userState.mUserId, componentName, installedService);
1275 } else if (userState.mBoundServices.contains(service)) {
1278 service.bindLocked();
1280 if (service != null) {
1281 service.unbindLocked();
1286 updateAccessibilityEnabledSetting(userState);
1289 private void scheduleUpdateClientsIfNeededLocked(UserState userState) {
1290 final int clientState = userState.getClientState();
1291 if (userState.mLastSentClientState != clientState
1292 && (mGlobalClients.getRegisteredCallbackCount() > 0
1293 || userState.mClients.getRegisteredCallbackCount() > 0)) {
1294 userState.mLastSentClientState = clientState;
1295 mMainHandler.obtainMessage(MainHandler.MSG_SEND_STATE_TO_CLIENTS,
1296 clientState, userState.mUserId) .sendToTarget();
1300 private void scheduleUpdateInputFilter(UserState userState) {
1301 mMainHandler.obtainMessage(MainHandler.MSG_UPDATE_INPUT_FILTER, userState).sendToTarget();
1304 private void updateInputFilter(UserState userState) {
1305 boolean setInputFilter = false;
1306 AccessibilityInputFilter inputFilter = null;
1307 synchronized (mLock) {
1309 if (userState.mIsDisplayMagnificationEnabled) {
1310 flags |= AccessibilityInputFilter.FLAG_FEATURE_SCREEN_MAGNIFIER;
1312 if (userHasMagnificationServicesLocked(userState)) {
1313 flags |= AccessibilityInputFilter.FLAG_FEATURE_CONTROL_SCREEN_MAGNIFIER;
1315 // Touch exploration without accessibility makes no sense.
1316 if (userState.isHandlingAccessibilityEvents()
1317 && userState.mIsTouchExplorationEnabled) {
1318 flags |= AccessibilityInputFilter.FLAG_FEATURE_TOUCH_EXPLORATION;
1320 if (userState.mIsFilterKeyEventsEnabled) {
1321 flags |= AccessibilityInputFilter.FLAG_FEATURE_FILTER_KEY_EVENTS;
1323 if (userState.mIsAutoclickEnabled) {
1324 flags |= AccessibilityInputFilter.FLAG_FEATURE_AUTOCLICK;
1326 if (userState.mIsPerformGesturesEnabled) {
1327 flags |= AccessibilityInputFilter.FLAG_FEATURE_INJECT_MOTION_EVENTS;
1330 if (!mHasInputFilter) {
1331 mHasInputFilter = true;
1332 if (mInputFilter == null) {
1333 mInputFilter = new AccessibilityInputFilter(mContext,
1334 AccessibilityManagerService.this);
1336 inputFilter = mInputFilter;
1337 setInputFilter = true;
1339 mInputFilter.setUserAndEnabledFeatures(userState.mUserId, flags);
1341 if (mHasInputFilter) {
1342 mHasInputFilter = false;
1343 mInputFilter.setUserAndEnabledFeatures(userState.mUserId, 0);
1345 setInputFilter = true;
1349 if (setInputFilter) {
1350 mWindowManagerService.setInputFilter(inputFilter);
1354 private void showEnableTouchExplorationDialog(final Service service) {
1355 synchronized (mLock) {
1356 String label = service.mResolveInfo.loadLabel(
1357 mContext.getPackageManager()).toString();
1359 final UserState state = getCurrentUserStateLocked();
1360 if (state.mIsTouchExplorationEnabled) {
1363 if (mEnableTouchExplorationDialog != null
1364 && mEnableTouchExplorationDialog.isShowing()) {
1367 mEnableTouchExplorationDialog = new AlertDialog.Builder(mContext)
1368 .setIconAttribute(android.R.attr.alertDialogIcon)
1369 .setPositiveButton(android.R.string.ok, new OnClickListener() {
1371 public void onClick(DialogInterface dialog, int which) {
1372 // The user allowed the service to toggle touch exploration.
1373 state.mTouchExplorationGrantedServices.add(service.mComponentName);
1374 persistComponentNamesToSettingLocked(
1375 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
1376 state.mTouchExplorationGrantedServices, state.mUserId);
1377 // Enable touch exploration.
1378 UserState userState = getUserStateLocked(service.mUserId);
1379 userState.mIsTouchExplorationEnabled = true;
1380 final long identity = Binder.clearCallingIdentity();
1382 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1383 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1,
1386 Binder.restoreCallingIdentity(identity);
1388 onUserStateChangedLocked(userState);
1391 .setNegativeButton(android.R.string.cancel, new OnClickListener() {
1393 public void onClick(DialogInterface dialog, int which) {
1397 .setTitle(R.string.enable_explore_by_touch_warning_title)
1398 .setMessage(mContext.getString(
1399 R.string.enable_explore_by_touch_warning_message, label))
1401 mEnableTouchExplorationDialog.getWindow().setType(
1402 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1403 mEnableTouchExplorationDialog.getWindow().getAttributes().privateFlags
1404 |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
1405 mEnableTouchExplorationDialog.setCanceledOnTouchOutside(true);
1406 mEnableTouchExplorationDialog.show();
1411 * Called when any property of the user state has changed.
1413 * @param userState the new user state
1415 private void onUserStateChangedLocked(UserState userState) {
1416 // TODO: Remove this hack
1417 mInitialized = true;
1418 updateLegacyCapabilitiesLocked(userState);
1419 updateServicesLocked(userState);
1420 updateWindowsForAccessibilityCallbackLocked(userState);
1421 updateAccessibilityFocusBehaviorLocked(userState);
1422 updateFilterKeyEventsLocked(userState);
1423 updateTouchExplorationLocked(userState);
1424 updatePerformGesturesLocked(userState);
1425 updateEnhancedWebAccessibilityLocked(userState);
1426 updateDisplayColorAdjustmentSettingsLocked(userState);
1427 updateMagnificationLocked(userState);
1428 updateSoftKeyboardShowModeLocked(userState);
1429 scheduleUpdateInputFilter(userState);
1430 scheduleUpdateClientsIfNeededLocked(userState);
1433 private void updateAccessibilityFocusBehaviorLocked(UserState userState) {
1434 // If there is no service that can operate with interactive windows
1435 // then we keep the old behavior where a window loses accessibility
1436 // focus if it is no longer active. This still changes the behavior
1437 // for services that do not operate with interactive windows and run
1438 // at the same time as the one(s) which does. In practice however,
1439 // there is only one service that uses accessibility focus and it
1440 // is typically the one that operates with interactive windows, So,
1441 // this is fine. Note that to allow a service to work across windows
1442 // we have to allow accessibility focus stay in any of them. Sigh...
1443 List<Service> boundServices = userState.mBoundServices;
1444 final int boundServiceCount = boundServices.size();
1445 for (int i = 0; i < boundServiceCount; i++) {
1446 Service boundService = boundServices.get(i);
1447 if (boundService.canRetrieveInteractiveWindowsLocked()) {
1448 userState.mAccessibilityFocusOnlyInActiveWindow = false;
1452 userState.mAccessibilityFocusOnlyInActiveWindow = true;
1455 private void updateWindowsForAccessibilityCallbackLocked(UserState userState) {
1456 // We observe windows for accessibility only if there is at least
1457 // one bound service that can retrieve window content that specified
1458 // it is interested in accessing such windows. For services that are
1459 // binding we do an update pass after each bind event, so we run this
1460 // code and register the callback if needed.
1462 List<Service> boundServices = userState.mBoundServices;
1463 final int boundServiceCount = boundServices.size();
1464 for (int i = 0; i < boundServiceCount; i++) {
1465 Service boundService = boundServices.get(i);
1466 if (boundService.canRetrieveInteractiveWindowsLocked()) {
1467 if (mWindowsForAccessibilityCallback == null) {
1468 mWindowsForAccessibilityCallback = new WindowsForAccessibilityCallback();
1469 mWindowManagerService.setWindowsForAccessibilityCallback(
1470 mWindowsForAccessibilityCallback);
1476 if (mWindowsForAccessibilityCallback != null) {
1477 mWindowsForAccessibilityCallback = null;
1478 mWindowManagerService.setWindowsForAccessibilityCallback(null);
1479 // Drop all windows we know about.
1480 mSecurityPolicy.clearWindowsLocked();
1484 private void updateLegacyCapabilitiesLocked(UserState userState) {
1485 // Up to JB-MR1 we had a white list with services that can enable touch
1486 // exploration. When a service is first started we show a dialog to the
1487 // use to get a permission to white list the service.
1488 final int installedServiceCount = userState.mInstalledServices.size();
1489 for (int i = 0; i < installedServiceCount; i++) {
1490 AccessibilityServiceInfo serviceInfo = userState.mInstalledServices.get(i);
1491 ResolveInfo resolveInfo = serviceInfo.getResolveInfo();
1492 if ((serviceInfo.getCapabilities()
1493 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION) == 0
1494 && resolveInfo.serviceInfo.applicationInfo.targetSdkVersion
1495 <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1496 ComponentName componentName = new ComponentName(
1497 resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
1498 if (userState.mTouchExplorationGrantedServices.contains(componentName)) {
1499 serviceInfo.setCapabilities(serviceInfo.getCapabilities()
1500 | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION);
1506 private void updatePerformGesturesLocked(UserState userState) {
1507 final int serviceCount = userState.mBoundServices.size();
1508 for (int i = 0; i < serviceCount; i++) {
1509 Service service = userState.mBoundServices.get(i);
1510 if ((service.mAccessibilityServiceInfo.getCapabilities()
1511 & AccessibilityServiceInfo.CAPABILITY_CAN_PERFORM_GESTURES) != 0) {
1512 userState.mIsPerformGesturesEnabled = true;
1516 userState.mIsPerformGesturesEnabled = false;
1519 private void updateFilterKeyEventsLocked(UserState userState) {
1520 final int serviceCount = userState.mBoundServices.size();
1521 for (int i = 0; i < serviceCount; i++) {
1522 Service service = userState.mBoundServices.get(i);
1523 if (service.mRequestFilterKeyEvents
1524 && (service.mAccessibilityServiceInfo.getCapabilities()
1525 & AccessibilityServiceInfo
1526 .CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS) != 0) {
1527 userState.mIsFilterKeyEventsEnabled = true;
1531 userState.mIsFilterKeyEventsEnabled = false;
1534 private boolean readConfigurationForUserStateLocked(UserState userState) {
1535 boolean somethingChanged = readInstalledAccessibilityServiceLocked(userState);
1536 somethingChanged |= readEnabledAccessibilityServicesLocked(userState);
1537 somethingChanged |= readTouchExplorationGrantedAccessibilityServicesLocked(userState);
1538 somethingChanged |= readTouchExplorationEnabledSettingLocked(userState);
1539 somethingChanged |= readHighTextContrastEnabledSettingLocked(userState);
1540 somethingChanged |= readEnhancedWebAccessibilityEnabledChangedLocked(userState);
1541 somethingChanged |= readDisplayMagnificationEnabledSettingLocked(userState);
1542 somethingChanged |= readAutoclickEnabledSettingLocked(userState);
1543 somethingChanged |= readDisplayColorAdjustmentSettingsLocked(userState);
1545 return somethingChanged;
1548 private void updateAccessibilityEnabledSetting(UserState userState) {
1549 final long identity = Binder.clearCallingIdentity();
1551 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1552 Settings.Secure.ACCESSIBILITY_ENABLED,
1553 userState.isHandlingAccessibilityEvents() ? 1 : 0,
1556 Binder.restoreCallingIdentity(identity);
1560 private boolean readTouchExplorationEnabledSettingLocked(UserState userState) {
1561 final boolean touchExplorationEnabled = Settings.Secure.getIntForUser(
1562 mContext.getContentResolver(),
1563 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId) == 1;
1564 if (touchExplorationEnabled != userState.mIsTouchExplorationEnabled) {
1565 userState.mIsTouchExplorationEnabled = touchExplorationEnabled;
1571 private boolean readDisplayMagnificationEnabledSettingLocked(UserState userState) {
1572 final boolean displayMagnificationEnabled = Settings.Secure.getIntForUser(
1573 mContext.getContentResolver(),
1574 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
1575 0, userState.mUserId) == 1;
1576 if (displayMagnificationEnabled != userState.mIsDisplayMagnificationEnabled) {
1577 userState.mIsDisplayMagnificationEnabled = displayMagnificationEnabled;
1583 private boolean readAutoclickEnabledSettingLocked(UserState userState) {
1584 final boolean autoclickEnabled = Settings.Secure.getIntForUser(
1585 mContext.getContentResolver(),
1586 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED,
1587 0, userState.mUserId) == 1;
1588 if (autoclickEnabled != userState.mIsAutoclickEnabled) {
1589 userState.mIsAutoclickEnabled = autoclickEnabled;
1595 private boolean readEnhancedWebAccessibilityEnabledChangedLocked(UserState userState) {
1596 final boolean enhancedWeAccessibilityEnabled = Settings.Secure.getIntForUser(
1597 mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
1598 0, userState.mUserId) == 1;
1599 if (enhancedWeAccessibilityEnabled != userState.mIsEnhancedWebAccessibilityEnabled) {
1600 userState.mIsEnhancedWebAccessibilityEnabled = enhancedWeAccessibilityEnabled;
1606 private boolean readDisplayColorAdjustmentSettingsLocked(UserState userState) {
1607 final boolean displayAdjustmentsEnabled = DisplayAdjustmentUtils.hasAdjustments(mContext,
1609 if (displayAdjustmentsEnabled != userState.mHasDisplayColorAdjustment) {
1610 userState.mHasDisplayColorAdjustment = displayAdjustmentsEnabled;
1613 // If display adjustment is enabled, always assume there was a change in
1614 // the adjustment settings.
1615 return displayAdjustmentsEnabled;
1618 private boolean readHighTextContrastEnabledSettingLocked(UserState userState) {
1619 final boolean highTextContrastEnabled = Settings.Secure.getIntForUser(
1620 mContext.getContentResolver(),
1621 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED, 0,
1622 userState.mUserId) == 1;
1623 if (highTextContrastEnabled != userState.mIsTextHighContrastEnabled) {
1624 userState.mIsTextHighContrastEnabled = highTextContrastEnabled;
1630 private boolean readSoftKeyboardShowModeChangedLocked(UserState userState) {
1631 final int softKeyboardShowMode = Settings.Secure.getIntForUser(
1632 mContext.getContentResolver(),
1633 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE, 0,
1635 if (softKeyboardShowMode != userState.mSoftKeyboardShowMode) {
1636 userState.mSoftKeyboardShowMode = softKeyboardShowMode;
1642 private void updateTouchExplorationLocked(UserState userState) {
1643 boolean enabled = false;
1644 final int serviceCount = userState.mBoundServices.size();
1645 for (int i = 0; i < serviceCount; i++) {
1646 Service service = userState.mBoundServices.get(i);
1647 if (canRequestAndRequestsTouchExplorationLocked(service)) {
1652 if (enabled != userState.mIsTouchExplorationEnabled) {
1653 userState.mIsTouchExplorationEnabled = enabled;
1654 final long identity = Binder.clearCallingIdentity();
1656 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1657 Settings.Secure.TOUCH_EXPLORATION_ENABLED, enabled ? 1 : 0,
1660 Binder.restoreCallingIdentity(identity);
1665 private boolean canRequestAndRequestsTouchExplorationLocked(Service service) {
1666 // Service not ready or cannot request the feature - well nothing to do.
1667 if (!service.canReceiveEventsLocked() || !service.mRequestTouchExplorationMode) {
1670 // UI test automation service can always enable it.
1671 if (service.mIsAutomation) {
1674 if (service.mResolveInfo.serviceInfo.applicationInfo.targetSdkVersion
1675 <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1676 // Up to JB-MR1 we had a white list with services that can enable touch
1677 // exploration. When a service is first started we show a dialog to the
1678 // use to get a permission to white list the service.
1679 UserState userState = getUserStateLocked(service.mUserId);
1680 if (userState.mTouchExplorationGrantedServices.contains(service.mComponentName)) {
1682 } else if (mEnableTouchExplorationDialog == null
1683 || !mEnableTouchExplorationDialog.isShowing()) {
1684 mMainHandler.obtainMessage(
1685 MainHandler.MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG,
1686 service).sendToTarget();
1689 // Starting in JB-MR2 we request an accessibility service to declare
1690 // certain capabilities in its meta-data to allow it to enable the
1691 // corresponding features.
1692 if ((service.mAccessibilityServiceInfo.getCapabilities()
1693 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION) != 0) {
1700 private void updateEnhancedWebAccessibilityLocked(UserState userState) {
1701 boolean enabled = false;
1702 final int serviceCount = userState.mBoundServices.size();
1703 for (int i = 0; i < serviceCount; i++) {
1704 Service service = userState.mBoundServices.get(i);
1705 if (canRequestAndRequestsEnhancedWebAccessibilityLocked(service)) {
1710 if (enabled != userState.mIsEnhancedWebAccessibilityEnabled) {
1711 userState.mIsEnhancedWebAccessibilityEnabled = enabled;
1712 final long identity = Binder.clearCallingIdentity();
1714 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1715 Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION, enabled ? 1 : 0,
1718 Binder.restoreCallingIdentity(identity);
1723 private boolean canRequestAndRequestsEnhancedWebAccessibilityLocked(Service service) {
1724 if (!service.canReceiveEventsLocked() || !service.mRequestEnhancedWebAccessibility ) {
1727 if (service.mIsAutomation || (service.mAccessibilityServiceInfo.getCapabilities()
1728 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_ENHANCED_WEB_ACCESSIBILITY) != 0) {
1734 private void updateDisplayColorAdjustmentSettingsLocked(UserState userState) {
1735 DisplayAdjustmentUtils.applyAdjustments(mContext, userState.mUserId);
1738 private void updateMagnificationLocked(UserState userState) {
1739 if (userState.mUserId != mCurrentUserId) {
1743 if (userState.mIsDisplayMagnificationEnabled ||
1744 userHasListeningMagnificationServicesLocked(userState)) {
1745 // Initialize the magnification controller if necessary
1746 getMagnificationController();
1747 mMagnificationController.register();
1748 } else if (mMagnificationController != null) {
1749 mMagnificationController.unregister();
1754 * Returns whether the specified user has any services that are capable of
1755 * controlling magnification.
1757 private boolean userHasMagnificationServicesLocked(UserState userState) {
1758 final List<Service> services = userState.mBoundServices;
1759 for (int i = 0, count = services.size(); i < count; i++) {
1760 final Service service = services.get(i);
1761 if (mSecurityPolicy.canControlMagnification(service)) {
1769 * Returns whether the specified user has any services that are capable of
1770 * controlling magnification and are actively listening for magnification updates.
1772 private boolean userHasListeningMagnificationServicesLocked(UserState userState) {
1773 final List<Service> services = userState.mBoundServices;
1774 for (int i = 0, count = services.size(); i < count; i++) {
1775 final Service service = services.get(i);
1776 if (mSecurityPolicy.canControlMagnification(service)
1777 && service.mInvocationHandler.mIsMagnificationCallbackEnabled) {
1784 private void updateSoftKeyboardShowModeLocked(UserState userState) {
1785 final int userId = userState.mUserId;
1786 // Only check whether we need to reset the soft keyboard mode if it is not set to the
1788 if ((userId == mCurrentUserId) && (userState.mSoftKeyboardShowMode != 0)) {
1789 // Check whether the last Accessibility Service that changed the soft keyboard mode to
1790 // something other than the default is still enabled and, if not, remove flag and
1791 // reset to the default soft keyboard behavior.
1792 boolean serviceChangingSoftKeyboardModeIsEnabled =
1793 userState.mEnabledServices.contains(userState.mServiceChangingSoftKeyboardMode);
1795 if (!serviceChangingSoftKeyboardModeIsEnabled) {
1796 final long identity = Binder.clearCallingIdentity();
1798 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1799 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
1803 Binder.restoreCallingIdentity(identity);
1805 userState.mSoftKeyboardShowMode = 0;
1806 userState.mServiceChangingSoftKeyboardMode = null;
1807 notifySoftKeyboardShowModeChangedLocked(userState.mSoftKeyboardShowMode);
1812 private MagnificationSpec getCompatibleMagnificationSpecLocked(int windowId) {
1813 IBinder windowToken = mGlobalWindowTokens.get(windowId);
1814 if (windowToken == null) {
1815 windowToken = getCurrentUserStateLocked().mWindowTokens.get(windowId);
1817 if (windowToken != null) {
1818 return mWindowManagerService.getCompatibleMagnificationSpecForWindow(
1824 private KeyEventDispatcher getKeyEventDispatcher() {
1825 if (mKeyEventDispatcher == null) {
1826 mKeyEventDispatcher = new KeyEventDispatcher(
1827 mMainHandler, MainHandler.MSG_SEND_KEY_EVENT_TO_INPUT_FILTER, mLock,
1830 return mKeyEventDispatcher;
1834 * Enables accessibility service specified by {@param componentName} for the {@param userId}.
1836 public void enableAccessibilityService(ComponentName componentName, int userId) {
1837 synchronized(mLock) {
1838 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
1839 throw new SecurityException("only SYSTEM can call enableAccessibilityService.");
1842 SettingsStringHelper settingsHelper = new SettingsStringHelper(
1843 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userId);
1844 settingsHelper.addService(componentName);
1845 settingsHelper.writeToSettings();
1847 UserState userState = getUserStateLocked(userId);
1848 if (userState.mEnabledServices.add(componentName)) {
1849 onUserStateChangedLocked(userState);
1855 * Disables accessibility service specified by {@param componentName} for the {@param userId}.
1857 public void disableAccessibilityService(ComponentName componentName, int userId) {
1858 synchronized(mLock) {
1859 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
1860 throw new SecurityException("only SYSTEM can call disableAccessibility");
1863 SettingsStringHelper settingsHelper = new SettingsStringHelper(
1864 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userId);
1865 settingsHelper.deleteService(componentName);
1866 settingsHelper.writeToSettings();
1868 UserState userState = getUserStateLocked(userId);
1869 if (userState.mEnabledServices.remove(componentName)) {
1870 onUserStateChangedLocked(userState);
1875 private class SettingsStringHelper {
1876 private static final String SETTINGS_DELIMITER = ":";
1877 private ContentResolver mContentResolver;
1878 private final String mSettingsName;
1879 private Set<String> mServices;
1880 private final int mUserId;
1882 public SettingsStringHelper(String name, int userId) {
1884 mSettingsName = name;
1885 mContentResolver = mContext.getContentResolver();
1886 String servicesString = Settings.Secure.getStringForUser(
1887 mContentResolver, mSettingsName, userId);
1888 mServices = new HashSet();
1889 if (!TextUtils.isEmpty(servicesString)) {
1890 final TextUtils.SimpleStringSplitter colonSplitter =
1891 new TextUtils.SimpleStringSplitter(SETTINGS_DELIMITER.charAt(0));
1892 colonSplitter.setString(servicesString);
1893 while (colonSplitter.hasNext()) {
1894 final String serviceName = colonSplitter.next();
1895 mServices.add(serviceName);
1900 public void addService(ComponentName component) {
1901 mServices.add(component.flattenToString());
1904 public void deleteService(ComponentName component) {
1905 mServices.remove(component.flattenToString());
1908 public void writeToSettings() {
1909 Settings.Secure.putStringForUser(mContentResolver, mSettingsName,
1910 TextUtils.join(SETTINGS_DELIMITER, mServices), mUserId);
1915 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1916 mSecurityPolicy.enforceCallingPermission(Manifest.permission.DUMP, FUNCTION_DUMP);
1917 synchronized (mLock) {
1918 pw.println("ACCESSIBILITY MANAGER (dumpsys accessibility)");
1920 final int userCount = mUserStates.size();
1921 for (int i = 0; i < userCount; i++) {
1922 UserState userState = mUserStates.valueAt(i);
1923 pw.append("User state[attributes:{id=" + userState.mUserId);
1924 pw.append(", currentUser=" + (userState.mUserId == mCurrentUserId));
1925 pw.append(", touchExplorationEnabled=" + userState.mIsTouchExplorationEnabled);
1926 pw.append(", displayMagnificationEnabled="
1927 + userState.mIsDisplayMagnificationEnabled);
1928 pw.append(", autoclickEnabled=" + userState.mIsAutoclickEnabled);
1929 if (userState.mUiAutomationService != null) {
1931 userState.mUiAutomationService.dump(fd, pw, args);
1936 pw.append(" services:{");
1937 final int serviceCount = userState.mBoundServices.size();
1938 for (int j = 0; j < serviceCount; j++) {
1944 Service service = userState.mBoundServices.get(j);
1945 service.dump(fd, pw, args);
1950 if (mSecurityPolicy.mWindows != null) {
1951 final int windowCount = mSecurityPolicy.mWindows.size();
1952 for (int j = 0; j < windowCount; j++) {
1957 pw.append("Window[");
1958 AccessibilityWindowInfo window = mSecurityPolicy.mWindows.get(j);
1959 pw.append(window.toString());
1966 private class AccessibilityConnectionWrapper implements DeathRecipient {
1967 private final int mWindowId;
1968 private final int mUserId;
1969 private final IAccessibilityInteractionConnection mConnection;
1971 public AccessibilityConnectionWrapper(int windowId,
1972 IAccessibilityInteractionConnection connection, int userId) {
1973 mWindowId = windowId;
1975 mConnection = connection;
1978 public void linkToDeath() throws RemoteException {
1979 mConnection.asBinder().linkToDeath(this, 0);
1982 public void unlinkToDeath() {
1983 mConnection.asBinder().unlinkToDeath(this, 0);
1987 public void binderDied() {
1989 synchronized (mLock) {
1990 removeAccessibilityInteractionConnectionLocked(mWindowId, mUserId);
1995 private final class MainHandler extends Handler {
1996 public static final int MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER = 1;
1997 public static final int MSG_SEND_STATE_TO_CLIENTS = 2;
1998 public static final int MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER = 3;
1999 public static final int MSG_ANNOUNCE_NEW_USER_IF_NEEDED = 5;
2000 public static final int MSG_UPDATE_INPUT_FILTER = 6;
2001 public static final int MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG = 7;
2002 public static final int MSG_SEND_KEY_EVENT_TO_INPUT_FILTER = 8;
2003 public static final int MSG_CLEAR_ACCESSIBILITY_FOCUS = 9;
2005 public MainHandler(Looper looper) {
2010 public void handleMessage(Message msg) {
2011 final int type = msg.what;
2013 case MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER: {
2014 AccessibilityEvent event = (AccessibilityEvent) msg.obj;
2015 synchronized (mLock) {
2016 if (mHasInputFilter && mInputFilter != null) {
2017 mInputFilter.notifyAccessibilityEvent(event);
2023 case MSG_SEND_KEY_EVENT_TO_INPUT_FILTER: {
2024 KeyEvent event = (KeyEvent) msg.obj;
2025 final int policyFlags = msg.arg1;
2026 synchronized (mLock) {
2027 if (mHasInputFilter && mInputFilter != null) {
2028 mInputFilter.sendInputEvent(event, policyFlags);
2034 case MSG_SEND_STATE_TO_CLIENTS: {
2035 final int clientState = msg.arg1;
2036 final int userId = msg.arg2;
2037 sendStateToClients(clientState, mGlobalClients);
2038 sendStateToClientsForUser(clientState, userId);
2041 case MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER: {
2042 final int userId = msg.arg1;
2043 sendStateToClientsForUser(0, userId);
2046 case MSG_ANNOUNCE_NEW_USER_IF_NEEDED: {
2047 announceNewUserIfNeeded();
2050 case MSG_UPDATE_INPUT_FILTER: {
2051 UserState userState = (UserState) msg.obj;
2052 updateInputFilter(userState);
2055 case MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG: {
2056 Service service = (Service) msg.obj;
2057 showEnableTouchExplorationDialog(service);
2060 case MSG_CLEAR_ACCESSIBILITY_FOCUS: {
2061 final int windowId = msg.arg1;
2062 InteractionBridge bridge;
2063 synchronized (mLock) {
2064 bridge = getInteractionBridgeLocked();
2066 bridge.clearAccessibilityFocusNotLocked(windowId);
2071 private void announceNewUserIfNeeded() {
2072 synchronized (mLock) {
2073 UserState userState = getCurrentUserStateLocked();
2074 if (userState.isHandlingAccessibilityEvents()) {
2075 UserManager userManager = (UserManager) mContext.getSystemService(
2076 Context.USER_SERVICE);
2077 String message = mContext.getString(R.string.user_switched,
2078 userManager.getUserInfo(mCurrentUserId).name);
2079 AccessibilityEvent event = AccessibilityEvent.obtain(
2080 AccessibilityEvent.TYPE_ANNOUNCEMENT);
2081 event.getText().add(message);
2082 sendAccessibilityEvent(event, mCurrentUserId);
2087 private void sendStateToClientsForUser(int clientState, int userId) {
2088 final UserState userState;
2089 synchronized (mLock) {
2090 userState = getUserStateLocked(userId);
2092 sendStateToClients(clientState, userState.mClients);
2095 private void sendStateToClients(int clientState,
2096 RemoteCallbackList<IAccessibilityManagerClient> clients) {
2098 final int userClientCount = clients.beginBroadcast();
2099 for (int i = 0; i < userClientCount; i++) {
2100 IAccessibilityManagerClient client = clients.getBroadcastItem(i);
2102 client.setState(clientState);
2103 } catch (RemoteException re) {
2108 clients.finishBroadcast();
2113 private int findWindowIdLocked(IBinder token) {
2114 final int globalIndex = mGlobalWindowTokens.indexOfValue(token);
2115 if (globalIndex >= 0) {
2116 return mGlobalWindowTokens.keyAt(globalIndex);
2118 UserState userState = getCurrentUserStateLocked();
2119 final int userIndex = userState.mWindowTokens.indexOfValue(token);
2120 if (userIndex >= 0) {
2121 return userState.mWindowTokens.keyAt(userIndex);
2126 private void ensureWindowsAvailableTimed() {
2127 synchronized (mLock) {
2128 if (mSecurityPolicy.mWindows != null) {
2131 // If we have no registered callback, update the state we
2132 // we may have to register one but it didn't happen yet.
2133 if (mWindowsForAccessibilityCallback == null) {
2134 UserState userState = getCurrentUserStateLocked();
2135 onUserStateChangedLocked(userState);
2137 // We have no windows but do not care about them, done.
2138 if (mWindowsForAccessibilityCallback == null) {
2142 // Wait for the windows with a timeout.
2143 final long startMillis = SystemClock.uptimeMillis();
2144 while (mSecurityPolicy.mWindows == null) {
2145 final long elapsedMillis = SystemClock.uptimeMillis() - startMillis;
2146 final long remainMillis = WAIT_WINDOWS_TIMEOUT_MILLIS - elapsedMillis;
2147 if (remainMillis <= 0) {
2151 mLock.wait(remainMillis);
2152 } catch (InterruptedException ie) {
2159 MagnificationController getMagnificationController() {
2160 synchronized (mLock) {
2161 if (mMagnificationController == null) {
2162 mMagnificationController = new MagnificationController(mContext, this, mLock);
2163 mMagnificationController.setUserId(mCurrentUserId);
2165 return mMagnificationController;
2170 * This class represents an accessibility service. It stores all per service
2171 * data required for the service management, provides API for starting/stopping the
2172 * service and is responsible for adding/removing the service in the data structures
2173 * for service management. The class also exposes configuration interface that is
2174 * passed to the service it represents as soon it is bound. It also serves as the
2175 * connection for the service.
2177 class Service extends IAccessibilityServiceConnection.Stub
2178 implements ServiceConnection, DeathRecipient {;
2184 AccessibilityServiceInfo mAccessibilityServiceInfo;
2188 IAccessibilityServiceClient mServiceInterface;
2194 Set<String> mPackageNames = new HashSet<>();
2198 boolean mRequestTouchExplorationMode;
2200 boolean mRequestEnhancedWebAccessibility;
2202 boolean mRequestFilterKeyEvents;
2204 boolean mRetrieveInteractiveWindows;
2208 long mNotificationTimeout;
2210 ComponentName mComponentName;
2214 boolean mIsAutomation;
2216 final ResolveInfo mResolveInfo;
2218 final IBinder mOverlayWindowToken = new Binder();
2220 // the events pending events to be dispatched to this service
2221 final SparseArray<AccessibilityEvent> mPendingEvents =
2222 new SparseArray<>();
2224 boolean mWasConnectedAndDied;
2226 // Handler only for dispatching accessibility events since we use event
2227 // types as message types allowing us to remove messages per event type.
2228 public Handler mEventDispatchHandler = new Handler(mMainHandler.getLooper()) {
2230 public void handleMessage(Message message) {
2231 final int eventType = message.what;
2232 AccessibilityEvent event = (AccessibilityEvent) message.obj;
2233 notifyAccessibilityEventInternal(eventType, event);
2237 // Handler for scheduling method invocations on the main thread.
2238 public final InvocationHandler mInvocationHandler = new InvocationHandler(
2239 mMainHandler.getLooper());
2241 public Service(int userId, ComponentName componentName,
2242 AccessibilityServiceInfo accessibilityServiceInfo) {
2244 mResolveInfo = accessibilityServiceInfo.getResolveInfo();
2246 mComponentName = componentName;
2247 mAccessibilityServiceInfo = accessibilityServiceInfo;
2248 mIsAutomation = (sFakeAccessibilityServiceComponentName.equals(componentName));
2249 if (!mIsAutomation) {
2250 mIntent = new Intent().setComponent(mComponentName);
2251 mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
2252 com.android.internal.R.string.accessibility_binding_label);
2253 final long idendtity = Binder.clearCallingIdentity();
2255 mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
2256 mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0));
2258 Binder.restoreCallingIdentity(idendtity);
2261 setDynamicallyConfigurableProperties(accessibilityServiceInfo);
2264 public void setDynamicallyConfigurableProperties(AccessibilityServiceInfo info) {
2265 mEventTypes = info.eventTypes;
2266 mFeedbackType = info.feedbackType;
2267 String[] packageNames = info.packageNames;
2268 if (packageNames != null) {
2269 mPackageNames.addAll(Arrays.asList(packageNames));
2271 mNotificationTimeout = info.notificationTimeout;
2272 mIsDefault = (info.flags & DEFAULT) != 0;
2274 if (mIsAutomation || info.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion
2275 >= Build.VERSION_CODES.JELLY_BEAN) {
2276 if ((info.flags & AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0) {
2277 mFetchFlags |= AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
2279 mFetchFlags &= ~AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
2283 if ((info.flags & AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS) != 0) {
2284 mFetchFlags |= AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS;
2286 mFetchFlags &= ~AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS;
2289 mRequestTouchExplorationMode = (info.flags
2290 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
2291 mRequestEnhancedWebAccessibility = (info.flags
2292 & AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY) != 0;
2293 mRequestFilterKeyEvents = (info.flags
2294 & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) != 0;
2295 mRetrieveInteractiveWindows = (info.flags
2296 & AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS) != 0;
2300 * Binds to the accessibility service.
2302 * @return True if binding is successful.
2304 public boolean bindLocked() {
2305 UserState userState = getUserStateLocked(mUserId);
2306 if (!mIsAutomation) {
2307 final long identity = Binder.clearCallingIdentity();
2309 if (mService == null && mContext.bindServiceAsUser(
2311 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
2312 new UserHandle(mUserId))) {
2313 userState.mBindingServices.add(mComponentName);
2316 Binder.restoreCallingIdentity(identity);
2319 userState.mBindingServices.add(mComponentName);
2320 mService = userState.mUiAutomationServiceClient.asBinder();
2321 mMainHandler.post(new Runnable() {
2324 // Simulate asynchronous connection since in onServiceConnected
2325 // we may modify the state data in case of an error but bind is
2326 // called while iterating over the data and bad things can happen.
2327 onServiceConnected(mComponentName, mService);
2330 userState.mUiAutomationService = this;
2336 * Unbinds form the accessibility service and removes it from the data
2337 * structures for service management.
2339 * @return True if unbinding is successful.
2341 public boolean unbindLocked() {
2342 if (mService == null) {
2345 UserState userState = getUserStateLocked(mUserId);
2346 getKeyEventDispatcher().flush(this);
2347 if (!mIsAutomation) {
2348 mContext.unbindService(this);
2350 userState.destroyUiAutomationService();
2352 removeServiceLocked(this, userState);
2358 public void disableSelf() {
2359 synchronized(mLock) {
2360 UserState userState = getUserStateLocked(mUserId);
2361 if (userState.mEnabledServices.remove(mComponentName)) {
2362 final long identity = Binder.clearCallingIdentity();
2364 persistComponentNamesToSettingLocked(
2365 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
2366 userState.mEnabledServices, mUserId);
2368 Binder.restoreCallingIdentity(identity);
2370 onUserStateChangedLocked(userState);
2375 public boolean canReceiveEventsLocked() {
2376 return (mEventTypes != 0 && mFeedbackType != 0 && mService != null);
2380 public void setOnKeyEventResult(boolean handled, int sequence) {
2381 getKeyEventDispatcher().setOnKeyEventResult(this, handled, sequence);
2385 public AccessibilityServiceInfo getServiceInfo() {
2386 synchronized (mLock) {
2387 return mAccessibilityServiceInfo;
2391 public boolean canRetrieveInteractiveWindowsLocked() {
2392 return mSecurityPolicy.canRetrieveWindowContentLocked(this)
2393 && mRetrieveInteractiveWindows;
2397 public void setServiceInfo(AccessibilityServiceInfo info) {
2398 final long identity = Binder.clearCallingIdentity();
2400 synchronized (mLock) {
2401 // If the XML manifest had data to configure the service its info
2402 // should be already set. In such a case update only the dynamically
2403 // configurable properties.
2404 AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo;
2405 if (oldInfo != null) {
2406 oldInfo.updateDynamicallyConfigurableProperties(info);
2407 setDynamicallyConfigurableProperties(oldInfo);
2409 setDynamicallyConfigurableProperties(info);
2411 UserState userState = getUserStateLocked(mUserId);
2412 onUserStateChangedLocked(userState);
2415 Binder.restoreCallingIdentity(identity);
2420 public void onServiceConnected(ComponentName componentName, IBinder service) {
2421 synchronized (mLock) {
2423 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
2424 UserState userState = getUserStateLocked(mUserId);
2425 addServiceLocked(this, userState);
2426 if (userState.mBindingServices.contains(mComponentName) || mWasConnectedAndDied) {
2427 userState.mBindingServices.remove(mComponentName);
2428 mWasConnectedAndDied = false;
2430 mServiceInterface.init(this, mId, mOverlayWindowToken);
2431 onUserStateChangedLocked(userState);
2432 } catch (RemoteException re) {
2433 Slog.w(LOG_TAG, "Error while setting connection for service: "
2443 private boolean isCalledForCurrentUserLocked() {
2444 // We treat calls from a profile as if made by its parent as profiles
2445 // share the accessibility state of the parent. The call below
2446 // performs the current profile parent resolution.
2447 final int resolvedUserId = mSecurityPolicy
2448 .resolveCallingUserIdEnforcingPermissionsLocked(UserHandle.USER_CURRENT);
2449 return resolvedUserId == mCurrentUserId;
2453 public List<AccessibilityWindowInfo> getWindows() {
2454 ensureWindowsAvailableTimed();
2455 synchronized (mLock) {
2456 if (!isCalledForCurrentUserLocked()) {
2459 final boolean permissionGranted =
2460 mSecurityPolicy.canRetrieveWindowsLocked(this);
2461 if (!permissionGranted) {
2464 if (mSecurityPolicy.mWindows == null) {
2467 List<AccessibilityWindowInfo> windows = new ArrayList<>();
2468 final int windowCount = mSecurityPolicy.mWindows.size();
2469 for (int i = 0; i < windowCount; i++) {
2470 AccessibilityWindowInfo window = mSecurityPolicy.mWindows.get(i);
2471 AccessibilityWindowInfo windowClone =
2472 AccessibilityWindowInfo.obtain(window);
2473 windowClone.setConnectionId(mId);
2474 windows.add(windowClone);
2481 public AccessibilityWindowInfo getWindow(int windowId) {
2482 ensureWindowsAvailableTimed();
2483 synchronized (mLock) {
2484 if (!isCalledForCurrentUserLocked()) {
2487 final boolean permissionGranted =
2488 mSecurityPolicy.canRetrieveWindowsLocked(this);
2489 if (!permissionGranted) {
2492 AccessibilityWindowInfo window = mSecurityPolicy.findWindowById(windowId);
2493 if (window != null) {
2494 AccessibilityWindowInfo windowClone = AccessibilityWindowInfo.obtain(window);
2495 windowClone.setConnectionId(mId);
2503 public boolean findAccessibilityNodeInfosByViewId(int accessibilityWindowId,
2504 long accessibilityNodeId, String viewIdResName, int interactionId,
2505 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2506 throws RemoteException {
2507 final int resolvedWindowId;
2508 IAccessibilityInteractionConnection connection = null;
2509 Region partialInteractiveRegion = Region.obtain();
2510 synchronized (mLock) {
2511 if (!isCalledForCurrentUserLocked()) {
2514 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
2515 final boolean permissionGranted =
2516 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2517 if (!permissionGranted) {
2520 connection = getConnectionLocked(resolvedWindowId);
2521 if (connection == null) {
2525 if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(
2526 resolvedWindowId, partialInteractiveRegion)) {
2527 partialInteractiveRegion.recycle();
2528 partialInteractiveRegion = null;
2531 final int interrogatingPid = Binder.getCallingPid();
2532 final long identityToken = Binder.clearCallingIdentity();
2533 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
2535 connection.findAccessibilityNodeInfosByViewId(accessibilityNodeId, viewIdResName,
2536 partialInteractiveRegion, interactionId, callback, mFetchFlags,
2537 interrogatingPid, interrogatingTid, spec);
2539 } catch (RemoteException re) {
2541 Slog.e(LOG_TAG, "Error findAccessibilityNodeInfoByViewId().");
2544 Binder.restoreCallingIdentity(identityToken);
2545 // Recycle if passed to another process.
2546 if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
2547 partialInteractiveRegion.recycle();
2554 public boolean findAccessibilityNodeInfosByText(int accessibilityWindowId,
2555 long accessibilityNodeId, String text, int interactionId,
2556 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2557 throws RemoteException {
2558 final int resolvedWindowId;
2559 IAccessibilityInteractionConnection connection = null;
2560 Region partialInteractiveRegion = Region.obtain();
2561 synchronized (mLock) {
2562 if (!isCalledForCurrentUserLocked()) {
2565 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
2566 final boolean permissionGranted =
2567 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2568 if (!permissionGranted) {
2571 connection = getConnectionLocked(resolvedWindowId);
2572 if (connection == null) {
2576 if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(
2577 resolvedWindowId, partialInteractiveRegion)) {
2578 partialInteractiveRegion.recycle();
2579 partialInteractiveRegion = null;
2582 final int interrogatingPid = Binder.getCallingPid();
2583 final long identityToken = Binder.clearCallingIdentity();
2584 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
2586 connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text,
2587 partialInteractiveRegion, interactionId, callback, mFetchFlags,
2588 interrogatingPid, interrogatingTid, spec);
2590 } catch (RemoteException re) {
2592 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()");
2595 Binder.restoreCallingIdentity(identityToken);
2596 // Recycle if passed to another process.
2597 if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
2598 partialInteractiveRegion.recycle();
2605 public boolean findAccessibilityNodeInfoByAccessibilityId(
2606 int accessibilityWindowId, long accessibilityNodeId, int interactionId,
2607 IAccessibilityInteractionConnectionCallback callback, int flags,
2608 long interrogatingTid) throws RemoteException {
2609 final int resolvedWindowId;
2610 IAccessibilityInteractionConnection connection = null;
2611 Region partialInteractiveRegion = Region.obtain();
2612 synchronized (mLock) {
2613 if (!isCalledForCurrentUserLocked()) {
2616 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
2617 final boolean permissionGranted =
2618 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2619 if (!permissionGranted) {
2622 connection = getConnectionLocked(resolvedWindowId);
2623 if (connection == null) {
2627 if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(
2628 resolvedWindowId, partialInteractiveRegion)) {
2629 partialInteractiveRegion.recycle();
2630 partialInteractiveRegion = null;
2633 final int interrogatingPid = Binder.getCallingPid();
2634 final long identityToken = Binder.clearCallingIdentity();
2635 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
2637 connection.findAccessibilityNodeInfoByAccessibilityId(accessibilityNodeId,
2638 partialInteractiveRegion, interactionId, callback, mFetchFlags | flags,
2639 interrogatingPid, interrogatingTid, spec);
2641 } catch (RemoteException re) {
2643 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfoByAccessibilityId()");
2646 Binder.restoreCallingIdentity(identityToken);
2647 // Recycle if passed to another process.
2648 if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
2649 partialInteractiveRegion.recycle();
2656 public boolean findFocus(int accessibilityWindowId, long accessibilityNodeId,
2657 int focusType, int interactionId,
2658 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2659 throws RemoteException {
2660 final int resolvedWindowId;
2661 IAccessibilityInteractionConnection connection = null;
2662 Region partialInteractiveRegion = Region.obtain();
2663 synchronized (mLock) {
2664 if (!isCalledForCurrentUserLocked()) {
2667 resolvedWindowId = resolveAccessibilityWindowIdForFindFocusLocked(
2668 accessibilityWindowId, focusType);
2669 final boolean permissionGranted =
2670 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2671 if (!permissionGranted) {
2674 connection = getConnectionLocked(resolvedWindowId);
2675 if (connection == null) {
2679 if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(
2680 resolvedWindowId, partialInteractiveRegion)) {
2681 partialInteractiveRegion.recycle();
2682 partialInteractiveRegion = null;
2685 final int interrogatingPid = Binder.getCallingPid();
2686 final long identityToken = Binder.clearCallingIdentity();
2687 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
2689 connection.findFocus(accessibilityNodeId, focusType, partialInteractiveRegion,
2690 interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid,
2693 } catch (RemoteException re) {
2695 Slog.e(LOG_TAG, "Error calling findFocus()");
2698 Binder.restoreCallingIdentity(identityToken);
2699 // Recycle if passed to another process.
2700 if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
2701 partialInteractiveRegion.recycle();
2708 public boolean focusSearch(int accessibilityWindowId, long accessibilityNodeId,
2709 int direction, int interactionId,
2710 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2711 throws RemoteException {
2712 final int resolvedWindowId;
2713 IAccessibilityInteractionConnection connection = null;
2714 Region partialInteractiveRegion = Region.obtain();
2715 synchronized (mLock) {
2716 if (!isCalledForCurrentUserLocked()) {
2719 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
2720 final boolean permissionGranted =
2721 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2722 if (!permissionGranted) {
2725 connection = getConnectionLocked(resolvedWindowId);
2726 if (connection == null) {
2730 if (!mSecurityPolicy.computePartialInteractiveRegionForWindowLocked(
2731 resolvedWindowId, partialInteractiveRegion)) {
2732 partialInteractiveRegion.recycle();
2733 partialInteractiveRegion = null;
2736 final int interrogatingPid = Binder.getCallingPid();
2737 final long identityToken = Binder.clearCallingIdentity();
2738 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(resolvedWindowId);
2740 connection.focusSearch(accessibilityNodeId, direction, partialInteractiveRegion,
2741 interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid,
2744 } catch (RemoteException re) {
2746 Slog.e(LOG_TAG, "Error calling accessibilityFocusSearch()");
2749 Binder.restoreCallingIdentity(identityToken);
2750 // Recycle if passed to another process.
2751 if (partialInteractiveRegion != null && Binder.isProxy(connection)) {
2752 partialInteractiveRegion.recycle();
2759 public void sendGesture(int sequence, ParceledListSlice gestureSteps) {
2760 synchronized (mLock) {
2761 if (mSecurityPolicy.canPerformGestures(this)) {
2762 final long endMillis =
2763 SystemClock.uptimeMillis() + WAIT_MOTION_INJECTOR_TIMEOUT_MILLIS;
2764 while ((mMotionEventInjector == null)
2765 && (SystemClock.uptimeMillis() < endMillis)) {
2767 mLock.wait(endMillis - SystemClock.uptimeMillis());
2768 } catch (InterruptedException ie) {
2772 if (mMotionEventInjector != null) {
2773 List<GestureDescription.GestureStep> steps = gestureSteps.getList();
2774 List<MotionEvent> events = GestureDescription.MotionEventGenerator
2775 .getMotionEventsFromGestureSteps(steps);
2776 // Confirm that the motion events end with an UP event.
2777 if (events.get(events.size() - 1).getAction() == MotionEvent.ACTION_UP) {
2778 mMotionEventInjector.injectEvents(events, mServiceInterface, sequence);
2781 Slog.e(LOG_TAG, "Gesture is not well-formed");
2784 Slog.e(LOG_TAG, "MotionEventInjector installation timed out");
2789 mServiceInterface.onPerformGestureResult(sequence, false);
2790 } catch (RemoteException re) {
2791 Slog.e(LOG_TAG, "Error sending motion event injection failure to "
2792 + mServiceInterface, re);
2797 public boolean performAccessibilityAction(int accessibilityWindowId,
2798 long accessibilityNodeId, int action, Bundle arguments, int interactionId,
2799 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2800 throws RemoteException {
2801 final int resolvedWindowId;
2802 IAccessibilityInteractionConnection connection = null;
2803 synchronized (mLock) {
2804 if (!isCalledForCurrentUserLocked()) {
2807 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
2808 final boolean permissionGranted = mSecurityPolicy.canGetAccessibilityNodeInfoLocked(
2809 this, resolvedWindowId);
2810 if (!permissionGranted) {
2813 connection = getConnectionLocked(resolvedWindowId);
2814 if (connection == null) {
2819 final int interrogatingPid = Binder.getCallingPid();
2820 final long identityToken = Binder.clearCallingIdentity();
2822 // Regardless of whether or not the action succeeds, it was generated by an
2823 // accessibility service that is driven by user actions, so note user activity.
2824 mPowerManager.userActivity(SystemClock.uptimeMillis(),
2825 PowerManager.USER_ACTIVITY_EVENT_ACCESSIBILITY, 0);
2827 connection.performAccessibilityAction(accessibilityNodeId, action, arguments,
2828 interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid);
2829 } catch (RemoteException re) {
2831 Slog.e(LOG_TAG, "Error calling performAccessibilityAction()");
2834 Binder.restoreCallingIdentity(identityToken);
2840 public boolean performGlobalAction(int action) {
2841 synchronized (mLock) {
2842 if (!isCalledForCurrentUserLocked()) {
2846 final long identity = Binder.clearCallingIdentity();
2848 mPowerManager.userActivity(SystemClock.uptimeMillis(),
2849 PowerManager.USER_ACTIVITY_EVENT_ACCESSIBILITY, 0);
2851 case AccessibilityService.GLOBAL_ACTION_BACK: {
2852 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_BACK);
2854 case AccessibilityService.GLOBAL_ACTION_HOME: {
2855 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HOME);
2857 case AccessibilityService.GLOBAL_ACTION_RECENTS: {
2860 case AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS: {
2861 expandNotifications();
2863 case AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS: {
2864 expandQuickSettings();
2866 case AccessibilityService.GLOBAL_ACTION_POWER_DIALOG: {
2867 showGlobalActions();
2869 case AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN: {
2870 toggleSplitScreen();
2875 Binder.restoreCallingIdentity(identity);
2880 public float getMagnificationScale() {
2881 synchronized (mLock) {
2882 if (!isCalledForCurrentUserLocked()) {
2886 final long identity = Binder.clearCallingIdentity();
2888 return getMagnificationController().getScale();
2890 Binder.restoreCallingIdentity(identity);
2895 public Region getMagnificationRegion() {
2896 synchronized (mLock) {
2897 final Region region = Region.obtain();
2898 if (!isCalledForCurrentUserLocked()) {
2901 MagnificationController magnificationController = getMagnificationController();
2902 boolean forceRegistration = mSecurityPolicy.canControlMagnification(this);
2903 boolean initiallyRegistered = magnificationController.isRegisteredLocked();
2904 if (!initiallyRegistered && forceRegistration) {
2905 magnificationController.register();
2907 final long identity = Binder.clearCallingIdentity();
2909 magnificationController.getMagnificationRegion(region);
2912 Binder.restoreCallingIdentity(identity);
2913 if (!initiallyRegistered && forceRegistration) {
2914 magnificationController.unregister();
2921 public float getMagnificationCenterX() {
2922 synchronized (mLock) {
2923 if (!isCalledForCurrentUserLocked()) {
2927 final long identity = Binder.clearCallingIdentity();
2929 return getMagnificationController().getCenterX();
2931 Binder.restoreCallingIdentity(identity);
2936 public float getMagnificationCenterY() {
2937 synchronized (mLock) {
2938 if (!isCalledForCurrentUserLocked()) {
2942 final long identity = Binder.clearCallingIdentity();
2944 return getMagnificationController().getCenterY();
2946 Binder.restoreCallingIdentity(identity);
2951 public boolean resetMagnification(boolean animate) {
2952 synchronized (mLock) {
2953 if (!isCalledForCurrentUserLocked()) {
2956 final boolean permissionGranted = mSecurityPolicy.canControlMagnification(this);
2957 if (!permissionGranted) {
2961 final long identity = Binder.clearCallingIdentity();
2963 return getMagnificationController().reset(animate);
2965 Binder.restoreCallingIdentity(identity);
2970 public boolean setMagnificationScaleAndCenter(float scale, float centerX, float centerY,
2972 synchronized (mLock) {
2973 if (!isCalledForCurrentUserLocked()) {
2976 final boolean permissionGranted = mSecurityPolicy.canControlMagnification(this);
2977 if (!permissionGranted) {
2980 final long identity = Binder.clearCallingIdentity();
2982 MagnificationController magnificationController = getMagnificationController();
2983 if (!magnificationController.isRegisteredLocked()) {
2984 magnificationController.register();
2986 return magnificationController
2987 .setScaleAndCenter(scale, centerX, centerY, animate, mId);
2989 Binder.restoreCallingIdentity(identity);
2995 public void setMagnificationCallbackEnabled(boolean enabled) {
2996 mInvocationHandler.setMagnificationCallbackEnabled(enabled);
3000 public boolean setSoftKeyboardShowMode(int showMode) {
3001 final UserState userState;
3002 synchronized (mLock) {
3003 if (!isCalledForCurrentUserLocked()) {
3007 userState = getCurrentUserStateLocked();
3010 final long identity = Binder.clearCallingIdentity();
3012 // Keep track of the last service to request a non-default show mode. The show mode
3013 // should be restored to default should this service be disabled.
3014 if (showMode == Settings.Secure.SHOW_MODE_AUTO) {
3015 userState.mServiceChangingSoftKeyboardMode = null;
3017 userState.mServiceChangingSoftKeyboardMode = mComponentName;
3020 Settings.Secure.putIntForUser(mContext.getContentResolver(),
3021 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE, showMode,
3024 Binder.restoreCallingIdentity(identity);
3030 public void setSoftKeyboardCallbackEnabled(boolean enabled) {
3031 mInvocationHandler.setSoftKeyboardCallbackEnabled(enabled);
3035 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
3036 mSecurityPolicy.enforceCallingPermission(Manifest.permission.DUMP, FUNCTION_DUMP);
3037 synchronized (mLock) {
3038 pw.append("Service[label=" + mAccessibilityServiceInfo.getResolveInfo()
3039 .loadLabel(mContext.getPackageManager()));
3040 pw.append(", feedbackType"
3041 + AccessibilityServiceInfo.feedbackTypeToString(mFeedbackType));
3042 pw.append(", capabilities=" + mAccessibilityServiceInfo.getCapabilities());
3043 pw.append(", eventTypes="
3044 + AccessibilityEvent.eventTypeToString(mEventTypes));
3045 pw.append(", notificationTimeout=" + mNotificationTimeout);
3051 public void onServiceDisconnected(ComponentName componentName) {
3052 /* do nothing - #binderDied takes care */
3055 public void onAdded() throws RemoteException {
3056 linkToOwnDeathLocked();
3057 final long identity = Binder.clearCallingIdentity();
3059 mWindowManagerService.addWindowToken(mOverlayWindowToken,
3060 WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY);
3062 Binder.restoreCallingIdentity(identity);
3066 public void onRemoved() {
3067 final long identity = Binder.clearCallingIdentity();
3069 mWindowManagerService.removeWindowToken(mOverlayWindowToken, true);
3071 Binder.restoreCallingIdentity(identity);
3073 unlinkToOwnDeathLocked();
3076 public void linkToOwnDeathLocked() throws RemoteException {
3077 mService.linkToDeath(this, 0);
3080 public void unlinkToOwnDeathLocked() {
3081 mService.unlinkToDeath(this, 0);
3084 public void resetLocked() {
3086 // Clear the proxy in the other process so this
3087 // IAccessibilityServiceConnection can be garbage collected.
3088 mServiceInterface.init(null, mId, null);
3089 } catch (RemoteException re) {
3093 mServiceInterface = null;
3096 public boolean isConnectedLocked() {
3097 return (mService != null);
3100 public void binderDied() {
3101 synchronized (mLock) {
3102 // It is possible that this service's package was force stopped during
3103 // whose handling the death recipient is unlinked and still get a call
3104 // on binderDied since the call was made before we unlink but was
3105 // waiting on the lock we held during the force stop handling.
3106 if (!isConnectedLocked()) {
3109 mWasConnectedAndDied = true;
3110 getKeyEventDispatcher().flush(this);
3111 UserState userState = getUserStateLocked(mUserId);
3112 // The death recipient is unregistered in removeServiceLocked
3113 removeServiceLocked(this, userState);
3115 if (mIsAutomation) {
3116 // We no longer have an automation service, so restore
3117 // the state based on values in the settings database.
3118 userState.mInstalledServices.remove(mAccessibilityServiceInfo);
3119 userState.mEnabledServices.remove(mComponentName);
3120 userState.destroyUiAutomationService();
3121 readConfigurationForUserStateLocked(userState);
3123 if (mId == getMagnificationController().getIdOfLastServiceToMagnify()) {
3124 getMagnificationController().resetIfNeeded(true);
3126 onUserStateChangedLocked(userState);
3131 * Performs a notification for an {@link AccessibilityEvent}.
3133 * @param event The event.
3135 public void notifyAccessibilityEvent(AccessibilityEvent event) {
3136 synchronized (mLock) {
3137 final int eventType = event.getEventType();
3138 // Make a copy since during dispatch it is possible the event to
3139 // be modified to remove its source if the receiving service does
3140 // not have permission to access the window content.
3141 AccessibilityEvent newEvent = AccessibilityEvent.obtain(event);
3143 if ((mNotificationTimeout > 0)
3144 && (eventType != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED)) {
3145 // Allow at most one pending event
3146 final AccessibilityEvent oldEvent = mPendingEvents.get(eventType);
3147 mPendingEvents.put(eventType, newEvent);
3148 if (oldEvent != null) {
3149 mEventDispatchHandler.removeMessages(eventType);
3152 message = mEventDispatchHandler.obtainMessage(eventType);
3154 // Send all messages, bypassing mPendingEvents
3155 message = mEventDispatchHandler.obtainMessage(eventType, newEvent);
3158 mEventDispatchHandler.sendMessageDelayed(message, mNotificationTimeout);
3163 * Notifies an accessibility service client for a scheduled event given the event type.
3165 * @param eventType The type of the event to dispatch.
3167 private void notifyAccessibilityEventInternal(int eventType, AccessibilityEvent event) {
3168 IAccessibilityServiceClient listener;
3170 synchronized (mLock) {
3171 listener = mServiceInterface;
3173 // If the service died/was disabled while the message for dispatching
3174 // the accessibility event was propagating the listener may be null.
3175 if (listener == null) {
3179 // There are two ways we notify for events, throttled and non-throttled. If we
3180 // are not throttling, then messages come with events, which we handle with
3182 if (event == null) {
3183 // We are throttling events, so we'll send the event for this type in
3184 // mPendingEvents as long as it it's null. It can only null due to a race
3187 // 1) A binder thread calls notifyAccessibilityServiceDelayedLocked
3188 // which posts a message for dispatching an event and stores the event
3189 // in mPendingEvents.
3190 // 2) The message is pulled from the queue by the handler on the service
3191 // thread and this method is just about to acquire the lock.
3192 // 3) Another binder thread acquires the lock in notifyAccessibilityEvent
3193 // 4) notifyAccessibilityEvent recycles the event that this method was about
3194 // to process, replaces it with a new one, and posts a second message
3195 // 5) This method grabs the new event, processes it, and removes it from
3197 // 6) The second message dispatched in (4) arrives, but the event has been
3199 event = mPendingEvents.get(eventType);
3200 if (event == null) {
3203 mPendingEvents.remove(eventType);
3205 if (mSecurityPolicy.canRetrieveWindowContentLocked(this)) {
3206 event.setConnectionId(mId);
3208 event.setSource(null);
3210 event.setSealed(true);
3214 listener.onAccessibilityEvent(event);
3216 Slog.i(LOG_TAG, "Event " + event + " sent to " + listener);
3218 } catch (RemoteException re) {
3219 Slog.e(LOG_TAG, "Error during sending " + event + " to " + listener, re);
3225 public void notifyGesture(int gestureId) {
3226 mInvocationHandler.obtainMessage(InvocationHandler.MSG_ON_GESTURE,
3227 gestureId, 0).sendToTarget();
3230 public void notifyClearAccessibilityNodeInfoCache() {
3231 mInvocationHandler.sendEmptyMessage(
3232 InvocationHandler.MSG_CLEAR_ACCESSIBILITY_CACHE);
3235 public void notifyMagnificationChangedLocked(@NonNull Region region,
3236 float scale, float centerX, float centerY) {
3238 .notifyMagnificationChangedLocked(region, scale, centerX, centerY);
3241 public void notifySoftKeyboardShowModeChangedLocked(int showState) {
3242 mInvocationHandler.notifySoftKeyboardShowModeChangedLocked(showState);
3246 * Called by the invocation handler to notify the service that the
3247 * state of magnification has changed.
3249 private void notifyMagnificationChangedInternal(@NonNull Region region,
3250 float scale, float centerX, float centerY) {
3251 final IAccessibilityServiceClient listener;
3252 synchronized (mLock) {
3253 listener = mServiceInterface;
3255 if (listener != null) {
3257 listener.onMagnificationChanged(region, scale, centerX, centerY);
3258 } catch (RemoteException re) {
3259 Slog.e(LOG_TAG, "Error sending magnification changes to " + mService, re);
3265 * Called by the invocation handler to notify the service that the state of the soft
3266 * keyboard show mode has changed.
3268 private void notifySoftKeyboardShowModeChangedInternal(int showState) {
3269 final IAccessibilityServiceClient listener;
3270 synchronized (mLock) {
3271 listener = mServiceInterface;
3273 if (listener != null) {
3275 listener.onSoftKeyboardShowModeChanged(showState);
3276 } catch (RemoteException re) {
3277 Slog.e(LOG_TAG, "Error sending soft keyboard show mode changes to " + mService,
3283 private void notifyGestureInternal(int gestureId) {
3284 final IAccessibilityServiceClient listener;
3285 synchronized (mLock) {
3286 listener = mServiceInterface;
3288 if (listener != null) {
3290 listener.onGesture(gestureId);
3291 } catch (RemoteException re) {
3292 Slog.e(LOG_TAG, "Error during sending gesture " + gestureId
3293 + " to " + mService, re);
3298 private void notifyClearAccessibilityCacheInternal() {
3299 final IAccessibilityServiceClient listener;
3300 synchronized (mLock) {
3301 listener = mServiceInterface;
3303 if (listener != null) {
3305 listener.clearAccessibilityCache();
3306 } catch (RemoteException re) {
3307 Slog.e(LOG_TAG, "Error during requesting accessibility info cache"
3308 + " to be cleared.", re);
3313 private void sendDownAndUpKeyEvents(int keyCode) {
3314 final long token = Binder.clearCallingIdentity();
3317 final long downTime = SystemClock.uptimeMillis();
3318 KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
3319 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
3320 InputDevice.SOURCE_KEYBOARD, null);
3321 InputManager.getInstance().injectInputEvent(down,
3322 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3326 final long upTime = SystemClock.uptimeMillis();
3327 KeyEvent up = KeyEvent.obtain(downTime, upTime, KeyEvent.ACTION_UP, keyCode, 0, 0,
3328 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
3329 InputDevice.SOURCE_KEYBOARD, null);
3330 InputManager.getInstance().injectInputEvent(up,
3331 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
3334 Binder.restoreCallingIdentity(token);
3337 private void expandNotifications() {
3338 final long token = Binder.clearCallingIdentity();
3340 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
3341 android.app.Service.STATUS_BAR_SERVICE);
3342 statusBarManager.expandNotificationsPanel();
3344 Binder.restoreCallingIdentity(token);
3347 private void expandQuickSettings() {
3348 final long token = Binder.clearCallingIdentity();
3350 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
3351 android.app.Service.STATUS_BAR_SERVICE);
3352 statusBarManager.expandSettingsPanel();
3354 Binder.restoreCallingIdentity(token);
3357 private void openRecents() {
3358 final long token = Binder.clearCallingIdentity();
3360 StatusBarManagerInternal statusBarService = LocalServices.getService(
3361 StatusBarManagerInternal.class);
3362 statusBarService.toggleRecentApps();
3364 Binder.restoreCallingIdentity(token);
3367 private void showGlobalActions() {
3368 mWindowManagerService.showGlobalActions();
3371 private void toggleSplitScreen() {
3372 LocalServices.getService(StatusBarManagerInternal.class).toggleSplitScreen();
3375 private IAccessibilityInteractionConnection getConnectionLocked(int windowId) {
3377 Slog.i(LOG_TAG, "Trying to get interaction connection to windowId: " + windowId);
3379 AccessibilityConnectionWrapper wrapper = mGlobalInteractionConnections.get(windowId);
3380 if (wrapper == null) {
3381 wrapper = getCurrentUserStateLocked().mInteractionConnections.get(windowId);
3383 if (wrapper != null && wrapper.mConnection != null) {
3384 return wrapper.mConnection;
3387 Slog.e(LOG_TAG, "No interaction connection to window: " + windowId);
3392 private int resolveAccessibilityWindowIdLocked(int accessibilityWindowId) {
3393 if (accessibilityWindowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) {
3394 return mSecurityPolicy.getActiveWindowId();
3396 return accessibilityWindowId;
3399 private int resolveAccessibilityWindowIdForFindFocusLocked(int windowId, int focusType) {
3400 if (windowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) {
3401 return mSecurityPolicy.mActiveWindowId;
3403 if (windowId == AccessibilityNodeInfo.ANY_WINDOW_ID) {
3404 if (focusType == AccessibilityNodeInfo.FOCUS_INPUT) {
3405 return mSecurityPolicy.mFocusedWindowId;
3406 } else if (focusType == AccessibilityNodeInfo.FOCUS_ACCESSIBILITY) {
3407 return mSecurityPolicy.mAccessibilityFocusedWindowId;
3413 private final class InvocationHandler extends Handler {
3414 public static final int MSG_ON_GESTURE = 1;
3415 public static final int MSG_CLEAR_ACCESSIBILITY_CACHE = 2;
3417 private static final int MSG_ON_MAGNIFICATION_CHANGED = 5;
3418 private static final int MSG_ON_SOFT_KEYBOARD_STATE_CHANGED = 6;
3420 private boolean mIsMagnificationCallbackEnabled = false;
3421 private boolean mIsSoftKeyboardCallbackEnabled = false;
3423 public InvocationHandler(Looper looper) {
3424 super(looper, null, true);
3428 public void handleMessage(Message message) {
3429 final int type = message.what;
3431 case MSG_ON_GESTURE: {
3432 final int gestureId = message.arg1;
3433 notifyGestureInternal(gestureId);
3436 case MSG_CLEAR_ACCESSIBILITY_CACHE: {
3437 notifyClearAccessibilityCacheInternal();
3440 case MSG_ON_MAGNIFICATION_CHANGED: {
3441 final SomeArgs args = (SomeArgs) message.obj;
3442 final Region region = (Region) args.arg1;
3443 final float scale = (float) args.arg2;
3444 final float centerX = (float) args.arg3;
3445 final float centerY = (float) args.arg4;
3446 notifyMagnificationChangedInternal(region, scale, centerX, centerY);
3449 case MSG_ON_SOFT_KEYBOARD_STATE_CHANGED: {
3450 final int showState = (int) message.arg1;
3451 notifySoftKeyboardShowModeChangedInternal(showState);
3455 throw new IllegalArgumentException("Unknown message: " + type);
3460 public void notifyMagnificationChangedLocked(@NonNull Region region, float scale,
3461 float centerX, float centerY) {
3462 if (!mIsMagnificationCallbackEnabled) {
3463 // Callback is disabled, don't bother packing args.
3467 final SomeArgs args = SomeArgs.obtain();
3470 args.arg3 = centerX;
3471 args.arg4 = centerY;
3473 final Message msg = obtainMessage(MSG_ON_MAGNIFICATION_CHANGED, args);
3477 public void setMagnificationCallbackEnabled(boolean enabled) {
3478 mIsMagnificationCallbackEnabled = enabled;
3481 public void notifySoftKeyboardShowModeChangedLocked(int showState) {
3482 if (!mIsSoftKeyboardCallbackEnabled) {
3486 final Message msg = obtainMessage(MSG_ON_SOFT_KEYBOARD_STATE_CHANGED, showState, 0);
3490 public void setSoftKeyboardCallbackEnabled(boolean enabled) {
3491 mIsSoftKeyboardCallbackEnabled = enabled;
3496 final class WindowsForAccessibilityCallback implements
3497 WindowManagerInternal.WindowsForAccessibilityCallback {
3500 public void onWindowsForAccessibilityChanged(List<WindowInfo> windows) {
3501 synchronized (mLock) {
3502 // Populate the windows to report.
3503 List<AccessibilityWindowInfo> reportedWindows = new ArrayList<>();
3504 final int receivedWindowCount = windows.size();
3505 for (int i = 0; i < receivedWindowCount; i++) {
3506 WindowInfo receivedWindow = windows.get(i);
3507 AccessibilityWindowInfo reportedWindow = populateReportedWindow(
3509 if (reportedWindow != null) {
3510 reportedWindows.add(reportedWindow);
3515 Slog.i(LOG_TAG, "Windows changed: " + reportedWindows);
3518 // Let the policy update the focused and active windows.
3519 mSecurityPolicy.updateWindowsLocked(reportedWindows);
3521 // Someone may be waiting for the windows - advertise it.
3526 private AccessibilityWindowInfo populateReportedWindow(WindowInfo window) {
3527 final int windowId = findWindowIdLocked(window.token);
3532 AccessibilityWindowInfo reportedWindow = AccessibilityWindowInfo.obtain();
3534 reportedWindow.setId(windowId);
3535 reportedWindow.setType(getTypeForWindowManagerWindowType(window.type));
3536 reportedWindow.setLayer(window.layer);
3537 reportedWindow.setFocused(window.focused);
3538 reportedWindow.setBoundsInScreen(window.boundsInScreen);
3539 reportedWindow.setTitle(window.title);
3540 reportedWindow.setAnchorId(window.accessibilityIdOfAnchor);
3542 final int parentId = findWindowIdLocked(window.parentToken);
3543 if (parentId >= 0) {
3544 reportedWindow.setParentId(parentId);
3547 if (window.childTokens != null) {
3548 final int childCount = window.childTokens.size();
3549 for (int i = 0; i < childCount; i++) {
3550 IBinder childToken = window.childTokens.get(i);
3551 final int childId = findWindowIdLocked(childToken);
3553 reportedWindow.addChild(childId);
3558 return reportedWindow;
3561 private int getTypeForWindowManagerWindowType(int windowType) {
3562 switch (windowType) {
3563 case WindowManager.LayoutParams.TYPE_APPLICATION:
3564 case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
3565 case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
3566 case WindowManager.LayoutParams.TYPE_APPLICATION_STARTING:
3567 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
3568 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
3569 case WindowManager.LayoutParams.TYPE_BASE_APPLICATION:
3570 case WindowManager.LayoutParams.TYPE_PHONE:
3571 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
3572 case WindowManager.LayoutParams.TYPE_TOAST:
3573 case WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG: {
3574 return AccessibilityWindowInfo.TYPE_APPLICATION;
3577 case WindowManager.LayoutParams.TYPE_INPUT_METHOD:
3578 case WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG: {
3579 return AccessibilityWindowInfo.TYPE_INPUT_METHOD;
3582 case WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG:
3583 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR:
3584 case WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL:
3585 case WindowManager.LayoutParams.TYPE_SEARCH_BAR:
3586 case WindowManager.LayoutParams.TYPE_STATUS_BAR:
3587 case WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL:
3588 case WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL:
3589 case WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY:
3590 case WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
3591 case WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG:
3592 case WindowManager.LayoutParams.TYPE_SYSTEM_ERROR:
3593 case WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY:
3594 case WindowManager.LayoutParams.TYPE_SCREENSHOT: {
3595 return AccessibilityWindowInfo.TYPE_SYSTEM;
3598 case WindowManager.LayoutParams.TYPE_DOCK_DIVIDER: {
3599 return AccessibilityWindowInfo.TYPE_SPLIT_SCREEN_DIVIDER;
3602 case WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY: {
3603 return AccessibilityWindowInfo.TYPE_ACCESSIBILITY_OVERLAY;
3613 private final class InteractionBridge {
3614 private final Display mDefaultDisplay;
3615 private final int mConnectionId;
3616 private final AccessibilityInteractionClient mClient;
3618 public InteractionBridge() {
3619 AccessibilityServiceInfo info = new AccessibilityServiceInfo();
3620 info.setCapabilities(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT);
3621 info.flags |= AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
3622 info.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
3623 Service service = new Service(UserHandle.USER_NULL,
3624 sFakeAccessibilityServiceComponentName, info);
3626 mConnectionId = service.mId;
3628 mClient = AccessibilityInteractionClient.getInstance();
3629 mClient.addConnection(mConnectionId, service);
3631 //TODO: (multi-display) We need to support multiple displays.
3632 DisplayManager displayManager = (DisplayManager)
3633 mContext.getSystemService(Context.DISPLAY_SERVICE);
3634 mDefaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
3637 public void clearAccessibilityFocusNotLocked(int windowId) {
3638 AccessibilityNodeInfo focus = getAccessibilityFocusNotLocked(windowId);
3639 if (focus != null) {
3640 focus.performAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
3644 public boolean getAccessibilityFocusClickPointInScreenNotLocked(Point outPoint) {
3645 AccessibilityNodeInfo focus = getAccessibilityFocusNotLocked();
3646 if (focus == null) {
3650 synchronized (mLock) {
3651 Rect boundsInScreen = mTempRect;
3652 focus.getBoundsInScreen(boundsInScreen);
3654 // Clip to the window bounds.
3655 Rect windowBounds = mTempRect1;
3656 getWindowBounds(focus.getWindowId(), windowBounds);
3657 if (!boundsInScreen.intersect(windowBounds)) {
3661 // Apply magnification if needed.
3662 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(focus.getWindowId());
3663 if (spec != null && !spec.isNop()) {
3664 boundsInScreen.offset((int) -spec.offsetX, (int) -spec.offsetY);
3665 boundsInScreen.scale(1 / spec.scale);
3668 // Clip to the screen bounds.
3669 Point screenSize = mTempPoint;
3670 mDefaultDisplay.getRealSize(screenSize);
3671 if (!boundsInScreen.intersect(0, 0, screenSize.x, screenSize.y)) {
3675 outPoint.set(boundsInScreen.centerX(), boundsInScreen.centerY());
3681 private AccessibilityNodeInfo getAccessibilityFocusNotLocked() {
3682 final int focusedWindowId;
3683 synchronized (mLock) {
3684 focusedWindowId = mSecurityPolicy.mAccessibilityFocusedWindowId;
3685 if (focusedWindowId == SecurityPolicy.INVALID_WINDOW_ID) {
3689 return getAccessibilityFocusNotLocked(focusedWindowId);
3692 private AccessibilityNodeInfo getAccessibilityFocusNotLocked(int windowId) {
3693 return mClient.findFocus(mConnectionId,
3694 windowId, AccessibilityNodeInfo.ROOT_NODE_ID,
3695 AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
3699 final class SecurityPolicy {
3700 public static final int INVALID_WINDOW_ID = -1;
3702 private static final int RETRIEVAL_ALLOWING_EVENT_TYPES =
3703 AccessibilityEvent.TYPE_VIEW_CLICKED
3704 | AccessibilityEvent.TYPE_VIEW_FOCUSED
3705 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
3706 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
3707 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
3708 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
3709 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
3710 | AccessibilityEvent.TYPE_VIEW_SELECTED
3711 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
3712 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
3713 | AccessibilityEvent.TYPE_VIEW_SCROLLED
3714 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
3715 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED
3716 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
3718 public List<AccessibilityWindowInfo> mWindows;
3720 public int mActiveWindowId = INVALID_WINDOW_ID;
3721 public int mFocusedWindowId = INVALID_WINDOW_ID;
3722 public int mAccessibilityFocusedWindowId = INVALID_WINDOW_ID;
3723 public long mAccessibilityFocusNodeId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
3725 private boolean mTouchInteractionInProgress;
3727 private boolean canDispatchAccessibilityEventLocked(AccessibilityEvent event) {
3728 final int eventType = event.getEventType();
3729 switch (eventType) {
3730 // All events that are for changes in a global window
3731 // state should *always* be dispatched.
3732 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
3733 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
3734 case AccessibilityEvent.TYPE_ANNOUNCEMENT:
3735 // All events generated by the user touching the
3736 // screen should *always* be dispatched.
3737 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
3738 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
3739 case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
3740 case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
3741 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
3742 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
3743 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
3744 case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT:
3745 // Also always dispatch the event that assist is reading context.
3746 case AccessibilityEvent.TYPE_ASSIST_READING_CONTEXT:
3747 // Also windows changing should always be anounced.
3748 case AccessibilityEvent.TYPE_WINDOWS_CHANGED: {
3751 // All events for changes in window content should be
3752 // dispatched *only* if this window is one of the windows
3753 // the accessibility layer reports which are windows
3754 // that a sighted user can touch.
3756 return isRetrievalAllowingWindow(event.getWindowId());
3761 public void clearWindowsLocked() {
3762 List<AccessibilityWindowInfo> windows = Collections.emptyList();
3763 final int activeWindowId = mActiveWindowId;
3764 updateWindowsLocked(windows);
3765 mActiveWindowId = activeWindowId;
3769 public void updateWindowsLocked(List<AccessibilityWindowInfo> windows) {
3770 if (mWindows == null) {
3771 mWindows = new ArrayList<>();
3774 final int oldWindowCount = mWindows.size();
3775 for (int i = oldWindowCount - 1; i >= 0; i--) {
3776 mWindows.remove(i).recycle();
3779 mFocusedWindowId = INVALID_WINDOW_ID;
3780 if (!mTouchInteractionInProgress) {
3781 mActiveWindowId = INVALID_WINDOW_ID;
3784 // If the active window goes away while the user is touch exploring we
3785 // reset the active window id and wait for the next hover event from
3786 // under the user's finger to determine which one is the new one. It
3787 // is possible that the finger is not moving and the input system
3788 // filters out such events.
3789 boolean activeWindowGone = true;
3791 final int windowCount = windows.size();
3792 if (windowCount > 0) {
3793 for (int i = 0; i < windowCount; i++) {
3794 AccessibilityWindowInfo window = windows.get(i);
3795 final int windowId = window.getId();
3796 if (window.isFocused()) {
3797 mFocusedWindowId = windowId;
3798 if (!mTouchInteractionInProgress) {
3799 mActiveWindowId = windowId;
3800 window.setActive(true);
3801 } else if (windowId == mActiveWindowId) {
3802 activeWindowGone = false;
3805 mWindows.add(window);
3808 if (mTouchInteractionInProgress && activeWindowGone) {
3809 mActiveWindowId = mFocusedWindowId;
3812 // Focused window may change the active one, so set the
3813 // active window once we decided which it is.
3814 for (int i = 0; i < windowCount; i++) {
3815 AccessibilityWindowInfo window = mWindows.get(i);
3816 if (window.getId() == mActiveWindowId) {
3817 window.setActive(true);
3819 if (window.getId() == mAccessibilityFocusedWindowId) {
3820 window.setAccessibilityFocused(true);
3825 notifyWindowsChanged();
3828 public boolean computePartialInteractiveRegionForWindowLocked(int windowId,
3830 if (mWindows == null) {
3834 // Windows are ordered in z order so start from the bottom and find
3835 // the window of interest. After that all windows that cover it should
3836 // be subtracted from the resulting region. Note that for accessibility
3837 // we are returning only interactive windows.
3838 Region windowInteractiveRegion = null;
3839 boolean windowInteractiveRegionChanged = false;
3841 final int windowCount = mWindows.size();
3842 for (int i = windowCount - 1; i >= 0; i--) {
3843 AccessibilityWindowInfo currentWindow = mWindows.get(i);
3844 if (windowInteractiveRegion == null) {
3845 if (currentWindow.getId() == windowId) {
3846 Rect currentWindowBounds = mTempRect;
3847 currentWindow.getBoundsInScreen(currentWindowBounds);
3848 outRegion.set(currentWindowBounds);
3849 windowInteractiveRegion = outRegion;
3852 } else if (currentWindow.getType()
3853 != AccessibilityWindowInfo.TYPE_ACCESSIBILITY_OVERLAY) {
3854 Rect currentWindowBounds = mTempRect;
3855 currentWindow.getBoundsInScreen(currentWindowBounds);
3856 if (windowInteractiveRegion.op(currentWindowBounds, Region.Op.DIFFERENCE)) {
3857 windowInteractiveRegionChanged = true;
3862 return windowInteractiveRegionChanged;
3865 public void updateEventSourceLocked(AccessibilityEvent event) {
3866 if ((event.getEventType() & RETRIEVAL_ALLOWING_EVENT_TYPES) == 0) {
3867 event.setSource(null);
3871 public void updateActiveAndAccessibilityFocusedWindowLocked(int windowId, long nodeId,
3872 int eventType, int eventAction) {
3873 // The active window is either the window that has input focus or
3874 // the window that the user is currently touching. If the user is
3875 // touching a window that does not have input focus as soon as the
3876 // the user stops touching that window the focused window becomes
3877 // the active one. Here we detect the touched window and make it
3878 // active. In updateWindowsLocked() we update the focused window
3879 // and if the user is not touching the screen, we make the focused
3880 // window the active one.
3881 switch (eventType) {
3882 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: {
3883 // If no service has the capability to introspect screen,
3884 // we do not register callback in the window manager for
3885 // window changes, so we have to ask the window manager
3886 // what the focused window is to update the active one.
3887 // The active window also determined events from which
3888 // windows are delivered.
3889 synchronized (mLock) {
3890 if (mWindowsForAccessibilityCallback == null) {
3891 mFocusedWindowId = getFocusedWindowId();
3892 if (windowId == mFocusedWindowId) {
3893 mActiveWindowId = windowId;
3899 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER: {
3900 // Do not allow delayed hover events to confuse us
3901 // which the active window is.
3902 synchronized (mLock) {
3903 if (mTouchInteractionInProgress && mActiveWindowId != windowId) {
3904 setActiveWindowLocked(windowId);
3909 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
3910 synchronized (mLock) {
3911 if (mAccessibilityFocusedWindowId != windowId) {
3912 mMainHandler.obtainMessage(MainHandler.MSG_CLEAR_ACCESSIBILITY_FOCUS,
3913 mAccessibilityFocusedWindowId, 0).sendToTarget();
3914 mSecurityPolicy.setAccessibilityFocusedWindowLocked(windowId);
3915 mAccessibilityFocusNodeId = nodeId;
3920 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
3921 synchronized (mLock) {
3922 if (mAccessibilityFocusNodeId == nodeId) {
3923 mAccessibilityFocusNodeId = AccessibilityNodeInfo.UNDEFINED_ITEM_ID;
3925 // Clear the window with focus if it no longer has focus and we aren't
3926 // just moving focus from one view to the other in the same window
3927 if ((mAccessibilityFocusNodeId == AccessibilityNodeInfo.UNDEFINED_ITEM_ID)
3928 && (mAccessibilityFocusedWindowId == windowId)
3929 && (eventAction != AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS)
3931 mAccessibilityFocusedWindowId = INVALID_WINDOW_ID;
3938 public void onTouchInteractionStart() {
3939 synchronized (mLock) {
3940 mTouchInteractionInProgress = true;
3944 public void onTouchInteractionEnd() {
3945 synchronized (mLock) {
3946 mTouchInteractionInProgress = false;
3947 // We want to set the active window to be current immediately
3948 // after the user has stopped touching the screen since if the
3949 // user types with the IME he should get a feedback for the
3950 // letter typed in the text view which is in the input focused
3951 // window. Note that we always deliver hover accessibility events
3952 // (they are a result of user touching the screen) so change of
3953 // the active window before all hover accessibility events from
3954 // the touched window are delivered is fine.
3955 final int oldActiveWindow = mSecurityPolicy.mActiveWindowId;
3956 setActiveWindowLocked(mFocusedWindowId);
3958 // If there is no service that can operate with active windows
3959 // we keep accessibility focus behavior to constrain it only in
3960 // the active window. Look at updateAccessibilityFocusBehaviorLocked
3962 if (oldActiveWindow != mSecurityPolicy.mActiveWindowId
3963 && mAccessibilityFocusedWindowId == oldActiveWindow
3964 && getCurrentUserStateLocked().mAccessibilityFocusOnlyInActiveWindow) {
3965 mMainHandler.obtainMessage(MainHandler.MSG_CLEAR_ACCESSIBILITY_FOCUS,
3966 oldActiveWindow, 0).sendToTarget();
3971 public int getActiveWindowId() {
3972 if (mActiveWindowId == INVALID_WINDOW_ID && !mTouchInteractionInProgress) {
3973 mActiveWindowId = getFocusedWindowId();
3975 return mActiveWindowId;
3978 private void setActiveWindowLocked(int windowId) {
3979 if (mActiveWindowId != windowId) {
3980 mActiveWindowId = windowId;
3981 if (mWindows != null) {
3982 final int windowCount = mWindows.size();
3983 for (int i = 0; i < windowCount; i++) {
3984 AccessibilityWindowInfo window = mWindows.get(i);
3985 window.setActive(window.getId() == windowId);
3988 notifyWindowsChanged();
3992 private void setAccessibilityFocusedWindowLocked(int windowId) {
3993 if (mAccessibilityFocusedWindowId != windowId) {
3994 mAccessibilityFocusedWindowId = windowId;
3995 if (mWindows != null) {
3996 final int windowCount = mWindows.size();
3997 for (int i = 0; i < windowCount; i++) {
3998 AccessibilityWindowInfo window = mWindows.get(i);
3999 window.setAccessibilityFocused(window.getId() == windowId);
4003 notifyWindowsChanged();
4007 private void notifyWindowsChanged() {
4008 if (mWindowsForAccessibilityCallback == null) {
4011 final long identity = Binder.clearCallingIdentity();
4013 // Let the client know the windows changed.
4014 AccessibilityEvent event = AccessibilityEvent.obtain(
4015 AccessibilityEvent.TYPE_WINDOWS_CHANGED);
4016 event.setEventTime(SystemClock.uptimeMillis());
4017 sendAccessibilityEvent(event, mCurrentUserId);
4019 Binder.restoreCallingIdentity(identity);
4023 public boolean canGetAccessibilityNodeInfoLocked(Service service, int windowId) {
4024 return canRetrieveWindowContentLocked(service) && isRetrievalAllowingWindow(windowId);
4027 public boolean canRetrieveWindowsLocked(Service service) {
4028 return canRetrieveWindowContentLocked(service) && service.mRetrieveInteractiveWindows;
4031 public boolean canRetrieveWindowContentLocked(Service service) {
4032 return (service.mAccessibilityServiceInfo.getCapabilities()
4033 & AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT) != 0;
4036 public boolean canControlMagnification(Service service) {
4037 return (service.mAccessibilityServiceInfo.getCapabilities()
4038 & AccessibilityServiceInfo.CAPABILITY_CAN_CONTROL_MAGNIFICATION) != 0;
4041 public boolean canPerformGestures(Service service) {
4042 return (service.mAccessibilityServiceInfo.getCapabilities()
4043 & AccessibilityServiceInfo.CAPABILITY_CAN_PERFORM_GESTURES) != 0;
4046 private int resolveProfileParentLocked(int userId) {
4047 if (userId != mCurrentUserId) {
4048 final long identity = Binder.clearCallingIdentity();
4050 UserInfo parent = mUserManager.getProfileParent(userId);
4051 if (parent != null) {
4052 return parent.getUserHandle().getIdentifier();
4055 Binder.restoreCallingIdentity(identity);
4061 public int resolveCallingUserIdEnforcingPermissionsLocked(int userId) {
4062 final int callingUid = Binder.getCallingUid();
4064 || callingUid == Process.SYSTEM_UID
4065 || callingUid == Process.SHELL_UID) {
4066 if (userId == UserHandle.USER_CURRENT
4067 || userId == UserHandle.USER_CURRENT_OR_SELF) {
4068 return mCurrentUserId;
4070 return resolveProfileParentLocked(userId);
4072 final int callingUserId = UserHandle.getUserId(callingUid);
4073 if (callingUserId == userId) {
4074 return resolveProfileParentLocked(userId);
4076 final int callingUserParentId = resolveProfileParentLocked(callingUserId);
4077 if (callingUserParentId == mCurrentUserId &&
4078 (userId == UserHandle.USER_CURRENT
4079 || userId == UserHandle.USER_CURRENT_OR_SELF)) {
4080 return mCurrentUserId;
4082 if (!hasPermission(Manifest.permission.INTERACT_ACROSS_USERS)
4083 && !hasPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL)) {
4084 throw new SecurityException("Call from user " + callingUserId + " as user "
4085 + userId + " without permission INTERACT_ACROSS_USERS or "
4086 + "INTERACT_ACROSS_USERS_FULL not allowed.");
4088 if (userId == UserHandle.USER_CURRENT
4089 || userId == UserHandle.USER_CURRENT_OR_SELF) {
4090 return mCurrentUserId;
4092 throw new IllegalArgumentException("Calling user can be changed to only "
4093 + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF.");
4096 public boolean isCallerInteractingAcrossUsers(int userId) {
4097 final int callingUid = Binder.getCallingUid();
4098 return (Binder.getCallingPid() == android.os.Process.myPid()
4099 || callingUid == Process.SHELL_UID
4100 || userId == UserHandle.USER_CURRENT
4101 || userId == UserHandle.USER_CURRENT_OR_SELF);
4104 private boolean isRetrievalAllowingWindow(int windowId) {
4105 // The system gets to interact with any window it wants.
4106 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
4109 if (windowId == mActiveWindowId) {
4112 return findWindowById(windowId) != null;
4115 private AccessibilityWindowInfo findWindowById(int windowId) {
4116 if (mWindows != null) {
4117 final int windowCount = mWindows.size();
4118 for (int i = 0; i < windowCount; i++) {
4119 AccessibilityWindowInfo window = mWindows.get(i);
4120 if (window.getId() == windowId) {
4128 private void enforceCallingPermission(String permission, String function) {
4129 if (OWN_PROCESS_ID == Binder.getCallingPid()) {
4132 if (!hasPermission(permission)) {
4133 throw new SecurityException("You do not have " + permission
4134 + " required to call " + function + " from pid="
4135 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
4139 private boolean hasPermission(String permission) {
4140 return mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED;
4143 private int getFocusedWindowId() {
4144 IBinder token = mWindowManagerService.getFocusedWindowToken();
4145 synchronized (mLock) {
4146 return findWindowIdLocked(token);
4151 private class UserState {
4152 public final int mUserId;
4154 // Non-transient state.
4156 public final RemoteCallbackList<IAccessibilityManagerClient> mClients =
4157 new RemoteCallbackList<>();
4159 public final SparseArray<AccessibilityConnectionWrapper> mInteractionConnections =
4160 new SparseArray<>();
4162 public final SparseArray<IBinder> mWindowTokens = new SparseArray<>();
4166 public final CopyOnWriteArrayList<Service> mBoundServices =
4167 new CopyOnWriteArrayList<>();
4169 public final Map<ComponentName, Service> mComponentNameToServiceMap =
4172 public final List<AccessibilityServiceInfo> mInstalledServices =
4175 public final Set<ComponentName> mBindingServices = new HashSet<>();
4177 public final Set<ComponentName> mEnabledServices = new HashSet<>();
4179 public final Set<ComponentName> mTouchExplorationGrantedServices =
4182 public ComponentName mServiceChangingSoftKeyboardMode;
4184 public int mLastSentClientState = -1;
4186 public int mSoftKeyboardShowMode = 0;
4188 public boolean mIsTouchExplorationEnabled;
4189 public boolean mIsTextHighContrastEnabled;
4190 public boolean mIsEnhancedWebAccessibilityEnabled;
4191 public boolean mIsDisplayMagnificationEnabled;
4192 public boolean mIsAutoclickEnabled;
4193 public boolean mIsPerformGesturesEnabled;
4194 public boolean mIsFilterKeyEventsEnabled;
4195 public boolean mHasDisplayColorAdjustment;
4196 public boolean mAccessibilityFocusOnlyInActiveWindow;
4198 private Service mUiAutomationService;
4199 private int mUiAutomationFlags;
4200 private IAccessibilityServiceClient mUiAutomationServiceClient;
4202 private IBinder mUiAutomationServiceOwner;
4203 private final DeathRecipient mUiAutomationSerivceOnwerDeathRecipient =
4204 new DeathRecipient() {
4206 public void binderDied() {
4207 mUiAutomationServiceOwner.unlinkToDeath(
4208 mUiAutomationSerivceOnwerDeathRecipient, 0);
4209 mUiAutomationServiceOwner = null;
4210 if (mUiAutomationService != null) {
4211 mUiAutomationService.binderDied();
4216 public UserState(int userId) {
4220 public int getClientState() {
4221 int clientState = 0;
4222 if (isHandlingAccessibilityEvents()) {
4223 clientState |= AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED;
4225 // Touch exploration relies on enabled accessibility.
4226 if (isHandlingAccessibilityEvents() && mIsTouchExplorationEnabled) {
4227 clientState |= AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED;
4229 if (mIsTextHighContrastEnabled) {
4230 clientState |= AccessibilityManager.STATE_FLAG_HIGH_TEXT_CONTRAST_ENABLED;
4235 public boolean isHandlingAccessibilityEvents() {
4236 return !mBoundServices.isEmpty() || !mBindingServices.isEmpty();
4239 public void onSwitchToAnotherUser() {
4240 // Clear UI test automation state.
4241 if (mUiAutomationService != null) {
4242 mUiAutomationService.binderDied();
4245 // Unbind all services.
4246 unbindAllServicesLocked(this);
4248 // Clear service management state.
4249 mBoundServices.clear();
4250 mBindingServices.clear();
4252 // Clear event management state.
4253 mLastSentClientState = -1;
4255 // Clear state persisted in settings.
4256 mEnabledServices.clear();
4257 mTouchExplorationGrantedServices.clear();
4258 mIsTouchExplorationEnabled = false;
4259 mIsEnhancedWebAccessibilityEnabled = false;
4260 mIsDisplayMagnificationEnabled = false;
4261 mIsAutoclickEnabled = false;
4262 mSoftKeyboardShowMode = 0;
4265 public void destroyUiAutomationService() {
4266 mUiAutomationService = null;
4267 mUiAutomationFlags = 0;
4268 mUiAutomationServiceClient = null;
4269 if (mUiAutomationServiceOwner != null) {
4270 mUiAutomationServiceOwner.unlinkToDeath(
4271 mUiAutomationSerivceOnwerDeathRecipient, 0);
4272 mUiAutomationServiceOwner = null;
4276 boolean isUiAutomationSuppressingOtherServices() {
4277 return ((mUiAutomationService != null) && (mUiAutomationFlags
4278 & UiAutomation.FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES) == 0);
4282 private final class AccessibilityContentObserver extends ContentObserver {
4284 private final Uri mTouchExplorationEnabledUri = Settings.Secure.getUriFor(
4285 Settings.Secure.TOUCH_EXPLORATION_ENABLED);
4287 private final Uri mDisplayMagnificationEnabledUri = Settings.Secure.getUriFor(
4288 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED);
4290 private final Uri mAutoclickEnabledUri = Settings.Secure.getUriFor(
4291 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED);
4293 private final Uri mEnabledAccessibilityServicesUri = Settings.Secure.getUriFor(
4294 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
4296 private final Uri mTouchExplorationGrantedAccessibilityServicesUri = Settings.Secure
4297 .getUriFor(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES);
4299 private final Uri mEnhancedWebAccessibilityUri = Settings.Secure
4300 .getUriFor(Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION);
4302 private final Uri mDisplayInversionEnabledUri = Settings.Secure.getUriFor(
4303 Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
4305 private final Uri mDisplayDaltonizerEnabledUri = Settings.Secure.getUriFor(
4306 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED);
4308 private final Uri mDisplayDaltonizerUri = Settings.Secure.getUriFor(
4309 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER);
4311 private final Uri mDisplayColorMatrixUri = Settings.Secure.getUriFor(
4312 Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX);
4314 private final Uri mHighTextContrastUri = Settings.Secure.getUriFor(
4315 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED);
4317 private final Uri mAccessibilitySoftKeyboardModeUri = Settings.Secure.getUriFor(
4318 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
4320 public AccessibilityContentObserver(Handler handler) {
4324 public void register(ContentResolver contentResolver) {
4325 contentResolver.registerContentObserver(mTouchExplorationEnabledUri,
4326 false, this, UserHandle.USER_ALL);
4327 contentResolver.registerContentObserver(mDisplayMagnificationEnabledUri,
4328 false, this, UserHandle.USER_ALL);
4329 contentResolver.registerContentObserver(mAutoclickEnabledUri,
4330 false, this, UserHandle.USER_ALL);
4331 contentResolver.registerContentObserver(mEnabledAccessibilityServicesUri,
4332 false, this, UserHandle.USER_ALL);
4333 contentResolver.registerContentObserver(
4334 mTouchExplorationGrantedAccessibilityServicesUri,
4335 false, this, UserHandle.USER_ALL);
4336 contentResolver.registerContentObserver(mEnhancedWebAccessibilityUri,
4337 false, this, UserHandle.USER_ALL);
4338 contentResolver.registerContentObserver(
4339 mDisplayInversionEnabledUri, false, this, UserHandle.USER_ALL);
4340 contentResolver.registerContentObserver(
4341 mDisplayDaltonizerEnabledUri, false, this, UserHandle.USER_ALL);
4342 contentResolver.registerContentObserver(
4343 mDisplayDaltonizerUri, false, this, UserHandle.USER_ALL);
4344 contentResolver.registerContentObserver(
4345 mDisplayColorMatrixUri, false, this, UserHandle.USER_ALL);
4346 contentResolver.registerContentObserver(
4347 mHighTextContrastUri, false, this, UserHandle.USER_ALL);
4348 contentResolver.registerContentObserver(
4349 mAccessibilitySoftKeyboardModeUri, false, this, UserHandle.USER_ALL);
4353 public void onChange(boolean selfChange, Uri uri) {
4354 synchronized (mLock) {
4355 // Profiles share the accessibility state of the parent. Therefore,
4356 // we are checking for changes only the parent settings.
4357 UserState userState = getCurrentUserStateLocked();
4359 // If the automation service is suppressing, we will update when it dies.
4360 if (userState.isUiAutomationSuppressingOtherServices()) {
4364 if (mTouchExplorationEnabledUri.equals(uri)) {
4365 if (readTouchExplorationEnabledSettingLocked(userState)) {
4366 onUserStateChangedLocked(userState);
4368 } else if (mDisplayMagnificationEnabledUri.equals(uri)) {
4369 if (readDisplayMagnificationEnabledSettingLocked(userState)) {
4370 onUserStateChangedLocked(userState);
4372 } else if (mAutoclickEnabledUri.equals(uri)) {
4373 if (readAutoclickEnabledSettingLocked(userState)) {
4374 onUserStateChangedLocked(userState);
4376 } else if (mEnabledAccessibilityServicesUri.equals(uri)) {
4377 if (readEnabledAccessibilityServicesLocked(userState)) {
4378 onUserStateChangedLocked(userState);
4380 } else if (mTouchExplorationGrantedAccessibilityServicesUri.equals(uri)) {
4381 if (readTouchExplorationGrantedAccessibilityServicesLocked(userState)) {
4382 onUserStateChangedLocked(userState);
4384 } else if (mEnhancedWebAccessibilityUri.equals(uri)) {
4385 if (readEnhancedWebAccessibilityEnabledChangedLocked(userState)) {
4386 onUserStateChangedLocked(userState);
4388 } else if (mDisplayInversionEnabledUri.equals(uri)
4389 || mDisplayDaltonizerEnabledUri.equals(uri)
4390 || mDisplayDaltonizerUri.equals(uri)) {
4391 if (readDisplayColorAdjustmentSettingsLocked(userState)) {
4392 updateDisplayColorAdjustmentSettingsLocked(userState);
4394 } else if (mDisplayColorMatrixUri.equals(uri)) {
4395 updateDisplayColorAdjustmentSettingsLocked(userState);
4396 } else if (mHighTextContrastUri.equals(uri)) {
4397 if (readHighTextContrastEnabledSettingLocked(userState)) {
4398 onUserStateChangedLocked(userState);
4400 } else if (mAccessibilitySoftKeyboardModeUri.equals(uri)) {
4401 if (readSoftKeyboardShowModeChangedLocked(userState)) {
4402 notifySoftKeyboardShowModeChangedLocked(userState.mSoftKeyboardShowMode);
4403 onUserStateChangedLocked(userState);