blob: 5e9e223dacb87fe8fb698a97c117dfe7a4d3628b [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 Ganove4abc512012-05-09 11:02:38 -070020import static android.accessibilityservice.AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
Svetoslav Ganov42138042012-03-20 11:51:39 -070021
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070022import android.Manifest;
svetoslavganov75986cf2009-05-14 22:28:01 -070023import android.accessibilityservice.AccessibilityService;
24import android.accessibilityservice.AccessibilityServiceInfo;
Svetoslav Ganov42138042012-03-20 11:51:39 -070025import android.accessibilityservice.IAccessibilityServiceClient;
svetoslavganov75986cf2009-05-14 22:28:01 -070026import android.accessibilityservice.IAccessibilityServiceConnection;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070027import android.app.AlertDialog;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070028import android.app.PendingIntent;
Svetoslav Ganov5c89f442012-05-15 13:28:09 -070029import android.app.StatusBarManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070030import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.ContentResolver;
33import android.content.Context;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070034import android.content.DialogInterface;
35import android.content.DialogInterface.OnClickListener;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.content.Intent;
37import android.content.IntentFilter;
38import android.content.ServiceConnection;
39import android.content.pm.PackageManager;
40import android.content.pm.ResolveInfo;
Svetoslav Ganov53e184d2012-05-16 15:57:10 -070041import android.content.pm.ServiceInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.database.ContentObserver;
Svetoslav Ganov7961be72011-06-21 12:31:56 -070043import android.graphics.Rect;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070044import android.hardware.input.InputManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070045import android.net.Uri;
46import android.os.Binder;
Svetoslav Ganov42138042012-03-20 11:51:39 -070047import android.os.Build;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070048import android.os.Bundle;
svetoslavganov75986cf2009-05-14 22:28:01 -070049import android.os.Handler;
50import android.os.IBinder;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -070051import android.os.Looper;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.Message;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -070053import android.os.Process;
54import android.os.RemoteCallbackList;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.os.RemoteException;
Jeff Brown0029c662011-03-30 02:25:18 -070056import android.os.ServiceManager;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070057import android.os.SystemClock;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -070058import android.os.UserHandle;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -070059import android.os.UserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.provider.Settings;
61import android.text.TextUtils;
62import android.text.TextUtils.SimpleStringSplitter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080063import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.util.SparseArray;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070065import android.view.IWindow;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070066import android.view.IWindowManager;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -070067import android.view.InputDevice;
68import android.view.KeyCharacterMap;
69import android.view.KeyEvent;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -070070import android.view.WindowInfo;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070071import android.view.WindowManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070073import android.view.accessibility.AccessibilityInteractionClient;
Svetoslav Ganov00aabf72011-07-21 11:35:03 -070074import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070075import android.view.accessibility.AccessibilityNodeInfo;
76import android.view.accessibility.IAccessibilityInteractionConnection;
77import android.view.accessibility.IAccessibilityInteractionConnectionCallback;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import android.view.accessibility.IAccessibilityManager;
79import android.view.accessibility.IAccessibilityManagerClient;
80
Svetoslav Ganove15ccb92012-05-16 15:48:55 -070081import com.android.internal.R;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070082import com.android.internal.content.PackageMonitor;
Svetoslav Ganovc682fc92012-06-04 14:02:09 -070083import com.android.internal.statusbar.IStatusBarService;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070084
85import org.xmlpull.v1.XmlPullParserException;
86
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -070087import java.io.IOException;
svetoslavganov75986cf2009-05-14 22:28:01 -070088import java.util.ArrayList;
89import java.util.Arrays;
90import java.util.HashMap;
91import java.util.HashSet;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080092import java.util.Iterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070093import java.util.List;
94import java.util.Map;
95import java.util.Set;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -070096import java.util.concurrent.CopyOnWriteArrayList;
svetoslavganov75986cf2009-05-14 22:28:01 -070097
98/**
99 * This class is instantiated by the system as a system level service and can be
100 * accessed only by the system. The task of this service is to be a centralized
101 * event dispatch for {@link AccessibilityEvent}s generated across all processes
102 * on the device. Events are dispatched to {@link AccessibilityService}s.
103 *
104 * @hide
105 */
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700106public class AccessibilityManagerService extends IAccessibilityManager.Stub {
svetoslavganov75986cf2009-05-14 22:28:01 -0700107
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700108 private static final boolean DEBUG = false;
109
svetoslavganov75986cf2009-05-14 22:28:01 -0700110 private static final String LOG_TAG = "AccessibilityManagerService";
111
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700112 // TODO: This is arbitrary. When there is time implement this by watching
113 // when that accessibility services are bound.
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700114 private static final int WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS = 3000;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700115
Svetoslav Ganov0d04e242012-02-21 13:46:36 -0800116 private static final String FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE =
117 "registerUiTestAutomationService";
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700118
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700119 private static final String TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED =
120 "temporaryEnableAccessibilityStateUntilKeyguardRemoved";
121
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700122 private static final char COMPONENT_NAME_SEPARATOR = ':';
123
svetoslavganov75986cf2009-05-14 22:28:01 -0700124 private static final int OWN_PROCESS_ID = android.os.Process.myPid();
125
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700126 private static int sIdCounter = 0;
127
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700128 private static int sNextWindowId;
129
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700130 private final Context mContext;
svetoslavganov75986cf2009-05-14 22:28:01 -0700131
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700132 private final Object mLock = new Object();
svetoslavganov75986cf2009-05-14 22:28:01 -0700133
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700134 private final SimpleStringSplitter mStringColonSplitter =
135 new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
svetoslavganov75986cf2009-05-14 22:28:01 -0700136
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700137 private final List<AccessibilityServiceInfo> mEnabledServicesForFeedbackTempList =
138 new ArrayList<AccessibilityServiceInfo>();
svetoslavganov75986cf2009-05-14 22:28:01 -0700139
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700140 private final PackageManager mPackageManager;
svetoslavganov75986cf2009-05-14 22:28:01 -0700141
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700142 private final IWindowManager mWindowManagerService;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700143
144 private final SecurityPolicy mSecurityPolicy;
145
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700146 private final MainHandler mMainHandler;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700147
Svetoslav Ganov9b666d02012-02-10 14:55:41 -0800148 private Service mUiAutomationService;
149
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700150 private Service mQueryBridge;
svetoslavganov75986cf2009-05-14 22:28:01 -0700151
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700152 private AlertDialog mEnableTouchExplorationDialog;
153
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700154 private AccessibilityInputFilter mInputFilter;
155
156 private boolean mHasInputFilter;
157
158 private final RemoteCallbackList<IAccessibilityManagerClient> mGlobalClients =
159 new RemoteCallbackList<IAccessibilityManagerClient>();
160
161 private final SparseArray<AccessibilityConnectionWrapper> mGlobalInteractionConnections =
162 new SparseArray<AccessibilityConnectionWrapper>();
163
164 private final SparseArray<IBinder> mGlobalWindowTokens = new SparseArray<IBinder>();
165
166 private final SparseArray<UserState> mUserStates = new SparseArray<UserState>();
167
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700168 private final TempUserStateChangeMemento mTempStateChangeForCurrentUserMemento =
169 new TempUserStateChangeMemento();
170
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700171 private int mCurrentUserId = UserHandle.USER_OWNER;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700172
173 private UserState getCurrentUserStateLocked() {
174 return getUserStateLocked(mCurrentUserId);
175 }
176
177 private UserState getUserStateLocked(int userId) {
178 UserState state = mUserStates.get(userId);
179 if (state == null) {
180 state = new UserState(userId);
181 mUserStates.put(userId, state);
182 }
183 return state;
184 }
185
svetoslavganov75986cf2009-05-14 22:28:01 -0700186 /**
187 * Creates a new instance.
188 *
189 * @param context A {@link Context} instance.
190 */
Jeff Brown6e6cd7a2011-03-30 03:27:08 -0700191 public AccessibilityManagerService(Context context) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700192 mContext = context;
193 mPackageManager = mContext.getPackageManager();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700194 mWindowManagerService = (IWindowManager) ServiceManager.getService(Context.WINDOW_SERVICE);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700195 mSecurityPolicy = new SecurityPolicy();
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -0700196 mMainHandler = new MainHandler(mContext.getMainLooper());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700197 registerBroadcastReceivers();
198 new AccessibilityContentObserver(mMainHandler).register(
199 context.getContentResolver());
svetoslavganov75986cf2009-05-14 22:28:01 -0700200 }
201
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700202 private void registerBroadcastReceivers() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800203 PackageMonitor monitor = new PackageMonitor() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700204 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800205 public void onSomePackagesChanged() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700206 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700207 if (getChangingUserId() != mCurrentUserId) {
208 return;
209 }
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -0700210 // We will update when the automation service dies.
211 if (mUiAutomationService == null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700212 UserState userState = getCurrentUserStateLocked();
213 populateInstalledAccessibilityServiceLocked(userState);
214 manageServicesLocked(userState);
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -0700215 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800216 }
217 }
Svetoslav Ganov00aabf72011-07-21 11:35:03 -0700218
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800219 @Override
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700220 public void onPackageRemoved(String packageName, int uid) {
221 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700222 final int userId = getChangingUserId();
223 if (userId != mCurrentUserId) {
224 return;
225 }
226 UserState state = getUserStateLocked(userId);
227 Iterator<ComponentName> it = state.mEnabledServices.iterator();
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700228 while (it.hasNext()) {
229 ComponentName comp = it.next();
230 String compPkg = comp.getPackageName();
231 if (compPkg.equals(packageName)) {
232 it.remove();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700233 // Update the enabled services setting.
234 persistComponentNamesToSettingLocked(
235 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700236 state.mEnabledServices, userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700237 // Update the touch exploration granted services setting.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700238 state.mTouchExplorationGrantedServices.remove(comp);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700239 persistComponentNamesToSettingLocked(
240 Settings.Secure.
241 TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700242 state.mEnabledServices, userId);
Svetoslav Ganovd07d60b2011-09-14 11:41:29 -0700243 return;
244 }
245 }
246 }
247 }
248
249 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800250 public boolean onHandleForceStop(Intent intent, String[] packages,
251 int uid, boolean doit) {
252 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700253 final int userId = getChangingUserId();
254 if (userId != mCurrentUserId) {
255 return false;
256 }
257 UserState state = getUserStateLocked(userId);
258 Iterator<ComponentName> it = state.mEnabledServices.iterator();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800259 while (it.hasNext()) {
260 ComponentName comp = it.next();
261 String compPkg = comp.getPackageName();
262 for (String pkg : packages) {
263 if (compPkg.equals(pkg)) {
264 if (!doit) {
265 return true;
266 }
267 it.remove();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700268 persistComponentNamesToSettingLocked(
269 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700270 state.mEnabledServices, userId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800271 }
272 }
273 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800274 return false;
275 }
276 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700277 };
278
279 // package changes
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700280 monitor.register(mContext, null, UserHandle.ALL, true);
svetoslavganov75986cf2009-05-14 22:28:01 -0700281
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700282 // user change and unlock
283 IntentFilter intentFilter = new IntentFilter();
284 intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
285 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
286 intentFilter.addAction(Intent.ACTION_USER_PRESENT);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700287
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700288 mContext.registerReceiverAsUser(new BroadcastReceiver() {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700289 @Override
290 public void onReceive(Context context, Intent intent) {
291 String action = intent.getAction();
292 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
293 switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
294 } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
295 removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700296 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
297 restoreStateFromMementoIfNeeded();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700298 }
299 }
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700300 }, UserHandle.ALL, intentFilter, null, null);
svetoslavganov75986cf2009-05-14 22:28:01 -0700301 }
302
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700303 public int addClient(IAccessibilityManagerClient client, int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700304 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700305 final int resolvedUserId = mSecurityPolicy
306 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
307 // If the client is from a process that runs across users such as
308 // the system UI or the system we add it to the global state that
309 // is shared across users.
310 UserState userState = getUserStateLocked(resolvedUserId);
311 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
312 mGlobalClients.register(client);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700313 if (DEBUG) {
314 Slog.i(LOG_TAG, "Added global client for pid:" + Binder.getCallingPid());
315 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700316 return getClientState(userState);
317 } else {
318 userState.mClients.register(client);
319 // If this client is not for the current user we do not
320 // return a state since it is not for the foreground user.
321 // We will send the state to the client on a user switch.
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700322 if (DEBUG) {
323 Slog.i(LOG_TAG, "Added user client for pid:" + Binder.getCallingPid()
324 + " and userId:" + mCurrentUserId);
325 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700326 return (resolvedUserId == mCurrentUserId) ? getClientState(userState) : 0;
327 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700328 }
329 }
330
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700331 public boolean sendAccessibilityEvent(AccessibilityEvent event, int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700332 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700333 final int resolvedUserId = mSecurityPolicy
334 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
335 // This method does nothing for a background user.
336 if (resolvedUserId != mCurrentUserId) {
337 return true; // yes, recycle the event
338 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700339 if (mSecurityPolicy.canDispatchAccessibilityEvent(event)) {
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700340 mSecurityPolicy.updateEventSourceLocked(event);
341 mMainHandler.obtainMessage(MainHandler.MSG_UPDATE_ACTIVE_WINDOW,
342 event.getWindowId(), event.getEventType()).sendToTarget();
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -0700343 notifyAccessibilityServicesDelayedLocked(event, false);
344 notifyAccessibilityServicesDelayedLocked(event, true);
345 }
Svetoslav Ganov86783472012-06-06 21:12:20 -0700346 if (mHasInputFilter && mInputFilter != null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700347 mMainHandler.obtainMessage(MainHandler.MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER,
Svetoslav Ganove45c0b22012-06-08 17:44:29 -0700348 AccessibilityEvent.obtain(event)).sendToTarget();
Svetoslav Ganov86783472012-06-06 21:12:20 -0700349 }
Svetoslav Ganovcd94caf2012-06-03 19:34:34 -0700350 event.recycle();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700351 getUserStateLocked(resolvedUserId).mHandledFeedbackTypes = 0;
svetoslavganov75986cf2009-05-14 22:28:01 -0700352 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700353 return (OWN_PROCESS_ID != Binder.getCallingPid());
354 }
355
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700356 public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList(int userId) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700357 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700358 final int resolvedUserId = mSecurityPolicy
359 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
360 return getUserStateLocked(resolvedUserId).mInstalledServices;
svetoslavganov75986cf2009-05-14 22:28:01 -0700361 }
362 }
363
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700364 public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList(int feedbackType,
365 int userId) {
366 List<AccessibilityServiceInfo> result = null;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700367 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700368 final int resolvedUserId = mSecurityPolicy
369 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
370 result = mEnabledServicesForFeedbackTempList;
371 result.clear();
372 List<Service> services = getUserStateLocked(resolvedUserId).mServices;
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700373 while (feedbackType != 0) {
374 final int feedbackTypeBit = (1 << Integer.numberOfTrailingZeros(feedbackType));
375 feedbackType &= ~feedbackTypeBit;
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700376 final int serviceCount = services.size();
377 for (int i = 0; i < serviceCount; i++) {
378 Service service = services.get(i);
Svetoslav Ganovc321c192011-06-03 19:43:23 -0700379 if ((service.mFeedbackType & feedbackTypeBit) != 0) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700380 result.add(service.mAccessibilityServiceInfo);
381 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700382 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700383 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700384 }
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700385 return result;
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700386 }
387
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700388 public void interrupt(int userId) {
389 CopyOnWriteArrayList<Service> services;
svetoslavganov75986cf2009-05-14 22:28:01 -0700390 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700391 final int resolvedUserId = mSecurityPolicy
392 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
393 // This method does nothing for a background user.
394 if (resolvedUserId != mCurrentUserId) {
395 return;
396 }
397 services = getUserStateLocked(resolvedUserId).mServices;
398 }
399 for (int i = 0, count = services.size(); i < count; i++) {
400 Service service = services.get(i);
401 try {
402 service.mServiceInterface.onInterrupt();
403 } catch (RemoteException re) {
404 Slog.e(LOG_TAG, "Error during sending interrupt request to "
405 + service.mService, re);
svetoslavganov75986cf2009-05-14 22:28:01 -0700406 }
407 }
408 }
409
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700410 public int addAccessibilityInteractionConnection(IWindow windowToken,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700411 IAccessibilityInteractionConnection connection, int userId) throws RemoteException {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700412 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700413 final int resolvedUserId = mSecurityPolicy
414 .resolveCallingUserIdEnforcingPermissionsLocked(userId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700415 final int windowId = sNextWindowId++;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700416 // If the window is from a process that runs across users such as
417 // the system UI or the system we add it to the global state that
418 // is shared across users.
419 if (mSecurityPolicy.isCallerInteractingAcrossUsers(userId)) {
420 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
421 windowId, connection, UserHandle.USER_ALL);
422 wrapper.linkToDeath();
423 mGlobalInteractionConnections.put(windowId, wrapper);
424 mGlobalWindowTokens.put(windowId, windowToken.asBinder());
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700425 if (DEBUG) {
426 Slog.i(LOG_TAG, "Added global connection for pid:" + Binder.getCallingPid()
427 + " with windowId: " + windowId);
428 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700429 } else {
430 AccessibilityConnectionWrapper wrapper = new AccessibilityConnectionWrapper(
431 windowId, connection, resolvedUserId);
432 wrapper.linkToDeath();
433 UserState userState = getUserStateLocked(resolvedUserId);
434 userState.mInteractionConnections.put(windowId, wrapper);
435 userState.mWindowTokens.put(windowId, windowToken.asBinder());
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700436 if (DEBUG) {
437 Slog.i(LOG_TAG, "Added user connection for pid:" + Binder.getCallingPid()
438 + " with windowId: " + windowId + " and userId:" + mCurrentUserId);
439 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700440 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700441 if (DEBUG) {
442 Slog.i(LOG_TAG, "Adding interaction connection to windowId: " + windowId);
443 }
444 return windowId;
445 }
446 }
447
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700448 public void removeAccessibilityInteractionConnection(IWindow window) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700449 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700450 mSecurityPolicy.resolveCallingUserIdEnforcingPermissionsLocked(
451 UserHandle.getCallingUserId());
452 IBinder token = window.asBinder();
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700453 final int removedWindowId = removeAccessibilityInteractionConnectionInternalLocked(
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700454 token, mGlobalWindowTokens, mGlobalInteractionConnections);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700455 if (removedWindowId >= 0) {
456 if (DEBUG) {
457 Slog.i(LOG_TAG, "Removed global connection for pid:" + Binder.getCallingPid()
458 + " with windowId: " + removedWindowId);
459 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700460 return;
461 }
462 final int userCount = mUserStates.size();
463 for (int i = 0; i < userCount; i++) {
464 UserState userState = mUserStates.valueAt(i);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700465 final int removedWindowIdForUser =
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700466 removeAccessibilityInteractionConnectionInternalLocked(
467 token, userState.mWindowTokens, userState.mInteractionConnections);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700468 if (removedWindowIdForUser >= 0) {
469 if (DEBUG) {
470 Slog.i(LOG_TAG, "Removed user connection for pid:" + Binder.getCallingPid()
471 + " with windowId: " + removedWindowIdForUser + " and userId:"
472 + mUserStates.keyAt(i));
473 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700474 return;
475 }
476 }
477 }
478 }
479
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700480 private int removeAccessibilityInteractionConnectionInternalLocked(IBinder windowToken,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700481 SparseArray<IBinder> windowTokens,
482 SparseArray<AccessibilityConnectionWrapper> interactionConnections) {
483 final int count = windowTokens.size();
484 for (int i = 0; i < count; i++) {
485 if (windowTokens.valueAt(i) == windowToken) {
486 final int windowId = windowTokens.keyAt(i);
487 windowTokens.removeAt(i);
488 AccessibilityConnectionWrapper wrapper = interactionConnections.get(windowId);
489 wrapper.unlinkToDeath();
490 interactionConnections.remove(windowId);
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700491 return windowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700492 }
493 }
Svetoslav Ganova8afa692012-09-25 13:03:18 -0700494 return -1;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700495 }
496
Svetoslav Ganov42138042012-03-20 11:51:39 -0700497 public void registerUiTestAutomationService(IAccessibilityServiceClient serviceClient,
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800498 AccessibilityServiceInfo accessibilityServiceInfo) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700499 mSecurityPolicy.enforceCallingPermission(Manifest.permission.RETRIEVE_WINDOW_CONTENT,
Svetoslav Ganov0d04e242012-02-21 13:46:36 -0800500 FUNCTION_REGISTER_UI_TEST_AUTOMATION_SERVICE);
Svetoslav Ganove8f95352011-07-06 17:24:03 -0700501 ComponentName componentName = new ComponentName("foo.bar",
502 "AutomationAccessibilityService");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700503 synchronized (mLock) {
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700504 // If an automation services is connected to the system all services are stopped
505 // so the automation one is the only one running. Settings are not changed so when
506 // the automation service goes away the state is restored from the settings.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700507 UserState userState = getCurrentUserStateLocked();
508 unbindAllServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700509
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700510 // If necessary enable accessibility and announce that.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700511 if (!userState.mIsAccessibilityEnabled) {
512 userState.mIsAccessibilityEnabled = true;
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700513 }
Svetoslav Ganov657968a2012-09-21 19:08:35 -0700514 // No touch exploration.
515 userState.mIsTouchExplorationEnabled = false;
516
517 // Hook the automation service up.
518 mUiAutomationService = new Service(mCurrentUserId, componentName,
519 accessibilityServiceInfo, true);
520 mUiAutomationService.onServiceConnected(componentName, serviceClient.asBinder());
521
522 updateInputFilterLocked(userState);
523 scheduleSendStateToClientsLocked(userState);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700524 }
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800525 }
526
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700527 public void temporaryEnableAccessibilityStateUntilKeyguardRemoved(
528 ComponentName service, boolean touchExplorationEnabled) {
529 mSecurityPolicy.enforceCallingPermission(
530 Manifest.permission.TEMPORARY_ENABLE_ACCESSIBILITY,
531 TEMPORARY_ENABLE_ACCESSIBILITY_UNTIL_KEYGUARD_REMOVED);
532 try {
533 if (!mWindowManagerService.isKeyguardLocked()) {
534 return;
535 }
536 } catch (RemoteException re) {
537 return;
538 }
539 synchronized (mLock) {
540 UserState userState = getCurrentUserStateLocked();
541 // Stash the old state so we can restore it when the keyguard is gone.
542 mTempStateChangeForCurrentUserMemento.initialize(mCurrentUserId, getCurrentUserStateLocked());
543 // Set the temporary state.
544 userState.mIsAccessibilityEnabled = true;
545 userState.mIsTouchExplorationEnabled= touchExplorationEnabled;
546 userState.mIsDisplayMagnificationEnabled = false;
547 userState.mEnabledServices.clear();
548 userState.mEnabledServices.add(service);
549 userState.mTouchExplorationGrantedServices.clear();
550 userState.mTouchExplorationGrantedServices.add(service);
551 // Update the internal state.
552 performServiceManagementLocked(userState);
553 updateInputFilterLocked(userState);
554 scheduleSendStateToClientsLocked(userState);
555 }
556 }
557
Svetoslav Ganov42138042012-03-20 11:51:39 -0700558 public void unregisterUiTestAutomationService(IAccessibilityServiceClient serviceClient) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800559 synchronized (mLock) {
Svetoslav Ganov9b666d02012-02-10 14:55:41 -0800560 // Automation service is not bound, so pretend it died to perform clean up.
Svetoslav Ganov42138042012-03-20 11:51:39 -0700561 if (mUiAutomationService != null
562 && mUiAutomationService.mServiceInterface == serviceClient) {
Svetoslav Ganov9b666d02012-02-10 14:55:41 -0800563 mUiAutomationService.binderDied();
Svetoslav Ganov79311c42012-01-17 20:24:26 -0800564 }
565 }
566 }
567
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700568 boolean onGesture(int gestureId) {
Svetoslav Ganov42138042012-03-20 11:51:39 -0700569 synchronized (mLock) {
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700570 boolean handled = notifyGestureLocked(gestureId, false);
571 if (!handled) {
572 handled = notifyGestureLocked(gestureId, true);
Svetoslav Ganov42138042012-03-20 11:51:39 -0700573 }
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700574 return handled;
575 }
576 }
577
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700578 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -0700579 * Gets the bounds of the accessibility focus in the active window.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700580 *
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700581 * @param outBounds The output to which to write the focus bounds.
Svetoslav Ganov86783472012-06-06 21:12:20 -0700582 * @return Whether accessibility focus was found and the bounds are populated.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700583 */
Svetoslav Ganov86783472012-06-06 21:12:20 -0700584 boolean getAccessibilityFocusBoundsInActiveWindow(Rect outBounds) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700585 // Instead of keeping track of accessibility focus events per
586 // window to be able to find the focus in the active window,
587 // we take a stateless approach and look it up. This is fine
588 // since we do this only when the user clicks/long presses.
589 Service service = getQueryBridge();
590 final int connectionId = service.mId;
591 AccessibilityInteractionClient client = AccessibilityInteractionClient.getInstance();
592 client.addConnection(connectionId, service);
593 try {
594 AccessibilityNodeInfo root = AccessibilityInteractionClient.getInstance()
595 .getRootInActiveWindow(connectionId);
596 if (root == null) {
597 return false;
598 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700599 AccessibilityNodeInfo focus = root.findFocus(
600 AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
601 if (focus == null) {
602 return false;
603 }
604 focus.getBoundsInScreen(outBounds);
605 return true;
606 } finally {
607 client.removeConnection(connectionId);
608 }
609 }
610
Svetoslav Ganov86783472012-06-06 21:12:20 -0700611 /**
612 * Gets the bounds of the active window.
613 *
614 * @param outBounds The output to which to write the bounds.
615 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700616 boolean getActiveWindowBounds(Rect outBounds) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700617 IBinder token;
Svetoslav Ganov86783472012-06-06 21:12:20 -0700618 synchronized (mLock) {
619 final int windowId = mSecurityPolicy.mActiveWindowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700620 token = mGlobalWindowTokens.get(windowId);
621 if (token == null) {
622 token = getCurrentUserStateLocked().mWindowTokens.get(windowId);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700623 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700624 }
625 WindowInfo info = null;
626 try {
627 info = mWindowManagerService.getWindowInfo(token);
628 if (info != null) {
629 outBounds.set(info.frame);
630 return true;
631 }
632 } catch (RemoteException re) {
633 /* ignore */
634 } finally {
635 if (info != null) {
636 info.recycle();
637 }
638 }
639 return false;
640 }
641
642 int getActiveWindowId() {
643 return mSecurityPolicy.mActiveWindowId;
644 }
645
646 private void switchUser(int userId) {
647 synchronized (mLock) {
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700648 // The user switched so we do not need to restore the current user
649 // state since we will fully rebuild it when he becomes current again.
650 mTempStateChangeForCurrentUserMemento.clear();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700651
652 // Disconnect from services for the old user.
653 UserState oldUserState = getUserStateLocked(mCurrentUserId);
654 unbindAllServicesLocked(oldUserState);
655
656 // Disable the local managers for the old user.
657 if (oldUserState.mClients.getRegisteredCallbackCount() > 0) {
658 mMainHandler.obtainMessage(MainHandler.MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER,
659 oldUserState.mUserId, 0).sendToTarget();
660 }
661
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700662 // Announce user changes only if more that one exist.
663 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
664 final boolean announceNewUser = userManager.getUsers().size() > 1;
665
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700666 // The user changed.
667 mCurrentUserId = userId;
668
669 // Recreate the internal state for the new user.
670 mMainHandler.obtainMessage(MainHandler.MSG_SEND_RECREATE_INTERNAL_STATE,
671 mCurrentUserId, 0).sendToTarget();
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700672
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700673 if (announceNewUser) {
674 // Schedule announcement of the current user if needed.
675 mMainHandler.sendEmptyMessageDelayed(MainHandler.MSG_ANNOUNCE_NEW_USER_IF_NEEDED,
676 WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS);
677 }
Svetoslav Ganov86783472012-06-06 21:12:20 -0700678 }
679 }
680
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700681 private void removeUser(int userId) {
682 synchronized (mLock) {
683 mUserStates.remove(userId);
684 }
Svetoslav Ganov385d9f22012-06-07 16:35:04 -0700685 }
686
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700687 private void restoreStateFromMementoIfNeeded() {
688 synchronized (mLock) {
689 if (mTempStateChangeForCurrentUserMemento.mUserId != UserHandle.USER_NULL) {
690 UserState userState = getCurrentUserStateLocked();
691 // Restore the state from the memento.
692 mTempStateChangeForCurrentUserMemento.applyTo(userState);
693 mTempStateChangeForCurrentUserMemento.clear();
694 // Update the internal state.
695 performServiceManagementLocked(userState);
696 updateInputFilterLocked(userState);
697 scheduleSendStateToClientsLocked(userState);
698 }
699 }
700 }
701
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700702 private Service getQueryBridge() {
703 if (mQueryBridge == null) {
704 AccessibilityServiceInfo info = new AccessibilityServiceInfo();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700705 mQueryBridge = new Service(UserHandle.USER_NULL, null, info, true);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700706 }
707 return mQueryBridge;
708 }
709
Svetoslav Ganov42138042012-03-20 11:51:39 -0700710 private boolean notifyGestureLocked(int gestureId, boolean isDefault) {
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700711 // TODO: Now we are giving the gestures to the last enabled
712 // service that can handle them which is the last one
713 // in our list since we write the last enabled as the
714 // last record in the enabled services setting. Ideally,
715 // the user should make the call which service handles
716 // gestures. However, only one service should handle
Svetoslav Ganove4abc512012-05-09 11:02:38 -0700717 // gestures to avoid user frustration when different
718 // behavior is observed from different combinations of
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700719 // enabled accessibility services.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700720 UserState state = getCurrentUserStateLocked();
721 for (int i = state.mServices.size() - 1; i >= 0; i--) {
722 Service service = state.mServices.get(i);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -0700723 if (service.mRequestTouchExplorationMode && service.mIsDefault == isDefault) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700724 service.notifyGesture(gestureId);
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700725 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700726 }
727 }
728 return false;
729 }
730
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800731 /**
732 * Removes an AccessibilityInteractionConnection.
733 *
734 * @param windowId The id of the window to which the connection is targeted.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700735 * @param userId The id of the user owning the connection. UserHandle.USER_ALL
736 * if global.
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800737 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700738 private void removeAccessibilityInteractionConnectionLocked(int windowId, int userId) {
739 if (userId == UserHandle.USER_ALL) {
740 mGlobalWindowTokens.remove(windowId);
741 mGlobalInteractionConnections.remove(windowId);
742 } else {
743 UserState userState = getCurrentUserStateLocked();
744 userState.mWindowTokens.remove(windowId);
745 userState.mInteractionConnections.remove(windowId);
746 }
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800747 if (DEBUG) {
748 Slog.i(LOG_TAG, "Removing interaction connection to windowId: " + windowId);
749 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700750 }
751
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700752 private void populateInstalledAccessibilityServiceLocked(UserState userState) {
753 userState.mInstalledServices.clear();
svetoslavganov75986cf2009-05-14 22:28:01 -0700754
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700755 List<ResolveInfo> installedServices = mPackageManager.queryIntentServicesAsUser(
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700756 new Intent(AccessibilityService.SERVICE_INTERFACE),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700757 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
758 mCurrentUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -0700759
760 for (int i = 0, count = installedServices.size(); i < count; i++) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700761 ResolveInfo resolveInfo = installedServices.get(i);
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700762 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700763 if (!android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE.equals(
764 serviceInfo.permission)) {
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700765 Slog.w(LOG_TAG, "Skipping accessibilty service " + new ComponentName(
766 serviceInfo.packageName, serviceInfo.name).flattenToShortString()
767 + ": it does not require the permission "
768 + android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE);
769 continue;
770 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700771 AccessibilityServiceInfo accessibilityServiceInfo;
772 try {
773 accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700774 userState.mInstalledServices.add(accessibilityServiceInfo);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700775 } catch (XmlPullParserException xppe) {
776 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", xppe);
777 } catch (IOException ioe) {
778 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", ioe);
779 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700780 }
781 }
782
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700783 private void populateEnabledAccessibilityServicesLocked(UserState userState) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700784 populateComponentNamesFromSettingLocked(
785 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700786 userState.mUserId,
787 userState.mEnabledServices);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700788 }
789
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700790 private void populateTouchExplorationGrantedAccessibilityServicesLocked(
791 UserState userState) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700792 populateComponentNamesFromSettingLocked(
793 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700794 userState.mUserId,
795 userState.mTouchExplorationGrantedServices);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700796 }
797
svetoslavganov75986cf2009-05-14 22:28:01 -0700798 /**
799 * Performs {@link AccessibilityService}s delayed notification. The delay is configurable
800 * and denotes the period after the last event before notifying the service.
801 *
802 * @param event The event.
803 * @param isDefault True to notify default listeners, not default services.
804 */
805 private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event,
806 boolean isDefault) {
Charles Chen85b598b2009-07-29 17:23:50 -0700807 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700808 UserState state = getCurrentUserStateLocked();
809 for (int i = 0, count = state.mServices.size(); i < count; i++) {
810 Service service = state.mServices.get(i);
svetoslavganov75986cf2009-05-14 22:28:01 -0700811
Charles Chen85b598b2009-07-29 17:23:50 -0700812 if (service.mIsDefault == isDefault) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700813 if (canDispathEventLocked(service, event, state.mHandledFeedbackTypes)) {
814 state.mHandledFeedbackTypes |= service.mFeedbackType;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700815 service.notifyAccessibilityEvent(event);
Charles Chen85b598b2009-07-29 17:23:50 -0700816 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700817 }
818 }
Charles Chen85b598b2009-07-29 17:23:50 -0700819 } catch (IndexOutOfBoundsException oobe) {
820 // An out of bounds exception can happen if services are going away
821 // as the for loop is running. If that happens, just bail because
822 // there are no more services to notify.
823 return;
svetoslavganov75986cf2009-05-14 22:28:01 -0700824 }
825 }
826
827 /**
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700828 * Adds a service for a user.
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700829 *
830 * @param service The service to add.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700831 * @param userId The user id.
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700832 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700833 private void tryAddServiceLocked(Service service, int userId) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700834 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700835 UserState userState = getUserStateLocked(userId);
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -0700836 if (userState.mServices.contains(service)) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700837 return;
838 }
839 service.linkToOwnDeath();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700840 userState.mServices.add(service);
841 userState.mComponentNameToServiceMap.put(service.mComponentName, service);
842 updateInputFilterLocked(userState);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700843 tryEnableTouchExplorationLocked(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700844 } catch (RemoteException e) {
845 /* do nothing */
846 }
847 }
848
849 /**
850 * Removes a service.
svetoslavganov75986cf2009-05-14 22:28:01 -0700851 *
852 * @param service The service.
853 * @return True if the service was removed, false otherwise.
854 */
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700855 private boolean tryRemoveServiceLocked(Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700856 UserState userState = getUserStateLocked(service.mUserId);
857 final boolean removed = userState.mServices.remove(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700858 if (!removed) {
859 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -0700860 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700861 userState.mComponentNameToServiceMap.remove(service.mComponentName);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700862 service.unlinkToOwnDeath();
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800863 service.dispose();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700864 updateInputFilterLocked(userState);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700865 tryDisableTouchExplorationLocked(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700866 return removed;
svetoslavganov75986cf2009-05-14 22:28:01 -0700867 }
868
869 /**
870 * Determines if given event can be dispatched to a service based on the package of the
871 * event source and already notified services for that event type. Specifically, a
872 * service is notified if it is interested in events from the package and no other service
873 * providing the same feedback type has been notified. Exception are services the
874 * provide generic feedback (feedback type left as a safety net for unforeseen feedback
875 * types) which are always notified.
876 *
877 * @param service The potential receiver.
878 * @param event The event.
879 * @param handledFeedbackTypes The feedback types for which services have been notified.
880 * @return True if the listener should be notified, false otherwise.
881 */
882 private boolean canDispathEventLocked(Service service, AccessibilityEvent event,
883 int handledFeedbackTypes) {
884
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -0700885 if (!service.canReceiveEvents()) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700886 return false;
887 }
888
Svetoslav Ganov42138042012-03-20 11:51:39 -0700889 if (!event.isImportantForAccessibility()
890 && !service.mIncludeNotImportantViews) {
891 return false;
892 }
893
svetoslavganov75986cf2009-05-14 22:28:01 -0700894 int eventType = event.getEventType();
895 if ((service.mEventTypes & eventType) != eventType) {
896 return false;
897 }
898
899 Set<String> packageNames = service.mPackageNames;
900 CharSequence packageName = event.getPackageName();
901
902 if (packageNames.isEmpty() || packageNames.contains(packageName)) {
903 int feedbackType = service.mFeedbackType;
904 if ((handledFeedbackTypes & feedbackType) != feedbackType
905 || feedbackType == AccessibilityServiceInfo.FEEDBACK_GENERIC) {
906 return true;
907 }
908 }
909
910 return false;
911 }
912
913 /**
914 * Manages services by starting enabled ones and stopping disabled ones.
915 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700916 private void manageServicesLocked(UserState userState) {
917 final int enabledInstalledServicesCount = updateServicesStateLocked(userState);
Svetoslav Ganov1e741b22011-09-20 15:50:07 -0700918 // No enabled installed services => disable accessibility to avoid
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700919 // sending accessibility events with no recipient across processes.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700920 if (userState.mIsAccessibilityEnabled && enabledInstalledServicesCount == 0) {
921 Settings.Secure.putIntForUser(mContext.getContentResolver(),
922 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId);
Svetoslav Ganov1e741b22011-09-20 15:50:07 -0700923 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700924 }
925
926 /**
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700927 * Unbinds all bound services for a user.
928 *
929 * @param userState The user state.
svetoslavganov75986cf2009-05-14 22:28:01 -0700930 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700931 private void unbindAllServicesLocked(UserState userState) {
932 List<Service> services = userState.mServices;
svetoslavganov75986cf2009-05-14 22:28:01 -0700933 for (int i = 0, count = services.size(); i < count; i++) {
934 Service service = services.get(i);
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -0800935 if (service.unbind()) {
936 i--;
937 count--;
938 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700939 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700940 }
941
942 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700943 * Populates a set with the {@link ComponentName}s stored in a colon
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700944 * separated value setting for a given user.
svetoslavganov75986cf2009-05-14 22:28:01 -0700945 *
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700946 * @param settingName The setting to parse.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700947 * @param userId The user id.
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700948 * @param outComponentNames The output component names.
svetoslavganov75986cf2009-05-14 22:28:01 -0700949 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700950 private void populateComponentNamesFromSettingLocked(String settingName, int userId,
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700951 Set<ComponentName> outComponentNames) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700952 String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(),
953 settingName, userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700954 outComponentNames.clear();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700955 if (settingValue != null) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700956 TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700957 splitter.setString(settingValue);
svetoslavganov75986cf2009-05-14 22:28:01 -0700958 while (splitter.hasNext()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800959 String str = splitter.next();
960 if (str == null || str.length() <= 0) {
961 continue;
962 }
963 ComponentName enabledService = ComponentName.unflattenFromString(str);
964 if (enabledService != null) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700965 outComponentNames.add(enabledService);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800966 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700967 }
968 }
969 }
970
971 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700972 * Persists the component names in the specified setting in a
973 * colon separated fashion.
974 *
975 * @param settingName The setting name.
976 * @param componentNames The component names.
977 */
978 private void persistComponentNamesToSettingLocked(String settingName,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700979 Set<ComponentName> componentNames, int userId) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700980 StringBuilder builder = new StringBuilder();
981 for (ComponentName componentName : componentNames) {
982 if (builder.length() > 0) {
983 builder.append(COMPONENT_NAME_SEPARATOR);
984 }
985 builder.append(componentName.flattenToShortString());
986 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700987 Settings.Secure.putStringForUser(mContext.getContentResolver(),
988 settingName, builder.toString(), userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700989 }
990
991 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700992 * Updates the state of each service by starting (or keeping running) enabled ones and
993 * stopping the rest.
994 *
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700995 * @param userState The user state for which to do that.
Svetoslav Ganov1e741b22011-09-20 15:50:07 -0700996 * @return The number of enabled installed services.
svetoslavganov75986cf2009-05-14 22:28:01 -0700997 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700998 private int updateServicesStateLocked(UserState userState) {
999 Map<ComponentName, Service> componentNameToServiceMap =
1000 userState.mComponentNameToServiceMap;
1001 boolean isEnabled = userState.mIsAccessibilityEnabled;
svetoslavganov75986cf2009-05-14 22:28:01 -07001002
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001003 int enabledInstalledServices = 0;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001004 for (int i = 0, count = userState.mInstalledServices.size(); i < count; i++) {
1005 AccessibilityServiceInfo installedService = userState.mInstalledServices.get(i);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001006 ComponentName componentName = ComponentName.unflattenFromString(
1007 installedService.getId());
svetoslavganov75986cf2009-05-14 22:28:01 -07001008 Service service = componentNameToServiceMap.get(componentName);
1009
Svetoslav Ganovf2245aa2010-12-28 15:48:52 -08001010 if (isEnabled) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001011 if (userState.mEnabledServices.contains(componentName)) {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001012 if (service == null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001013 service = new Service(userState.mUserId, componentName,
1014 installedService, false);
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001015 }
1016 service.bind();
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001017 enabledInstalledServices++;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001018 } else {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001019 if (service != null) {
1020 service.unbind();
1021 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001022 }
1023 } else {
1024 if (service != null) {
1025 service.unbind();
svetoslavganov75986cf2009-05-14 22:28:01 -07001026 }
1027 }
1028 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001029
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001030 return enabledInstalledServices;
Svetoslav Ganov37fedf82011-09-14 10:44:09 -07001031 }
1032
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001033 private void scheduleSendStateToClientsLocked(UserState userState) {
1034 if (mGlobalClients.getRegisteredCallbackCount() > 0
1035 || userState.mClients.getRegisteredCallbackCount() > 0) {
1036 final int clientState = getClientState(userState);
1037 mMainHandler.obtainMessage(MainHandler.MSG_SEND_STATE_TO_CLIENTS,
1038 clientState, userState.mUserId) .sendToTarget();
1039 }
1040 }
1041
1042 private void updateInputFilterLocked(UserState userState) {
1043 boolean setInputFilter = false;
1044 AccessibilityInputFilter inputFilter = null;
1045 synchronized (mLock) {
1046 if ((userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled)
1047 || userState.mIsDisplayMagnificationEnabled) {
1048 if (!mHasInputFilter) {
1049 mHasInputFilter = true;
1050 if (mInputFilter == null) {
1051 mInputFilter = new AccessibilityInputFilter(mContext,
1052 AccessibilityManagerService.this);
1053 }
1054 inputFilter = mInputFilter;
1055 setInputFilter = true;
1056 }
1057 int flags = 0;
1058 if (userState.mIsDisplayMagnificationEnabled) {
1059 flags |= AccessibilityInputFilter.FLAG_FEATURE_SCREEN_MAGNIFIER;
1060 }
1061 if (userState.mIsTouchExplorationEnabled) {
1062 flags |= AccessibilityInputFilter.FLAG_FEATURE_TOUCH_EXPLORATION;
1063 }
1064 mInputFilter.setEnabledFeatures(flags);
1065 } else {
1066 if (mHasInputFilter) {
1067 mHasInputFilter = false;
1068 mInputFilter.setEnabledFeatures(0);
1069 inputFilter = null;
1070 setInputFilter = true;
1071 }
1072 }
1073 }
1074 if (setInputFilter) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001075 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001076 mWindowManagerService.setInputFilter(inputFilter);
svetoslavganov75986cf2009-05-14 22:28:01 -07001077 } catch (RemoteException re) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001078 /* ignore */
svetoslavganov75986cf2009-05-14 22:28:01 -07001079 }
1080 }
1081 }
1082
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001083 private void showEnableTouchExplorationDialog(final Service service) {
1084 String label = service.mResolveInfo.loadLabel(
1085 mContext.getPackageManager()).toString();
1086 synchronized (mLock) {
1087 final UserState state = getCurrentUserStateLocked();
1088 if (state.mIsTouchExplorationEnabled) {
1089 return;
1090 }
1091 if (mEnableTouchExplorationDialog != null
1092 && mEnableTouchExplorationDialog.isShowing()) {
1093 return;
1094 }
1095 mEnableTouchExplorationDialog = new AlertDialog.Builder(mContext)
1096 .setIcon(android.R.drawable.ic_dialog_alert)
1097 .setPositiveButton(android.R.string.ok, new OnClickListener() {
1098 @Override
1099 public void onClick(DialogInterface dialog, int which) {
1100 // The user allowed the service to toggle touch exploration.
1101 state.mTouchExplorationGrantedServices.add(service.mComponentName);
1102 persistComponentNamesToSettingLocked(
1103 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
1104 state.mTouchExplorationGrantedServices, state.mUserId);
1105 // Enable touch exploration.
1106 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1107 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1,
1108 service.mUserId);
1109 }
1110 })
1111 .setNegativeButton(android.R.string.cancel, new OnClickListener() {
1112 @Override
1113 public void onClick(DialogInterface dialog, int which) {
1114 dialog.dismiss();
1115 }
1116 })
1117 .setTitle(R.string.enable_explore_by_touch_warning_title)
1118 .setMessage(mContext.getString(
1119 R.string.enable_explore_by_touch_warning_message, label))
1120 .create();
1121 mEnableTouchExplorationDialog.getWindow().setType(
1122 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1123 mEnableTouchExplorationDialog.setCanceledOnTouchOutside(true);
1124 mEnableTouchExplorationDialog.show();
1125 }
1126 }
1127
1128 private int getClientState(UserState userState) {
1129 int clientState = 0;
1130 if (userState.mIsAccessibilityEnabled) {
1131 clientState |= AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001132 }
Svetoslav Ganov00aabf72011-07-21 11:35:03 -07001133 // Touch exploration relies on enabled accessibility.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001134 if (userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled) {
1135 clientState |= AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED;
Svetoslav Ganov00aabf72011-07-21 11:35:03 -07001136 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001137 return clientState;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001138 }
1139
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001140 private void recreateInternalStateLocked(UserState userState) {
1141 populateInstalledAccessibilityServiceLocked(userState);
1142 populateEnabledAccessibilityServicesLocked(userState);
1143 populateTouchExplorationGrantedAccessibilityServicesLocked(userState);
1144
1145 handleTouchExplorationEnabledSettingChangedLocked(userState);
1146 handleDisplayMagnificationEnabledSettingChangedLocked(userState);
1147 handleAccessibilityEnabledSettingChangedLocked(userState);
1148
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001149 performServiceManagementLocked(userState);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001150 updateInputFilterLocked(userState);
1151 scheduleSendStateToClientsLocked(userState);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001152 }
1153
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001154 private void handleAccessibilityEnabledSettingChangedLocked(UserState userState) {
1155 userState.mIsAccessibilityEnabled = Settings.Secure.getIntForUser(
1156 mContext.getContentResolver(),
1157 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId) == 1;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001158 }
1159
1160 private void performServiceManagementLocked(UserState userState) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001161 if (userState.mIsAccessibilityEnabled ) {
1162 manageServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001163 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001164 unbindAllServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001165 }
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001166 }
1167
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001168 private void handleTouchExplorationEnabledSettingChangedLocked(UserState userState) {
1169 userState.mIsTouchExplorationEnabled = Settings.Secure.getIntForUser(
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001170 mContext.getContentResolver(),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001171 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId) == 1;
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001172 }
1173
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001174 private void handleDisplayMagnificationEnabledSettingChangedLocked(UserState userState) {
1175 userState.mIsDisplayMagnificationEnabled = Settings.Secure.getIntForUser(
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001176 mContext.getContentResolver(),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001177 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
1178 0, userState.mUserId) == 1;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001179 }
1180
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001181 private void handleTouchExplorationGrantedAccessibilityServicesChangedLocked(
1182 UserState userState) {
1183 final int serviceCount = userState.mServices.size();
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001184 for (int i = 0; i < serviceCount; i++) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001185 Service service = userState.mServices.get(i);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001186 if (service.mRequestTouchExplorationMode
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001187 && userState.mTouchExplorationGrantedServices.contains(
1188 service.mComponentName)) {
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001189 tryEnableTouchExplorationLocked(service);
1190 return;
1191 }
1192 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001193 if (userState.mIsTouchExplorationEnabled) {
1194 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1195 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001196 }
1197 }
1198
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001199 private void tryEnableTouchExplorationLocked(final Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001200 UserState userState = getUserStateLocked(service.mUserId);
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001201 if (!userState.mIsTouchExplorationEnabled && service.mRequestTouchExplorationMode
1202 && service.canReceiveEvents()) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001203 final boolean canToggleTouchExploration =
1204 userState.mTouchExplorationGrantedServices.contains(service.mComponentName);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001205 if (!service.mIsAutomation && !canToggleTouchExploration) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001206 showEnableTouchExplorationDialog(service);
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -07001207 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001208 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1209 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1, userState.mUserId);
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -07001210 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001211 }
1212 }
1213
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001214 private void tryDisableTouchExplorationLocked(Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001215 UserState userState = getUserStateLocked(service.mUserId);
1216 if (userState.mIsTouchExplorationEnabled) {
1217 final int serviceCount = userState.mServices.size();
1218 for (int i = 0; i < serviceCount; i++) {
1219 Service other = userState.mServices.get(i);
1220 if (other != service && other.mRequestTouchExplorationMode) {
1221 return;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001222 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001223 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001224 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1225 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001226 }
1227 }
1228
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001229 private class AccessibilityConnectionWrapper implements DeathRecipient {
1230 private final int mWindowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001231 private final int mUserId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001232 private final IAccessibilityInteractionConnection mConnection;
1233
1234 public AccessibilityConnectionWrapper(int windowId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001235 IAccessibilityInteractionConnection connection, int userId) {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001236 mWindowId = windowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001237 mUserId = userId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001238 mConnection = connection;
1239 }
1240
1241 public void linkToDeath() throws RemoteException {
1242 mConnection.asBinder().linkToDeath(this, 0);
1243 }
1244
1245 public void unlinkToDeath() {
1246 mConnection.asBinder().unlinkToDeath(this, 0);
1247 }
1248
1249 @Override
1250 public void binderDied() {
1251 unlinkToDeath();
1252 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001253 removeAccessibilityInteractionConnectionLocked(mWindowId, mUserId);
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001254 }
1255 }
1256 }
1257
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001258 private final class MainHandler extends Handler {
1259 public static final int MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER = 1;
1260 public static final int MSG_SEND_STATE_TO_CLIENTS = 2;
1261 public static final int MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER = 3;
1262 public static final int MSG_SEND_RECREATE_INTERNAL_STATE = 4;
Svetoslav Ganova8afa692012-09-25 13:03:18 -07001263 public static final int MSG_UPDATE_ACTIVE_WINDOW = 5;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001264 public static final int MSG_ANNOUNCE_NEW_USER_IF_NEEDED = 6;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001265
1266 public MainHandler(Looper looper) {
1267 super(looper);
1268 }
1269
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001270 @Override
1271 public void handleMessage(Message msg) {
1272 final int type = msg.what;
1273 switch (type) {
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001274 case MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER: {
1275 AccessibilityEvent event = (AccessibilityEvent) msg.obj;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001276 synchronized (mLock) {
1277 if (mHasInputFilter && mInputFilter != null) {
1278 mInputFilter.notifyAccessibilityEvent(event);
1279 }
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001280 }
1281 event.recycle();
1282 } break;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001283 case MSG_SEND_STATE_TO_CLIENTS: {
1284 final int clientState = msg.arg1;
1285 final int userId = msg.arg2;
1286 sendStateToClients(clientState, mGlobalClients);
1287 sendStateToClientsForUser(clientState, userId);
1288 } break;
1289 case MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER: {
1290 final int userId = msg.arg1;
1291 sendStateToClientsForUser(0, userId);
1292 } break;
1293 case MSG_SEND_RECREATE_INTERNAL_STATE: {
1294 final int userId = msg.arg1;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001295 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001296 UserState userState = getUserStateLocked(userId);
1297 recreateInternalStateLocked(userState);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001298 }
1299 } break;
Svetoslav Ganova8afa692012-09-25 13:03:18 -07001300 case MSG_UPDATE_ACTIVE_WINDOW: {
1301 final int windowId = msg.arg1;
1302 final int eventType = msg.arg2;
1303 mSecurityPolicy.updateActiveWindow(windowId, eventType);
1304 } break;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001305 case MSG_ANNOUNCE_NEW_USER_IF_NEEDED: {
1306 announceNewUserIfNeeded();
1307 } break;
1308 }
1309 }
1310
1311 private void announceNewUserIfNeeded() {
1312 synchronized (mLock) {
1313 UserState userState = getCurrentUserStateLocked();
1314 if (userState.mIsAccessibilityEnabled) {
1315 UserManager userManager = (UserManager) mContext.getSystemService(
1316 Context.USER_SERVICE);
1317 String message = mContext.getString(R.string.user_switched,
1318 userManager.getUserInfo(mCurrentUserId).name);
1319 AccessibilityEvent event = AccessibilityEvent.obtain(
1320 AccessibilityEvent.TYPE_ANNOUNCEMENT);
1321 event.getText().add(message);
1322 sendAccessibilityEvent(event, mCurrentUserId);
1323 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001324 }
1325 }
1326
1327 private void sendStateToClientsForUser(int clientState, int userId) {
1328 final UserState userState;
1329 synchronized (mLock) {
1330 userState = getUserStateLocked(userId);
1331 }
1332 sendStateToClients(clientState, userState.mClients);
1333 }
1334
1335 private void sendStateToClients(int clientState,
1336 RemoteCallbackList<IAccessibilityManagerClient> clients) {
1337 try {
1338 final int userClientCount = clients.beginBroadcast();
1339 for (int i = 0; i < userClientCount; i++) {
1340 IAccessibilityManagerClient client = clients.getBroadcastItem(i);
1341 try {
1342 client.setState(clientState);
1343 } catch (RemoteException re) {
1344 /* ignore */
1345 }
1346 }
1347 } finally {
1348 clients.finishBroadcast();
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001349 }
1350 }
1351 }
1352
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001353 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001354 * This class represents an accessibility service. It stores all per service
1355 * data required for the service management, provides API for starting/stopping the
1356 * service and is responsible for adding/removing the service in the data structures
1357 * for service management. The class also exposes configuration interface that is
1358 * passed to the service it represents as soon it is bound. It also serves as the
1359 * connection for the service.
1360 */
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001361 class Service extends IAccessibilityServiceConnection.Stub
1362 implements ServiceConnection, DeathRecipient {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001363
1364 // We pick the MSB to avoid collision since accessibility event types are
1365 // used as message types allowing us to remove messages per event type.
1366 private static final int MSG_ON_GESTURE = 0x80000000;
1367
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001368 final int mUserId;
1369
svetoslavganov75986cf2009-05-14 22:28:01 -07001370 int mId = 0;
1371
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001372 AccessibilityServiceInfo mAccessibilityServiceInfo;
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001373
svetoslavganov75986cf2009-05-14 22:28:01 -07001374 IBinder mService;
1375
Svetoslav Ganov42138042012-03-20 11:51:39 -07001376 IAccessibilityServiceClient mServiceInterface;
svetoslavganov75986cf2009-05-14 22:28:01 -07001377
1378 int mEventTypes;
1379
1380 int mFeedbackType;
1381
1382 Set<String> mPackageNames = new HashSet<String>();
1383
1384 boolean mIsDefault;
1385
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001386 boolean mRequestTouchExplorationMode;
1387
Svetoslav Ganov42138042012-03-20 11:51:39 -07001388 boolean mIncludeNotImportantViews;
1389
svetoslavganov75986cf2009-05-14 22:28:01 -07001390 long mNotificationTimeout;
1391
svetoslavganov75986cf2009-05-14 22:28:01 -07001392 ComponentName mComponentName;
1393
1394 Intent mIntent;
1395
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001396 boolean mCanRetrieveScreenContent;
1397
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001398 boolean mIsAutomation;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001399
Svetoslav Ganov7961be72011-06-21 12:31:56 -07001400 final Rect mTempBounds = new Rect();
1401
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001402 final ResolveInfo mResolveInfo;
1403
svetoslavganov75986cf2009-05-14 22:28:01 -07001404 // the events pending events to be dispatched to this service
1405 final SparseArray<AccessibilityEvent> mPendingEvents =
1406 new SparseArray<AccessibilityEvent>();
1407
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001408 /**
1409 * Handler for delayed event dispatch.
1410 */
Svetoslav Ganovec2c1712012-05-22 11:31:57 -07001411 public Handler mHandler = new Handler(mMainHandler.getLooper()) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001412 @Override
1413 public void handleMessage(Message message) {
1414 final int type = message.what;
1415 switch (type) {
1416 case MSG_ON_GESTURE: {
1417 final int gestureId = message.arg1;
1418 notifyGestureInternal(gestureId);
1419 } break;
1420 default: {
1421 final int eventType = type;
1422 notifyAccessibilityEventInternal(eventType);
1423 } break;
1424 }
1425 }
1426 };
1427
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001428 public Service(int userId, ComponentName componentName,
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001429 AccessibilityServiceInfo accessibilityServiceInfo, boolean isAutomation) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001430 mUserId = userId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001431 mResolveInfo = accessibilityServiceInfo.getResolveInfo();
svetoslavganov75986cf2009-05-14 22:28:01 -07001432 mId = sIdCounter++;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001433 mComponentName = componentName;
1434 mAccessibilityServiceInfo = accessibilityServiceInfo;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001435 mIsAutomation = isAutomation;
1436 if (!isAutomation) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001437 mCanRetrieveScreenContent = accessibilityServiceInfo.getCanRetrieveWindowContent();
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001438 mRequestTouchExplorationMode =
Svetoslav Ganove4abc512012-05-09 11:02:38 -07001439 (accessibilityServiceInfo.flags
1440 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001441 mIntent = new Intent().setComponent(mComponentName);
1442 mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1443 com.android.internal.R.string.accessibility_binding_label);
1444 mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1445 mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0));
1446 } else {
1447 mCanRetrieveScreenContent = true;
1448 }
1449 setDynamicallyConfigurableProperties(accessibilityServiceInfo);
svetoslavganov75986cf2009-05-14 22:28:01 -07001450 }
1451
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001452 public void setDynamicallyConfigurableProperties(AccessibilityServiceInfo info) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001453 mEventTypes = info.eventTypes;
1454 mFeedbackType = info.feedbackType;
1455 String[] packageNames = info.packageNames;
1456 if (packageNames != null) {
1457 mPackageNames.addAll(Arrays.asList(packageNames));
1458 }
1459 mNotificationTimeout = info.notificationTimeout;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001460 mIsDefault = (info.flags & DEFAULT) != 0;
1461
Guang Zhudf549f82012-05-08 23:09:24 -07001462 if (mIsAutomation || info.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion
Svetoslav Ganov5a48f972012-05-13 13:33:53 -07001463 >= Build.VERSION_CODES.JELLY_BEAN) {
Guang Zhudf549f82012-05-08 23:09:24 -07001464 mIncludeNotImportantViews =
Svetoslav Ganove4abc512012-05-09 11:02:38 -07001465 (info.flags & FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001466 }
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001467
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001468 mRequestTouchExplorationMode = (info.flags
1469 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
1470
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001471 // If this service is up and running we may have to enable touch
1472 // exploration, otherwise this will happen when the service connects.
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001473 synchronized (mLock) {
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001474 if (canReceiveEvents()) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001475 if (mRequestTouchExplorationMode) {
1476 tryEnableTouchExplorationLocked(this);
1477 } else {
1478 tryDisableTouchExplorationLocked(this);
1479 }
1480 }
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001481 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001482 }
1483
svetoslavganov75986cf2009-05-14 22:28:01 -07001484 /**
1485 * Binds to the accessibility service.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001486 *
1487 * @return True if binding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001488 */
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001489 public boolean bind() {
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001490 if (!mIsAutomation && mService == null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001491 return mContext.bindService(mIntent, this, Context.BIND_AUTO_CREATE, mUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001492 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001493 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001494 }
1495
1496 /**
1497 * Unbinds form the accessibility service and removes it from the data
1498 * structures for service management.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001499 *
1500 * @return True if unbinding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001501 */
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001502 public boolean unbind() {
svetoslavganov75986cf2009-05-14 22:28:01 -07001503 if (mService != null) {
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001504 synchronized (mLock) {
1505 tryRemoveServiceLocked(this);
1506 }
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001507 if (!mIsAutomation) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001508 mContext.unbindService(this);
1509 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001510 return true;
svetoslavganov75986cf2009-05-14 22:28:01 -07001511 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001512 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001513 }
1514
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001515 public boolean canReceiveEvents() {
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001516 return (mEventTypes != 0 && mFeedbackType != 0 && mService != null);
svetoslavganov75986cf2009-05-14 22:28:01 -07001517 }
1518
Svetoslav Ganov42138042012-03-20 11:51:39 -07001519 @Override
1520 public AccessibilityServiceInfo getServiceInfo() {
1521 synchronized (mLock) {
1522 return mAccessibilityServiceInfo;
1523 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001524 }
1525
Svetoslav Ganov42138042012-03-20 11:51:39 -07001526 @Override
1527 public void setServiceInfo(AccessibilityServiceInfo info) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001528 final long identity = Binder.clearCallingIdentity();
1529 try {
1530 synchronized (mLock) {
1531 // If the XML manifest had data to configure the service its info
1532 // should be already set. In such a case update only the dynamically
1533 // configurable properties.
1534 AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo;
1535 if (oldInfo != null) {
1536 oldInfo.updateDynamicallyConfigurableProperties(info);
1537 setDynamicallyConfigurableProperties(oldInfo);
1538 } else {
1539 setDynamicallyConfigurableProperties(info);
1540 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001541 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001542 } finally {
1543 Binder.restoreCallingIdentity(identity);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001544 }
1545 }
1546
1547 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001548 public void onServiceConnected(ComponentName componentName, IBinder service) {
1549 mService = service;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001550 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
svetoslavganov75986cf2009-05-14 22:28:01 -07001551 try {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001552 mServiceInterface.setConnection(this, mId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001553 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001554 tryAddServiceLocked(this, mUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001555 }
1556 } catch (RemoteException re) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001557 Slog.w(LOG_TAG, "Error while setting Controller for service: " + service, re);
svetoslavganov75986cf2009-05-14 22:28:01 -07001558 }
1559 }
1560
Svetoslav Ganov42138042012-03-20 11:51:39 -07001561 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001562 public float findAccessibilityNodeInfoByViewId(int accessibilityWindowId,
1563 long accessibilityNodeId, int viewId, int interactionId,
1564 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001565 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001566 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001567 IAccessibilityInteractionConnection connection = null;
1568 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001569 final int resolvedUserId = mSecurityPolicy
1570 .resolveCallingUserIdEnforcingPermissionsLocked(
1571 UserHandle.getCallingUserId());
1572 if (resolvedUserId != mCurrentUserId) {
1573 return -1;
1574 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001575 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001576 final boolean permissionGranted = mSecurityPolicy.canRetrieveWindowContent(this);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001577 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001578 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001579 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001580 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001581 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001582 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001583 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001584 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001585 }
1586 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001587 final int flags = (mIncludeNotImportantViews) ?
1588 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001589 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001590 final long identityToken = Binder.clearCallingIdentity();
1591 try {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001592 connection.findAccessibilityNodeInfoByViewId(accessibilityNodeId, viewId,
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001593 interactionId, callback, flags, interrogatingPid, interrogatingTid);
1594 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001595 } catch (RemoteException re) {
1596 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001597 Slog.e(LOG_TAG, "Error findAccessibilityNodeInfoByViewId().");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001598 }
1599 } finally {
1600 Binder.restoreCallingIdentity(identityToken);
1601 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001602 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001603 }
1604
Svetoslav Ganov42138042012-03-20 11:51:39 -07001605 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001606 public float findAccessibilityNodeInfosByText(int accessibilityWindowId,
1607 long accessibilityNodeId, String text, int interactionId,
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001608 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1609 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001610 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001611 IAccessibilityInteractionConnection connection = null;
1612 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001613 final int resolvedUserId = mSecurityPolicy
1614 .resolveCallingUserIdEnforcingPermissionsLocked(
1615 UserHandle.getCallingUserId());
1616 if (resolvedUserId != mCurrentUserId) {
1617 return -1;
1618 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001619 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001620 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07001621 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001622 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001623 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001624 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001625 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001626 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001627 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001628 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001629 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001630 }
1631 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001632 final int flags = (mIncludeNotImportantViews) ?
1633 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001634 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001635 final long identityToken = Binder.clearCallingIdentity();
1636 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001637 connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text,
1638 interactionId, callback, flags, interrogatingPid,
Svetoslav Ganov86783472012-06-06 21:12:20 -07001639 interrogatingTid);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001640 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001641 } catch (RemoteException re) {
1642 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001643 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001644 }
1645 } finally {
1646 Binder.restoreCallingIdentity(identityToken);
1647 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001648 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001649 }
1650
Svetoslav Ganov42138042012-03-20 11:51:39 -07001651 @Override
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001652 public float findAccessibilityNodeInfoByAccessibilityId(int accessibilityWindowId,
Svetoslav Ganov02107852011-10-03 17:06:56 -07001653 long accessibilityNodeId, int interactionId,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001654 IAccessibilityInteractionConnectionCallback callback, int flags,
1655 long interrogatingTid) throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001656 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001657 IAccessibilityInteractionConnection connection = null;
1658 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001659 final int resolvedUserId = mSecurityPolicy
1660 .resolveCallingUserIdEnforcingPermissionsLocked(
1661 UserHandle.getCallingUserId());
1662 if (resolvedUserId != mCurrentUserId) {
1663 return -1;
1664 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001665 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001666 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001667 final boolean permissionGranted =
1668 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
1669 if (!permissionGranted) {
1670 return 0;
1671 } else {
1672 connection = getConnectionLocked(resolvedWindowId);
1673 if (connection == null) {
1674 return 0;
1675 }
1676 }
1677 }
1678 final int allFlags = flags | ((mIncludeNotImportantViews) ?
1679 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0);
1680 final int interrogatingPid = Binder.getCallingPid();
1681 final long identityToken = Binder.clearCallingIdentity();
1682 try {
1683 connection.findAccessibilityNodeInfoByAccessibilityId(accessibilityNodeId,
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001684 interactionId, callback, allFlags, interrogatingPid, interrogatingTid);
1685 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001686 } catch (RemoteException re) {
1687 if (DEBUG) {
1688 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfoByAccessibilityId()");
1689 }
1690 } finally {
1691 Binder.restoreCallingIdentity(identityToken);
1692 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001693 return 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001694 }
1695
1696 @Override
1697 public float findFocus(int accessibilityWindowId, long accessibilityNodeId,
1698 int focusType, int interactionId,
1699 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001700 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001701 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001702 IAccessibilityInteractionConnection connection = null;
1703 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001704 final int resolvedUserId = mSecurityPolicy
1705 .resolveCallingUserIdEnforcingPermissionsLocked(
1706 UserHandle.getCallingUserId());
1707 if (resolvedUserId != mCurrentUserId) {
1708 return -1;
1709 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001710 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001711 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001712 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001713 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001714 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001715 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001716 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001717 connection = getConnectionLocked(resolvedWindowId);
1718 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001719 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001720 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001721 }
1722 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001723 final int flags = (mIncludeNotImportantViews) ?
1724 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001725 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001726 final long identityToken = Binder.clearCallingIdentity();
1727 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001728 connection.findFocus(accessibilityNodeId, focusType, interactionId, callback,
1729 flags, interrogatingPid, interrogatingTid);
1730 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001731 } catch (RemoteException re) {
1732 if (DEBUG) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07001733 Slog.e(LOG_TAG, "Error calling findAccessibilityFocus()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001734 }
1735 } finally {
1736 Binder.restoreCallingIdentity(identityToken);
1737 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001738 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001739 }
1740
Svetoslav Ganov42138042012-03-20 11:51:39 -07001741 @Override
1742 public float focusSearch(int accessibilityWindowId, long accessibilityNodeId,
1743 int direction, int interactionId,
1744 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1745 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001746 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001747 IAccessibilityInteractionConnection connection = null;
1748 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001749 final int resolvedUserId = mSecurityPolicy
1750 .resolveCallingUserIdEnforcingPermissionsLocked(
1751 UserHandle.getCallingUserId());
1752 if (resolvedUserId != mCurrentUserId) {
1753 return -1;
1754 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001755 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001756 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001757 final boolean permissionGranted =
1758 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
1759 if (!permissionGranted) {
1760 return 0;
1761 } else {
1762 connection = getConnectionLocked(resolvedWindowId);
1763 if (connection == null) {
1764 return 0;
1765 }
1766 }
1767 }
1768 final int flags = (mIncludeNotImportantViews) ?
1769 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
1770 final int interrogatingPid = Binder.getCallingPid();
1771 final long identityToken = Binder.clearCallingIdentity();
1772 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001773 connection.focusSearch(accessibilityNodeId, direction, interactionId, callback,
1774 flags, interrogatingPid, interrogatingTid);
1775 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001776 } catch (RemoteException re) {
1777 if (DEBUG) {
1778 Slog.e(LOG_TAG, "Error calling accessibilityFocusSearch()");
1779 }
1780 } finally {
1781 Binder.restoreCallingIdentity(identityToken);
1782 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001783 return 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001784 }
1785
1786 @Override
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001787 public boolean performAccessibilityAction(int accessibilityWindowId,
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07001788 long accessibilityNodeId, int action, Bundle arguments, int interactionId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001789 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1790 throws RemoteException {
1791 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001792 IAccessibilityInteractionConnection connection = null;
1793 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001794 final int resolvedUserId = mSecurityPolicy
1795 .resolveCallingUserIdEnforcingPermissionsLocked(
1796 UserHandle.getCallingUserId());
1797 if (resolvedUserId != mCurrentUserId) {
1798 return false;
1799 }
1800 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
1801 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001802 final boolean permissionGranted = mSecurityPolicy.canPerformActionLocked(this,
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07001803 resolvedWindowId, action, arguments);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001804 if (!permissionGranted) {
1805 return false;
1806 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001807 connection = getConnectionLocked(resolvedWindowId);
1808 if (connection == null) {
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001809 return false;
1810 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001811 }
1812 }
Svetoslav Ganov9bf21873c2012-05-22 17:43:23 -07001813 final int flags = (mIncludeNotImportantViews) ?
1814 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
1815 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001816 final long identityToken = Binder.clearCallingIdentity();
1817 try {
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07001818 connection.performAccessibilityAction(accessibilityNodeId, action, arguments,
1819 interactionId, callback, flags, interrogatingPid, interrogatingTid);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001820 } catch (RemoteException re) {
1821 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001822 Slog.e(LOG_TAG, "Error calling performAccessibilityAction()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001823 }
1824 } finally {
1825 Binder.restoreCallingIdentity(identityToken);
1826 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001827 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001828 }
1829
Svetoslav Ganove20a1772012-09-25 16:07:46 -07001830 public boolean performGlobalAction(int action) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001831 synchronized (mLock) {
1832 final int resolvedUserId = mSecurityPolicy
1833 .resolveCallingUserIdEnforcingPermissionsLocked(
1834 UserHandle.getCallingUserId());
1835 if (resolvedUserId != mCurrentUserId) {
1836 return false;
1837 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07001838 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001839 final long identity = Binder.clearCallingIdentity();
1840 try {
1841 switch (action) {
1842 case AccessibilityService.GLOBAL_ACTION_BACK: {
1843 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_BACK);
1844 } return true;
1845 case AccessibilityService.GLOBAL_ACTION_HOME: {
1846 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HOME);
1847 } return true;
1848 case AccessibilityService.GLOBAL_ACTION_RECENTS: {
1849 openRecents();
1850 } return true;
1851 case AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS: {
Svetoslav Ganove20a1772012-09-25 16:07:46 -07001852 expandNotifications();
1853 } return true;
1854 case AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS: {
1855 expandQuickSettings();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001856 } return true;
1857 }
1858 return false;
1859 } finally {
1860 Binder.restoreCallingIdentity(identity);
1861 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07001862 }
1863
svetoslavganov75986cf2009-05-14 22:28:01 -07001864 public void onServiceDisconnected(ComponentName componentName) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001865 /* do nothing - #binderDied takes care */
1866 }
1867
1868 public void linkToOwnDeath() throws RemoteException {
1869 mService.linkToDeath(this, 0);
1870 }
1871
1872 public void unlinkToOwnDeath() {
1873 mService.unlinkToDeath(this, 0);
1874 }
1875
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001876 public void dispose() {
1877 try {
1878 // Clear the proxy in the other process so this
1879 // IAccessibilityServiceConnection can be garbage collected.
1880 mServiceInterface.setConnection(null, mId);
1881 } catch (RemoteException re) {
1882 /* ignore */
1883 }
1884 mService = null;
1885 mServiceInterface = null;
1886 }
1887
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001888 public void binderDied() {
svetoslavganov75986cf2009-05-14 22:28:01 -07001889 synchronized (mLock) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001890 // The death recipient is unregistered in tryRemoveServiceLocked
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001891 tryRemoveServiceLocked(this);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001892 // We no longer have an automation service, so restore
1893 // the state based on values in the settings database.
1894 if (mIsAutomation) {
Svetoslav Ganov9b666d02012-02-10 14:55:41 -08001895 mUiAutomationService = null;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001896 recreateInternalStateLocked(getUserStateLocked(mUserId));
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001897 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001898 }
1899 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001900
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001901 /**
1902 * Performs a notification for an {@link AccessibilityEvent}.
1903 *
1904 * @param event The event.
1905 */
1906 public void notifyAccessibilityEvent(AccessibilityEvent event) {
1907 synchronized (mLock) {
1908 final int eventType = event.getEventType();
1909 // Make a copy since during dispatch it is possible the event to
1910 // be modified to remove its source if the receiving service does
1911 // not have permission to access the window content.
1912 AccessibilityEvent newEvent = AccessibilityEvent.obtain(event);
1913 AccessibilityEvent oldEvent = mPendingEvents.get(eventType);
1914 mPendingEvents.put(eventType, newEvent);
1915
1916 final int what = eventType;
1917 if (oldEvent != null) {
1918 mHandler.removeMessages(what);
1919 oldEvent.recycle();
1920 }
1921
1922 Message message = mHandler.obtainMessage(what);
1923 mHandler.sendMessageDelayed(message, mNotificationTimeout);
1924 }
1925 }
1926
1927 /**
1928 * Notifies an accessibility service client for a scheduled event given the event type.
1929 *
1930 * @param eventType The type of the event to dispatch.
1931 */
1932 private void notifyAccessibilityEventInternal(int eventType) {
1933 IAccessibilityServiceClient listener;
1934 AccessibilityEvent event;
1935
1936 synchronized (mLock) {
1937 listener = mServiceInterface;
1938
1939 // If the service died/was disabled while the message for dispatching
1940 // the accessibility event was propagating the listener may be null.
1941 if (listener == null) {
1942 return;
1943 }
1944
1945 event = mPendingEvents.get(eventType);
1946
1947 // Check for null here because there is a concurrent scenario in which this
1948 // happens: 1) A binder thread calls notifyAccessibilityServiceDelayedLocked
1949 // which posts a message for dispatching an event. 2) The message is pulled
1950 // from the queue by the handler on the service thread and the latter is
1951 // just about to acquire the lock and call this method. 3) Now another binder
1952 // thread acquires the lock calling notifyAccessibilityServiceDelayedLocked
1953 // so the service thread waits for the lock; 4) The binder thread replaces
1954 // the event with a more recent one (assume the same event type) and posts a
1955 // dispatch request releasing the lock. 5) Now the main thread is unblocked and
1956 // dispatches the event which is removed from the pending ones. 6) And ... now
1957 // the service thread handles the last message posted by the last binder call
1958 // but the event is already dispatched and hence looking it up in the pending
1959 // ones yields null. This check is much simpler that keeping count for each
1960 // event type of each service to catch such a scenario since only one message
1961 // is processed at a time.
1962 if (event == null) {
1963 return;
1964 }
1965
1966 mPendingEvents.remove(eventType);
1967 if (mSecurityPolicy.canRetrieveWindowContent(this)) {
1968 event.setConnectionId(mId);
1969 } else {
1970 event.setSource(null);
1971 }
1972 event.setSealed(true);
1973 }
1974
1975 try {
1976 listener.onAccessibilityEvent(event);
1977 if (DEBUG) {
1978 Slog.i(LOG_TAG, "Event " + event + " sent to " + listener);
1979 }
1980 } catch (RemoteException re) {
1981 Slog.e(LOG_TAG, "Error during sending " + event + " to " + listener, re);
1982 } finally {
1983 event.recycle();
1984 }
1985 }
1986
1987 public void notifyGesture(int gestureId) {
1988 mHandler.obtainMessage(MSG_ON_GESTURE, gestureId, 0).sendToTarget();
1989 }
1990
1991 private void notifyGestureInternal(int gestureId) {
1992 IAccessibilityServiceClient listener = mServiceInterface;
1993 if (listener != null) {
1994 try {
1995 listener.onGesture(gestureId);
1996 } catch (RemoteException re) {
1997 Slog.e(LOG_TAG, "Error during sending gesture " + gestureId
1998 + " to " + mService, re);
1999 }
2000 }
2001 }
2002
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07002003 private void sendDownAndUpKeyEvents(int keyCode) {
2004 final long token = Binder.clearCallingIdentity();
2005
2006 // Inject down.
2007 final long downTime = SystemClock.uptimeMillis();
2008 KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
2009 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2010 InputDevice.SOURCE_KEYBOARD, null);
2011 InputManager.getInstance().injectInputEvent(down,
2012 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2013 down.recycle();
2014
2015 // Inject up.
2016 final long upTime = SystemClock.uptimeMillis();
2017 KeyEvent up = KeyEvent.obtain(downTime, upTime, KeyEvent.ACTION_UP, keyCode, 0, 0,
2018 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2019 InputDevice.SOURCE_KEYBOARD, null);
2020 InputManager.getInstance().injectInputEvent(up,
2021 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2022 up.recycle();
2023
2024 Binder.restoreCallingIdentity(token);
2025 }
2026
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002027 private void expandNotifications() {
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002028 final long token = Binder.clearCallingIdentity();
2029
2030 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2031 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002032 statusBarManager.expandNotificationsPanel();
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002033
2034 Binder.restoreCallingIdentity(token);
2035 }
2036
2037 private void expandQuickSettings() {
2038 final long token = Binder.clearCallingIdentity();
2039
2040 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2041 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002042 statusBarManager.expandSettingsPanel();
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002043
2044 Binder.restoreCallingIdentity(token);
2045 }
2046
Svetoslav Ganovc682fc92012-06-04 14:02:09 -07002047 private void openRecents() {
2048 final long token = Binder.clearCallingIdentity();
2049
2050 IStatusBarService statusBarService = IStatusBarService.Stub.asInterface(
2051 ServiceManager.getService("statusbar"));
2052 try {
2053 statusBarService.toggleRecentApps();
2054 } catch (RemoteException e) {
2055 Slog.e(LOG_TAG, "Error toggling recent apps.");
2056 }
2057
2058 Binder.restoreCallingIdentity(token);
2059 }
2060
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002061 private IAccessibilityInteractionConnection getConnectionLocked(int windowId) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002062 if (DEBUG) {
2063 Slog.i(LOG_TAG, "Trying to get interaction connection to windowId: " + windowId);
2064 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002065 AccessibilityConnectionWrapper wrapper = mGlobalInteractionConnections.get(windowId);
2066 if (wrapper == null) {
2067 wrapper = getCurrentUserStateLocked().mInteractionConnections.get(windowId);
2068 }
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002069 if (wrapper != null && wrapper.mConnection != null) {
2070 return wrapper.mConnection;
2071 }
2072 if (DEBUG) {
2073 Slog.e(LOG_TAG, "No interaction connection to window: " + windowId);
2074 }
2075 return null;
2076 }
2077
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002078 private int resolveAccessibilityWindowIdLocked(int accessibilityWindowId) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08002079 if (accessibilityWindowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002080 return mSecurityPolicy.mActiveWindowId;
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002081 }
2082 return accessibilityWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002083 }
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002084
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07002085 private float getCompatibilityScale(int windowId) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002086 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002087 IBinder windowToken = mGlobalWindowTokens.get(windowId);
2088 if (windowToken != null) {
2089 return mWindowManagerService.getWindowCompatibilityScale(windowToken);
2090 }
2091 windowToken = getCurrentUserStateLocked().mWindowTokens.get(windowId);
2092 if (windowToken != null) {
2093 return mWindowManagerService.getWindowCompatibilityScale(windowToken);
2094 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002095 } catch (RemoteException re) {
2096 /* ignore */
2097 }
2098 return 1.0f;
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002099 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002100 }
2101
2102 final class SecurityPolicy {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002103 private static final int VALID_ACTIONS =
2104 AccessibilityNodeInfo.ACTION_CLICK
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002105 | AccessibilityNodeInfo.ACTION_LONG_CLICK
Svetoslav Ganov42138042012-03-20 11:51:39 -07002106 | AccessibilityNodeInfo.ACTION_FOCUS
2107 | AccessibilityNodeInfo.ACTION_CLEAR_FOCUS
2108 | AccessibilityNodeInfo.ACTION_SELECT
2109 | AccessibilityNodeInfo.ACTION_CLEAR_SELECTION
2110 | AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07002111 | AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07002112 | AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
2113 | AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002114 | AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT
Svetoslav Ganova1dc7612012-05-10 04:14:53 -07002115 | AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT
2116 | AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
2117 | AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002118
2119 private static final int RETRIEVAL_ALLOWING_EVENT_TYPES =
Svetoslav Ganov42138042012-03-20 11:51:39 -07002120 AccessibilityEvent.TYPE_VIEW_CLICKED
2121 | AccessibilityEvent.TYPE_VIEW_FOCUSED
2122 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
2123 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
2124 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
2125 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
2126 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
2127 | AccessibilityEvent.TYPE_VIEW_SELECTED
Svetoslav Ganova0156172011-06-26 17:55:44 -07002128 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
2129 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07002130 | AccessibilityEvent.TYPE_VIEW_SCROLLED
2131 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
2132 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002133
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002134 private int mActiveWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002135
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002136 private boolean canDispatchAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov58fd9f82012-10-03 18:10:27 -07002137 final int eventType = event.getEventType();
2138 switch (eventType) {
2139 // All events that are for changes in a global window
2140 // state should *always* be dispatched.
2141 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
2142 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
2143 // All events generated by the user touching the
2144 // screen should *always* be dispatched.
2145 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
2146 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
2147 case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
2148 case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
2149 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
2150 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
2151 // These will change the active window, so dispatch.
2152 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
2153 case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: {
2154 return true;
2155 }
2156 // All events for changes in window content should be
2157 // dispatched *only* if this window is the active one.
2158 default:
2159 return event.getWindowId() == mActiveWindowId;
2160 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002161 }
2162
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002163 public void updateEventSourceLocked(AccessibilityEvent event) {
2164 if ((event.getEventType() & RETRIEVAL_ALLOWING_EVENT_TYPES) == 0) {
2165 event.setSource(null);
2166 }
2167 }
2168
2169 public void updateActiveWindow(int windowId, int eventType) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002170 // The active window is either the window that has input focus or
2171 // the window that the user is currently touching. If the user is
2172 // touching a window that does not have input focus as soon as the
2173 // the user stops touching that window the focused window becomes
2174 // the active one.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002175 switch (eventType) {
2176 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: {
2177 if (getFocusedWindowId() == windowId) {
2178 mActiveWindowId = windowId;
2179 }
2180 } break;
2181 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
2182 case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: {
2183 mActiveWindowId = windowId;
2184 } break;
Svetoslav Ganov76c0dd42012-09-24 19:16:16 -07002185 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END: {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002186 mActiveWindowId = getFocusedWindowId();
2187 } break;
Svetoslav Ganov4e2a7622011-07-26 20:08:46 -07002188 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002189 }
2190
2191 public int getRetrievalAllowingWindowLocked() {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002192 return mActiveWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002193 }
2194
2195 public boolean canGetAccessibilityNodeInfoLocked(Service service, int windowId) {
2196 return canRetrieveWindowContent(service) && isRetrievalAllowingWindow(windowId);
2197 }
2198
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002199 public boolean canPerformActionLocked(Service service, int windowId, int action,
2200 Bundle arguments) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002201 return canRetrieveWindowContent(service)
2202 && isRetrievalAllowingWindow(windowId)
Svetoslav Ganovafe8cf22012-04-28 13:26:57 -07002203 && isActionPermitted(action);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002204 }
2205
2206 public boolean canRetrieveWindowContent(Service service) {
2207 return service.mCanRetrieveScreenContent;
2208 }
2209
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07002210 public void enforceCanRetrieveWindowContent(Service service) throws RemoteException {
2211 // This happens due to incorrect registration so make it apparent.
2212 if (!canRetrieveWindowContent(service)) {
2213 Slog.e(LOG_TAG, "Accessibility serivce " + service.mComponentName + " does not " +
2214 "declare android:canRetrieveWindowContent.");
2215 throw new RemoteException();
2216 }
2217 }
2218
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002219 public int resolveCallingUserIdEnforcingPermissionsLocked(int userId) {
2220 final int callingUid = Binder.getCallingUid();
2221 if (callingUid == Process.SYSTEM_UID
2222 || callingUid == Process.SHELL_UID) {
2223 return mCurrentUserId;
2224 }
2225 final int callingUserId = UserHandle.getUserId(callingUid);
2226 if (callingUserId == userId) {
2227 return userId;
2228 }
2229 if (!hasPermission(Manifest.permission.INTERACT_ACROSS_USERS)
2230 && !hasPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL)) {
2231 throw new SecurityException("Call from user " + callingUserId + " as user "
2232 + userId + " without permission INTERACT_ACROSS_USERS or "
2233 + "INTERACT_ACROSS_USERS_FULL not allowed.");
2234 }
2235 if (userId == UserHandle.USER_CURRENT
2236 || userId == UserHandle.USER_CURRENT_OR_SELF) {
2237 return mCurrentUserId;
2238 }
2239 throw new IllegalArgumentException("Calling user can be changed to only "
2240 + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF.");
2241 }
2242
2243 public boolean isCallerInteractingAcrossUsers(int userId) {
2244 final int callingUid = Binder.getCallingUid();
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002245 return (Binder.getCallingPid() == android.os.Process.myPid()
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002246 || callingUid == Process.SHELL_UID
2247 || userId == UserHandle.USER_CURRENT
2248 || userId == UserHandle.USER_CURRENT_OR_SELF);
2249 }
2250
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002251 private boolean isRetrievalAllowingWindow(int windowId) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002252 return (mActiveWindowId == windowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002253 }
2254
2255 private boolean isActionPermitted(int action) {
2256 return (VALID_ACTIONS & action) != 0;
2257 }
2258
2259 private void enforceCallingPermission(String permission, String function) {
2260 if (OWN_PROCESS_ID == Binder.getCallingPid()) {
2261 return;
2262 }
Svetoslav Ganov9371a0a2012-09-21 18:20:37 -07002263 if (!hasPermission(permission)) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002264 throw new SecurityException("You do not have " + permission
2265 + " required to call " + function);
2266 }
2267 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002268
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002269 private boolean hasPermission(String permission) {
2270 return mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED;
2271 }
2272
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002273 private int getFocusedWindowId() {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002274 try {
2275 // We call this only on window focus change or after touch
2276 // exploration gesture end and the shown windows are not that
2277 // many, so the linear look up is just fine.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002278 IBinder token = mWindowManagerService.getFocusedWindowToken();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002279 if (token != null) {
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002280 synchronized (mLock) {
2281 int windowId = getFocusedWindowIdLocked(token, mGlobalWindowTokens);
2282 if (windowId < 0) {
2283 windowId = getFocusedWindowIdLocked(token,
2284 getCurrentUserStateLocked().mWindowTokens);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002285 }
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002286 return windowId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002287 }
2288 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002289 } catch (RemoteException re) {
2290 /* ignore */
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002291 }
2292 return -1;
2293 }
2294
2295 private int getFocusedWindowIdLocked(IBinder token, SparseArray<IBinder> windows) {
2296 final int windowCount = windows.size();
2297 for (int i = 0; i < windowCount; i++) {
2298 if (windows.valueAt(i) == token) {
2299 return windows.keyAt(i);
2300 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002301 }
2302 return -1;
2303 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002304 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002305
2306 private class UserState {
2307 public final int mUserId;
2308
2309 public final CopyOnWriteArrayList<Service> mServices = new CopyOnWriteArrayList<Service>();
2310
2311 public final RemoteCallbackList<IAccessibilityManagerClient> mClients =
2312 new RemoteCallbackList<IAccessibilityManagerClient>();
2313
2314 public final Map<ComponentName, Service> mComponentNameToServiceMap =
2315 new HashMap<ComponentName, Service>();
2316
2317 public final List<AccessibilityServiceInfo> mInstalledServices =
2318 new ArrayList<AccessibilityServiceInfo>();
2319
2320 public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>();
2321
2322 public final Set<ComponentName> mTouchExplorationGrantedServices =
2323 new HashSet<ComponentName>();
2324
2325 public final SparseArray<AccessibilityConnectionWrapper>
2326 mInteractionConnections =
2327 new SparseArray<AccessibilityConnectionWrapper>();
2328
2329 public final SparseArray<IBinder> mWindowTokens = new SparseArray<IBinder>();
2330
2331 public int mHandledFeedbackTypes = 0;
2332
2333 public boolean mIsAccessibilityEnabled;
2334 public boolean mIsTouchExplorationEnabled;
2335 public boolean mIsDisplayMagnificationEnabled;
2336
2337 public UserState(int userId) {
2338 mUserId = userId;
2339 }
2340 }
2341
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07002342 private class TempUserStateChangeMemento {
2343 public int mUserId = UserHandle.USER_NULL;
2344 public boolean mIsAccessibilityEnabled;
2345 public boolean mIsTouchExplorationEnabled;
2346 public boolean mIsDisplayMagnificationEnabled;
2347 public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>();
2348 public final Set<ComponentName> mTouchExplorationGrantedServices =
2349 new HashSet<ComponentName>();
2350
2351 public void initialize(int userId, UserState userState) {
2352 mUserId = userId;
2353 mIsAccessibilityEnabled = userState.mIsAccessibilityEnabled;
2354 mIsTouchExplorationEnabled = userState.mIsTouchExplorationEnabled;
2355 mIsDisplayMagnificationEnabled = userState.mIsDisplayMagnificationEnabled;
2356 mEnabledServices.clear();
2357 mEnabledServices.addAll(userState.mEnabledServices);
2358 mTouchExplorationGrantedServices.clear();
2359 mTouchExplorationGrantedServices.addAll(userState.mTouchExplorationGrantedServices);
2360 }
2361
2362 public void applyTo(UserState userState) {
2363 userState.mIsAccessibilityEnabled = mIsAccessibilityEnabled;
2364 userState.mIsTouchExplorationEnabled = mIsTouchExplorationEnabled;
2365 userState.mIsDisplayMagnificationEnabled = mIsDisplayMagnificationEnabled;
2366 userState.mEnabledServices.clear();
2367 userState.mEnabledServices.addAll(mEnabledServices);
2368 userState.mTouchExplorationGrantedServices.clear();
2369 userState.mTouchExplorationGrantedServices.addAll(mTouchExplorationGrantedServices);
2370 }
2371
2372 public void clear() {
2373 mUserId = UserHandle.USER_NULL;
2374 mIsAccessibilityEnabled = false;
2375 mIsTouchExplorationEnabled = false;
2376 mIsDisplayMagnificationEnabled = false;
2377 mEnabledServices.clear();
2378 mTouchExplorationGrantedServices.clear();
2379 }
2380 }
2381
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002382 private final class AccessibilityContentObserver extends ContentObserver {
2383
2384 private final Uri mAccessibilityEnabledUri = Settings.Secure.getUriFor(
2385 Settings.Secure.ACCESSIBILITY_ENABLED);
2386
2387 private final Uri mTouchExplorationEnabledUri = Settings.Secure.getUriFor(
2388 Settings.Secure.TOUCH_EXPLORATION_ENABLED);
2389
2390 private final Uri mDisplayMagnificationEnabledUri = Settings.Secure.getUriFor(
2391 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED);
2392
2393 private final Uri mEnabledAccessibilityServicesUri = Settings.Secure.getUriFor(
2394 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
2395
2396 private final Uri mTouchExplorationGrantedAccessibilityServicesUri = Settings.Secure
2397 .getUriFor(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES);
2398
2399 public AccessibilityContentObserver(Handler handler) {
2400 super(handler);
2401 }
2402
2403 public void register(ContentResolver contentResolver) {
2404 contentResolver.registerContentObserver(mAccessibilityEnabledUri,
2405 false, this, UserHandle.USER_ALL);
2406 contentResolver.registerContentObserver(mTouchExplorationEnabledUri,
2407 false, this, UserHandle.USER_ALL);
2408 contentResolver.registerContentObserver(mDisplayMagnificationEnabledUri,
2409 false, this, UserHandle.USER_ALL);
2410 contentResolver.registerContentObserver(mEnabledAccessibilityServicesUri,
2411 false, this, UserHandle.USER_ALL);
2412 contentResolver.registerContentObserver(
2413 mTouchExplorationGrantedAccessibilityServicesUri,
2414 false, this, UserHandle.USER_ALL);
2415 }
2416
2417 @Override
2418 public void onChange(boolean selfChange, Uri uri) {
2419 if (mAccessibilityEnabledUri.equals(uri)) {
2420 synchronized (mLock) {
2421 // We will update when the automation service dies.
2422 if (mUiAutomationService == null) {
2423 UserState userState = getCurrentUserStateLocked();
2424 handleAccessibilityEnabledSettingChangedLocked(userState);
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07002425 performServiceManagementLocked(userState);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002426 updateInputFilterLocked(userState);
2427 scheduleSendStateToClientsLocked(userState);
2428 }
2429 }
2430 } else if (mTouchExplorationEnabledUri.equals(uri)) {
2431 synchronized (mLock) {
2432 // We will update when the automation service dies.
2433 if (mUiAutomationService == null) {
2434 UserState userState = getCurrentUserStateLocked();
2435 handleTouchExplorationEnabledSettingChangedLocked(userState);
2436 updateInputFilterLocked(userState);
2437 scheduleSendStateToClientsLocked(userState);
2438 }
2439 }
2440 } else if (mDisplayMagnificationEnabledUri.equals(uri)) {
2441 synchronized (mLock) {
2442 // We will update when the automation service dies.
2443 if (mUiAutomationService == null) {
2444 UserState userState = getCurrentUserStateLocked();
2445 handleDisplayMagnificationEnabledSettingChangedLocked(userState);
2446 updateInputFilterLocked(userState);
2447 scheduleSendStateToClientsLocked(userState);
2448 }
2449 }
2450 } else if (mEnabledAccessibilityServicesUri.equals(uri)) {
2451 synchronized (mLock) {
2452 // We will update when the automation service dies.
2453 if (mUiAutomationService == null) {
2454 UserState userState = getCurrentUserStateLocked();
2455 populateEnabledAccessibilityServicesLocked(userState);
2456 manageServicesLocked(userState);
2457 }
2458 }
2459 } else if (mTouchExplorationGrantedAccessibilityServicesUri.equals(uri)) {
2460 synchronized (mLock) {
2461 // We will update when the automation service dies.
2462 if (mUiAutomationService == null) {
2463 UserState userState = getCurrentUserStateLocked();
2464 populateTouchExplorationGrantedAccessibilityServicesLocked(userState);
2465 handleTouchExplorationGrantedAccessibilityServicesChangedLocked(userState);
2466 }
2467 }
2468 }
2469 }
2470 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002471}