blob: f1e4b0c74a20d092f10c85ea22b6783705addedc [file] [log] [blame]
svetoslavganov75986cf2009-05-14 22:28:01 -07001/*
2 ** Copyright 2009, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
Jeff Brown6e6cd7a2011-03-30 03:27:08 -070017package com.android.server.accessibility;
svetoslavganov75986cf2009-05-14 22:28:01 -070018
Svetoslav Ganov42138042012-03-20 11:51:39 -070019import static android.accessibilityservice.AccessibilityServiceInfo.DEFAULT;
Svetoslav Ganov42138042012-03-20 11:51:39 -070020
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070021import android.Manifest;
svetoslavganov75986cf2009-05-14 22:28:01 -070022import android.accessibilityservice.AccessibilityService;
23import android.accessibilityservice.AccessibilityServiceInfo;
Svetoslav Ganov42138042012-03-20 11:51:39 -070024import android.accessibilityservice.IAccessibilityServiceClient;
svetoslavganov75986cf2009-05-14 22:28:01 -070025import android.accessibilityservice.IAccessibilityServiceConnection;
Svetoslav Ganov447d9462013-02-01 19:46:20 +000026import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070027import android.app.PendingIntent;
Svetoslav Ganov5c89f442012-05-15 13:28:09 -070028import android.app.StatusBarManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070029import android.content.BroadcastReceiver;
30import android.content.ComponentName;
31import android.content.ContentResolver;
32import android.content.Context;
Svetoslav Ganov447d9462013-02-01 19:46:20 +000033import android.content.DialogInterface;
34import android.content.DialogInterface.OnClickListener;
svetoslavganov75986cf2009-05-14 22:28:01 -070035import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.ServiceConnection;
38import android.content.pm.PackageManager;
39import android.content.pm.ResolveInfo;
Svetoslav Ganov53e184d2012-05-16 15:57:10 -070040import android.content.pm.ServiceInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070041import android.database.ContentObserver;
Svetoslav Ganova94c3192012-10-31 18:28:49 -070042import android.graphics.Point;
Svetoslav Ganov7961be72011-06-21 12:31:56 -070043import android.graphics.Rect;
Svetoslav Ganova94c3192012-10-31 18:28:49 -070044import android.hardware.display.DisplayManager;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070045import android.hardware.input.InputManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070046import android.net.Uri;
47import android.os.Binder;
Svetoslav Ganov42138042012-03-20 11:51:39 -070048import android.os.Build;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070049import android.os.Bundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070050import android.os.Handler;
51import android.os.IBinder;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -070052import android.os.Looper;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.os.Message;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -070054import android.os.Process;
55import android.os.RemoteCallbackList;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.os.RemoteException;
Jeff Brown0029c662011-03-30 02:25:18 -070057import android.os.ServiceManager;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070058import android.os.SystemClock;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -070059import android.os.UserHandle;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -070060import android.os.UserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.provider.Settings;
62import android.text.TextUtils;
63import android.text.TextUtils.SimpleStringSplitter;
Svetoslavc4fccd12013-04-09 12:58:41 -070064import android.util.Pools.Pool;
65import android.util.Pools.SimplePool;
Joe Onorato8a9b2202010-02-26 18:56:32 -080066import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.util.SparseArray;
Svetoslav Ganova94c3192012-10-31 18:28:49 -070068import android.view.Display;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.IWindow;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070070import android.view.IWindowManager;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070071import android.view.InputDevice;
Svetoslavc4fccd12013-04-09 12:58:41 -070072import android.view.InputEventConsistencyVerifier;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070073import android.view.KeyCharacterMap;
74import android.view.KeyEvent;
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -070075import android.view.MagnificationSpec;
Svetoslav Ganov447d9462013-02-01 19:46:20 +000076import android.view.WindowManager;
Svetoslavc4fccd12013-04-09 12:58:41 -070077import android.view.WindowManagerPolicy;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070079import android.view.accessibility.AccessibilityInteractionClient;
Svetoslav Ganov00aabf72011-07-21 11:35:03 -070080import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070081import android.view.accessibility.AccessibilityNodeInfo;
82import android.view.accessibility.IAccessibilityInteractionConnection;
83import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
svetoslavganov75986cf2009-05-14 22:28:01 -070084import android.view.accessibility.IAccessibilityManager;
85import android.view.accessibility.IAccessibilityManagerClient;
86
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070087import com.android.internal.R;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070088import com.android.internal.content.PackageMonitor;
Svetoslav Ganovc682fc92012-06-04 14:02:09 -070089import com.android.internal.statusbar.IStatusBarService;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070090
91import org.xmlpull.v1.XmlPullParserException;
92
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -080093import java.io.FileDescriptor;
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -070094import java.io.IOException;
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -080095import java.io.PrintWriter;
svetoslavganov75986cf2009-05-14 22:28:01 -070096import java.util.ArrayList;
97import java.util.Arrays;
Svetoslav Ganov319d55b2013-05-17 19:12:38 -070098import java.util.Collections;
svetoslavganov75986cf2009-05-14 22:28:01 -070099import java.util.HashMap;
100import java.util.HashSet;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800101import java.util.Iterator;
svetoslavganov75986cf2009-05-14 22:28:01 -0700102import java.util.List;
103import java.util.Map;
104import java.util.Set;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700105import java.util.concurrent.CopyOnWriteArrayList;
svetoslavganov75986cf2009-05-14 22:28:01 -0700106
107/**
108 * This class is instantiated by the system as a system level service and can be
109 * accessed only by the system. The task of this service is to be a centralized
110 * event dispatch for {@link AccessibilityEvent}s generated across all processes
111 * on the device. Events are dispatched to {@link AccessibilityService}s.
112 *
113 * @hide
114 */
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700115public class AccessibilityManagerService extends IAccessibilityManager.Stub {
svetoslavganov75986cf2009-05-14 22:28:01 -0700116
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700117 private static final boolean DEBUG = false;
118
svetoslavganov75986cf2009-05-14 22:28:01 -0700119 private static final String LOG_TAG = "AccessibilityManagerService";
120
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700121 // TODO: This is arbitrary. When there is time implement this by watching
122 // when that accessibility services are bound.
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700123 private static final int WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS = 3000;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700124
Svetoslav Ganov0d04e242012-02-21 13:46:36 -0800125 private static final String FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE =
126 "registerUiTestAutomationService";
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700127
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700128 private static final String TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED =
129 "temporaryEnableAccessibilityStateUntilKeyguardRemoved";
130
Svetoslav57bf8852013-02-07 19:21:42 -0800131 private static final ComponentName sFakeAccessibilityServiceComponentName =
132 new ComponentName("foo.bar", "FakeService");
133
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -0800134 private static final String FUNCTION_DUMP = "dump";
135
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700136 private static final char COMPONENT_NAME_SEPARATOR = ':';
137
svetoslavganov75986cf2009-05-14 22:28:01 -0700138 private static final int OWN_PROCESS_ID = android.os.Process.myPid();
139
Svetoslavc4fccd12013-04-09 12:58:41 -0700140 private static final int MAX_POOL_SIZE = 10;
141
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700142 private static int sIdCounter = 0;
143
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700144 private static int sNextWindowId;
145
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700146 private final Context mContext;
svetoslavganov75986cf2009-05-14 22:28:01 -0700147
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700148 private final Object mLock = new Object();
svetoslavganov75986cf2009-05-14 22:28:01 -0700149
Svetoslavc4fccd12013-04-09 12:58:41 -0700150 private final Pool<PendingEvent> mPendingEventPool =
151 new SimplePool<PendingEvent>(MAX_POOL_SIZE);
152
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700153 private final SimpleStringSplitter mStringColonSplitter =
154 new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
svetoslavganov75986cf2009-05-14 22:28:01 -0700155
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700156 private final List<AccessibilityServiceInfo> mEnabledServicesForFeedbackTempList =
157 new ArrayList<AccessibilityServiceInfo>();
svetoslavganov75986cf2009-05-14 22:28:01 -0700158
Svetoslav Ganova94c3192012-10-31 18:28:49 -0700159 private final Rect mTempRect = new Rect();
160
161 private final Point mTempPoint = new Point();
162
163 private final Display mDefaultDisplay;
164
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700165 private final PackageManager mPackageManager;
svetoslavganov75986cf2009-05-14 22:28:01 -0700166
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700167 private final IWindowManager mWindowManagerService;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700168
169 private final SecurityPolicy mSecurityPolicy;
170
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700171 private final MainHandler mMainHandler;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700172
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700173 private Service mQueryBridge;
svetoslavganov75986cf2009-05-14 22:28:01 -0700174
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000175 private AlertDialog mEnableTouchExplorationDialog;
176
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700177 private AccessibilityInputFilter mInputFilter;
178
179 private boolean mHasInputFilter;
180
Svetoslav57bf8852013-02-07 19:21:42 -0800181 private final Set<ComponentName> mTempComponentNameSet = new HashSet<ComponentName>();
182
183 private final List<AccessibilityServiceInfo> mTempAccessibilityServiceInfoList =
184 new ArrayList<AccessibilityServiceInfo>();
185
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700186 private final RemoteCallbackList<IAccessibilityManagerClient> mGlobalClients =
187 new RemoteCallbackList<IAccessibilityManagerClient>();
188
189 private final SparseArray<AccessibilityConnectionWrapper> mGlobalInteractionConnections =
190 new SparseArray<AccessibilityConnectionWrapper>();
191
192 private final SparseArray<IBinder> mGlobalWindowTokens = new SparseArray<IBinder>();
193
194 private final SparseArray<UserState> mUserStates = new SparseArray<UserState>();
195
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700196 private int mCurrentUserId = UserHandle.USER_OWNER;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700197
Svetoslav3b817ae2013-06-19 16:28:30 -0700198 //TODO: Remove this hack
199 private boolean mInitialized;
200
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700201 private UserState getCurrentUserStateLocked() {
202 return getUserStateLocked(mCurrentUserId);
203 }
204
205 private UserState getUserStateLocked(int userId) {
206 UserState state = mUserStates.get(userId);
207 if (state == null) {
208 state = new UserState(userId);
209 mUserStates.put(userId, state);
210 }
211 return state;
212 }
213
svetoslavganov75986cf2009-05-14 22:28:01 -0700214 /**
215 * Creates a new instance.
216 *
217 * @param context A {@link Context} instance.
218 */
Jeff Brown6e6cd7a2011-03-30 03:27:08 -0700219 public AccessibilityManagerService(Context context) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700220 mContext = context;
221 mPackageManager = mContext.getPackageManager();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700222 mWindowManagerService = (IWindowManager) ServiceManager.getService(Context.WINDOW_SERVICE);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700223 mSecurityPolicy = new SecurityPolicy();
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700224 mMainHandler = new MainHandler(mContext.getMainLooper());
Svetoslav Ganova94c3192012-10-31 18:28:49 -0700225 //TODO: (multi-display) We need to support multiple displays.
226 DisplayManager displayManager = (DisplayManager)
227 mContext.getSystemService(Context.DISPLAY_SERVICE);
228 mDefaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700229 registerBroadcastReceivers();
230 new AccessibilityContentObserver(mMainHandler).register(
231 context.getContentResolver());
svetoslavganov75986cf2009-05-14 22:28:01 -0700232 }
233
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700234 private void registerBroadcastReceivers() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800235 PackageMonitor monitor = new PackageMonitor() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700236 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800237 public void onSomePackagesChanged() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700238 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700239 if (getChangingUserId() != mCurrentUserId) {
240 return;
241 }
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -0700242 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -0800243 UserState userState = getCurrentUserStateLocked();
Svetoslav Ganov0ecfa012013-04-18 09:35:43 -0700244 // We have to reload the installed services since some services may
245 // have different attributes, resolve info (does not support equals),
246 // etc. Remove them then to force reload. Do it even if automation is
247 // running since when it goes away, we will have to reload as well.
248 userState.mInstalledServices.clear();
Svetoslav57bf8852013-02-07 19:21:42 -0800249 if (userState.mUiAutomationService == null) {
250 if (readConfigurationForUserStateLocked(userState)) {
251 onUserStateChangedLocked(userState);
252 }
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -0700253 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800254 }
255 }
Svetoslav Ganov00aabf72011-07-21 11:35:03 -0700256
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800257 @Override
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700258 public void onPackageRemoved(String packageName, int uid) {
259 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700260 final int userId = getChangingUserId();
261 if (userId != mCurrentUserId) {
262 return;
263 }
Svetoslav57bf8852013-02-07 19:21:42 -0800264 UserState userState = getUserStateLocked(userId);
265 Iterator<ComponentName> it = userState.mEnabledServices.iterator();
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700266 while (it.hasNext()) {
267 ComponentName comp = it.next();
268 String compPkg = comp.getPackageName();
269 if (compPkg.equals(packageName)) {
270 it.remove();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700271 // Update the enabled services setting.
272 persistComponentNamesToSettingLocked(
273 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav57bf8852013-02-07 19:21:42 -0800274 userState.mEnabledServices, userId);
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000275 // Update the touch exploration granted services setting.
Svetoslav57bf8852013-02-07 19:21:42 -0800276 userState.mTouchExplorationGrantedServices.remove(comp);
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000277 persistComponentNamesToSettingLocked(
278 Settings.Secure.
279 TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
Svetoslav57bf8852013-02-07 19:21:42 -0800280 userState.mTouchExplorationGrantedServices, userId);
281 // We will update when the automation service dies.
282 if (userState.mUiAutomationService == null) {
283 onUserStateChangedLocked(userState);
284 }
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700285 return;
286 }
287 }
288 }
289 }
290
291 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800292 public boolean onHandleForceStop(Intent intent, String[] packages,
293 int uid, boolean doit) {
294 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700295 final int userId = getChangingUserId();
296 if (userId != mCurrentUserId) {
297 return false;
298 }
Svetoslav57bf8852013-02-07 19:21:42 -0800299 UserState userState = getUserStateLocked(userId);
300 Iterator<ComponentName> it = userState.mEnabledServices.iterator();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800301 while (it.hasNext()) {
302 ComponentName comp = it.next();
303 String compPkg = comp.getPackageName();
304 for (String pkg : packages) {
305 if (compPkg.equals(pkg)) {
306 if (!doit) {
307 return true;
308 }
309 it.remove();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700310 persistComponentNamesToSettingLocked(
311 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav57bf8852013-02-07 19:21:42 -0800312 userState.mEnabledServices, userId);
313 // We will update when the automation service dies.
314 if (userState.mUiAutomationService == null) {
315 onUserStateChangedLocked(userState);
316 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800317 }
318 }
319 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800320 return false;
321 }
322 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700323 };
324
325 // package changes
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700326 monitor.register(mContext, null, UserHandle.ALL, true);
svetoslavganov75986cf2009-05-14 22:28:01 -0700327
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700328 // user change and unlock
329 IntentFilter intentFilter = new IntentFilter();
330 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
331 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
332 intentFilter.addAction(Intent.ACTION_USER_PRESENT);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700333
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700334 mContext.registerReceiverAsUser(new BroadcastReceiver() {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700335 @Override
336 public void onReceive(Context context, Intent intent) {
337 String action = intent.getAction();
338 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
339 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
340 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
341 removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700342 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
Svetoslav57bf8852013-02-07 19:21:42 -0800343 // We will update when the automation service dies.
344 UserState userState = getCurrentUserStateLocked();
345 if (userState.mUiAutomationService == null) {
346 if (readConfigurationForUserStateLocked(userState)) {
347 onUserStateChangedLocked(userState);
348 }
349 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700350 }
351 }
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700352 }, UserHandle.ALL, intentFilter, null, null);
svetoslavganov75986cf2009-05-14 22:28:01 -0700353 }
354
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700355 public int addClient(IAccessibilityManagerClient client, int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700356 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700357 final int resolvedUserId = mSecurityPolicy
358 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
359 // If the client is from a process that runs across users such as
360 // the system UI or the system we add it to the global state that
361 // is shared across users.
362 UserState userState = getUserStateLocked(resolvedUserId);
363 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
364 mGlobalClients.register(client);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700365 if (DEBUG) {
366 Slog.i(LOG_TAG, "Added global client for pid:" + Binder.getCallingPid());
367 }
Svetoslav57bf8852013-02-07 19:21:42 -0800368 return userState.getClientState();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700369 } else {
370 userState.mClients.register(client);
371 // If this client is not for the current user we do not
372 // return a state since it is not for the foreground user.
373 // We will send the state to the client on a user switch.
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700374 if (DEBUG) {
375 Slog.i(LOG_TAG, "Added user client for pid:" + Binder.getCallingPid()
376 + " and userId:" + mCurrentUserId);
377 }
Svetoslav57bf8852013-02-07 19:21:42 -0800378 return (resolvedUserId == mCurrentUserId) ? userState.getClientState() : 0;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700379 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700380 }
381 }
382
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700383 public boolean sendAccessibilityEvent(AccessibilityEvent event, int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700384 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700385 final int resolvedUserId = mSecurityPolicy
386 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
387 // This method does nothing for a background user.
388 if (resolvedUserId != mCurrentUserId) {
389 return true; // yes, recycle the event
390 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700391 if (mSecurityPolicy.canDispatchAccessibilityEvent(event)) {
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700392 mSecurityPolicy.updateEventSourceLocked(event);
393 mMainHandler.obtainMessage(MainHandler.MSG_UPDATE_ACTIVE_WINDOW,
394 event.getWindowId(), event.getEventType()).sendToTarget();
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700395 notifyAccessibilityServicesDelayedLocked(event, false);
396 notifyAccessibilityServicesDelayedLocked(event, true);
397 }
Svetoslav Ganov86783472012-06-06 21:12:20 -0700398 if (mHasInputFilter && mInputFilter != null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700399 mMainHandler.obtainMessage(MainHandler.MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER,
Svetoslav Ganove45c0b22012-06-08 17:44:29 -0700400 AccessibilityEvent.obtain(event)).sendToTarget();
Svetoslav Ganov86783472012-06-06 21:12:20 -0700401 }
Svetoslav Ganovcd94caf2012-06-03 19:34:34 -0700402 event.recycle();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700403 getUserStateLocked(resolvedUserId).mHandledFeedbackTypes = 0;
svetoslavganov75986cf2009-05-14 22:28:01 -0700404 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700405 return (OWN_PROCESS_ID != Binder.getCallingPid());
406 }
407
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700408 public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList(int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700409 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700410 final int resolvedUserId = mSecurityPolicy
411 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
Svetoslav Ganov319d55b2013-05-17 19:12:38 -0700412 // The automation service is a fake one and should not be reported
413 // to clients as being installed - it really is not.
414 UserState userState = getUserStateLocked(resolvedUserId);
415 if (userState.mUiAutomationService != null) {
416 List<AccessibilityServiceInfo> installedServices =
417 new ArrayList<AccessibilityServiceInfo>();
418 installedServices.addAll(userState.mInstalledServices);
419 installedServices.remove(userState.mUiAutomationService);
420 return installedServices;
421 }
422 return userState.mInstalledServices;
svetoslavganov75986cf2009-05-14 22:28:01 -0700423 }
424 }
425
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700426 public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType,
427 int userId) {
428 List<AccessibilityServiceInfo> result = null;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700429 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700430 final int resolvedUserId = mSecurityPolicy
431 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
Svetoslav Ganov319d55b2013-05-17 19:12:38 -0700432
433 // The automation service is a fake one and should not be reported
434 // to clients as being enabled. The automation service is always the
435 // only active one, if it exists.
436 UserState userState = getUserStateLocked(resolvedUserId);
437 if (userState.mUiAutomationService != null) {
438 return Collections.emptyList();
439 }
440
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700441 result = mEnabledServicesForFeedbackTempList;
442 result.clear();
Svetoslav Ganov319d55b2013-05-17 19:12:38 -0700443 List<Service> services = userState.mBoundServices;
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700444 while (feedbackType != 0) {
445 final int feedbackTypeBit = (1 << Integer.numberOfTrailingZeros(feedbackType));
446 feedbackType &= ~feedbackTypeBit;
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700447 final int serviceCount = services.size();
448 for (int i = 0; i < serviceCount; i++) {
449 Service service = services.get(i);
Svetoslav Ganovc321c192011-06-03 19:43:23 -0700450 if ((service.mFeedbackType & feedbackTypeBit) != 0) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700451 result.add(service.mAccessibilityServiceInfo);
452 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700453 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700454 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700455 }
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700456 return result;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700457 }
458
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700459 public void interrupt(int userId) {
460 CopyOnWriteArrayList<Service> services;
svetoslavganov75986cf2009-05-14 22:28:01 -0700461 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700462 final int resolvedUserId = mSecurityPolicy
463 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
464 // This method does nothing for a background user.
465 if (resolvedUserId != mCurrentUserId) {
466 return;
467 }
Svetoslav57bf8852013-02-07 19:21:42 -0800468 services = getUserStateLocked(resolvedUserId).mBoundServices;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700469 }
470 for (int i = 0, count = services.size(); i < count; i++) {
471 Service service = services.get(i);
472 try {
473 service.mServiceInterface.onInterrupt();
474 } catch (RemoteException re) {
475 Slog.e(LOG_TAG, "Error during sending interrupt request to "
476 + service.mService, re);
svetoslavganov75986cf2009-05-14 22:28:01 -0700477 }
478 }
479 }
480
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700481 public int addAccessibilityInteractionConnection(IWindow windowToken,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700482 IAccessibilityInteractionConnection connection, int userId) throws RemoteException {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700483 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700484 final int resolvedUserId = mSecurityPolicy
485 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700486 final int windowId = sNextWindowId++;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700487 // If the window is from a process that runs across users such as
488 // the system UI or the system we add it to the global state that
489 // is shared across users.
490 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
491 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
492 windowId, connection, UserHandle.USER_ALL);
493 wrapper.linkToDeath();
494 mGlobalInteractionConnections.put(windowId, wrapper);
495 mGlobalWindowTokens.put(windowId, windowToken.asBinder());
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700496 if (DEBUG) {
497 Slog.i(LOG_TAG, "Added global connection for pid:" + Binder.getCallingPid()
498 + " with windowId: " + windowId);
499 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700500 } else {
501 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
502 windowId, connection, resolvedUserId);
503 wrapper.linkToDeath();
504 UserState userState = getUserStateLocked(resolvedUserId);
505 userState.mInteractionConnections.put(windowId, wrapper);
506 userState.mWindowTokens.put(windowId, windowToken.asBinder());
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700507 if (DEBUG) {
508 Slog.i(LOG_TAG, "Added user connection for pid:" + Binder.getCallingPid()
509 + " with windowId: " + windowId + " and userId:" + mCurrentUserId);
510 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700511 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700512 if (DEBUG) {
513 Slog.i(LOG_TAG, "Adding interaction connection to windowId: " + windowId);
514 }
515 return windowId;
516 }
517 }
518
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700519 public void removeAccessibilityInteractionConnection(IWindow window) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700520 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700521 mSecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(
522 UserHandle.getCallingUserId());
523 IBinder token = window.asBinder();
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700524 final int removedWindowId = removeAccessibilityInteractionConnectionInternalLocked(
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700525 token, mGlobalWindowTokens, mGlobalInteractionConnections);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700526 if (removedWindowId >= 0) {
527 if (DEBUG) {
528 Slog.i(LOG_TAG, "Removed global connection for pid:" + Binder.getCallingPid()
529 + " with windowId: " + removedWindowId);
530 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700531 return;
532 }
533 final int userCount = mUserStates.size();
534 for (int i = 0; i < userCount; i++) {
535 UserState userState = mUserStates.valueAt(i);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700536 final int removedWindowIdForUser =
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700537 removeAccessibilityInteractionConnectionInternalLocked(
538 token, userState.mWindowTokens, userState.mInteractionConnections);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700539 if (removedWindowIdForUser >= 0) {
540 if (DEBUG) {
541 Slog.i(LOG_TAG, "Removed user connection for pid:" + Binder.getCallingPid()
542 + " with windowId: " + removedWindowIdForUser + " and userId:"
543 + mUserStates.keyAt(i));
544 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700545 return;
546 }
547 }
548 }
549 }
550
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700551 private int removeAccessibilityInteractionConnectionInternalLocked(IBinder windowToken,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700552 SparseArray<IBinder> windowTokens,
553 SparseArray<AccessibilityConnectionWrapper> interactionConnections) {
554 final int count = windowTokens.size();
555 for (int i = 0; i < count; i++) {
556 if (windowTokens.valueAt(i) == windowToken) {
557 final int windowId = windowTokens.keyAt(i);
558 windowTokens.removeAt(i);
559 AccessibilityConnectionWrapper wrapper = interactionConnections.get(windowId);
560 wrapper.unlinkToDeath();
561 interactionConnections.remove(windowId);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700562 return windowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700563 }
564 }
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700565 return -1;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700566 }
567
Svetoslavdb7da0e2013-04-22 18:34:02 -0700568 public void registerUiTestAutomationService(IBinder owner,
569 IAccessibilityServiceClient serviceClient,
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800570 AccessibilityServiceInfo accessibilityServiceInfo) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700571 mSecurityPolicy.enforceCallingPermission(Manifest.permission.RETRIEVE_WINDOW_CONTENT,
Svetoslav Ganov0d04e242012-02-21 13:46:36 -0800572 FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE);
Svetoslav57bf8852013-02-07 19:21:42 -0800573
574 accessibilityServiceInfo.setComponentName(sFakeAccessibilityServiceComponentName);
575
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700576 synchronized (mLock) {
Svetoslav57bf8852013-02-07 19:21:42 -0800577 UserState userState = getCurrentUserStateLocked();
578
579 if (userState.mUiAutomationService != null) {
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800580 throw new IllegalStateException("UiAutomationService " + serviceClient
581 + "already registered!");
582 }
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700583
Svetoslav3c55e5c2013-02-27 18:24:28 -0800584 try {
585 owner.linkToDeath(userState.mUiAutomationSerivceOnwerDeathRecipient, 0);
586 } catch (RemoteException re) {
587 Slog.e(LOG_TAG, "Couldn't register for the death of a"
588 + " UiTestAutomationService!", re);
589 return;
590 }
591
592 userState.mUiAutomationServiceOwner = owner;
Svetoslav57bf8852013-02-07 19:21:42 -0800593 userState.mUiAutomationServiceClient = serviceClient;
594
595 // Set the temporary state.
596 userState.mIsAccessibilityEnabled = true;
Svetoslav Ganov657968a2012-09-21 19:08:35 -0700597 userState.mIsTouchExplorationEnabled = false;
Svetoslav38228962013-01-29 01:04:35 -0800598 userState.mIsEnhancedWebAccessibilityEnabled = false;
Svetoslav57bf8852013-02-07 19:21:42 -0800599 userState.mIsDisplayMagnificationEnabled = false;
600 userState.mInstalledServices.add(accessibilityServiceInfo);
601 userState.mEnabledServices.clear();
602 userState.mEnabledServices.add(sFakeAccessibilityServiceComponentName);
603 userState.mTouchExplorationGrantedServices.add(sFakeAccessibilityServiceComponentName);
Svetoslav38228962013-01-29 01:04:35 -0800604
Svetoslav57bf8852013-02-07 19:21:42 -0800605 // Use the new state instead of settings.
606 onUserStateChangedLocked(userState);
607 }
608 }
Svetoslav Ganov657968a2012-09-21 19:08:35 -0700609
Svetoslav57bf8852013-02-07 19:21:42 -0800610 public void unregisterUiTestAutomationService(IAccessibilityServiceClient serviceClient) {
611 synchronized (mLock) {
612 UserState userState = getCurrentUserStateLocked();
613 // Automation service is not bound, so pretend it died to perform clean up.
614 if (userState.mUiAutomationService != null
Svetoslav57bf8852013-02-07 19:21:42 -0800615 && serviceClient != null
Svetoslav07268432013-02-13 14:55:19 -0800616 && userState.mUiAutomationService != null
617 && userState.mUiAutomationService.mServiceInterface != null
Svetoslav57bf8852013-02-07 19:21:42 -0800618 && userState.mUiAutomationService.mServiceInterface.asBinder()
619 == serviceClient.asBinder()) {
620 userState.mUiAutomationService.binderDied();
621 } else {
622 throw new IllegalStateException("UiAutomationService " + serviceClient
623 + " not registered!");
624 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700625 }
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800626 }
627
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700628 public void temporaryEnableAccessibilityStateUntilKeyguardRemoved(
629 ComponentName service, boolean touchExplorationEnabled) {
630 mSecurityPolicy.enforceCallingPermission(
631 Manifest.permission.TEMPORARY_ENABLE_ACCESSIBILITY,
632 TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED);
633 try {
634 if (!mWindowManagerService.isKeyguardLocked()) {
635 return;
636 }
637 } catch (RemoteException re) {
638 return;
639 }
640 synchronized (mLock) {
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700641 // Set the temporary state.
Svetoslav57bf8852013-02-07 19:21:42 -0800642 UserState userState = getCurrentUserStateLocked();
643
644 // This is a nop if UI automation is enabled.
645 if (userState.mUiAutomationService != null) {
646 return;
647 }
648
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700649 userState.mIsAccessibilityEnabled = true;
Svetoslav38228962013-01-29 01:04:35 -0800650 userState.mIsTouchExplorationEnabled = touchExplorationEnabled;
651 userState.mIsEnhancedWebAccessibilityEnabled = false;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700652 userState.mIsDisplayMagnificationEnabled = false;
653 userState.mEnabledServices.clear();
654 userState.mEnabledServices.add(service);
Svetoslav57bf8852013-02-07 19:21:42 -0800655 userState.mBindingServices.clear();
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000656 userState.mTouchExplorationGrantedServices.clear();
657 userState.mTouchExplorationGrantedServices.add(service);
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700658
Svetoslav57bf8852013-02-07 19:21:42 -0800659 // User the current state instead settings.
660 onUserStateChangedLocked(userState);
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800661 }
662 }
663
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700664 boolean onGesture(int gestureId) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700665 synchronized (mLock) {
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700666 boolean handled = notifyGestureLocked(gestureId, false);
667 if (!handled) {
668 handled = notifyGestureLocked(gestureId, true);
Svetoslav Ganov42138042012-03-20 11:51:39 -0700669 }
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700670 return handled;
671 }
672 }
673
Svetoslavc4fccd12013-04-09 12:58:41 -0700674 boolean notifyKeyEvent(KeyEvent event, int policyFlags) {
675 synchronized (mLock) {
676 KeyEvent localClone = KeyEvent.obtain(event);
677 boolean handled = notifyKeyEventLocked(localClone, policyFlags, false);
678 if (!handled) {
679 handled = notifyKeyEventLocked(localClone, policyFlags, true);
680 }
681 return handled;
682 }
683 }
684
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700685 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -0700686 * Gets the bounds of the accessibility focus in the active window.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700687 *
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700688 * @param outBounds The output to which to write the focus bounds.
Svetoslav Ganov86783472012-06-06 21:12:20 -0700689 * @return Whether accessibility focus was found and the bounds are populated.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700690 */
Svetoslav Ganova94c3192012-10-31 18:28:49 -0700691 // TODO: (multi-display) Make sure this works for multiple displays.
Svetoslav Ganov86783472012-06-06 21:12:20 -0700692 boolean getAccessibilityFocusBoundsInActiveWindow(Rect outBounds) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700693 // Instead of keeping track of accessibility focus events per
694 // window to be able to find the focus in the active window,
695 // we take a stateless approach and look it up. This is fine
696 // since we do this only when the user clicks/long presses.
697 Service service = getQueryBridge();
698 final int connectionId = service.mId;
699 AccessibilityInteractionClient client = AccessibilityInteractionClient.getInstance();
700 client.addConnection(connectionId, service);
701 try {
702 AccessibilityNodeInfo root = AccessibilityInteractionClient.getInstance()
703 .getRootInActiveWindow(connectionId);
704 if (root == null) {
705 return false;
706 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700707 AccessibilityNodeInfo focus = root.findFocus(
708 AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
709 if (focus == null) {
710 return false;
711 }
712 focus.getBoundsInScreen(outBounds);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700713
714 MagnificationSpec spec = service.getCompatibleMagnificationSpec(focus.getWindowId());
715 if (spec != null && !spec.isNop()) {
716 outBounds.offset((int) -spec.offsetX, (int) -spec.offsetY);
717 outBounds.scale(1 / spec.scale);
718 }
719
Svetoslav Ganova94c3192012-10-31 18:28:49 -0700720 // Clip to the window rectangle.
721 Rect windowBounds = mTempRect;
722 getActiveWindowBounds(windowBounds);
723 outBounds.intersect(windowBounds);
724 // Clip to the screen rectangle.
725 mDefaultDisplay.getRealSize(mTempPoint);
726 outBounds.intersect(0, 0, mTempPoint.x, mTempPoint.y);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700727
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700728 return true;
729 } finally {
730 client.removeConnection(connectionId);
731 }
732 }
733
Svetoslav Ganov86783472012-06-06 21:12:20 -0700734 /**
735 * Gets the bounds of the active window.
736 *
737 * @param outBounds The output to which to write the bounds.
738 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700739 boolean getActiveWindowBounds(Rect outBounds) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700740 IBinder token;
Svetoslav Ganov86783472012-06-06 21:12:20 -0700741 synchronized (mLock) {
742 final int windowId = mSecurityPolicy.mActiveWindowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700743 token = mGlobalWindowTokens.get(windowId);
744 if (token == null) {
745 token = getCurrentUserStateLocked().mWindowTokens.get(windowId);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700746 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700747 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700748 try {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700749 mWindowManagerService.getWindowFrame(token, outBounds);
750 if (!outBounds.isEmpty()) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700751 return true;
752 }
753 } catch (RemoteException re) {
754 /* ignore */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700755 }
756 return false;
757 }
758
759 int getActiveWindowId() {
760 return mSecurityPolicy.mActiveWindowId;
761 }
762
Svetoslav Ganov6ae8a242012-10-10 13:09:00 -0700763 void onTouchInteractionStart() {
764 mSecurityPolicy.onTouchInteractionStart();
765 }
766
Svetoslav Ganovf772cba2012-10-05 18:49:17 -0700767 void onTouchInteractionEnd() {
768 mSecurityPolicy.onTouchInteractionEnd();
769 }
770
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700771 void onMagnificationStateChanged() {
772 notifyClearAccessibilityNodeInfoCacheLocked();
773 }
774
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700775 private void switchUser(int userId) {
776 synchronized (mLock) {
Svetoslav3b817ae2013-06-19 16:28:30 -0700777 if (mCurrentUserId == userId && mInitialized) {
778 return;
779 }
780
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700781 // Disconnect from services for the old user.
782 UserState oldUserState = getUserStateLocked(mCurrentUserId);
Svetoslav5fec0c52013-02-15 12:18:33 -0800783 oldUserState.onSwitchToAnotherUser();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700784
785 // Disable the local managers for the old user.
786 if (oldUserState.mClients.getRegisteredCallbackCount() > 0) {
787 mMainHandler.obtainMessage(MainHandler.MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER,
788 oldUserState.mUserId, 0).sendToTarget();
789 }
790
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700791 // Announce user changes only if more that one exist.
792 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
793 final boolean announceNewUser = userManager.getUsers().size() > 1;
794
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700795 // The user changed.
796 mCurrentUserId = userId;
797
Svetoslav57bf8852013-02-07 19:21:42 -0800798 UserState userState = getCurrentUserStateLocked();
799 if (userState.mUiAutomationService != null) {
800 // Switching users disables the UI automation service.
801 userState.mUiAutomationService.binderDied();
Svetoslav57bf8852013-02-07 19:21:42 -0800802 }
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700803
Svetoslav5fec0c52013-02-15 12:18:33 -0800804 readConfigurationForUserStateLocked(userState);
805 // Even if reading did not yield change, we have to update
806 // the state since the context in which the current user
807 // state was used has changed since it was inactive.
808 onUserStateChangedLocked(userState);
809
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700810 if (announceNewUser) {
811 // Schedule announcement of the current user if needed.
812 mMainHandler.sendEmptyMessageDelayed(MainHandler.MSG_ANNOUNCE_NEW_USER_IF_NEEDED,
813 WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS);
814 }
Svetoslav Ganov86783472012-06-06 21:12:20 -0700815 }
816 }
817
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700818 private void removeUser(int userId) {
819 synchronized (mLock) {
820 mUserStates.remove(userId);
821 }
Svetoslav Ganov385d9f22012-06-07 16:35:04 -0700822 }
823
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700824 private Service getQueryBridge() {
825 if (mQueryBridge == null) {
826 AccessibilityServiceInfo info = new AccessibilityServiceInfo();
Svetoslav108fe592013-04-26 15:08:16 -0700827 info.setCapabilities(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT);
Svetoslav57bf8852013-02-07 19:21:42 -0800828 mQueryBridge = new Service(UserHandle.USER_NULL,
829 sFakeAccessibilityServiceComponentName, info);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700830 }
831 return mQueryBridge;
832 }
833
Svetoslav Ganov42138042012-03-20 11:51:39 -0700834 private boolean notifyGestureLocked(int gestureId, boolean isDefault) {
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700835 // TODO: Now we are giving the gestures to the last enabled
836 // service that can handle them which is the last one
837 // in our list since we write the last enabled as the
838 // last record in the enabled services setting. Ideally,
839 // the user should make the call which service handles
840 // gestures. However, only one service should handle
Svetoslav Ganove4abc512012-05-09 11:02:38 -0700841 // gestures to avoid user frustration when different
842 // behavior is observed from different combinations of
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700843 // enabled accessibility services.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700844 UserState state = getCurrentUserStateLocked();
Svetoslav57bf8852013-02-07 19:21:42 -0800845 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
846 Service service = state.mBoundServices.get(i);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -0700847 if (service.mRequestTouchExplorationMode && service.mIsDefault == isDefault) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700848 service.notifyGesture(gestureId);
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700849 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700850 }
851 }
852 return false;
853 }
854
Svetoslavc4fccd12013-04-09 12:58:41 -0700855 private boolean notifyKeyEventLocked(KeyEvent event, int policyFlags, boolean isDefault) {
856 // TODO: Now we are giving the key events to the last enabled
Svetoslav688a6972013-04-16 18:55:38 -0700857 // service that can handle them Ideally, the user should
858 // make the call which service handles key events. However,
859 // only one service should handle key events to avoid user
860 // frustration when different behavior is observed from
861 // different combinations of enabled accessibility services.
Svetoslavc4fccd12013-04-09 12:58:41 -0700862 UserState state = getCurrentUserStateLocked();
863 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
864 Service service = state.mBoundServices.get(i);
Svetoslav688a6972013-04-16 18:55:38 -0700865 // Key events are handled only by services that declared
866 // this capability and requested to filter key events.
867 if (!service.mRequestFilterKeyEvents ||
868 (service.mAccessibilityServiceInfo.getCapabilities() & AccessibilityServiceInfo
869 .CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS) == 0) {
870 continue;
871 }
Svetoslavc4fccd12013-04-09 12:58:41 -0700872 if (service.mIsDefault == isDefault) {
873 service.notifyKeyEvent(event, policyFlags);
874 return true;
875 }
876 }
877 return false;
878 }
879
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700880 private void notifyClearAccessibilityNodeInfoCacheLocked() {
881 UserState state = getCurrentUserStateLocked();
Svetoslav57bf8852013-02-07 19:21:42 -0800882 for (int i = state.mBoundServices.size() - 1; i >= 0; i--) {
883 Service service = state.mBoundServices.get(i);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -0700884 service.notifyClearAccessibilityNodeInfoCache();
885 }
886 }
887
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800888 /**
889 * Removes an AccessibilityInteractionConnection.
890 *
891 * @param windowId The id of the window to which the connection is targeted.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700892 * @param userId The id of the user owning the connection. UserHandle.USER_ALL
893 * if global.
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800894 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700895 private void removeAccessibilityInteractionConnectionLocked(int windowId, int userId) {
896 if (userId == UserHandle.USER_ALL) {
897 mGlobalWindowTokens.remove(windowId);
898 mGlobalInteractionConnections.remove(windowId);
899 } else {
900 UserState userState = getCurrentUserStateLocked();
901 userState.mWindowTokens.remove(windowId);
902 userState.mInteractionConnections.remove(windowId);
903 }
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800904 if (DEBUG) {
905 Slog.i(LOG_TAG, "Removing interaction connection to windowId: " + windowId);
906 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700907 }
908
Svetoslav57bf8852013-02-07 19:21:42 -0800909 private boolean readInstalledAccessibilityServiceLocked(UserState userState) {
910 mTempAccessibilityServiceInfoList.clear();
svetoslavganov75986cf2009-05-14 22:28:01 -0700911
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700912 List<ResolveInfo> installedServices = mPackageManager.queryIntentServicesAsUser(
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700913 new Intent(AccessibilityService.SERVICE_INTERFACE),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700914 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
915 mCurrentUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -0700916
917 for (int i = 0, count = installedServices.size(); i < count; i++) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700918 ResolveInfo resolveInfo = installedServices.get(i);
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700919 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700920 if (!android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE.equals(
921 serviceInfo.permission)) {
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700922 Slog.w(LOG_TAG, "Skipping accessibilty service " + new ComponentName(
923 serviceInfo.packageName, serviceInfo.name).flattenToShortString()
924 + ": it does not require the permission "
925 + android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE);
926 continue;
927 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700928 AccessibilityServiceInfo accessibilityServiceInfo;
929 try {
930 accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext);
Svetoslav57bf8852013-02-07 19:21:42 -0800931 mTempAccessibilityServiceInfoList.add(accessibilityServiceInfo);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700932 } catch (XmlPullParserException xppe) {
933 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", xppe);
934 } catch (IOException ioe) {
935 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", ioe);
936 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700937 }
Svetoslav57bf8852013-02-07 19:21:42 -0800938
939 if (!mTempAccessibilityServiceInfoList.equals(userState.mInstalledServices)) {
940 userState.mInstalledServices.clear();
941 userState.mInstalledServices.addAll(mTempAccessibilityServiceInfoList);
942 mTempAccessibilityServiceInfoList.clear();
943 return true;
944 }
945
946 mTempAccessibilityServiceInfoList.clear();
947 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -0700948 }
949
Svetoslav57bf8852013-02-07 19:21:42 -0800950 private boolean readEnabledAccessibilityServicesLocked(UserState userState) {
951 mTempComponentNameSet.clear();
952 readComponentNamesFromSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
953 userState.mUserId, mTempComponentNameSet);
954 if (!mTempComponentNameSet.equals(userState.mEnabledServices)) {
955 userState.mEnabledServices.clear();
956 userState.mEnabledServices.addAll(mTempComponentNameSet);
957 mTempComponentNameSet.clear();
958 return true;
959 }
960 mTempComponentNameSet.clear();
961 return false;
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700962 }
963
Svetoslav57bf8852013-02-07 19:21:42 -0800964 private boolean readTouchExplorationGrantedAccessibilityServicesLocked(
965 UserState userState) {
966 mTempComponentNameSet.clear();
967 readComponentNamesFromSettingLocked(
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000968 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
Svetoslav57bf8852013-02-07 19:21:42 -0800969 userState.mUserId, mTempComponentNameSet);
970 if (!mTempComponentNameSet.equals(userState.mTouchExplorationGrantedServices)) {
971 userState.mTouchExplorationGrantedServices.clear();
972 userState.mTouchExplorationGrantedServices.addAll(mTempComponentNameSet);
973 mTempComponentNameSet.clear();
974 return true;
975 }
976 mTempComponentNameSet.clear();
977 return false;
Svetoslav Ganov447d9462013-02-01 19:46:20 +0000978 }
979
svetoslavganov75986cf2009-05-14 22:28:01 -0700980 /**
981 * Performs {@link AccessibilityService}s delayed notification. The delay is configurable
982 * and denotes the period after the last event before notifying the service.
983 *
984 * @param event The event.
985 * @param isDefault True to notify default listeners, not default services.
986 */
987 private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event,
988 boolean isDefault) {
Charles Chen85b598b2009-07-29 17:23:50 -0700989 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700990 UserState state = getCurrentUserStateLocked();
Svetoslav57bf8852013-02-07 19:21:42 -0800991 for (int i = 0, count = state.mBoundServices.size(); i < count; i++) {
992 Service service = state.mBoundServices.get(i);
svetoslavganov75986cf2009-05-14 22:28:01 -0700993
Charles Chen85b598b2009-07-29 17:23:50 -0700994 if (service.mIsDefault == isDefault) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700995 if (canDispathEventLocked(service, event, state.mHandledFeedbackTypes)) {
996 state.mHandledFeedbackTypes |= service.mFeedbackType;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700997 service.notifyAccessibilityEvent(event);
Charles Chen85b598b2009-07-29 17:23:50 -0700998 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700999 }
1000 }
Charles Chen85b598b2009-07-29 17:23:50 -07001001 } catch (IndexOutOfBoundsException oobe) {
1002 // An out of bounds exception can happen if services are going away
1003 // as the for loop is running. If that happens, just bail because
1004 // there are no more services to notify.
1005 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07001006 }
1007 }
1008
Svetoslav57bf8852013-02-07 19:21:42 -08001009 private void addServiceLocked(Service service, UserState userState) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001010 try {
Svetoslav2602a392013-04-15 16:26:39 -07001011 service.linkToOwnDeathLocked();
Svetoslav57bf8852013-02-07 19:21:42 -08001012 userState.mBoundServices.add(service);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001013 userState.mComponentNameToServiceMap.put(service.mComponentName, service);
Svetoslav57bf8852013-02-07 19:21:42 -08001014 } catch (RemoteException re) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001015 /* do nothing */
1016 }
1017 }
1018
1019 /**
1020 * Removes a service.
svetoslavganov75986cf2009-05-14 22:28:01 -07001021 *
1022 * @param service The service.
1023 * @return True if the service was removed, false otherwise.
1024 */
Svetoslav6f8218a2013-02-20 16:28:39 -08001025 private void removeServiceLocked(Service service, UserState userState) {
Svetoslav57bf8852013-02-07 19:21:42 -08001026 userState.mBoundServices.remove(service);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001027 userState.mComponentNameToServiceMap.remove(service.mComponentName);
Svetoslav2602a392013-04-15 16:26:39 -07001028 service.unlinkToOwnDeathLocked();
svetoslavganov75986cf2009-05-14 22:28:01 -07001029 }
1030
1031 /**
1032 * Determines if given event can be dispatched to a service based on the package of the
1033 * event source and already notified services for that event type. Specifically, a
1034 * service is notified if it is interested in events from the package and no other service
1035 * providing the same feedback type has been notified. Exception are services the
1036 * provide generic feedback (feedback type left as a safety net for unforeseen feedback
1037 * types) which are always notified.
1038 *
1039 * @param service The potential receiver.
1040 * @param event The event.
1041 * @param handledFeedbackTypes The feedback types for which services have been notified.
1042 * @return True if the listener should be notified, false otherwise.
1043 */
1044 private boolean canDispathEventLocked(Service service, AccessibilityEvent event,
1045 int handledFeedbackTypes) {
1046
Svetoslav38228962013-01-29 01:04:35 -08001047 if (!service.canReceiveEventsLocked()) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001048 return false;
1049 }
1050
Svetoslav Ganov42138042012-03-20 11:51:39 -07001051 if (!event.isImportantForAccessibility()
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001052 && (service.mFetchFlags
1053 & AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07001054 return false;
1055 }
1056
svetoslavganov75986cf2009-05-14 22:28:01 -07001057 int eventType = event.getEventType();
1058 if ((service.mEventTypes & eventType) != eventType) {
1059 return false;
1060 }
1061
1062 Set<String> packageNames = service.mPackageNames;
1063 CharSequence packageName = event.getPackageName();
1064
1065 if (packageNames.isEmpty() || packageNames.contains(packageName)) {
1066 int feedbackType = service.mFeedbackType;
1067 if ((handledFeedbackTypes & feedbackType) != feedbackType
1068 || feedbackType == AccessibilityServiceInfo.FEEDBACK_GENERIC) {
1069 return true;
1070 }
1071 }
1072
1073 return false;
1074 }
1075
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001076 private void unbindAllServicesLocked(UserState userState) {
Svetoslav57bf8852013-02-07 19:21:42 -08001077 List<Service> services = userState.mBoundServices;
svetoslavganov75986cf2009-05-14 22:28:01 -07001078 for (int i = 0, count = services.size(); i < count; i++) {
1079 Service service = services.get(i);
Svetoslav57bf8852013-02-07 19:21:42 -08001080 if (service.unbindLocked()) {
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001081 i--;
1082 count--;
1083 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001084 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001085 }
1086
1087 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001088 * Populates a set with the {@link ComponentName}s stored in a colon
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001089 * separated value setting for a given user.
svetoslavganov75986cf2009-05-14 22:28:01 -07001090 *
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001091 * @param settingName The setting to parse.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001092 * @param userId The user id.
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001093 * @param outComponentNames The output component names.
svetoslavganov75986cf2009-05-14 22:28:01 -07001094 */
Svetoslav57bf8852013-02-07 19:21:42 -08001095 private void readComponentNamesFromSettingLocked(String settingName, int userId,
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001096 Set<ComponentName> outComponentNames) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001097 String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(),
1098 settingName, userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001099 outComponentNames.clear();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001100 if (settingValue != null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001101 TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001102 splitter.setString(settingValue);
svetoslavganov75986cf2009-05-14 22:28:01 -07001103 while (splitter.hasNext()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001104 String str = splitter.next();
1105 if (str == null || str.length() <= 0) {
1106 continue;
1107 }
1108 ComponentName enabledService = ComponentName.unflattenFromString(str);
1109 if (enabledService != null) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001110 outComponentNames.add(enabledService);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001111 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001112 }
1113 }
1114 }
1115
1116 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001117 * Persists the component names in the specified setting in a
1118 * colon separated fashion.
1119 *
1120 * @param settingName The setting name.
1121 * @param componentNames The component names.
1122 */
1123 private void persistComponentNamesToSettingLocked(String settingName,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001124 Set<ComponentName> componentNames, int userId) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001125 StringBuilder builder = new StringBuilder();
1126 for (ComponentName componentName : componentNames) {
1127 if (builder.length() > 0) {
1128 builder.append(COMPONENT_NAME_SEPARATOR);
1129 }
1130 builder.append(componentName.flattenToShortString());
1131 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001132 Settings.Secure.putStringForUser(mContext.getContentResolver(),
1133 settingName, builder.toString(), userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001134 }
1135
Svetoslav57bf8852013-02-07 19:21:42 -08001136 private void manageServicesLocked(UserState userState) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001137 Map<ComponentName, Service> componentNameToServiceMap =
1138 userState.mComponentNameToServiceMap;
1139 boolean isEnabled = userState.mIsAccessibilityEnabled;
svetoslavganov75986cf2009-05-14 22:28:01 -07001140
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001141 for (int i = 0, count = userState.mInstalledServices.size(); i < count; i++) {
1142 AccessibilityServiceInfo installedService = userState.mInstalledServices.get(i);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001143 ComponentName componentName = ComponentName.unflattenFromString(
1144 installedService.getId());
svetoslavganov75986cf2009-05-14 22:28:01 -07001145 Service service = componentNameToServiceMap.get(componentName);
1146
Svetoslav Ganovf2245aa2010-12-28 15:48:52 -08001147 if (isEnabled) {
Svetoslav57bf8852013-02-07 19:21:42 -08001148 // Wait for the binding if it is in process.
1149 if (userState.mBindingServices.contains(componentName)) {
1150 continue;
1151 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001152 if (userState.mEnabledServices.contains(componentName)) {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001153 if (service == null) {
Svetoslav57bf8852013-02-07 19:21:42 -08001154 service = new Service(userState.mUserId, componentName, installedService);
1155 } else if (userState.mBoundServices.contains(service)) {
1156 continue;
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001157 }
Svetoslav57bf8852013-02-07 19:21:42 -08001158 service.bindLocked();
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001159 } else {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001160 if (service != null) {
Svetoslav57bf8852013-02-07 19:21:42 -08001161 service.unbindLocked();
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001162 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001163 }
1164 } else {
1165 if (service != null) {
Svetoslav57bf8852013-02-07 19:21:42 -08001166 service.unbindLocked();
1167 } else {
1168 userState.mBindingServices.remove(componentName);
svetoslavganov75986cf2009-05-14 22:28:01 -07001169 }
1170 }
1171 }
Svetoslavcefac5a2013-03-22 17:09:11 -07001172
1173 // No enabled installed services => disable accessibility to avoid
1174 // sending accessibility events with no recipient across processes.
1175 if (isEnabled && userState.mEnabledServices.isEmpty()) {
1176 userState.mIsAccessibilityEnabled = false;
1177 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1178 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId);
1179 }
Svetoslav Ganov37fedf82011-09-14 10:44:09 -07001180 }
1181
Svetoslav57bf8852013-02-07 19:21:42 -08001182 private void scheduleUpdateClientsIfNeededLocked(UserState userState) {
1183 final int clientState = userState.getClientState();
1184 if (userState.mLastSentClientState != clientState
1185 && (mGlobalClients.getRegisteredCallbackCount() > 0
1186 || userState.mClients.getRegisteredCallbackCount() > 0)) {
1187 userState.mLastSentClientState = clientState;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001188 mMainHandler.obtainMessage(MainHandler.MSG_SEND_STATE_TO_CLIENTS,
1189 clientState, userState.mUserId) .sendToTarget();
1190 }
1191 }
1192
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001193 private void scheduleUpdateInputFilter(UserState userState) {
1194 mMainHandler.obtainMessage(MainHandler.MSG_UPDATE_INPUT_FILTER, userState).sendToTarget();
1195 }
1196
1197 private void updateInputFilter(UserState userState) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001198 boolean setInputFilter = false;
1199 AccessibilityInputFilter inputFilter = null;
1200 synchronized (mLock) {
Svetoslavca523d62013-04-26 13:11:11 -07001201 int flags = 0;
1202 if (userState.mIsDisplayMagnificationEnabled) {
1203 flags |= AccessibilityInputFilter.FLAG_FEATURE_SCREEN_MAGNIFIER;
1204 }
1205 // Touch exploration without accessibility makes no sense.
1206 if (userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled) {
1207 flags |= AccessibilityInputFilter.FLAG_FEATURE_TOUCH_EXPLORATION;
1208 }
1209 if (userState.mIsFilterKeyEventsEnabled) {
1210 flags |= AccessibilityInputFilter.FLAG_FEATURE_FILTER_KEY_EVENTS;
1211 }
1212 if (flags != 0) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001213 if (!mHasInputFilter) {
1214 mHasInputFilter = true;
1215 if (mInputFilter == null) {
1216 mInputFilter = new AccessibilityInputFilter(mContext,
1217 AccessibilityManagerService.this);
1218 }
1219 inputFilter = mInputFilter;
1220 setInputFilter = true;
1221 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001222 mInputFilter.setEnabledFeatures(flags);
1223 } else {
1224 if (mHasInputFilter) {
1225 mHasInputFilter = false;
Svetoslavca523d62013-04-26 13:11:11 -07001226 mInputFilter.disableFeatures();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001227 inputFilter = null;
1228 setInputFilter = true;
1229 }
1230 }
1231 }
1232 if (setInputFilter) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001233 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001234 mWindowManagerService.setInputFilter(inputFilter);
svetoslavganov75986cf2009-05-14 22:28:01 -07001235 } catch (RemoteException re) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001236 /* ignore */
svetoslavganov75986cf2009-05-14 22:28:01 -07001237 }
1238 }
1239 }
1240
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001241 private void showEnableTouchExplorationDialog(final Service service) {
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001242 synchronized (mLock) {
Svetoslav57bf8852013-02-07 19:21:42 -08001243 String label = service.mResolveInfo.loadLabel(
1244 mContext.getPackageManager()).toString();
1245
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001246 final UserState state = getCurrentUserStateLocked();
1247 if (state.mIsTouchExplorationEnabled) {
1248 return;
1249 }
1250 if (mEnableTouchExplorationDialog != null
1251 && mEnableTouchExplorationDialog.isShowing()) {
1252 return;
1253 }
1254 mEnableTouchExplorationDialog = new AlertDialog.Builder(mContext)
1255 .setIconAttribute(android.R.attr.alertDialogIcon)
1256 .setPositiveButton(android.R.string.ok, new OnClickListener() {
1257 @Override
1258 public void onClick(DialogInterface dialog, int which) {
1259 // The user allowed the service to toggle touch exploration.
1260 state.mTouchExplorationGrantedServices.add(service.mComponentName);
1261 persistComponentNamesToSettingLocked(
1262 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
1263 state.mTouchExplorationGrantedServices, state.mUserId);
1264 // Enable touch exploration.
Svetoslav57bf8852013-02-07 19:21:42 -08001265 UserState userState = getUserStateLocked(service.mUserId);
1266 userState.mIsTouchExplorationEnabled = true;
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001267 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1268 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1,
1269 service.mUserId);
Svetoslav57bf8852013-02-07 19:21:42 -08001270 onUserStateChangedLocked(userState);
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001271 }
1272 })
1273 .setNegativeButton(android.R.string.cancel, new OnClickListener() {
1274 @Override
1275 public void onClick(DialogInterface dialog, int which) {
1276 dialog.dismiss();
1277 }
1278 })
1279 .setTitle(R.string.enable_explore_by_touch_warning_title)
1280 .setMessage(mContext.getString(
1281 R.string.enable_explore_by_touch_warning_message, label))
1282 .create();
1283 mEnableTouchExplorationDialog.getWindow().setType(
1284 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
1285 mEnableTouchExplorationDialog.getWindow().getAttributes().privateFlags
1286 |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
1287 mEnableTouchExplorationDialog.setCanceledOnTouchOutside(true);
1288 mEnableTouchExplorationDialog.show();
1289 }
1290 }
1291
Svetoslav57bf8852013-02-07 19:21:42 -08001292 private void onUserStateChangedLocked(UserState userState) {
Svetoslav3b817ae2013-06-19 16:28:30 -07001293 // TODO: Remove this hack
1294 mInitialized = true;
Svetoslav11adf6d2013-04-24 14:51:29 -07001295 updateLegacyCapabilities(userState);
Svetoslav57bf8852013-02-07 19:21:42 -08001296 updateServicesLocked(userState);
Svetoslavca523d62013-04-26 13:11:11 -07001297 updateFilterKeyEventsLocked(userState);
Svetoslav57bf8852013-02-07 19:21:42 -08001298 updateTouchExplorationLocked(userState);
1299 updateEnhancedWebAccessibilityLocked(userState);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001300 scheduleUpdateInputFilter(userState);
Svetoslav57bf8852013-02-07 19:21:42 -08001301 scheduleUpdateClientsIfNeededLocked(userState);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001302 }
1303
Svetoslav11adf6d2013-04-24 14:51:29 -07001304 private void updateLegacyCapabilities(UserState userState) {
1305 // Up to JB-MR1 we had a white list with services that can enable touch
1306 // exploration. When a service is first started we show a dialog to the
1307 // use to get a permission to white list the service.
1308 final int installedServiceCount = userState.mInstalledServices.size();
1309 for (int i = 0; i < installedServiceCount; i++) {
1310 AccessibilityServiceInfo serviceInfo = userState.mInstalledServices.get(i);
1311 ResolveInfo resolveInfo = serviceInfo.getResolveInfo();
1312 if ((serviceInfo.getCapabilities()
1313 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION) == 0
1314 && resolveInfo.serviceInfo.applicationInfo.targetSdkVersion
1315 <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1316 ComponentName componentName = new ComponentName(
1317 resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
1318 if (userState.mTouchExplorationGrantedServices.contains(componentName)) {
1319 serviceInfo.setCapabilities(serviceInfo.getCapabilities()
1320 | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION);
1321 }
1322 }
1323 }
1324 }
1325
Svetoslavca523d62013-04-26 13:11:11 -07001326 private void updateFilterKeyEventsLocked(UserState userState) {
1327 final int serviceCount = userState.mBoundServices.size();
1328 for (int i = 0; i < serviceCount; i++) {
1329 Service service = userState.mBoundServices.get(i);
1330 if (service.mRequestFilterKeyEvents
1331 && (service.mAccessibilityServiceInfo.getCapabilities()
1332 & AccessibilityServiceInfo
1333 .CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS) != 0) {
1334 userState.mIsFilterKeyEventsEnabled = true;
1335 return;
1336 }
1337 }
1338 userState.mIsFilterKeyEventsEnabled = false;
1339 }
1340
Svetoslav57bf8852013-02-07 19:21:42 -08001341 private void updateServicesLocked(UserState userState) {
1342 if (userState.mIsAccessibilityEnabled) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001343 manageServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001344 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001345 unbindAllServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001346 }
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001347 }
1348
Svetoslav57bf8852013-02-07 19:21:42 -08001349 private boolean readConfigurationForUserStateLocked(UserState userState) {
1350 boolean somthingChanged = false;
1351 somthingChanged |= readAccessibilityEnabledSettingLocked(userState);
1352 somthingChanged |= readInstalledAccessibilityServiceLocked(userState);
1353 somthingChanged |= readEnabledAccessibilityServicesLocked(userState);
1354 somthingChanged |= readTouchExplorationGrantedAccessibilityServicesLocked(userState);
1355 somthingChanged |= readTouchExplorationEnabledSettingLocked(userState);
1356 somthingChanged |= readEnhancedWebAccessibilityEnabledChangedLocked(userState);
1357 somthingChanged |= readDisplayMagnificationEnabledSettingLocked(userState);
1358 return somthingChanged;
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001359 }
1360
Svetoslav57bf8852013-02-07 19:21:42 -08001361 private boolean readAccessibilityEnabledSettingLocked(UserState userState) {
1362 final boolean accessibilityEnabled = Settings.Secure.getIntForUser(
1363 mContext.getContentResolver(),
1364 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId) == 1;
1365 if (accessibilityEnabled != userState.mIsAccessibilityEnabled) {
1366 userState.mIsAccessibilityEnabled = accessibilityEnabled;
1367 return true;
1368 }
1369 return false;
1370 }
1371
1372 private boolean readTouchExplorationEnabledSettingLocked(UserState userState) {
1373 final boolean touchExplorationEnabled = Settings.Secure.getIntForUser(
1374 mContext.getContentResolver(),
1375 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId) == 1;
1376 if (touchExplorationEnabled != userState.mIsTouchExplorationEnabled) {
1377 userState.mIsTouchExplorationEnabled = touchExplorationEnabled;
1378 return true;
1379 }
1380 return false;
1381 }
1382
1383 private boolean readDisplayMagnificationEnabledSettingLocked(UserState userState) {
1384 final boolean displayMagnificationEnabled = Settings.Secure.getIntForUser(
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001385 mContext.getContentResolver(),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001386 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
1387 0, userState.mUserId) == 1;
Svetoslav57bf8852013-02-07 19:21:42 -08001388 if (displayMagnificationEnabled != userState.mIsDisplayMagnificationEnabled) {
1389 userState.mIsDisplayMagnificationEnabled = displayMagnificationEnabled;
1390 return true;
1391 }
1392 return false;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001393 }
1394
Svetoslav57bf8852013-02-07 19:21:42 -08001395 private boolean readEnhancedWebAccessibilityEnabledChangedLocked(UserState userState) {
1396 final boolean enhancedWeAccessibilityEnabled = Settings.Secure.getIntForUser(
1397 mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
1398 0, userState.mUserId) == 1;
1399 if (enhancedWeAccessibilityEnabled != userState.mIsEnhancedWebAccessibilityEnabled) {
1400 userState.mIsEnhancedWebAccessibilityEnabled = enhancedWeAccessibilityEnabled;
1401 return true;
1402 }
1403 return false;
1404 }
1405
1406 private void updateTouchExplorationLocked(UserState userState) {
Svetoslav042ccec2013-05-08 11:13:11 -07001407 boolean enabled = false;
Svetoslav57bf8852013-02-07 19:21:42 -08001408 final int serviceCount = userState.mBoundServices.size();
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001409 for (int i = 0; i < serviceCount; i++) {
Svetoslav57bf8852013-02-07 19:21:42 -08001410 Service service = userState.mBoundServices.get(i);
Svetoslav042ccec2013-05-08 11:13:11 -07001411 if (canRequestAndRequestsTouchExplorationLocked(service)) {
1412 enabled = true;
Svetoslav57bf8852013-02-07 19:21:42 -08001413 break;
1414 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001415 }
Svetoslav042ccec2013-05-08 11:13:11 -07001416 if (enabled != userState.mIsTouchExplorationEnabled) {
1417 userState.mIsTouchExplorationEnabled = enabled;
1418 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1419 Settings.Secure.TOUCH_EXPLORATION_ENABLED, enabled ? 1 : 0,
1420 userState.mUserId);
1421 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001422 }
1423
Svetoslav042ccec2013-05-08 11:13:11 -07001424 private boolean canRequestAndRequestsTouchExplorationLocked(Service service) {
1425 // Service not ready or cannot request the feature - well nothing to do.
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001426 if (!service.canReceiveEventsLocked() || !service.mRequestTouchExplorationMode) {
Svetoslav57bf8852013-02-07 19:21:42 -08001427 return false;
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001428 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001429 // UI test automation service can always enable it.
1430 if (service.mIsAutomation) {
Svetoslav57bf8852013-02-07 19:21:42 -08001431 return true;
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001432 }
1433 if (service.mResolveInfo.serviceInfo.applicationInfo.targetSdkVersion
1434 <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
1435 // Up to JB-MR1 we had a white list with services that can enable touch
1436 // exploration. When a service is first started we show a dialog to the
1437 // use to get a permission to white list the service.
Svetoslav042ccec2013-05-08 11:13:11 -07001438 UserState userState = getUserStateLocked(service.mUserId);
1439 if (userState.mTouchExplorationGrantedServices.contains(service.mComponentName)) {
Svetoslav57bf8852013-02-07 19:21:42 -08001440 return true;
Svetoslav042ccec2013-05-08 11:13:11 -07001441 } else if (mEnableTouchExplorationDialog == null
1442 || !mEnableTouchExplorationDialog.isShowing()) {
1443 mMainHandler.obtainMessage(
1444 MainHandler.MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG,
1445 service).sendToTarget();
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001446 }
1447 } else {
Svetoslav688a6972013-04-16 18:55:38 -07001448 // Starting in JB-MR2 we request an accessibility service to declare
1449 // certain capabilities in its meta-data to allow it to enable the
1450 // corresponding features.
Svetoslav042ccec2013-05-08 11:13:11 -07001451 if ((service.mAccessibilityServiceInfo.getCapabilities()
Svetoslav688a6972013-04-16 18:55:38 -07001452 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION) != 0) {
Svetoslav57bf8852013-02-07 19:21:42 -08001453 return true;
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001454 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001455 }
Svetoslav57bf8852013-02-07 19:21:42 -08001456 return false;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001457 }
1458
Svetoslav57bf8852013-02-07 19:21:42 -08001459 private void updateEnhancedWebAccessibilityLocked(UserState userState) {
Svetoslav042ccec2013-05-08 11:13:11 -07001460 boolean enabled = false;
Svetoslav57bf8852013-02-07 19:21:42 -08001461 final int serviceCount = userState.mBoundServices.size();
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001462 for (int i = 0; i < serviceCount; i++) {
Svetoslav57bf8852013-02-07 19:21:42 -08001463 Service service = userState.mBoundServices.get(i);
Svetoslav042ccec2013-05-08 11:13:11 -07001464 if (canRequestAndRequestsEnhancedWebAccessibilityLocked(service)) {
1465 enabled = true;
1466 break;
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001467 }
1468 }
Svetoslav042ccec2013-05-08 11:13:11 -07001469 if (enabled != userState.mIsEnhancedWebAccessibilityEnabled) {
1470 userState.mIsEnhancedWebAccessibilityEnabled = enabled;
1471 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1472 Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION, enabled ? 1 : 0,
1473 userState.mUserId);
1474 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001475 }
1476
Svetoslav042ccec2013-05-08 11:13:11 -07001477 private boolean canRequestAndRequestsEnhancedWebAccessibilityLocked(Service service) {
Svetoslav57bf8852013-02-07 19:21:42 -08001478 if (!service.canReceiveEventsLocked() || !service.mRequestEnhancedWebAccessibility ) {
1479 return false;
1480 }
Svetoslav688a6972013-04-16 18:55:38 -07001481 if (service.mIsAutomation || (service.mAccessibilityServiceInfo.getCapabilities()
1482 & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_ENHANCED_WEB_ACCESSIBILITY) != 0) {
Svetoslav57bf8852013-02-07 19:21:42 -08001483 return true;
1484 }
1485 return false;
Svetoslav38228962013-01-29 01:04:35 -08001486 }
1487
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08001488 @Override
1489 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1490 mSecurityPolicy.enforceCallingPermission(Manifest.permission.DUMP, FUNCTION_DUMP);
1491 synchronized (mLock) {
1492 pw.println("ACCESSIBILITY MANAGER (dumpsys accessibility)");
1493 pw.println();
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08001494 final int userCount = mUserStates.size();
1495 for (int i = 0; i < userCount; i++) {
1496 UserState userState = mUserStates.valueAt(i);
1497 pw.append("User state[attributes:{id=" + userState.mUserId);
1498 pw.append(", currentUser=" + (userState.mUserId == mCurrentUserId));
1499 pw.append(", accessibilityEnabled=" + userState.mIsAccessibilityEnabled);
1500 pw.append(", touchExplorationEnabled=" + userState.mIsTouchExplorationEnabled);
1501 pw.append(", displayMagnificationEnabled="
1502 + userState.mIsDisplayMagnificationEnabled);
Svetoslav57bf8852013-02-07 19:21:42 -08001503 if (userState.mUiAutomationService != null) {
1504 pw.append(", ");
1505 userState.mUiAutomationService.dump(fd, pw, args);
1506 pw.println();
1507 }
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08001508 pw.append("}");
1509 pw.println();
1510 pw.append(" services:{");
Svetoslav57bf8852013-02-07 19:21:42 -08001511 final int serviceCount = userState.mBoundServices.size();
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08001512 for (int j = 0; j < serviceCount; j++) {
1513 if (j > 0) {
1514 pw.append(", ");
1515 pw.println();
1516 pw.append(" ");
1517 }
Svetoslav57bf8852013-02-07 19:21:42 -08001518 Service service = userState.mBoundServices.get(j);
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08001519 service.dump(fd, pw, args);
1520 }
1521 pw.println("}]");
1522 pw.println();
1523 }
1524 }
1525 }
1526
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001527 private class AccessibilityConnectionWrapper implements DeathRecipient {
1528 private final int mWindowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001529 private final int mUserId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001530 private final IAccessibilityInteractionConnection mConnection;
1531
1532 public AccessibilityConnectionWrapper(int windowId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001533 IAccessibilityInteractionConnection connection, int userId) {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001534 mWindowId = windowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001535 mUserId = userId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001536 mConnection = connection;
1537 }
1538
1539 public void linkToDeath() throws RemoteException {
1540 mConnection.asBinder().linkToDeath(this, 0);
1541 }
1542
1543 public void unlinkToDeath() {
1544 mConnection.asBinder().unlinkToDeath(this, 0);
1545 }
1546
1547 @Override
1548 public void binderDied() {
1549 unlinkToDeath();
1550 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001551 removeAccessibilityInteractionConnectionLocked(mWindowId, mUserId);
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001552 }
1553 }
1554 }
1555
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001556 private final class MainHandler extends Handler {
1557 public static final int MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER = 1;
1558 public static final int MSG_SEND_STATE_TO_CLIENTS = 2;
1559 public static final int MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER = 3;
Svetoslav57bf8852013-02-07 19:21:42 -08001560 public static final int MSG_UPDATE_ACTIVE_WINDOW = 4;
1561 public static final int MSG_ANNOUNCE_NEW_USER_IF_NEEDED = 5;
1562 public static final int MSG_UPDATE_INPUT_FILTER = 6;
1563 public static final int MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG = 7;
Svetoslavc4fccd12013-04-09 12:58:41 -07001564 public static final int MSG_SEND_KEY_EVENT_TO_INPUT_FILTER = 8;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001565
1566 public MainHandler(Looper looper) {
1567 super(looper);
1568 }
1569
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001570 @Override
1571 public void handleMessage(Message msg) {
1572 final int type = msg.what;
1573 switch (type) {
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001574 case MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER: {
1575 AccessibilityEvent event = (AccessibilityEvent) msg.obj;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001576 synchronized (mLock) {
1577 if (mHasInputFilter && mInputFilter != null) {
1578 mInputFilter.notifyAccessibilityEvent(event);
1579 }
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001580 }
1581 event.recycle();
1582 } break;
Svetoslavc4fccd12013-04-09 12:58:41 -07001583 case MSG_SEND_KEY_EVENT_TO_INPUT_FILTER: {
1584 KeyEvent event = (KeyEvent) msg.obj;
1585 final int policyFlags = msg.arg1;
1586 synchronized (mLock) {
1587 if (mHasInputFilter && mInputFilter != null) {
1588 mInputFilter.sendInputEvent(event, policyFlags);
1589 }
1590 }
1591 event.recycle();
1592 } break;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001593 case MSG_SEND_STATE_TO_CLIENTS: {
1594 final int clientState = msg.arg1;
1595 final int userId = msg.arg2;
1596 sendStateToClients(clientState, mGlobalClients);
1597 sendStateToClientsForUser(clientState, userId);
1598 } break;
1599 case MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER: {
1600 final int userId = msg.arg1;
1601 sendStateToClientsForUser(0, userId);
1602 } break;
Svetoslav Ganova8afa692012-09-25 13:03:18 -07001603 case MSG_UPDATE_ACTIVE_WINDOW: {
1604 final int windowId = msg.arg1;
1605 final int eventType = msg.arg2;
1606 mSecurityPolicy.updateActiveWindow(windowId, eventType);
1607 } break;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001608 case MSG_ANNOUNCE_NEW_USER_IF_NEEDED: {
1609 announceNewUserIfNeeded();
1610 } break;
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001611 case MSG_UPDATE_INPUT_FILTER: {
1612 UserState userState = (UserState) msg.obj;
Svetoslav Ganov447d9462013-02-01 19:46:20 +00001613 updateInputFilter(userState);
Svetoslav Ganov545252f2012-12-10 18:29:24 -08001614 } break;
Svetoslav57bf8852013-02-07 19:21:42 -08001615 case MSG_SHOW_ENABLED_TOUCH_EXPLORATION_DIALOG: {
1616 Service service = (Service) msg.obj;
1617 showEnableTouchExplorationDialog(service);
1618 } break;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001619 }
1620 }
1621
1622 private void announceNewUserIfNeeded() {
1623 synchronized (mLock) {
1624 UserState userState = getCurrentUserStateLocked();
1625 if (userState.mIsAccessibilityEnabled) {
1626 UserManager userManager = (UserManager) mContext.getSystemService(
1627 Context.USER_SERVICE);
1628 String message = mContext.getString(R.string.user_switched,
1629 userManager.getUserInfo(mCurrentUserId).name);
1630 AccessibilityEvent event = AccessibilityEvent.obtain(
1631 AccessibilityEvent.TYPE_ANNOUNCEMENT);
1632 event.getText().add(message);
Svetoslav Ganovfc9c4cd2012-11-02 11:49:22 -07001633 event.setWindowId(mSecurityPolicy.getRetrievalAllowingWindowLocked());
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001634 sendAccessibilityEvent(event, mCurrentUserId);
1635 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001636 }
1637 }
1638
1639 private void sendStateToClientsForUser(int clientState, int userId) {
1640 final UserState userState;
1641 synchronized (mLock) {
1642 userState = getUserStateLocked(userId);
1643 }
1644 sendStateToClients(clientState, userState.mClients);
1645 }
1646
1647 private void sendStateToClients(int clientState,
1648 RemoteCallbackList<IAccessibilityManagerClient> clients) {
1649 try {
1650 final int userClientCount = clients.beginBroadcast();
1651 for (int i = 0; i < userClientCount; i++) {
1652 IAccessibilityManagerClient client = clients.getBroadcastItem(i);
1653 try {
1654 client.setState(clientState);
1655 } catch (RemoteException re) {
1656 /* ignore */
1657 }
1658 }
1659 } finally {
1660 clients.finishBroadcast();
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001661 }
1662 }
1663 }
1664
Svetoslavc4fccd12013-04-09 12:58:41 -07001665 private PendingEvent obtainPendingEventLocked(KeyEvent event, int policyFlags, int sequence) {
1666 PendingEvent pendingEvent = mPendingEventPool.acquire();
1667 if (pendingEvent == null) {
1668 pendingEvent = new PendingEvent();
1669 }
1670 pendingEvent.event = event;
1671 pendingEvent.policyFlags = policyFlags;
1672 pendingEvent.sequence = sequence;
1673 return pendingEvent;
1674 }
1675
1676 private void recyclePendingEventLocked(PendingEvent pendingEvent) {
1677 pendingEvent.clear();
1678 mPendingEventPool.release(pendingEvent);
1679 }
1680
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001681 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001682 * This class represents an accessibility service. It stores all per service
1683 * data required for the service management, provides API for starting/stopping the
1684 * service and is responsible for adding/removing the service in the data structures
1685 * for service management. The class also exposes configuration interface that is
1686 * passed to the service it represents as soon it is bound. It also serves as the
1687 * connection for the service.
1688 */
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001689 class Service extends IAccessibilityServiceConnection.Stub
Svetoslavc4fccd12013-04-09 12:58:41 -07001690 implements ServiceConnection, DeathRecipient {;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001691
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001692 final int mUserId;
1693
svetoslavganov75986cf2009-05-14 22:28:01 -07001694 int mId = 0;
1695
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001696 AccessibilityServiceInfo mAccessibilityServiceInfo;
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001697
svetoslavganov75986cf2009-05-14 22:28:01 -07001698 IBinder mService;
1699
Svetoslav Ganov42138042012-03-20 11:51:39 -07001700 IAccessibilityServiceClient mServiceInterface;
svetoslavganov75986cf2009-05-14 22:28:01 -07001701
1702 int mEventTypes;
1703
1704 int mFeedbackType;
1705
1706 Set<String> mPackageNames = new HashSet<String>();
1707
1708 boolean mIsDefault;
1709
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001710 boolean mRequestTouchExplorationMode;
1711
Svetoslav38228962013-01-29 01:04:35 -08001712 boolean mRequestEnhancedWebAccessibility;
1713
Svetoslav688a6972013-04-16 18:55:38 -07001714 boolean mRequestFilterKeyEvents;
1715
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001716 int mFetchFlags;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001717
svetoslavganov75986cf2009-05-14 22:28:01 -07001718 long mNotificationTimeout;
1719
svetoslavganov75986cf2009-05-14 22:28:01 -07001720 ComponentName mComponentName;
1721
1722 Intent mIntent;
1723
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001724 boolean mIsAutomation;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001725
Svetoslav Ganov7961be72011-06-21 12:31:56 -07001726 final Rect mTempBounds = new Rect();
1727
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001728 final ResolveInfo mResolveInfo;
1729
svetoslavganov75986cf2009-05-14 22:28:01 -07001730 // the events pending events to be dispatched to this service
1731 final SparseArray<AccessibilityEvent> mPendingEvents =
1732 new SparseArray<AccessibilityEvent>();
1733
Svetoslavc4fccd12013-04-09 12:58:41 -07001734 final KeyEventDispatcher mKeyEventDispatcher = new KeyEventDispatcher();
1735
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07001736 boolean mWasConnectedAndDied;
1737
Svetoslavc4fccd12013-04-09 12:58:41 -07001738 // Handler only for dispatching accessibility events since we use event
1739 // types as message types allowing us to remove messages per event type.
1740 public Handler mEventDispatchHandler = new Handler(mMainHandler.getLooper()) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001741 @Override
1742 public void handleMessage(Message message) {
Svetoslavc4fccd12013-04-09 12:58:41 -07001743 final int eventType = message.what;
1744 notifyAccessibilityEventInternal(eventType);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001745 }
1746 };
1747
Svetoslavc4fccd12013-04-09 12:58:41 -07001748 // Handler for scheduling method invocations on the main thread.
1749 public InvocationHandler mInvocationHandler = new InvocationHandler(
1750 mMainHandler.getLooper());
1751
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001752 public Service(int userId, ComponentName componentName,
Svetoslav57bf8852013-02-07 19:21:42 -08001753 AccessibilityServiceInfo accessibilityServiceInfo) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001754 mUserId = userId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001755 mResolveInfo = accessibilityServiceInfo.getResolveInfo();
svetoslavganov75986cf2009-05-14 22:28:01 -07001756 mId = sIdCounter++;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001757 mComponentName = componentName;
1758 mAccessibilityServiceInfo = accessibilityServiceInfo;
Svetoslav57bf8852013-02-07 19:21:42 -08001759 mIsAutomation = (sFakeAccessibilityServiceComponentName.equals(componentName));
1760 if (!mIsAutomation) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001761 mIntent = new Intent().setComponent(mComponentName);
1762 mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1763 com.android.internal.R.string.accessibility_binding_label);
1764 mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1765 mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0));
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001766 }
1767 setDynamicallyConfigurableProperties(accessibilityServiceInfo);
svetoslavganov75986cf2009-05-14 22:28:01 -07001768 }
1769
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001770 public void setDynamicallyConfigurableProperties(AccessibilityServiceInfo info) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001771 mEventTypes = info.eventTypes;
1772 mFeedbackType = info.feedbackType;
1773 String[] packageNames = info.packageNames;
1774 if (packageNames != null) {
1775 mPackageNames.addAll(Arrays.asList(packageNames));
1776 }
1777 mNotificationTimeout = info.notificationTimeout;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001778 mIsDefault = (info.flags & DEFAULT) != 0;
1779
Guang Zhudf549f82012-05-08 23:09:24 -07001780 if (mIsAutomation || info.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion
Svetoslav Ganov5a48f972012-05-13 13:33:53 -07001781 >= Build.VERSION_CODES.JELLY_BEAN) {
Svetoslav9e065122013-02-15 15:24:09 -08001782 if ((info.flags & AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0) {
1783 mFetchFlags |= AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
1784 } else {
1785 mFetchFlags &= ~AccessibilityNodeInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
1786 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001787 }
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001788
Svetoslav9e065122013-02-15 15:24:09 -08001789 if ((info.flags & AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS) != 0) {
1790 mFetchFlags |= AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS;
1791 } else {
1792 mFetchFlags &= ~AccessibilityNodeInfo.FLAG_REPORT_VIEW_IDS;
1793 }
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001794
Svetoslavdb7da0e2013-04-22 18:34:02 -07001795 mRequestTouchExplorationMode = (info.flags
1796 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
1797 mRequestEnhancedWebAccessibility = (info.flags
1798 & AccessibilityServiceInfo.FLAG_REQUEST_ENHANCED_WEB_ACCESSIBILITY) != 0;
1799 mRequestFilterKeyEvents = (info.flags
1800 & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) != 0;
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001801 }
1802
svetoslavganov75986cf2009-05-14 22:28:01 -07001803 /**
1804 * Binds to the accessibility service.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001805 *
1806 * @return True if binding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001807 */
Svetoslav57bf8852013-02-07 19:21:42 -08001808 public boolean bindLocked() {
1809 UserState userState = getUserStateLocked(mUserId);
1810 if (!mIsAutomation) {
Svetoslav6f8218a2013-02-20 16:28:39 -08001811 if (mService == null && mContext.bindServiceAsUser(
1812 mIntent, this, Context.BIND_AUTO_CREATE, new UserHandle(mUserId))) {
1813 userState.mBindingServices.add(mComponentName);
Svetoslav57bf8852013-02-07 19:21:42 -08001814 }
1815 } else {
1816 userState.mBindingServices.add(mComponentName);
1817 mService = userState.mUiAutomationServiceClient.asBinder();
1818 onServiceConnected(mComponentName, mService);
1819 userState.mUiAutomationService = this;
svetoslavganov75986cf2009-05-14 22:28:01 -07001820 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001821 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001822 }
1823
1824 /**
1825 * Unbinds form the accessibility service and removes it from the data
1826 * structures for service management.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001827 *
1828 * @return True if unbinding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001829 */
Svetoslav57bf8852013-02-07 19:21:42 -08001830 public boolean unbindLocked() {
1831 if (mService == null) {
1832 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001833 }
Svetoslav6f8218a2013-02-20 16:28:39 -08001834 UserState userState = getUserStateLocked(mUserId);
Svetoslavc4fccd12013-04-09 12:58:41 -07001835 mKeyEventDispatcher.flush();
Svetoslav57bf8852013-02-07 19:21:42 -08001836 if (!mIsAutomation) {
1837 mContext.unbindService(this);
1838 } else {
Svetoslav3c55e5c2013-02-27 18:24:28 -08001839 userState.destroyUiAutomationService();
Svetoslav57bf8852013-02-07 19:21:42 -08001840 }
Svetoslav6f8218a2013-02-20 16:28:39 -08001841 removeServiceLocked(this, userState);
Svetoslav2602a392013-04-15 16:26:39 -07001842 resetLocked();
Svetoslav57bf8852013-02-07 19:21:42 -08001843 return true;
svetoslavganov75986cf2009-05-14 22:28:01 -07001844 }
1845
Svetoslav38228962013-01-29 01:04:35 -08001846 public boolean canReceiveEventsLocked() {
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001847 return (mEventTypes != 0 && mFeedbackType != 0 && mService != null);
svetoslavganov75986cf2009-05-14 22:28:01 -07001848 }
1849
Svetoslav Ganov42138042012-03-20 11:51:39 -07001850 @Override
Svetoslavc4fccd12013-04-09 12:58:41 -07001851 public void setOnKeyEventResult(boolean handled, int sequence) {
1852 mKeyEventDispatcher.setOnKeyEventResult(handled, sequence);
1853 }
1854
1855 @Override
Svetoslav Ganov42138042012-03-20 11:51:39 -07001856 public AccessibilityServiceInfo getServiceInfo() {
1857 synchronized (mLock) {
1858 return mAccessibilityServiceInfo;
1859 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001860 }
1861
Svetoslav Ganov42138042012-03-20 11:51:39 -07001862 @Override
1863 public void setServiceInfo(AccessibilityServiceInfo info) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001864 final long identity = Binder.clearCallingIdentity();
1865 try {
1866 synchronized (mLock) {
1867 // If the XML manifest had data to configure the service its info
1868 // should be already set. In such a case update only the dynamically
1869 // configurable properties.
1870 AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo;
1871 if (oldInfo != null) {
1872 oldInfo.updateDynamicallyConfigurableProperties(info);
1873 setDynamicallyConfigurableProperties(oldInfo);
1874 } else {
1875 setDynamicallyConfigurableProperties(info);
1876 }
Svetoslav57bf8852013-02-07 19:21:42 -08001877 UserState userState = getUserStateLocked(mUserId);
1878 onUserStateChangedLocked(userState);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001879 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001880 } finally {
1881 Binder.restoreCallingIdentity(identity);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001882 }
1883 }
1884
1885 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001886 public void onServiceConnected(ComponentName componentName, IBinder service) {
Svetoslav57bf8852013-02-07 19:21:42 -08001887 synchronized (mLock) {
Svetoslav57bf8852013-02-07 19:21:42 -08001888 mService = service;
1889 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
1890 UserState userState = getUserStateLocked(mUserId);
Svetoslav6f8218a2013-02-20 16:28:39 -08001891 addServiceLocked(this, userState);
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07001892 if (userState.mBindingServices.contains(mComponentName) || mWasConnectedAndDied) {
Svetoslav57bf8852013-02-07 19:21:42 -08001893 userState.mBindingServices.remove(mComponentName);
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07001894 mWasConnectedAndDied = false;
Svetoslava60fdfa2013-02-26 19:16:18 -08001895 try {
Svetoslav072906e2013-04-15 12:10:36 -07001896 mServiceInterface.setConnection(this, mId);
1897 onUserStateChangedLocked(userState);
Svetoslava60fdfa2013-02-26 19:16:18 -08001898 } catch (RemoteException re) {
Svetoslav072906e2013-04-15 12:10:36 -07001899 Slog.w(LOG_TAG, "Error while setting connection for service: "
1900 + service, re);
1901 binderDied();
Svetoslava60fdfa2013-02-26 19:16:18 -08001902 }
1903 } else {
1904 binderDied();
svetoslavganov75986cf2009-05-14 22:28:01 -07001905 }
Svetoslav57bf8852013-02-07 19:21:42 -08001906 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001907 }
1908
Svetoslav Ganov42138042012-03-20 11:51:39 -07001909 @Override
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001910 public boolean findAccessibilityNodeInfosByViewId(int accessibilityWindowId,
1911 long accessibilityNodeId, String viewIdResName, int interactionId,
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001912 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001913 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001914 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001915 IAccessibilityInteractionConnection connection = null;
1916 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001917 final int resolvedUserId = mSecurityPolicy
1918 .resolveCallingUserIdEnforcingPermissionsLocked(
1919 UserHandle.getCallingUserId());
1920 if (resolvedUserId != mCurrentUserId) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001921 return false;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001922 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001923 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001924 final boolean permissionGranted = mSecurityPolicy.canRetrieveWindowContent(this);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001925 if (!permissionGranted) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001926 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001927 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001928 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001929 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001930 if (connection == null) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001931 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001932 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001933 }
1934 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001935 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001936 final long identityToken = Binder.clearCallingIdentity();
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001937 MagnificationSpec spec = getCompatibleMagnificationSpec(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001938 try {
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001939 connection.findAccessibilityNodeInfosByViewId(accessibilityNodeId,
1940 viewIdResName, interactionId, callback, mFetchFlags, interrogatingPid,
1941 interrogatingTid, spec);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001942 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001943 } catch (RemoteException re) {
1944 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001945 Slog.e(LOG_TAG, "Error findAccessibilityNodeInfoByViewId().");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001946 }
1947 } finally {
1948 Binder.restoreCallingIdentity(identityToken);
1949 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001950 return false;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001951 }
1952
Svetoslav Ganov42138042012-03-20 11:51:39 -07001953 @Override
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001954 public boolean findAccessibilityNodeInfosByText(int accessibilityWindowId,
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001955 long accessibilityNodeId, String text, int interactionId,
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001956 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1957 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001958 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001959 IAccessibilityInteractionConnection connection = null;
1960 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001961 final int resolvedUserId = mSecurityPolicy
1962 .resolveCallingUserIdEnforcingPermissionsLocked(
1963 UserHandle.getCallingUserId());
1964 if (resolvedUserId != mCurrentUserId) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001965 return false;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001966 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001967 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001968 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07001969 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001970 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001971 if (!permissionGranted) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001972 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001973 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001974 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001975 if (connection == null) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001976 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001977 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001978 }
1979 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001980 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001981 final long identityToken = Binder.clearCallingIdentity();
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001982 MagnificationSpec spec = getCompatibleMagnificationSpec(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001983 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001984 connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001985 interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid,
1986 spec);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001987 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001988 } catch (RemoteException re) {
1989 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001990 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001991 }
1992 } finally {
1993 Binder.restoreCallingIdentity(identityToken);
1994 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001995 return false;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001996 }
1997
Svetoslav Ganov42138042012-03-20 11:51:39 -07001998 @Override
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07001999 public boolean findAccessibilityNodeInfoByAccessibilityId(
2000 int accessibilityWindowId, long accessibilityNodeId, int interactionId,
Svetoslav Ganov42138042012-03-20 11:51:39 -07002001 IAccessibilityInteractionConnectionCallback callback, int flags,
2002 long interrogatingTid) throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002003 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002004 IAccessibilityInteractionConnection connection = null;
2005 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002006 final int resolvedUserId = mSecurityPolicy
2007 .resolveCallingUserIdEnforcingPermissionsLocked(
2008 UserHandle.getCallingUserId());
2009 if (resolvedUserId != mCurrentUserId) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002010 return false;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002011 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002012 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002013 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002014 final boolean permissionGranted =
2015 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2016 if (!permissionGranted) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002017 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002018 } else {
2019 connection = getConnectionLocked(resolvedWindowId);
2020 if (connection == null) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002021 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002022 }
2023 }
2024 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002025 final int interrogatingPid = Binder.getCallingPid();
2026 final long identityToken = Binder.clearCallingIdentity();
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002027 MagnificationSpec spec = getCompatibleMagnificationSpec(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002028 try {
2029 connection.findAccessibilityNodeInfoByAccessibilityId(accessibilityNodeId,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08002030 interactionId, callback, mFetchFlags | flags, interrogatingPid,
2031 interrogatingTid, spec);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002032 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002033 } catch (RemoteException re) {
2034 if (DEBUG) {
2035 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfoByAccessibilityId()");
2036 }
2037 } finally {
2038 Binder.restoreCallingIdentity(identityToken);
2039 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002040 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002041 }
2042
2043 @Override
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002044 public boolean findFocus(int accessibilityWindowId, long accessibilityNodeId,
Svetoslav Ganov42138042012-03-20 11:51:39 -07002045 int focusType, int interactionId,
2046 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07002047 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002048 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002049 IAccessibilityInteractionConnection connection = null;
2050 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002051 final int resolvedUserId = mSecurityPolicy
2052 .resolveCallingUserIdEnforcingPermissionsLocked(
2053 UserHandle.getCallingUserId());
2054 if (resolvedUserId != mCurrentUserId) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002055 return false;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002056 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07002057 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002058 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002059 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002060 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07002061 if (!permissionGranted) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002062 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07002063 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002064 connection = getConnectionLocked(resolvedWindowId);
2065 if (connection == null) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002066 return false;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07002067 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002068 }
2069 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07002070 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002071 final long identityToken = Binder.clearCallingIdentity();
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002072 MagnificationSpec spec = getCompatibleMagnificationSpec(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002073 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002074 connection.findFocus(accessibilityNodeId, focusType, interactionId, callback,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08002075 mFetchFlags, interrogatingPid, interrogatingTid, spec);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002076 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002077 } catch (RemoteException re) {
2078 if (DEBUG) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002079 Slog.e(LOG_TAG, "Error calling findAccessibilityFocus()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002080 }
2081 } finally {
2082 Binder.restoreCallingIdentity(identityToken);
2083 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002084 return false;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002085 }
2086
Svetoslav Ganov42138042012-03-20 11:51:39 -07002087 @Override
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002088 public boolean focusSearch(int accessibilityWindowId, long accessibilityNodeId,
Svetoslav Ganov42138042012-03-20 11:51:39 -07002089 int direction, int interactionId,
2090 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2091 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002092 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002093 IAccessibilityInteractionConnection connection = null;
2094 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002095 final int resolvedUserId = mSecurityPolicy
2096 .resolveCallingUserIdEnforcingPermissionsLocked(
2097 UserHandle.getCallingUserId());
2098 if (resolvedUserId != mCurrentUserId) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002099 return false;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002100 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002101 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002102 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002103 final boolean permissionGranted =
2104 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
2105 if (!permissionGranted) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002106 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002107 } else {
2108 connection = getConnectionLocked(resolvedWindowId);
2109 if (connection == null) {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002110 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002111 }
2112 }
2113 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07002114 final int interrogatingPid = Binder.getCallingPid();
2115 final long identityToken = Binder.clearCallingIdentity();
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002116 MagnificationSpec spec = getCompatibleMagnificationSpec(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07002117 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002118 connection.focusSearch(accessibilityNodeId, direction, interactionId, callback,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08002119 mFetchFlags, interrogatingPid, interrogatingTid, spec);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002120 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002121 } catch (RemoteException re) {
2122 if (DEBUG) {
2123 Slog.e(LOG_TAG, "Error calling accessibilityFocusSearch()");
2124 }
2125 } finally {
2126 Binder.restoreCallingIdentity(identityToken);
2127 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002128 return false;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002129 }
2130
2131 @Override
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002132 public boolean performAccessibilityAction(int accessibilityWindowId,
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07002133 long accessibilityNodeId, int action, Bundle arguments, int interactionId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002134 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
2135 throws RemoteException {
2136 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002137 IAccessibilityInteractionConnection connection = null;
2138 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002139 final int resolvedUserId = mSecurityPolicy
2140 .resolveCallingUserIdEnforcingPermissionsLocked(
2141 UserHandle.getCallingUserId());
2142 if (resolvedUserId != mCurrentUserId) {
2143 return false;
2144 }
2145 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
2146 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002147 final boolean permissionGranted = mSecurityPolicy.canPerformActionLocked(this,
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002148 resolvedWindowId, action, arguments);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07002149 if (!permissionGranted) {
2150 return false;
2151 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002152 connection = getConnectionLocked(resolvedWindowId);
2153 if (connection == null) {
Svetoslav Ganove8f95352011-07-06 17:24:03 -07002154 return false;
2155 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002156 }
2157 }
Svetoslav Ganov9bf21873c2012-05-22 17:43:23 -07002158 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002159 final long identityToken = Binder.clearCallingIdentity();
2160 try {
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07002161 connection.performAccessibilityAction(accessibilityNodeId, action, arguments,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08002162 interactionId, callback, mFetchFlags, interrogatingPid, interrogatingTid);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002163 } catch (RemoteException re) {
2164 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002165 Slog.e(LOG_TAG, "Error calling performAccessibilityAction()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002166 }
2167 } finally {
2168 Binder.restoreCallingIdentity(identityToken);
2169 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07002170 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002171 }
2172
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002173 public boolean performGlobalAction(int action) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002174 synchronized (mLock) {
2175 final int resolvedUserId = mSecurityPolicy
2176 .resolveCallingUserIdEnforcingPermissionsLocked(
2177 UserHandle.getCallingUserId());
2178 if (resolvedUserId != mCurrentUserId) {
2179 return false;
2180 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07002181 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002182 final long identity = Binder.clearCallingIdentity();
2183 try {
2184 switch (action) {
2185 case AccessibilityService.GLOBAL_ACTION_BACK: {
2186 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_BACK);
2187 } return true;
2188 case AccessibilityService.GLOBAL_ACTION_HOME: {
2189 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HOME);
2190 } return true;
2191 case AccessibilityService.GLOBAL_ACTION_RECENTS: {
2192 openRecents();
2193 } return true;
2194 case AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS: {
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002195 expandNotifications();
2196 } return true;
2197 case AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS: {
2198 expandQuickSettings();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002199 } return true;
2200 }
2201 return false;
2202 } finally {
2203 Binder.restoreCallingIdentity(identity);
2204 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07002205 }
2206
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08002207 @Override
2208 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
2209 mSecurityPolicy.enforceCallingPermission(Manifest.permission.DUMP, FUNCTION_DUMP);
2210 synchronized (mLock) {
2211 pw.append("Service[label=" + mAccessibilityServiceInfo.getResolveInfo()
2212 .loadLabel(mContext.getPackageManager()));
2213 pw.append(", feedbackType"
2214 + AccessibilityServiceInfo.feedbackTypeToString(mFeedbackType));
Svetoslav11adf6d2013-04-24 14:51:29 -07002215 pw.append(", capabilities=" + mAccessibilityServiceInfo.getCapabilities());
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08002216 pw.append(", eventTypes="
2217 + AccessibilityEvent.eventTypeToString(mEventTypes));
2218 pw.append(", notificationTimeout=" + mNotificationTimeout);
2219 pw.append("]");
2220 }
2221 }
2222
2223 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07002224 public void onServiceDisconnected(ComponentName componentName) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07002225 /* do nothing - #binderDied takes care */
2226 }
2227
Svetoslav2602a392013-04-15 16:26:39 -07002228 public void linkToOwnDeathLocked() throws RemoteException {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07002229 mService.linkToDeath(this, 0);
2230 }
2231
Svetoslav2602a392013-04-15 16:26:39 -07002232 public void unlinkToOwnDeathLocked() {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07002233 mService.unlinkToDeath(this, 0);
2234 }
2235
Svetoslav2602a392013-04-15 16:26:39 -07002236 public void resetLocked() {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08002237 try {
2238 // Clear the proxy in the other process so this
2239 // IAccessibilityServiceConnection can be garbage collected.
2240 mServiceInterface.setConnection(null, mId);
2241 } catch (RemoteException re) {
2242 /* ignore */
2243 }
2244 mService = null;
2245 mServiceInterface = null;
2246 }
2247
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07002248 public boolean isConnectedLocked() {
Svetoslav2602a392013-04-15 16:26:39 -07002249 return (mService != null);
2250 }
2251
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07002252 public void binderDied() {
svetoslavganov75986cf2009-05-14 22:28:01 -07002253 synchronized (mLock) {
Svetoslav2602a392013-04-15 16:26:39 -07002254 // It is possible that this service's package was force stopped during
2255 // whose handling the death recipient is unlinked and still get a call
2256 // on binderDied since the call was made before we unlink but was
2257 // waiting on the lock we held during the force stop handling.
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07002258 if (!isConnectedLocked()) {
Svetoslav2602a392013-04-15 16:26:39 -07002259 return;
2260 }
Svetoslav Ganovbfa1bf02013-05-03 01:13:05 -07002261 mWasConnectedAndDied = true;
Svetoslavc4fccd12013-04-09 12:58:41 -07002262 mKeyEventDispatcher.flush();
Svetoslav57bf8852013-02-07 19:21:42 -08002263 UserState userState = getUserStateLocked(mUserId);
Svetoslav6f8218a2013-02-20 16:28:39 -08002264 // The death recipient is unregistered in removeServiceLocked
2265 removeServiceLocked(this, userState);
Svetoslav2602a392013-04-15 16:26:39 -07002266 resetLocked();
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07002267 if (mIsAutomation) {
Svetoslav57bf8852013-02-07 19:21:42 -08002268 // We no longer have an automation service, so restore
2269 // the state based on values in the settings database.
Svetoslav7379a952013-02-13 15:47:29 -08002270 userState.mInstalledServices.remove(mAccessibilityServiceInfo);
2271 userState.mEnabledServices.remove(mComponentName);
Svetoslav3c55e5c2013-02-27 18:24:28 -08002272 userState.destroyUiAutomationService();
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07002273 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002274 }
2275 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002276
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002277 /**
2278 * Performs a notification for an {@link AccessibilityEvent}.
2279 *
2280 * @param event The event.
2281 */
2282 public void notifyAccessibilityEvent(AccessibilityEvent event) {
2283 synchronized (mLock) {
2284 final int eventType = event.getEventType();
2285 // Make a copy since during dispatch it is possible the event to
2286 // be modified to remove its source if the receiving service does
2287 // not have permission to access the window content.
2288 AccessibilityEvent newEvent = AccessibilityEvent.obtain(event);
2289 AccessibilityEvent oldEvent = mPendingEvents.get(eventType);
2290 mPendingEvents.put(eventType, newEvent);
2291
2292 final int what = eventType;
2293 if (oldEvent != null) {
Svetoslavc4fccd12013-04-09 12:58:41 -07002294 mEventDispatchHandler.removeMessages(what);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002295 oldEvent.recycle();
2296 }
2297
Svetoslavc4fccd12013-04-09 12:58:41 -07002298 Message message = mEventDispatchHandler.obtainMessage(what);
2299 mEventDispatchHandler.sendMessageDelayed(message, mNotificationTimeout);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002300 }
2301 }
2302
2303 /**
2304 * Notifies an accessibility service client for a scheduled event given the event type.
2305 *
2306 * @param eventType The type of the event to dispatch.
2307 */
2308 private void notifyAccessibilityEventInternal(int eventType) {
2309 IAccessibilityServiceClient listener;
2310 AccessibilityEvent event;
2311
2312 synchronized (mLock) {
2313 listener = mServiceInterface;
2314
2315 // If the service died/was disabled while the message for dispatching
2316 // the accessibility event was propagating the listener may be null.
2317 if (listener == null) {
2318 return;
2319 }
2320
2321 event = mPendingEvents.get(eventType);
2322
2323 // Check for null here because there is a concurrent scenario in which this
2324 // happens: 1) A binder thread calls notifyAccessibilityServiceDelayedLocked
2325 // which posts a message for dispatching an event. 2) The message is pulled
2326 // from the queue by the handler on the service thread and the latter is
2327 // just about to acquire the lock and call this method. 3) Now another binder
2328 // thread acquires the lock calling notifyAccessibilityServiceDelayedLocked
2329 // so the service thread waits for the lock; 4) The binder thread replaces
2330 // the event with a more recent one (assume the same event type) and posts a
2331 // dispatch request releasing the lock. 5) Now the main thread is unblocked and
2332 // dispatches the event which is removed from the pending ones. 6) And ... now
2333 // the service thread handles the last message posted by the last binder call
2334 // but the event is already dispatched and hence looking it up in the pending
2335 // ones yields null. This check is much simpler that keeping count for each
2336 // event type of each service to catch such a scenario since only one message
2337 // is processed at a time.
2338 if (event == null) {
2339 return;
2340 }
2341
2342 mPendingEvents.remove(eventType);
2343 if (mSecurityPolicy.canRetrieveWindowContent(this)) {
2344 event.setConnectionId(mId);
2345 } else {
2346 event.setSource(null);
2347 }
2348 event.setSealed(true);
2349 }
2350
2351 try {
2352 listener.onAccessibilityEvent(event);
2353 if (DEBUG) {
2354 Slog.i(LOG_TAG, "Event " + event + " sent to " + listener);
2355 }
2356 } catch (RemoteException re) {
2357 Slog.e(LOG_TAG, "Error during sending " + event + " to " + listener, re);
2358 } finally {
2359 event.recycle();
2360 }
2361 }
2362
2363 public void notifyGesture(int gestureId) {
Svetoslavc4fccd12013-04-09 12:58:41 -07002364 mInvocationHandler.obtainMessage(InvocationHandler.MSG_ON_GESTURE,
2365 gestureId, 0).sendToTarget();
2366 }
2367
2368 public void notifyKeyEvent(KeyEvent event, int policyFlags) {
2369 mInvocationHandler.obtainMessage(InvocationHandler.MSG_ON_KEY_EVENT,
2370 policyFlags, 0, event).sendToTarget();
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002371 }
2372
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002373 public void notifyClearAccessibilityNodeInfoCache() {
Svetoslavc4fccd12013-04-09 12:58:41 -07002374 mInvocationHandler.sendEmptyMessage(
2375 InvocationHandler.MSG_CLEAR_ACCESSIBILITY_NODE_INFO_CACHE);
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002376 }
2377
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002378 private void notifyGestureInternal(int gestureId) {
2379 IAccessibilityServiceClient listener = mServiceInterface;
2380 if (listener != null) {
2381 try {
2382 listener.onGesture(gestureId);
2383 } catch (RemoteException re) {
2384 Slog.e(LOG_TAG, "Error during sending gesture " + gestureId
2385 + " to " + mService, re);
2386 }
2387 }
2388 }
2389
Svetoslavc4fccd12013-04-09 12:58:41 -07002390 private void notifyKeyEventInternal(KeyEvent event, int policyFlags) {
2391 mKeyEventDispatcher.notifyKeyEvent(event, policyFlags);
2392 }
2393
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002394 private void notifyClearAccessibilityNodeInfoCacheInternal() {
2395 IAccessibilityServiceClient listener = mServiceInterface;
2396 if (listener != null) {
2397 try {
2398 listener.clearAccessibilityNodeInfoCache();
2399 } catch (RemoteException re) {
2400 Slog.e(LOG_TAG, "Error during requesting accessibility info cache"
2401 + " to be cleared.", re);
2402 }
2403 }
2404 }
2405
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07002406 private void sendDownAndUpKeyEvents(int keyCode) {
2407 final long token = Binder.clearCallingIdentity();
2408
2409 // Inject down.
2410 final long downTime = SystemClock.uptimeMillis();
2411 KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
2412 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2413 InputDevice.SOURCE_KEYBOARD, null);
2414 InputManager.getInstance().injectInputEvent(down,
2415 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2416 down.recycle();
2417
2418 // Inject up.
2419 final long upTime = SystemClock.uptimeMillis();
2420 KeyEvent up = KeyEvent.obtain(downTime, upTime, KeyEvent.ACTION_UP, keyCode, 0, 0,
2421 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2422 InputDevice.SOURCE_KEYBOARD, null);
2423 InputManager.getInstance().injectInputEvent(up,
2424 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2425 up.recycle();
2426
2427 Binder.restoreCallingIdentity(token);
2428 }
2429
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002430 private void expandNotifications() {
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002431 final long token = Binder.clearCallingIdentity();
2432
2433 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2434 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002435 statusBarManager.expandNotificationsPanel();
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002436
2437 Binder.restoreCallingIdentity(token);
2438 }
2439
2440 private void expandQuickSettings() {
2441 final long token = Binder.clearCallingIdentity();
2442
2443 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2444 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002445 statusBarManager.expandSettingsPanel();
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002446
2447 Binder.restoreCallingIdentity(token);
2448 }
2449
Svetoslav Ganovc682fc92012-06-04 14:02:09 -07002450 private void openRecents() {
2451 final long token = Binder.clearCallingIdentity();
2452
2453 IStatusBarService statusBarService = IStatusBarService.Stub.asInterface(
2454 ServiceManager.getService("statusbar"));
2455 try {
2456 statusBarService.toggleRecentApps();
2457 } catch (RemoteException e) {
2458 Slog.e(LOG_TAG, "Error toggling recent apps.");
2459 }
2460
2461 Binder.restoreCallingIdentity(token);
2462 }
2463
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002464 private IAccessibilityInteractionConnection getConnectionLocked(int windowId) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002465 if (DEBUG) {
2466 Slog.i(LOG_TAG, "Trying to get interaction connection to windowId: " + windowId);
2467 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002468 AccessibilityConnectionWrapper wrapper = mGlobalInteractionConnections.get(windowId);
2469 if (wrapper == null) {
2470 wrapper = getCurrentUserStateLocked().mInteractionConnections.get(windowId);
2471 }
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002472 if (wrapper != null && wrapper.mConnection != null) {
2473 return wrapper.mConnection;
2474 }
2475 if (DEBUG) {
2476 Slog.e(LOG_TAG, "No interaction connection to window: " + windowId);
2477 }
2478 return null;
2479 }
2480
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002481 private int resolveAccessibilityWindowIdLocked(int accessibilityWindowId) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08002482 if (accessibilityWindowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002483 return mSecurityPolicy.mActiveWindowId;
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002484 }
2485 return accessibilityWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002486 }
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002487
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002488 private MagnificationSpec getCompatibleMagnificationSpec(int windowId) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002489 try {
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002490 IBinder windowToken = mGlobalWindowTokens.get(windowId);
2491 if (windowToken == null) {
2492 windowToken = getCurrentUserStateLocked().mWindowTokens.get(windowId);
2493 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002494 if (windowToken != null) {
Svetoslav Ganov545252f2012-12-10 18:29:24 -08002495 return mWindowManagerService.getCompatibleMagnificationSpecForWindow(
2496 windowToken);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002497 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002498 } catch (RemoteException re) {
2499 /* ignore */
2500 }
Svetoslav Ganov152e9bb2012-10-12 20:15:29 -07002501 return null;
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002502 }
Svetoslavc4fccd12013-04-09 12:58:41 -07002503
2504 private final class InvocationHandler extends Handler {
2505
2506 public static final int MSG_ON_GESTURE = 1;
2507 public static final int MSG_ON_KEY_EVENT = 2;
2508 public static final int MSG_CLEAR_ACCESSIBILITY_NODE_INFO_CACHE = 3;
2509 public static final int MSG_ON_KEY_EVENT_TIMEOUT = 4;
2510
2511 public InvocationHandler(Looper looper) {
2512 super(looper, null, true);
2513 }
2514
2515 @Override
2516 public void handleMessage(Message message) {
2517 final int type = message.what;
2518 switch (type) {
2519 case MSG_ON_GESTURE: {
2520 final int gestureId = message.arg1;
2521 notifyGestureInternal(gestureId);
2522 } break;
2523 case MSG_ON_KEY_EVENT: {
2524 KeyEvent event = (KeyEvent) message.obj;
2525 final int policyFlags = message.arg1;
2526 notifyKeyEventInternal(event, policyFlags);
2527 } break;
2528 case MSG_CLEAR_ACCESSIBILITY_NODE_INFO_CACHE: {
2529 notifyClearAccessibilityNodeInfoCacheInternal();
2530 } break;
2531 case MSG_ON_KEY_EVENT_TIMEOUT: {
2532 PendingEvent eventState = (PendingEvent) message.obj;
2533 setOnKeyEventResult(false, eventState.sequence);
2534 } break;
2535 default: {
2536 throw new IllegalArgumentException("Unknown message: " + type);
2537 }
2538 }
2539 }
2540 }
2541
2542 private final class KeyEventDispatcher {
2543
2544 private static final long ON_KEY_EVENT_TIMEOUT_MILLIS = 500;
2545
2546 private PendingEvent mPendingEvents;
2547
2548 private final InputEventConsistencyVerifier mSentEventsVerifier =
2549 InputEventConsistencyVerifier.isInstrumentationEnabled()
2550 ? new InputEventConsistencyVerifier(
2551 this, 0, KeyEventDispatcher.class.getSimpleName()) : null;
2552
2553 public void notifyKeyEvent(KeyEvent event, int policyFlags) {
2554 final PendingEvent pendingEvent;
2555
2556 synchronized (mLock) {
2557 pendingEvent = addPendingEventLocked(event, policyFlags);
2558 }
2559
2560 Message message = mInvocationHandler.obtainMessage(
2561 InvocationHandler.MSG_ON_KEY_EVENT_TIMEOUT, pendingEvent);
2562 mInvocationHandler.sendMessageDelayed(message, ON_KEY_EVENT_TIMEOUT_MILLIS);
2563
2564 try {
2565 // Accessibility services are exclusively not in the system
2566 // process, therefore no need to clone the motion event to
2567 // prevent tampering. It will be cloned in the IPC call.
2568 mServiceInterface.onKeyEvent(pendingEvent.event, pendingEvent.sequence);
2569 } catch (RemoteException re) {
2570 setOnKeyEventResult(false, pendingEvent.sequence);
2571 }
2572 }
2573
2574 public void setOnKeyEventResult(boolean handled, int sequence) {
2575 synchronized (mLock) {
2576 PendingEvent pendingEvent = removePendingEventLocked(sequence);
2577 if (pendingEvent != null) {
2578 mInvocationHandler.removeMessages(
2579 InvocationHandler.MSG_ON_KEY_EVENT_TIMEOUT,
2580 pendingEvent);
2581 pendingEvent.handled = handled;
2582 finishPendingEventLocked(pendingEvent);
2583 }
2584 }
2585 }
2586
2587 public void flush() {
2588 synchronized (mLock) {
2589 cancelAllPendingEventsLocked();
Svetoslav1487cd52013-04-15 12:28:42 -07002590 if (mSentEventsVerifier != null) {
2591 mSentEventsVerifier.reset();
2592 }
Svetoslavc4fccd12013-04-09 12:58:41 -07002593 }
2594 }
2595
2596 private PendingEvent addPendingEventLocked(KeyEvent event, int policyFlags) {
2597 final int sequence = event.getSequenceNumber();
2598 PendingEvent pendingEvent = obtainPendingEventLocked(event, policyFlags, sequence);
2599 pendingEvent.next = mPendingEvents;
2600 mPendingEvents = pendingEvent;
2601 return pendingEvent;
2602 }
2603
2604 private PendingEvent removePendingEventLocked(int sequence) {
2605 PendingEvent previous = null;
2606 PendingEvent current = mPendingEvents;
2607
2608 while (current != null) {
2609 if (current.sequence == sequence) {
2610 if (previous != null) {
2611 previous.next = current.next;
2612 } else {
2613 mPendingEvents = current.next;
2614 }
2615 current.next = null;
2616 return current;
2617 }
2618 previous = current;
2619 current = current.next;
2620 }
2621 return null;
2622 }
2623
2624 private void finishPendingEventLocked(PendingEvent pendingEvent) {
2625 if (!pendingEvent.handled) {
2626 sendKeyEventToInputFilter(pendingEvent.event, pendingEvent.policyFlags);
2627 }
2628 // Nullify the event since we do not want it to be
2629 // recycled yet. It will be sent to the input filter.
2630 pendingEvent.event = null;
2631 recyclePendingEventLocked(pendingEvent);
2632 }
2633
2634 private void sendKeyEventToInputFilter(KeyEvent event, int policyFlags) {
2635 if (DEBUG) {
2636 Slog.i(LOG_TAG, "Injecting event: " + event);
2637 }
2638 if (mSentEventsVerifier != null) {
2639 mSentEventsVerifier.onKeyEvent(event, 0);
2640 }
2641 policyFlags |= WindowManagerPolicy.FLAG_PASS_TO_USER;
2642 mMainHandler.obtainMessage(MainHandler.MSG_SEND_KEY_EVENT_TO_INPUT_FILTER,
2643 policyFlags, 0, event).sendToTarget();
2644 }
2645
2646 private void cancelAllPendingEventsLocked() {
2647 while (mPendingEvents != null) {
2648 PendingEvent pendingEvent = removePendingEventLocked(mPendingEvents.sequence);
2649 pendingEvent.handled = false;
2650 mInvocationHandler.removeMessages(InvocationHandler.MSG_ON_KEY_EVENT_TIMEOUT,
2651 pendingEvent);
2652 finishPendingEventLocked(pendingEvent);
2653 }
2654 }
2655 }
2656 }
2657
2658 private static final class PendingEvent {
2659 PendingEvent next;
2660
2661 KeyEvent event;
2662 int policyFlags;
2663 int sequence;
2664 boolean handled;
2665
2666 public void clear() {
2667 if (event != null) {
2668 event.recycle();
2669 event = null;
2670 }
2671 next = null;
2672 policyFlags = 0;
2673 sequence = 0;
2674 handled = false;
2675 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002676 }
2677
2678 final class SecurityPolicy {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002679 private static final int VALID_ACTIONS =
2680 AccessibilityNodeInfo.ACTION_CLICK
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002681 | AccessibilityNodeInfo.ACTION_LONG_CLICK
Svetoslav Ganov42138042012-03-20 11:51:39 -07002682 | AccessibilityNodeInfo.ACTION_FOCUS
2683 | AccessibilityNodeInfo.ACTION_CLEAR_FOCUS
2684 | AccessibilityNodeInfo.ACTION_SELECT
2685 | AccessibilityNodeInfo.ACTION_CLEAR_SELECTION
2686 | AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07002687 | AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07002688 | AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
2689 | AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002690 | AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT
Svetoslav Ganova1dc7612012-05-10 04:14:53 -07002691 | AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT
2692 | AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
Svetoslav7c512842013-01-30 23:02:08 -08002693 | AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
2694 | AccessibilityNodeInfo.ACTION_COPY
2695 | AccessibilityNodeInfo.ACTION_PASTE
2696 | AccessibilityNodeInfo.ACTION_CUT
2697 | AccessibilityNodeInfo.ACTION_SET_SELECTION;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002698
2699 private static final int RETRIEVAL_ALLOWING_EVENT_TYPES =
Svetoslav Ganov42138042012-03-20 11:51:39 -07002700 AccessibilityEvent.TYPE_VIEW_CLICKED
2701 | AccessibilityEvent.TYPE_VIEW_FOCUSED
2702 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
2703 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
2704 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
2705 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
2706 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
2707 | AccessibilityEvent.TYPE_VIEW_SELECTED
Svetoslav Ganova0156172011-06-26 17:55:44 -07002708 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
2709 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07002710 | AccessibilityEvent.TYPE_VIEW_SCROLLED
2711 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
2712 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002713
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002714 private int mActiveWindowId;
Svetoslav Ganov6ae8a242012-10-10 13:09:00 -07002715 private boolean mTouchInteractionInProgress;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002716
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002717 private boolean canDispatchAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov58fd9f82012-10-03 18:10:27 -07002718 final int eventType = event.getEventType();
2719 switch (eventType) {
2720 // All events that are for changes in a global window
2721 // state should *always* be dispatched.
2722 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
2723 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
2724 // All events generated by the user touching the
2725 // screen should *always* be dispatched.
2726 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
2727 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
2728 case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
2729 case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
2730 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
2731 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
2732 // These will change the active window, so dispatch.
2733 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
2734 case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: {
2735 return true;
2736 }
2737 // All events for changes in window content should be
2738 // dispatched *only* if this window is the active one.
2739 default:
2740 return event.getWindowId() == mActiveWindowId;
2741 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002742 }
2743
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002744 public void updateEventSourceLocked(AccessibilityEvent event) {
2745 if ((event.getEventType() & RETRIEVAL_ALLOWING_EVENT_TYPES) == 0) {
2746 event.setSource(null);
2747 }
2748 }
2749
2750 public void updateActiveWindow(int windowId, int eventType) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002751 // The active window is either the window that has input focus or
2752 // the window that the user is currently touching. If the user is
2753 // touching a window that does not have input focus as soon as the
2754 // the user stops touching that window the focused window becomes
2755 // the active one.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002756 switch (eventType) {
2757 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: {
2758 if (getFocusedWindowId() == windowId) {
2759 mActiveWindowId = windowId;
2760 }
2761 } break;
Svetoslav Ganovf772cba2012-10-05 18:49:17 -07002762 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER: {
Svetoslav Ganov6ae8a242012-10-10 13:09:00 -07002763 // Do not allow delayed hover events to confuse us
2764 // which the active window is.
2765 if (mTouchInteractionInProgress) {
2766 mActiveWindowId = windowId;
2767 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002768 } break;
Svetoslav Ganov4e2a7622011-07-26 20:08:46 -07002769 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002770 }
2771
Svetoslav Ganov6ae8a242012-10-10 13:09:00 -07002772 public void onTouchInteractionStart() {
2773 mTouchInteractionInProgress = true;
2774 }
2775
Svetoslav Ganovf772cba2012-10-05 18:49:17 -07002776 public void onTouchInteractionEnd() {
Svetoslav Ganov6ae8a242012-10-10 13:09:00 -07002777 mTouchInteractionInProgress = false;
Svetoslav Ganovf772cba2012-10-05 18:49:17 -07002778 // We want to set the active window to be current immediately
2779 // after the user has stopped touching the screen since if the
2780 // user types with the IME he should get a feedback for the
2781 // letter typed in the text view which is in the input focused
2782 // window. Note that we always deliver hover accessibility events
2783 // (they are a result of user touching the screen) so change of
2784 // the active window before all hover accessibility events from
2785 // the touched window are delivered is fine.
2786 mActiveWindowId = getFocusedWindowId();
2787 }
2788
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002789 public int getRetrievalAllowingWindowLocked() {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002790 return mActiveWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002791 }
2792
2793 public boolean canGetAccessibilityNodeInfoLocked(Service service, int windowId) {
2794 return canRetrieveWindowContent(service) && isRetrievalAllowingWindow(windowId);
2795 }
2796
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002797 public boolean canPerformActionLocked(Service service, int windowId, int action,
2798 Bundle arguments) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002799 return canRetrieveWindowContent(service)
2800 && isRetrievalAllowingWindow(windowId)
Svetoslav Ganovafe8cf22012-04-28 13:26:57 -07002801 && isActionPermitted(action);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002802 }
2803
2804 public boolean canRetrieveWindowContent(Service service) {
Svetoslav11adf6d2013-04-24 14:51:29 -07002805 return (service.mAccessibilityServiceInfo.getCapabilities()
2806 & AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT) != 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002807 }
2808
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07002809 public void enforceCanRetrieveWindowContent(Service service) throws RemoteException {
2810 // This happens due to incorrect registration so make it apparent.
2811 if (!canRetrieveWindowContent(service)) {
2812 Slog.e(LOG_TAG, "Accessibility serivce " + service.mComponentName + " does not " +
2813 "declare android:canRetrieveWindowContent.");
2814 throw new RemoteException();
2815 }
2816 }
2817
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002818 public int resolveCallingUserIdEnforcingPermissionsLocked(int userId) {
2819 final int callingUid = Binder.getCallingUid();
2820 if (callingUid == Process.SYSTEM_UID
2821 || callingUid == Process.SHELL_UID) {
2822 return mCurrentUserId;
2823 }
2824 final int callingUserId = UserHandle.getUserId(callingUid);
2825 if (callingUserId == userId) {
2826 return userId;
2827 }
2828 if (!hasPermission(Manifest.permission.INTERACT_ACROSS_USERS)
2829 && !hasPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL)) {
2830 throw new SecurityException("Call from user " + callingUserId + " as user "
2831 + userId + " without permission INTERACT_ACROSS_USERS or "
2832 + "INTERACT_ACROSS_USERS_FULL not allowed.");
2833 }
2834 if (userId == UserHandle.USER_CURRENT
2835 || userId == UserHandle.USER_CURRENT_OR_SELF) {
2836 return mCurrentUserId;
2837 }
2838 throw new IllegalArgumentException("Calling user can be changed to only "
2839 + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF.");
2840 }
2841
2842 public boolean isCallerInteractingAcrossUsers(int userId) {
2843 final int callingUid = Binder.getCallingUid();
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002844 return (Binder.getCallingPid() == android.os.Process.myPid()
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002845 || callingUid == Process.SHELL_UID
2846 || userId == UserHandle.USER_CURRENT
2847 || userId == UserHandle.USER_CURRENT_OR_SELF);
2848 }
2849
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002850 private boolean isRetrievalAllowingWindow(int windowId) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002851 return (mActiveWindowId == windowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002852 }
2853
2854 private boolean isActionPermitted(int action) {
2855 return (VALID_ACTIONS & action) != 0;
2856 }
2857
2858 private void enforceCallingPermission(String permission, String function) {
2859 if (OWN_PROCESS_ID == Binder.getCallingPid()) {
2860 return;
2861 }
Svetoslav Ganov9371a0a2012-09-21 18:20:37 -07002862 if (!hasPermission(permission)) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002863 throw new SecurityException("You do not have " + permission
Svetoslav Ganov6ce77cd2012-11-21 16:35:57 -08002864 + " required to call " + function + " from pid="
2865 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002866 }
2867 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002868
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002869 private boolean hasPermission(String permission) {
2870 return mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED;
2871 }
2872
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002873 private int getFocusedWindowId() {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002874 try {
2875 // We call this only on window focus change or after touch
2876 // exploration gesture end and the shown windows are not that
2877 // many, so the linear look up is just fine.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002878 IBinder token = mWindowManagerService.getFocusedWindowToken();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002879 if (token != null) {
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002880 synchronized (mLock) {
2881 int windowId = getFocusedWindowIdLocked(token, mGlobalWindowTokens);
2882 if (windowId < 0) {
2883 windowId = getFocusedWindowIdLocked(token,
2884 getCurrentUserStateLocked().mWindowTokens);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002885 }
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002886 return windowId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002887 }
2888 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002889 } catch (RemoteException re) {
2890 /* ignore */
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002891 }
2892 return -1;
2893 }
2894
2895 private int getFocusedWindowIdLocked(IBinder token, SparseArray<IBinder> windows) {
2896 final int windowCount = windows.size();
2897 for (int i = 0; i < windowCount; i++) {
2898 if (windows.valueAt(i) == token) {
2899 return windows.keyAt(i);
2900 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002901 }
2902 return -1;
2903 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002904 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002905
2906 private class UserState {
2907 public final int mUserId;
2908
Svetoslav5fec0c52013-02-15 12:18:33 -08002909 // Non-transient state.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002910
2911 public final RemoteCallbackList<IAccessibilityManagerClient> mClients =
2912 new RemoteCallbackList<IAccessibilityManagerClient>();
2913
Svetoslav5fec0c52013-02-15 12:18:33 -08002914 public final SparseArray<AccessibilityConnectionWrapper> mInteractionConnections =
2915 new SparseArray<AccessibilityConnectionWrapper>();
2916
2917 public final SparseArray<IBinder> mWindowTokens = new SparseArray<IBinder>();
2918
2919 // Transient state.
2920
2921 public final CopyOnWriteArrayList<Service> mBoundServices =
2922 new CopyOnWriteArrayList<Service>();
2923
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002924 public final Map<ComponentName, Service> mComponentNameToServiceMap =
2925 new HashMap<ComponentName, Service>();
2926
2927 public final List<AccessibilityServiceInfo> mInstalledServices =
2928 new ArrayList<AccessibilityServiceInfo>();
2929
Svetoslav57bf8852013-02-07 19:21:42 -08002930 public final Set<ComponentName> mBindingServices = new HashSet<ComponentName>();
2931
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002932 public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>();
2933
Svetoslav Ganov447d9462013-02-01 19:46:20 +00002934 public final Set<ComponentName> mTouchExplorationGrantedServices =
2935 new HashSet<ComponentName>();
2936
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002937 public int mHandledFeedbackTypes = 0;
2938
Svetoslav5fec0c52013-02-15 12:18:33 -08002939 public int mLastSentClientState = -1;
Svetoslav57bf8852013-02-07 19:21:42 -08002940
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002941 public boolean mIsAccessibilityEnabled;
2942 public boolean mIsTouchExplorationEnabled;
Svetoslav38228962013-01-29 01:04:35 -08002943 public boolean mIsEnhancedWebAccessibilityEnabled;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002944 public boolean mIsDisplayMagnificationEnabled;
Svetoslavca523d62013-04-26 13:11:11 -07002945 public boolean mIsFilterKeyEventsEnabled;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002946
Svetoslav57bf8852013-02-07 19:21:42 -08002947 private Service mUiAutomationService;
2948 private IAccessibilityServiceClient mUiAutomationServiceClient;
2949
Svetoslav3c55e5c2013-02-27 18:24:28 -08002950 private IBinder mUiAutomationServiceOwner;
2951 private final DeathRecipient mUiAutomationSerivceOnwerDeathRecipient =
2952 new DeathRecipient() {
2953 @Override
2954 public void binderDied() {
2955 mUiAutomationServiceOwner.unlinkToDeath(
2956 mUiAutomationSerivceOnwerDeathRecipient, 0);
2957 mUiAutomationServiceOwner = null;
2958 if (mUiAutomationService != null) {
2959 mUiAutomationService.binderDied();
2960 }
2961 }
2962 };
2963
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002964 public UserState(int userId) {
2965 mUserId = userId;
2966 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002967
Svetoslav57bf8852013-02-07 19:21:42 -08002968 public int getClientState() {
2969 int clientState = 0;
2970 if (mIsAccessibilityEnabled) {
2971 clientState |= AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED;
2972 }
2973 // Touch exploration relies on enabled accessibility.
2974 if (mIsAccessibilityEnabled && mIsTouchExplorationEnabled) {
2975 clientState |= AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED;
2976 }
2977 return clientState;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07002978 }
Svetoslav5fec0c52013-02-15 12:18:33 -08002979
2980 public void onSwitchToAnotherUser() {
2981 // Clear UI test automation state.
2982 if (mUiAutomationService != null) {
2983 mUiAutomationService.binderDied();
Svetoslav5fec0c52013-02-15 12:18:33 -08002984 }
2985
2986 // Unbind all services.
2987 unbindAllServicesLocked(this);
2988
2989 // Clear service management state.
2990 mBoundServices.clear();
2991 mBindingServices.clear();
2992
2993 // Clear event management state.
2994 mHandledFeedbackTypes = 0;
2995 mLastSentClientState = -1;
2996
2997 // Clear state persisted in settings.
2998 mEnabledServices.clear();
2999 mTouchExplorationGrantedServices.clear();
3000 mIsAccessibilityEnabled = false;
3001 mIsTouchExplorationEnabled = false;
3002 mIsEnhancedWebAccessibilityEnabled = false;
3003 mIsDisplayMagnificationEnabled = false;
3004 }
Svetoslav3c55e5c2013-02-27 18:24:28 -08003005
3006 public void destroyUiAutomationService() {
3007 mUiAutomationService = null;
3008 mUiAutomationServiceClient = null;
3009 if (mUiAutomationServiceOwner != null) {
3010 mUiAutomationServiceOwner.unlinkToDeath(
3011 mUiAutomationSerivceOnwerDeathRecipient, 0);
3012 mUiAutomationServiceOwner = null;
3013 }
3014 }
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07003015 }
3016
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003017 private final class AccessibilityContentObserver extends ContentObserver {
3018
3019 private final Uri mAccessibilityEnabledUri = Settings.Secure.getUriFor(
3020 Settings.Secure.ACCESSIBILITY_ENABLED);
3021
3022 private final Uri mTouchExplorationEnabledUri = Settings.Secure.getUriFor(
3023 Settings.Secure.TOUCH_EXPLORATION_ENABLED);
3024
3025 private final Uri mDisplayMagnificationEnabledUri = Settings.Secure.getUriFor(
3026 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED);
3027
3028 private final Uri mEnabledAccessibilityServicesUri = Settings.Secure.getUriFor(
3029 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
3030
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003031 private final Uri mTouchExplorationGrantedAccessibilityServicesUri = Settings.Secure
3032 .getUriFor(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES);
3033
Svetoslav57bf8852013-02-07 19:21:42 -08003034 private final Uri mEnhancedWebAccessibilityUri = Settings.Secure
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003035 .getUriFor(Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION);
3036
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003037 public AccessibilityContentObserver(Handler handler) {
3038 super(handler);
3039 }
3040
3041 public void register(ContentResolver contentResolver) {
3042 contentResolver.registerContentObserver(mAccessibilityEnabledUri,
3043 false, this, UserHandle.USER_ALL);
3044 contentResolver.registerContentObserver(mTouchExplorationEnabledUri,
3045 false, this, UserHandle.USER_ALL);
3046 contentResolver.registerContentObserver(mDisplayMagnificationEnabledUri,
3047 false, this, UserHandle.USER_ALL);
3048 contentResolver.registerContentObserver(mEnabledAccessibilityServicesUri,
3049 false, this, UserHandle.USER_ALL);
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003050 contentResolver.registerContentObserver(
3051 mTouchExplorationGrantedAccessibilityServicesUri,
3052 false, this, UserHandle.USER_ALL);
Svetoslav57bf8852013-02-07 19:21:42 -08003053 contentResolver.registerContentObserver(mEnhancedWebAccessibilityUri,
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003054 false, this, UserHandle.USER_ALL);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003055 }
3056
3057 @Override
3058 public void onChange(boolean selfChange, Uri uri) {
3059 if (mAccessibilityEnabledUri.equals(uri)) {
3060 synchronized (mLock) {
3061 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003062 UserState userState = getCurrentUserStateLocked();
3063 if (userState.mUiAutomationService == null) {
3064 if (readAccessibilityEnabledSettingLocked(userState)) {
3065 onUserStateChangedLocked(userState);
3066 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003067 }
3068 }
3069 } else if (mTouchExplorationEnabledUri.equals(uri)) {
3070 synchronized (mLock) {
3071 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003072 UserState userState = getCurrentUserStateLocked();
3073 if (userState.mUiAutomationService == null) {
3074 if (readTouchExplorationEnabledSettingLocked(userState)) {
3075 onUserStateChangedLocked(userState);
3076 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003077 }
3078 }
3079 } else if (mDisplayMagnificationEnabledUri.equals(uri)) {
3080 synchronized (mLock) {
3081 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003082 UserState userState = getCurrentUserStateLocked();
3083 if (userState.mUiAutomationService == null) {
3084 if (readDisplayMagnificationEnabledSettingLocked(userState)) {
3085 onUserStateChangedLocked(userState);
3086 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003087 }
3088 }
3089 } else if (mEnabledAccessibilityServicesUri.equals(uri)) {
3090 synchronized (mLock) {
3091 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003092 UserState userState = getCurrentUserStateLocked();
3093 if (userState.mUiAutomationService == null) {
3094 if (readEnabledAccessibilityServicesLocked(userState)) {
3095 onUserStateChangedLocked(userState);
3096 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003097 }
3098 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003099 } else if (mTouchExplorationGrantedAccessibilityServicesUri.equals(uri)) {
3100 synchronized (mLock) {
3101 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003102 UserState userState = getCurrentUserStateLocked();
3103 if (userState.mUiAutomationService == null) {
3104 if (readTouchExplorationGrantedAccessibilityServicesLocked(userState)) {
3105 onUserStateChangedLocked(userState);
3106 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003107 }
3108 }
Svetoslav57bf8852013-02-07 19:21:42 -08003109 } else if (mEnhancedWebAccessibilityUri.equals(uri)) {
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003110 synchronized (mLock) {
3111 // We will update when the automation service dies.
Svetoslav57bf8852013-02-07 19:21:42 -08003112 UserState userState = getCurrentUserStateLocked();
3113 if (userState.mUiAutomationService == null) {
3114 if (readEnhancedWebAccessibilityEnabledChangedLocked(userState)) {
3115 onUserStateChangedLocked(userState);
3116 }
Svetoslav Ganov447d9462013-02-01 19:46:20 +00003117 }
3118 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07003119 }
3120 }
3121 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003122}