blob: 6782f5ee18c17088ef3f22eec27869d1edd8542e [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
Svetoslav Ganovf772cba2012-10-05 18:49:17 -0700646 void onTouchInteractionEnd() {
647 mSecurityPolicy.onTouchInteractionEnd();
648 }
649
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700650 private void switchUser(int userId) {
651 synchronized (mLock) {
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700652 // The user switched so we do not need to restore the current user
653 // state since we will fully rebuild it when he becomes current again.
654 mTempStateChangeForCurrentUserMemento.clear();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700655
656 // Disconnect from services for the old user.
657 UserState oldUserState = getUserStateLocked(mCurrentUserId);
658 unbindAllServicesLocked(oldUserState);
659
660 // Disable the local managers for the old user.
661 if (oldUserState.mClients.getRegisteredCallbackCount() > 0) {
662 mMainHandler.obtainMessage(MainHandler.MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER,
663 oldUserState.mUserId, 0).sendToTarget();
664 }
665
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700666 // Announce user changes only if more that one exist.
667 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
668 final boolean announceNewUser = userManager.getUsers().size() > 1;
669
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700670 // The user changed.
671 mCurrentUserId = userId;
672
673 // Recreate the internal state for the new user.
674 mMainHandler.obtainMessage(MainHandler.MSG_SEND_RECREATE_INTERNAL_STATE,
675 mCurrentUserId, 0).sendToTarget();
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700676
Svetoslav Ganov59f07692012-10-01 19:01:32 -0700677 if (announceNewUser) {
678 // Schedule announcement of the current user if needed.
679 mMainHandler.sendEmptyMessageDelayed(MainHandler.MSG_ANNOUNCE_NEW_USER_IF_NEEDED,
680 WAIT_FOR_USER_STATE_FULLY_INITIALIZED_MILLIS);
681 }
Svetoslav Ganov86783472012-06-06 21:12:20 -0700682 }
683 }
684
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700685 private void removeUser(int userId) {
686 synchronized (mLock) {
687 mUserStates.remove(userId);
688 }
Svetoslav Ganov385d9f22012-06-07 16:35:04 -0700689 }
690
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -0700691 private void restoreStateFromMementoIfNeeded() {
692 synchronized (mLock) {
693 if (mTempStateChangeForCurrentUserMemento.mUserId != UserHandle.USER_NULL) {
694 UserState userState = getCurrentUserStateLocked();
695 // Restore the state from the memento.
696 mTempStateChangeForCurrentUserMemento.applyTo(userState);
697 mTempStateChangeForCurrentUserMemento.clear();
698 // Update the internal state.
699 performServiceManagementLocked(userState);
700 updateInputFilterLocked(userState);
701 scheduleSendStateToClientsLocked(userState);
702 }
703 }
704 }
705
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700706 private Service getQueryBridge() {
707 if (mQueryBridge == null) {
708 AccessibilityServiceInfo info = new AccessibilityServiceInfo();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700709 mQueryBridge = new Service(UserHandle.USER_NULL, null, info, true);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700710 }
711 return mQueryBridge;
712 }
713
Svetoslav Ganov42138042012-03-20 11:51:39 -0700714 private boolean notifyGestureLocked(int gestureId, boolean isDefault) {
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700715 // TODO: Now we are giving the gestures to the last enabled
716 // service that can handle them which is the last one
717 // in our list since we write the last enabled as the
718 // last record in the enabled services setting. Ideally,
719 // the user should make the call which service handles
720 // gestures. However, only one service should handle
Svetoslav Ganove4abc512012-05-09 11:02:38 -0700721 // gestures to avoid user frustration when different
722 // behavior is observed from different combinations of
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700723 // enabled accessibility services.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700724 UserState state = getCurrentUserStateLocked();
725 for (int i = state.mServices.size() - 1; i >= 0; i--) {
726 Service service = state.mServices.get(i);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -0700727 if (service.mRequestTouchExplorationMode && service.mIsDefault == isDefault) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700728 service.notifyGesture(gestureId);
Svetoslav Ganovfefd20e2012-04-19 21:44:35 -0700729 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -0700730 }
731 }
732 return false;
733 }
734
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800735 /**
736 * Removes an AccessibilityInteractionConnection.
737 *
738 * @param windowId The id of the window to which the connection is targeted.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700739 * @param userId The id of the user owning the connection. UserHandle.USER_ALL
740 * if global.
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800741 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700742 private void removeAccessibilityInteractionConnectionLocked(int windowId, int userId) {
743 if (userId == UserHandle.USER_ALL) {
744 mGlobalWindowTokens.remove(windowId);
745 mGlobalInteractionConnections.remove(windowId);
746 } else {
747 UserState userState = getCurrentUserStateLocked();
748 userState.mWindowTokens.remove(windowId);
749 userState.mInteractionConnections.remove(windowId);
750 }
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800751 if (DEBUG) {
752 Slog.i(LOG_TAG, "Removing interaction connection to windowId: " + windowId);
753 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -0700754 }
755
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700756 private void populateInstalledAccessibilityServiceLocked(UserState userState) {
757 userState.mInstalledServices.clear();
svetoslavganov75986cf2009-05-14 22:28:01 -0700758
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700759 List<ResolveInfo> installedServices = mPackageManager.queryIntentServicesAsUser(
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700760 new Intent(AccessibilityService.SERVICE_INTERFACE),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700761 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
762 mCurrentUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -0700763
764 for (int i = 0, count = installedServices.size(); i < count; i++) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700765 ResolveInfo resolveInfo = installedServices.get(i);
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700766 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700767 if (!android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE.equals(
768 serviceInfo.permission)) {
Svetoslav Ganov53e184d2012-05-16 15:57:10 -0700769 Slog.w(LOG_TAG, "Skipping accessibilty service " + new ComponentName(
770 serviceInfo.packageName, serviceInfo.name).flattenToShortString()
771 + ": it does not require the permission "
772 + android.Manifest.permission.BIND_ACCESSIBILITY_SERVICE);
773 continue;
774 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700775 AccessibilityServiceInfo accessibilityServiceInfo;
776 try {
777 accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700778 userState.mInstalledServices.add(accessibilityServiceInfo);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -0700779 } catch (XmlPullParserException xppe) {
780 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", xppe);
781 } catch (IOException ioe) {
782 Slog.e(LOG_TAG, "Error while initializing AccessibilityServiceInfo", ioe);
783 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700784 }
785 }
786
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700787 private void populateEnabledAccessibilityServicesLocked(UserState userState) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700788 populateComponentNamesFromSettingLocked(
789 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700790 userState.mUserId,
791 userState.mEnabledServices);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700792 }
793
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700794 private void populateTouchExplorationGrantedAccessibilityServicesLocked(
795 UserState userState) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700796 populateComponentNamesFromSettingLocked(
797 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700798 userState.mUserId,
799 userState.mTouchExplorationGrantedServices);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700800 }
801
svetoslavganov75986cf2009-05-14 22:28:01 -0700802 /**
803 * Performs {@link AccessibilityService}s delayed notification. The delay is configurable
804 * and denotes the period after the last event before notifying the service.
805 *
806 * @param event The event.
807 * @param isDefault True to notify default listeners, not default services.
808 */
809 private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event,
810 boolean isDefault) {
Charles Chen85b598b2009-07-29 17:23:50 -0700811 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700812 UserState state = getCurrentUserStateLocked();
813 for (int i = 0, count = state.mServices.size(); i < count; i++) {
814 Service service = state.mServices.get(i);
svetoslavganov75986cf2009-05-14 22:28:01 -0700815
Charles Chen85b598b2009-07-29 17:23:50 -0700816 if (service.mIsDefault == isDefault) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700817 if (canDispathEventLocked(service, event, state.mHandledFeedbackTypes)) {
818 state.mHandledFeedbackTypes |= service.mFeedbackType;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -0700819 service.notifyAccessibilityEvent(event);
Charles Chen85b598b2009-07-29 17:23:50 -0700820 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700821 }
822 }
Charles Chen85b598b2009-07-29 17:23:50 -0700823 } catch (IndexOutOfBoundsException oobe) {
824 // An out of bounds exception can happen if services are going away
825 // as the for loop is running. If that happens, just bail because
826 // there are no more services to notify.
827 return;
svetoslavganov75986cf2009-05-14 22:28:01 -0700828 }
829 }
830
831 /**
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700832 * Adds a service for a user.
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700833 *
834 * @param service The service to add.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700835 * @param userId The user id.
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700836 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700837 private void tryAddServiceLocked(Service service, int userId) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700838 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700839 UserState userState = getUserStateLocked(userId);
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -0700840 if (userState.mServices.contains(service)) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700841 return;
842 }
843 service.linkToOwnDeath();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700844 userState.mServices.add(service);
845 userState.mComponentNameToServiceMap.put(service.mComponentName, service);
846 updateInputFilterLocked(userState);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700847 tryEnableTouchExplorationLocked(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700848 } catch (RemoteException e) {
849 /* do nothing */
850 }
851 }
852
853 /**
854 * Removes a service.
svetoslavganov75986cf2009-05-14 22:28:01 -0700855 *
856 * @param service The service.
857 * @return True if the service was removed, false otherwise.
858 */
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700859 private boolean tryRemoveServiceLocked(Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700860 UserState userState = getUserStateLocked(service.mUserId);
861 final boolean removed = userState.mServices.remove(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700862 if (!removed) {
863 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -0700864 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700865 userState.mComponentNameToServiceMap.remove(service.mComponentName);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700866 service.unlinkToOwnDeath();
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -0800867 service.dispose();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700868 updateInputFilterLocked(userState);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700869 tryDisableTouchExplorationLocked(service);
Svetoslav Ganovf9886f32011-05-10 15:28:33 -0700870 return removed;
svetoslavganov75986cf2009-05-14 22:28:01 -0700871 }
872
873 /**
874 * Determines if given event can be dispatched to a service based on the package of the
875 * event source and already notified services for that event type. Specifically, a
876 * service is notified if it is interested in events from the package and no other service
877 * providing the same feedback type has been notified. Exception are services the
878 * provide generic feedback (feedback type left as a safety net for unforeseen feedback
879 * types) which are always notified.
880 *
881 * @param service The potential receiver.
882 * @param event The event.
883 * @param handledFeedbackTypes The feedback types for which services have been notified.
884 * @return True if the listener should be notified, false otherwise.
885 */
886 private boolean canDispathEventLocked(Service service, AccessibilityEvent event,
887 int handledFeedbackTypes) {
888
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -0700889 if (!service.canReceiveEvents()) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700890 return false;
891 }
892
Svetoslav Ganov42138042012-03-20 11:51:39 -0700893 if (!event.isImportantForAccessibility()
894 && !service.mIncludeNotImportantViews) {
895 return false;
896 }
897
svetoslavganov75986cf2009-05-14 22:28:01 -0700898 int eventType = event.getEventType();
899 if ((service.mEventTypes & eventType) != eventType) {
900 return false;
901 }
902
903 Set<String> packageNames = service.mPackageNames;
904 CharSequence packageName = event.getPackageName();
905
906 if (packageNames.isEmpty() || packageNames.contains(packageName)) {
907 int feedbackType = service.mFeedbackType;
908 if ((handledFeedbackTypes & feedbackType) != feedbackType
909 || feedbackType == AccessibilityServiceInfo.FEEDBACK_GENERIC) {
910 return true;
911 }
912 }
913
914 return false;
915 }
916
917 /**
918 * Manages services by starting enabled ones and stopping disabled ones.
919 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700920 private void manageServicesLocked(UserState userState) {
921 final int enabledInstalledServicesCount = updateServicesStateLocked(userState);
Svetoslav Ganov1e741b22011-09-20 15:50:07 -0700922 // No enabled installed services => disable accessibility to avoid
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -0700923 // sending accessibility events with no recipient across processes.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700924 if (userState.mIsAccessibilityEnabled && enabledInstalledServicesCount == 0) {
925 Settings.Secure.putIntForUser(mContext.getContentResolver(),
926 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId);
Svetoslav Ganov1e741b22011-09-20 15:50:07 -0700927 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700928 }
929
930 /**
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700931 * Unbinds all bound services for a user.
932 *
933 * @param userState The user state.
svetoslavganov75986cf2009-05-14 22:28:01 -0700934 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700935 private void unbindAllServicesLocked(UserState userState) {
936 List<Service> services = userState.mServices;
svetoslavganov75986cf2009-05-14 22:28:01 -0700937 for (int i = 0, count = services.size(); i < count; i++) {
938 Service service = services.get(i);
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -0800939 if (service.unbind()) {
940 i--;
941 count--;
942 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700943 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700944 }
945
946 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700947 * Populates a set with the {@link ComponentName}s stored in a colon
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700948 * separated value setting for a given user.
svetoslavganov75986cf2009-05-14 22:28:01 -0700949 *
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700950 * @param settingName The setting to parse.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700951 * @param userId The user id.
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700952 * @param outComponentNames The output component names.
svetoslavganov75986cf2009-05-14 22:28:01 -0700953 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700954 private void populateComponentNamesFromSettingLocked(String settingName, int userId,
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700955 Set<ComponentName> outComponentNames) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700956 String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(),
957 settingName, userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700958 outComponentNames.clear();
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700959 if (settingValue != null) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700960 TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700961 splitter.setString(settingValue);
svetoslavganov75986cf2009-05-14 22:28:01 -0700962 while (splitter.hasNext()) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800963 String str = splitter.next();
964 if (str == null || str.length() <= 0) {
965 continue;
966 }
967 ComponentName enabledService = ComponentName.unflattenFromString(str);
968 if (enabledService != null) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700969 outComponentNames.add(enabledService);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800970 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700971 }
972 }
973 }
974
975 /**
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700976 * Persists the component names in the specified setting in a
977 * colon separated fashion.
978 *
979 * @param settingName The setting name.
980 * @param componentNames The component names.
981 */
982 private void persistComponentNamesToSettingLocked(String settingName,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700983 Set<ComponentName> componentNames, int userId) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700984 StringBuilder builder = new StringBuilder();
985 for (ComponentName componentName : componentNames) {
986 if (builder.length() > 0) {
987 builder.append(COMPONENT_NAME_SEPARATOR);
988 }
989 builder.append(componentName.flattenToShortString());
990 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700991 Settings.Secure.putStringForUser(mContext.getContentResolver(),
992 settingName, builder.toString(), userId);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -0700993 }
994
995 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700996 * Updates the state of each service by starting (or keeping running) enabled ones and
997 * stopping the rest.
998 *
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700999 * @param userState The user state for which to do that.
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001000 * @return The number of enabled installed services.
svetoslavganov75986cf2009-05-14 22:28:01 -07001001 */
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001002 private int updateServicesStateLocked(UserState userState) {
1003 Map<ComponentName, Service> componentNameToServiceMap =
1004 userState.mComponentNameToServiceMap;
1005 boolean isEnabled = userState.mIsAccessibilityEnabled;
svetoslavganov75986cf2009-05-14 22:28:01 -07001006
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001007 int enabledInstalledServices = 0;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001008 for (int i = 0, count = userState.mInstalledServices.size(); i < count; i++) {
1009 AccessibilityServiceInfo installedService = userState.mInstalledServices.get(i);
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001010 ComponentName componentName = ComponentName.unflattenFromString(
1011 installedService.getId());
svetoslavganov75986cf2009-05-14 22:28:01 -07001012 Service service = componentNameToServiceMap.get(componentName);
1013
Svetoslav Ganovf2245aa2010-12-28 15:48:52 -08001014 if (isEnabled) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001015 if (userState.mEnabledServices.contains(componentName)) {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001016 if (service == null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001017 service = new Service(userState.mUserId, componentName,
1018 installedService, false);
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001019 }
1020 service.bind();
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001021 enabledInstalledServices++;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001022 } else {
Svetoslav Ganov563d7842011-01-06 17:40:26 -08001023 if (service != null) {
1024 service.unbind();
1025 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001026 }
1027 } else {
1028 if (service != null) {
1029 service.unbind();
svetoslavganov75986cf2009-05-14 22:28:01 -07001030 }
1031 }
1032 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001033
Svetoslav Ganov1e741b22011-09-20 15:50:07 -07001034 return enabledInstalledServices;
Svetoslav Ganov37fedf82011-09-14 10:44:09 -07001035 }
1036
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001037 private void scheduleSendStateToClientsLocked(UserState userState) {
1038 if (mGlobalClients.getRegisteredCallbackCount() > 0
1039 || userState.mClients.getRegisteredCallbackCount() > 0) {
1040 final int clientState = getClientState(userState);
1041 mMainHandler.obtainMessage(MainHandler.MSG_SEND_STATE_TO_CLIENTS,
1042 clientState, userState.mUserId) .sendToTarget();
1043 }
1044 }
1045
1046 private void updateInputFilterLocked(UserState userState) {
1047 boolean setInputFilter = false;
1048 AccessibilityInputFilter inputFilter = null;
1049 synchronized (mLock) {
1050 if ((userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled)
1051 || userState.mIsDisplayMagnificationEnabled) {
1052 if (!mHasInputFilter) {
1053 mHasInputFilter = true;
1054 if (mInputFilter == null) {
1055 mInputFilter = new AccessibilityInputFilter(mContext,
1056 AccessibilityManagerService.this);
1057 }
1058 inputFilter = mInputFilter;
1059 setInputFilter = true;
1060 }
1061 int flags = 0;
1062 if (userState.mIsDisplayMagnificationEnabled) {
1063 flags |= AccessibilityInputFilter.FLAG_FEATURE_SCREEN_MAGNIFIER;
1064 }
1065 if (userState.mIsTouchExplorationEnabled) {
1066 flags |= AccessibilityInputFilter.FLAG_FEATURE_TOUCH_EXPLORATION;
1067 }
1068 mInputFilter.setEnabledFeatures(flags);
1069 } else {
1070 if (mHasInputFilter) {
1071 mHasInputFilter = false;
1072 mInputFilter.setEnabledFeatures(0);
1073 inputFilter = null;
1074 setInputFilter = true;
1075 }
1076 }
1077 }
1078 if (setInputFilter) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001079 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001080 mWindowManagerService.setInputFilter(inputFilter);
svetoslavganov75986cf2009-05-14 22:28:01 -07001081 } catch (RemoteException re) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001082 /* ignore */
svetoslavganov75986cf2009-05-14 22:28:01 -07001083 }
1084 }
1085 }
1086
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001087 private void showEnableTouchExplorationDialog(final Service service) {
1088 String label = service.mResolveInfo.loadLabel(
1089 mContext.getPackageManager()).toString();
1090 synchronized (mLock) {
1091 final UserState state = getCurrentUserStateLocked();
1092 if (state.mIsTouchExplorationEnabled) {
1093 return;
1094 }
1095 if (mEnableTouchExplorationDialog != null
1096 && mEnableTouchExplorationDialog.isShowing()) {
1097 return;
1098 }
1099 mEnableTouchExplorationDialog = new AlertDialog.Builder(mContext)
1100 .setIcon(android.R.drawable.ic_dialog_alert)
1101 .setPositiveButton(android.R.string.ok, new OnClickListener() {
1102 @Override
1103 public void onClick(DialogInterface dialog, int which) {
1104 // The user allowed the service to toggle touch exploration.
1105 state.mTouchExplorationGrantedServices.add(service.mComponentName);
1106 persistComponentNamesToSettingLocked(
1107 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
1108 state.mTouchExplorationGrantedServices, state.mUserId);
1109 // Enable touch exploration.
1110 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1111 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1,
1112 service.mUserId);
1113 }
1114 })
1115 .setNegativeButton(android.R.string.cancel, new OnClickListener() {
1116 @Override
1117 public void onClick(DialogInterface dialog, int which) {
1118 dialog.dismiss();
1119 }
1120 })
1121 .setTitle(R.string.enable_explore_by_touch_warning_title)
1122 .setMessage(mContext.getString(
1123 R.string.enable_explore_by_touch_warning_message, label))
1124 .create();
1125 mEnableTouchExplorationDialog.getWindow().setType(
1126 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
1127 mEnableTouchExplorationDialog.setCanceledOnTouchOutside(true);
1128 mEnableTouchExplorationDialog.show();
1129 }
1130 }
1131
1132 private int getClientState(UserState userState) {
1133 int clientState = 0;
1134 if (userState.mIsAccessibilityEnabled) {
1135 clientState |= AccessibilityManager.STATE_FLAG_ACCESSIBILITY_ENABLED;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001136 }
Svetoslav Ganov00aabf72011-07-21 11:35:03 -07001137 // Touch exploration relies on enabled accessibility.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001138 if (userState.mIsAccessibilityEnabled && userState.mIsTouchExplorationEnabled) {
1139 clientState |= AccessibilityManager.STATE_FLAG_TOUCH_EXPLORATION_ENABLED;
Svetoslav Ganov00aabf72011-07-21 11:35:03 -07001140 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001141 return clientState;
Svetoslav Ganov35bfede2011-07-14 17:57:06 -07001142 }
1143
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001144 private void recreateInternalStateLocked(UserState userState) {
1145 populateInstalledAccessibilityServiceLocked(userState);
1146 populateEnabledAccessibilityServicesLocked(userState);
1147 populateTouchExplorationGrantedAccessibilityServicesLocked(userState);
1148
1149 handleTouchExplorationEnabledSettingChangedLocked(userState);
1150 handleDisplayMagnificationEnabledSettingChangedLocked(userState);
1151 handleAccessibilityEnabledSettingChangedLocked(userState);
1152
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001153 performServiceManagementLocked(userState);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001154 updateInputFilterLocked(userState);
1155 scheduleSendStateToClientsLocked(userState);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001156 }
1157
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001158 private void handleAccessibilityEnabledSettingChangedLocked(UserState userState) {
1159 userState.mIsAccessibilityEnabled = Settings.Secure.getIntForUser(
1160 mContext.getContentResolver(),
1161 Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId) == 1;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001162 }
1163
1164 private void performServiceManagementLocked(UserState userState) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001165 if (userState.mIsAccessibilityEnabled ) {
1166 manageServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001167 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001168 unbindAllServicesLocked(userState);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001169 }
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001170 }
1171
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001172 private void handleTouchExplorationEnabledSettingChangedLocked(UserState userState) {
1173 userState.mIsTouchExplorationEnabled = Settings.Secure.getIntForUser(
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001174 mContext.getContentResolver(),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001175 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId) == 1;
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001176 }
1177
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001178 private void handleDisplayMagnificationEnabledSettingChangedLocked(UserState userState) {
1179 userState.mIsDisplayMagnificationEnabled = Settings.Secure.getIntForUser(
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001180 mContext.getContentResolver(),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001181 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
1182 0, userState.mUserId) == 1;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001183 }
1184
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001185 private void handleTouchExplorationGrantedAccessibilityServicesChangedLocked(
1186 UserState userState) {
1187 final int serviceCount = userState.mServices.size();
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001188 for (int i = 0; i < serviceCount; i++) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001189 Service service = userState.mServices.get(i);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001190 if (service.mRequestTouchExplorationMode
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001191 && userState.mTouchExplorationGrantedServices.contains(
1192 service.mComponentName)) {
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001193 tryEnableTouchExplorationLocked(service);
1194 return;
1195 }
1196 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001197 if (userState.mIsTouchExplorationEnabled) {
1198 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1199 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId);
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001200 }
1201 }
1202
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001203 private void tryEnableTouchExplorationLocked(final Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001204 UserState userState = getUserStateLocked(service.mUserId);
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001205 if (!userState.mIsTouchExplorationEnabled && service.mRequestTouchExplorationMode
1206 && service.canReceiveEvents()) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001207 final boolean canToggleTouchExploration =
1208 userState.mTouchExplorationGrantedServices.contains(service.mComponentName);
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001209 if (!service.mIsAutomation && !canToggleTouchExploration) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001210 showEnableTouchExplorationDialog(service);
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -07001211 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001212 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1213 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 1, userState.mUserId);
Svetoslav Ganov4074e8a2012-05-23 11:52:36 -07001214 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001215 }
1216 }
1217
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001218 private void tryDisableTouchExplorationLocked(Service service) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001219 UserState userState = getUserStateLocked(service.mUserId);
1220 if (userState.mIsTouchExplorationEnabled) {
1221 final int serviceCount = userState.mServices.size();
1222 for (int i = 0; i < serviceCount; i++) {
1223 Service other = userState.mServices.get(i);
1224 if (other != service && other.mRequestTouchExplorationMode) {
1225 return;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001226 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001227 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001228 Settings.Secure.putIntForUser(mContext.getContentResolver(),
1229 Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0, userState.mUserId);
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001230 }
1231 }
1232
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001233 private class AccessibilityConnectionWrapper implements DeathRecipient {
1234 private final int mWindowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001235 private final int mUserId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001236 private final IAccessibilityInteractionConnection mConnection;
1237
1238 public AccessibilityConnectionWrapper(int windowId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001239 IAccessibilityInteractionConnection connection, int userId) {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001240 mWindowId = windowId;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001241 mUserId = userId;
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001242 mConnection = connection;
1243 }
1244
1245 public void linkToDeath() throws RemoteException {
1246 mConnection.asBinder().linkToDeath(this, 0);
1247 }
1248
1249 public void unlinkToDeath() {
1250 mConnection.asBinder().unlinkToDeath(this, 0);
1251 }
1252
1253 @Override
1254 public void binderDied() {
1255 unlinkToDeath();
1256 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001257 removeAccessibilityInteractionConnectionLocked(mWindowId, mUserId);
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001258 }
1259 }
1260 }
1261
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001262 private final class MainHandler extends Handler {
1263 public static final int MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER = 1;
1264 public static final int MSG_SEND_STATE_TO_CLIENTS = 2;
1265 public static final int MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER = 3;
1266 public static final int MSG_SEND_RECREATE_INTERNAL_STATE = 4;
Svetoslav Ganova8afa692012-09-25 13:03:18 -07001267 public static final int MSG_UPDATE_ACTIVE_WINDOW = 5;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001268 public static final int MSG_ANNOUNCE_NEW_USER_IF_NEEDED = 6;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001269
1270 public MainHandler(Looper looper) {
1271 super(looper);
1272 }
1273
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001274 @Override
1275 public void handleMessage(Message msg) {
1276 final int type = msg.what;
1277 switch (type) {
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001278 case MSG_SEND_ACCESSIBILITY_EVENT_TO_INPUT_FILTER: {
1279 AccessibilityEvent event = (AccessibilityEvent) msg.obj;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001280 synchronized (mLock) {
1281 if (mHasInputFilter && mInputFilter != null) {
1282 mInputFilter.notifyAccessibilityEvent(event);
1283 }
Svetoslav Ganove45c0b22012-06-08 17:44:29 -07001284 }
1285 event.recycle();
1286 } break;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001287 case MSG_SEND_STATE_TO_CLIENTS: {
1288 final int clientState = msg.arg1;
1289 final int userId = msg.arg2;
1290 sendStateToClients(clientState, mGlobalClients);
1291 sendStateToClientsForUser(clientState, userId);
1292 } break;
1293 case MSG_SEND_CLEARED_STATE_TO_CLIENTS_FOR_USER: {
1294 final int userId = msg.arg1;
1295 sendStateToClientsForUser(0, userId);
1296 } break;
1297 case MSG_SEND_RECREATE_INTERNAL_STATE: {
1298 final int userId = msg.arg1;
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001299 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001300 UserState userState = getUserStateLocked(userId);
1301 recreateInternalStateLocked(userState);
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07001302 }
1303 } break;
Svetoslav Ganova8afa692012-09-25 13:03:18 -07001304 case MSG_UPDATE_ACTIVE_WINDOW: {
1305 final int windowId = msg.arg1;
1306 final int eventType = msg.arg2;
1307 mSecurityPolicy.updateActiveWindow(windowId, eventType);
1308 } break;
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07001309 case MSG_ANNOUNCE_NEW_USER_IF_NEEDED: {
1310 announceNewUserIfNeeded();
1311 } break;
1312 }
1313 }
1314
1315 private void announceNewUserIfNeeded() {
1316 synchronized (mLock) {
1317 UserState userState = getCurrentUserStateLocked();
1318 if (userState.mIsAccessibilityEnabled) {
1319 UserManager userManager = (UserManager) mContext.getSystemService(
1320 Context.USER_SERVICE);
1321 String message = mContext.getString(R.string.user_switched,
1322 userManager.getUserInfo(mCurrentUserId).name);
1323 AccessibilityEvent event = AccessibilityEvent.obtain(
1324 AccessibilityEvent.TYPE_ANNOUNCEMENT);
1325 event.getText().add(message);
1326 sendAccessibilityEvent(event, mCurrentUserId);
1327 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001328 }
1329 }
1330
1331 private void sendStateToClientsForUser(int clientState, int userId) {
1332 final UserState userState;
1333 synchronized (mLock) {
1334 userState = getUserStateLocked(userId);
1335 }
1336 sendStateToClients(clientState, userState.mClients);
1337 }
1338
1339 private void sendStateToClients(int clientState,
1340 RemoteCallbackList<IAccessibilityManagerClient> clients) {
1341 try {
1342 final int userClientCount = clients.beginBroadcast();
1343 for (int i = 0; i < userClientCount; i++) {
1344 IAccessibilityManagerClient client = clients.getBroadcastItem(i);
1345 try {
1346 client.setState(clientState);
1347 } catch (RemoteException re) {
1348 /* ignore */
1349 }
1350 }
1351 } finally {
1352 clients.finishBroadcast();
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001353 }
1354 }
1355 }
1356
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001357 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001358 * This class represents an accessibility service. It stores all per service
1359 * data required for the service management, provides API for starting/stopping the
1360 * service and is responsible for adding/removing the service in the data structures
1361 * for service management. The class also exposes configuration interface that is
1362 * passed to the service it represents as soon it is bound. It also serves as the
1363 * connection for the service.
1364 */
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001365 class Service extends IAccessibilityServiceConnection.Stub
1366 implements ServiceConnection, DeathRecipient {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001367
1368 // We pick the MSB to avoid collision since accessibility event types are
1369 // used as message types allowing us to remove messages per event type.
1370 private static final int MSG_ON_GESTURE = 0x80000000;
1371
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001372 final int mUserId;
1373
svetoslavganov75986cf2009-05-14 22:28:01 -07001374 int mId = 0;
1375
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001376 AccessibilityServiceInfo mAccessibilityServiceInfo;
Svetoslav Ganov736c2752011-04-22 18:30:36 -07001377
svetoslavganov75986cf2009-05-14 22:28:01 -07001378 IBinder mService;
1379
Svetoslav Ganov42138042012-03-20 11:51:39 -07001380 IAccessibilityServiceClient mServiceInterface;
svetoslavganov75986cf2009-05-14 22:28:01 -07001381
1382 int mEventTypes;
1383
1384 int mFeedbackType;
1385
1386 Set<String> mPackageNames = new HashSet<String>();
1387
1388 boolean mIsDefault;
1389
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001390 boolean mRequestTouchExplorationMode;
1391
Svetoslav Ganov42138042012-03-20 11:51:39 -07001392 boolean mIncludeNotImportantViews;
1393
svetoslavganov75986cf2009-05-14 22:28:01 -07001394 long mNotificationTimeout;
1395
svetoslavganov75986cf2009-05-14 22:28:01 -07001396 ComponentName mComponentName;
1397
1398 Intent mIntent;
1399
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001400 boolean mCanRetrieveScreenContent;
1401
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001402 boolean mIsAutomation;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001403
Svetoslav Ganov7961be72011-06-21 12:31:56 -07001404 final Rect mTempBounds = new Rect();
1405
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001406 final ResolveInfo mResolveInfo;
1407
svetoslavganov75986cf2009-05-14 22:28:01 -07001408 // the events pending events to be dispatched to this service
1409 final SparseArray<AccessibilityEvent> mPendingEvents =
1410 new SparseArray<AccessibilityEvent>();
1411
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001412 /**
1413 * Handler for delayed event dispatch.
1414 */
Svetoslav Ganovec2c1712012-05-22 11:31:57 -07001415 public Handler mHandler = new Handler(mMainHandler.getLooper()) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001416 @Override
1417 public void handleMessage(Message message) {
1418 final int type = message.what;
1419 switch (type) {
1420 case MSG_ON_GESTURE: {
1421 final int gestureId = message.arg1;
1422 notifyGestureInternal(gestureId);
1423 } break;
1424 default: {
1425 final int eventType = type;
1426 notifyAccessibilityEventInternal(eventType);
1427 } break;
1428 }
1429 }
1430 };
1431
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001432 public Service(int userId, ComponentName componentName,
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001433 AccessibilityServiceInfo accessibilityServiceInfo, boolean isAutomation) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001434 mUserId = userId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001435 mResolveInfo = accessibilityServiceInfo.getResolveInfo();
svetoslavganov75986cf2009-05-14 22:28:01 -07001436 mId = sIdCounter++;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001437 mComponentName = componentName;
1438 mAccessibilityServiceInfo = accessibilityServiceInfo;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001439 mIsAutomation = isAutomation;
1440 if (!isAutomation) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001441 mCanRetrieveScreenContent = accessibilityServiceInfo.getCanRetrieveWindowContent();
Svetoslav Ganova43ef3d2012-07-12 13:07:55 -07001442 mRequestTouchExplorationMode =
Svetoslav Ganove4abc512012-05-09 11:02:38 -07001443 (accessibilityServiceInfo.flags
1444 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001445 mIntent = new Intent().setComponent(mComponentName);
1446 mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1447 com.android.internal.R.string.accessibility_binding_label);
1448 mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1449 mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0));
1450 } else {
1451 mCanRetrieveScreenContent = true;
1452 }
1453 setDynamicallyConfigurableProperties(accessibilityServiceInfo);
svetoslavganov75986cf2009-05-14 22:28:01 -07001454 }
1455
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001456 public void setDynamicallyConfigurableProperties(AccessibilityServiceInfo info) {
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001457 mEventTypes = info.eventTypes;
1458 mFeedbackType = info.feedbackType;
1459 String[] packageNames = info.packageNames;
1460 if (packageNames != null) {
1461 mPackageNames.addAll(Arrays.asList(packageNames));
1462 }
1463 mNotificationTimeout = info.notificationTimeout;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001464 mIsDefault = (info.flags & DEFAULT) != 0;
1465
Guang Zhudf549f82012-05-08 23:09:24 -07001466 if (mIsAutomation || info.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion
Svetoslav Ganov5a48f972012-05-13 13:33:53 -07001467 >= Build.VERSION_CODES.JELLY_BEAN) {
Guang Zhudf549f82012-05-08 23:09:24 -07001468 mIncludeNotImportantViews =
Svetoslav Ganove4abc512012-05-09 11:02:38 -07001469 (info.flags & FLAG_INCLUDE_NOT_IMPORTANT_VIEWS) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001470 }
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001471
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001472 mRequestTouchExplorationMode = (info.flags
1473 & AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE) != 0;
1474
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001475 // If this service is up and running we may have to enable touch
1476 // exploration, otherwise this will happen when the service connects.
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001477 synchronized (mLock) {
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001478 if (canReceiveEvents()) {
Svetoslav Ganov9a4c5cd2012-05-30 14:06:32 -07001479 if (mRequestTouchExplorationMode) {
1480 tryEnableTouchExplorationLocked(this);
1481 } else {
1482 tryDisableTouchExplorationLocked(this);
1483 }
1484 }
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001485 }
Svetoslav Ganovcc4053e2011-05-23 13:37:44 -07001486 }
1487
svetoslavganov75986cf2009-05-14 22:28:01 -07001488 /**
1489 * Binds to the accessibility service.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001490 *
1491 * @return True if binding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001492 */
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001493 public boolean bind() {
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001494 if (!mIsAutomation && mService == null) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001495 return mContext.bindService(mIntent, this, Context.BIND_AUTO_CREATE, mUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001496 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001497 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001498 }
1499
1500 /**
1501 * Unbinds form the accessibility service and removes it from the data
1502 * structures for service management.
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001503 *
1504 * @return True if unbinding is successful.
svetoslavganov75986cf2009-05-14 22:28:01 -07001505 */
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001506 public boolean unbind() {
svetoslavganov75986cf2009-05-14 22:28:01 -07001507 if (mService != null) {
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001508 synchronized (mLock) {
1509 tryRemoveServiceLocked(this);
1510 }
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001511 if (!mIsAutomation) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001512 mContext.unbindService(this);
1513 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001514 return true;
svetoslavganov75986cf2009-05-14 22:28:01 -07001515 }
Svetoslav Ganov6ff5f102011-01-10 13:12:55 -08001516 return false;
svetoslavganov75986cf2009-05-14 22:28:01 -07001517 }
1518
Svetoslav Ganov1f22b6a2012-09-28 09:27:35 -07001519 public boolean canReceiveEvents() {
Svetoslav Ganovc321c192011-06-03 19:43:23 -07001520 return (mEventTypes != 0 && mFeedbackType != 0 && mService != null);
svetoslavganov75986cf2009-05-14 22:28:01 -07001521 }
1522
Svetoslav Ganov42138042012-03-20 11:51:39 -07001523 @Override
1524 public AccessibilityServiceInfo getServiceInfo() {
1525 synchronized (mLock) {
1526 return mAccessibilityServiceInfo;
1527 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001528 }
1529
Svetoslav Ganov42138042012-03-20 11:51:39 -07001530 @Override
1531 public void setServiceInfo(AccessibilityServiceInfo info) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001532 final long identity = Binder.clearCallingIdentity();
1533 try {
1534 synchronized (mLock) {
1535 // If the XML manifest had data to configure the service its info
1536 // should be already set. In such a case update only the dynamically
1537 // configurable properties.
1538 AccessibilityServiceInfo oldInfo = mAccessibilityServiceInfo;
1539 if (oldInfo != null) {
1540 oldInfo.updateDynamicallyConfigurableProperties(info);
1541 setDynamicallyConfigurableProperties(oldInfo);
1542 } else {
1543 setDynamicallyConfigurableProperties(info);
1544 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001545 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001546 } finally {
1547 Binder.restoreCallingIdentity(identity);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001548 }
1549 }
1550
1551 @Override
svetoslavganov75986cf2009-05-14 22:28:01 -07001552 public void onServiceConnected(ComponentName componentName, IBinder service) {
1553 mService = service;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001554 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
svetoslavganov75986cf2009-05-14 22:28:01 -07001555 try {
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001556 mServiceInterface.setConnection(this, mId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001557 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001558 tryAddServiceLocked(this, mUserId);
svetoslavganov75986cf2009-05-14 22:28:01 -07001559 }
1560 } catch (RemoteException re) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 Slog.w(LOG_TAG, "Error while setting Controller for service: " + service, re);
svetoslavganov75986cf2009-05-14 22:28:01 -07001562 }
1563 }
1564
Svetoslav Ganov42138042012-03-20 11:51:39 -07001565 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001566 public float findAccessibilityNodeInfoByViewId(int accessibilityWindowId,
1567 long accessibilityNodeId, int viewId, int interactionId,
1568 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001569 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001570 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001571 IAccessibilityInteractionConnection connection = null;
1572 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001573 final int resolvedUserId = mSecurityPolicy
1574 .resolveCallingUserIdEnforcingPermissionsLocked(
1575 UserHandle.getCallingUserId());
1576 if (resolvedUserId != mCurrentUserId) {
1577 return -1;
1578 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001579 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001580 final boolean permissionGranted = mSecurityPolicy.canRetrieveWindowContent(this);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001581 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001582 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001583 } else {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001584 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001585 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001586 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001587 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001588 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001589 }
1590 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001591 final int flags = (mIncludeNotImportantViews) ?
1592 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001593 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001594 final long identityToken = Binder.clearCallingIdentity();
1595 try {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001596 connection.findAccessibilityNodeInfoByViewId(accessibilityNodeId, viewId,
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001597 interactionId, callback, flags, interrogatingPid, interrogatingTid);
1598 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001599 } catch (RemoteException re) {
1600 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001601 Slog.e(LOG_TAG, "Error findAccessibilityNodeInfoByViewId().");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001602 }
1603 } finally {
1604 Binder.restoreCallingIdentity(identityToken);
1605 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001606 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001607 }
1608
Svetoslav Ganov42138042012-03-20 11:51:39 -07001609 @Override
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001610 public float findAccessibilityNodeInfosByText(int accessibilityWindowId,
1611 long accessibilityNodeId, String text, int interactionId,
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001612 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1613 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001614 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001615 IAccessibilityInteractionConnection connection = null;
1616 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001617 final int resolvedUserId = mSecurityPolicy
1618 .resolveCallingUserIdEnforcingPermissionsLocked(
1619 UserHandle.getCallingUserId());
1620 if (resolvedUserId != mCurrentUserId) {
1621 return -1;
1622 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001623 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001624 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07001625 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001626 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001627 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001628 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001629 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001630 connection = getConnectionLocked(resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001631 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001632 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001633 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001634 }
1635 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001636 final int flags = (mIncludeNotImportantViews) ?
1637 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001638 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001639 final long identityToken = Binder.clearCallingIdentity();
1640 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001641 connection.findAccessibilityNodeInfosByText(accessibilityNodeId, text,
1642 interactionId, callback, flags, interrogatingPid,
Svetoslav Ganov86783472012-06-06 21:12:20 -07001643 interrogatingTid);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001644 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001645 } catch (RemoteException re) {
1646 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001647 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfosByText()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001648 }
1649 } finally {
1650 Binder.restoreCallingIdentity(identityToken);
1651 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001652 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001653 }
1654
Svetoslav Ganov42138042012-03-20 11:51:39 -07001655 @Override
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001656 public float findAccessibilityNodeInfoByAccessibilityId(int accessibilityWindowId,
Svetoslav Ganov02107852011-10-03 17:06:56 -07001657 long accessibilityNodeId, int interactionId,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001658 IAccessibilityInteractionConnectionCallback callback, int flags,
1659 long interrogatingTid) throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001660 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001661 IAccessibilityInteractionConnection connection = null;
1662 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001663 final int resolvedUserId = mSecurityPolicy
1664 .resolveCallingUserIdEnforcingPermissionsLocked(
1665 UserHandle.getCallingUserId());
1666 if (resolvedUserId != mCurrentUserId) {
1667 return -1;
1668 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001669 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001670 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001671 final boolean permissionGranted =
1672 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
1673 if (!permissionGranted) {
1674 return 0;
1675 } else {
1676 connection = getConnectionLocked(resolvedWindowId);
1677 if (connection == null) {
1678 return 0;
1679 }
1680 }
1681 }
1682 final int allFlags = flags | ((mIncludeNotImportantViews) ?
1683 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0);
1684 final int interrogatingPid = Binder.getCallingPid();
1685 final long identityToken = Binder.clearCallingIdentity();
1686 try {
1687 connection.findAccessibilityNodeInfoByAccessibilityId(accessibilityNodeId,
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001688 interactionId, callback, allFlags, interrogatingPid, interrogatingTid);
1689 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001690 } catch (RemoteException re) {
1691 if (DEBUG) {
1692 Slog.e(LOG_TAG, "Error calling findAccessibilityNodeInfoByAccessibilityId()");
1693 }
1694 } finally {
1695 Binder.restoreCallingIdentity(identityToken);
1696 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001697 return 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001698 }
1699
1700 @Override
1701 public float findFocus(int accessibilityWindowId, long accessibilityNodeId,
1702 int focusType, int interactionId,
1703 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001704 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001705 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001706 IAccessibilityInteractionConnection connection = null;
1707 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001708 final int resolvedUserId = mSecurityPolicy
1709 .resolveCallingUserIdEnforcingPermissionsLocked(
1710 UserHandle.getCallingUserId());
1711 if (resolvedUserId != mCurrentUserId) {
1712 return -1;
1713 }
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07001714 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001715 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001716 final boolean permissionGranted =
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001717 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001718 if (!permissionGranted) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001719 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001720 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001721 connection = getConnectionLocked(resolvedWindowId);
1722 if (connection == null) {
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001723 return 0;
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001724 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001725 }
1726 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001727 final int flags = (mIncludeNotImportantViews) ?
1728 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001729 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001730 final long identityToken = Binder.clearCallingIdentity();
1731 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001732 connection.findFocus(accessibilityNodeId, focusType, interactionId, callback,
1733 flags, interrogatingPid, interrogatingTid);
1734 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001735 } catch (RemoteException re) {
1736 if (DEBUG) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07001737 Slog.e(LOG_TAG, "Error calling findAccessibilityFocus()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001738 }
1739 } finally {
1740 Binder.restoreCallingIdentity(identityToken);
1741 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001742 return 0;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001743 }
1744
Svetoslav Ganov42138042012-03-20 11:51:39 -07001745 @Override
1746 public float focusSearch(int accessibilityWindowId, long accessibilityNodeId,
1747 int direction, int interactionId,
1748 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1749 throws RemoteException {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001750 final int resolvedWindowId;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001751 IAccessibilityInteractionConnection connection = null;
1752 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001753 final int resolvedUserId = mSecurityPolicy
1754 .resolveCallingUserIdEnforcingPermissionsLocked(
1755 UserHandle.getCallingUserId());
1756 if (resolvedUserId != mCurrentUserId) {
1757 return -1;
1758 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07001759 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001760 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001761 final boolean permissionGranted =
1762 mSecurityPolicy.canGetAccessibilityNodeInfoLocked(this, resolvedWindowId);
1763 if (!permissionGranted) {
1764 return 0;
1765 } else {
1766 connection = getConnectionLocked(resolvedWindowId);
1767 if (connection == null) {
1768 return 0;
1769 }
1770 }
1771 }
1772 final int flags = (mIncludeNotImportantViews) ?
1773 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
1774 final int interrogatingPid = Binder.getCallingPid();
1775 final long identityToken = Binder.clearCallingIdentity();
1776 try {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001777 connection.focusSearch(accessibilityNodeId, direction, interactionId, callback,
1778 flags, interrogatingPid, interrogatingTid);
1779 return getCompatibilityScale(resolvedWindowId);
Svetoslav Ganov42138042012-03-20 11:51:39 -07001780 } catch (RemoteException re) {
1781 if (DEBUG) {
1782 Slog.e(LOG_TAG, "Error calling accessibilityFocusSearch()");
1783 }
1784 } finally {
1785 Binder.restoreCallingIdentity(identityToken);
1786 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001787 return 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07001788 }
1789
1790 @Override
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001791 public boolean performAccessibilityAction(int accessibilityWindowId,
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07001792 long accessibilityNodeId, int action, Bundle arguments, int interactionId,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001793 IAccessibilityInteractionConnectionCallback callback, long interrogatingTid)
1794 throws RemoteException {
1795 final int resolvedWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001796 IAccessibilityInteractionConnection connection = null;
1797 synchronized (mLock) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001798 final int resolvedUserId = mSecurityPolicy
1799 .resolveCallingUserIdEnforcingPermissionsLocked(
1800 UserHandle.getCallingUserId());
1801 if (resolvedUserId != mCurrentUserId) {
1802 return false;
1803 }
1804 mSecurityPolicy.enforceCanRetrieveWindowContent(this);
1805 resolvedWindowId = resolveAccessibilityWindowIdLocked(accessibilityWindowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001806 final boolean permissionGranted = mSecurityPolicy.canPerformActionLocked(this,
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07001807 resolvedWindowId, action, arguments);
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001808 if (!permissionGranted) {
1809 return false;
1810 } else {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001811 connection = getConnectionLocked(resolvedWindowId);
1812 if (connection == null) {
Svetoslav Ganove8f95352011-07-06 17:24:03 -07001813 return false;
1814 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001815 }
1816 }
Svetoslav Ganov9bf21873c2012-05-22 17:43:23 -07001817 final int flags = (mIncludeNotImportantViews) ?
1818 AccessibilityNodeInfo.INCLUDE_NOT_IMPORTANT_VIEWS : 0;
1819 final int interrogatingPid = Binder.getCallingPid();
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001820 final long identityToken = Binder.clearCallingIdentity();
1821 try {
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07001822 connection.performAccessibilityAction(accessibilityNodeId, action, arguments,
1823 interactionId, callback, flags, interrogatingPid, interrogatingTid);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001824 } catch (RemoteException re) {
1825 if (DEBUG) {
Svetoslav Ganov79311c42012-01-17 20:24:26 -08001826 Slog.e(LOG_TAG, "Error calling performAccessibilityAction()");
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001827 }
1828 } finally {
1829 Binder.restoreCallingIdentity(identityToken);
1830 }
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07001831 return true;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001832 }
1833
Svetoslav Ganove20a1772012-09-25 16:07:46 -07001834 public boolean performGlobalAction(int action) {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001835 synchronized (mLock) {
1836 final int resolvedUserId = mSecurityPolicy
1837 .resolveCallingUserIdEnforcingPermissionsLocked(
1838 UserHandle.getCallingUserId());
1839 if (resolvedUserId != mCurrentUserId) {
1840 return false;
1841 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07001842 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001843 final long identity = Binder.clearCallingIdentity();
1844 try {
1845 switch (action) {
1846 case AccessibilityService.GLOBAL_ACTION_BACK: {
1847 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_BACK);
1848 } return true;
1849 case AccessibilityService.GLOBAL_ACTION_HOME: {
1850 sendDownAndUpKeyEvents(KeyEvent.KEYCODE_HOME);
1851 } return true;
1852 case AccessibilityService.GLOBAL_ACTION_RECENTS: {
1853 openRecents();
1854 } return true;
1855 case AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS: {
Svetoslav Ganove20a1772012-09-25 16:07:46 -07001856 expandNotifications();
1857 } return true;
1858 case AccessibilityService.GLOBAL_ACTION_QUICK_SETTINGS: {
1859 expandQuickSettings();
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001860 } return true;
1861 }
1862 return false;
1863 } finally {
1864 Binder.restoreCallingIdentity(identity);
1865 }
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07001866 }
1867
svetoslavganov75986cf2009-05-14 22:28:01 -07001868 public void onServiceDisconnected(ComponentName componentName) {
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001869 /* do nothing - #binderDied takes care */
1870 }
1871
1872 public void linkToOwnDeath() throws RemoteException {
1873 mService.linkToDeath(this, 0);
1874 }
1875
1876 public void unlinkToOwnDeath() {
1877 mService.unlinkToDeath(this, 0);
1878 }
1879
Svetoslav Ganovd116d7c2011-11-21 18:41:59 -08001880 public void dispose() {
1881 try {
1882 // Clear the proxy in the other process so this
1883 // IAccessibilityServiceConnection can be garbage collected.
1884 mServiceInterface.setConnection(null, mId);
1885 } catch (RemoteException re) {
1886 /* ignore */
1887 }
1888 mService = null;
1889 mServiceInterface = null;
1890 }
1891
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001892 public void binderDied() {
svetoslavganov75986cf2009-05-14 22:28:01 -07001893 synchronized (mLock) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08001894 // The death recipient is unregistered in tryRemoveServiceLocked
Svetoslav Ganovf9886f32011-05-10 15:28:33 -07001895 tryRemoveServiceLocked(this);
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001896 // We no longer have an automation service, so restore
1897 // the state based on values in the settings database.
1898 if (mIsAutomation) {
Svetoslav Ganov9b666d02012-02-10 14:55:41 -08001899 mUiAutomationService = null;
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001900 recreateInternalStateLocked(getUserStateLocked(mUserId));
Svetoslav Ganovb6eca6e2011-09-27 10:47:58 -07001901 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001902 }
1903 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001904
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07001905 /**
1906 * Performs a notification for an {@link AccessibilityEvent}.
1907 *
1908 * @param event The event.
1909 */
1910 public void notifyAccessibilityEvent(AccessibilityEvent event) {
1911 synchronized (mLock) {
1912 final int eventType = event.getEventType();
1913 // Make a copy since during dispatch it is possible the event to
1914 // be modified to remove its source if the receiving service does
1915 // not have permission to access the window content.
1916 AccessibilityEvent newEvent = AccessibilityEvent.obtain(event);
1917 AccessibilityEvent oldEvent = mPendingEvents.get(eventType);
1918 mPendingEvents.put(eventType, newEvent);
1919
1920 final int what = eventType;
1921 if (oldEvent != null) {
1922 mHandler.removeMessages(what);
1923 oldEvent.recycle();
1924 }
1925
1926 Message message = mHandler.obtainMessage(what);
1927 mHandler.sendMessageDelayed(message, mNotificationTimeout);
1928 }
1929 }
1930
1931 /**
1932 * Notifies an accessibility service client for a scheduled event given the event type.
1933 *
1934 * @param eventType The type of the event to dispatch.
1935 */
1936 private void notifyAccessibilityEventInternal(int eventType) {
1937 IAccessibilityServiceClient listener;
1938 AccessibilityEvent event;
1939
1940 synchronized (mLock) {
1941 listener = mServiceInterface;
1942
1943 // If the service died/was disabled while the message for dispatching
1944 // the accessibility event was propagating the listener may be null.
1945 if (listener == null) {
1946 return;
1947 }
1948
1949 event = mPendingEvents.get(eventType);
1950
1951 // Check for null here because there is a concurrent scenario in which this
1952 // happens: 1) A binder thread calls notifyAccessibilityServiceDelayedLocked
1953 // which posts a message for dispatching an event. 2) The message is pulled
1954 // from the queue by the handler on the service thread and the latter is
1955 // just about to acquire the lock and call this method. 3) Now another binder
1956 // thread acquires the lock calling notifyAccessibilityServiceDelayedLocked
1957 // so the service thread waits for the lock; 4) The binder thread replaces
1958 // the event with a more recent one (assume the same event type) and posts a
1959 // dispatch request releasing the lock. 5) Now the main thread is unblocked and
1960 // dispatches the event which is removed from the pending ones. 6) And ... now
1961 // the service thread handles the last message posted by the last binder call
1962 // but the event is already dispatched and hence looking it up in the pending
1963 // ones yields null. This check is much simpler that keeping count for each
1964 // event type of each service to catch such a scenario since only one message
1965 // is processed at a time.
1966 if (event == null) {
1967 return;
1968 }
1969
1970 mPendingEvents.remove(eventType);
1971 if (mSecurityPolicy.canRetrieveWindowContent(this)) {
1972 event.setConnectionId(mId);
1973 } else {
1974 event.setSource(null);
1975 }
1976 event.setSealed(true);
1977 }
1978
1979 try {
1980 listener.onAccessibilityEvent(event);
1981 if (DEBUG) {
1982 Slog.i(LOG_TAG, "Event " + event + " sent to " + listener);
1983 }
1984 } catch (RemoteException re) {
1985 Slog.e(LOG_TAG, "Error during sending " + event + " to " + listener, re);
1986 } finally {
1987 event.recycle();
1988 }
1989 }
1990
1991 public void notifyGesture(int gestureId) {
1992 mHandler.obtainMessage(MSG_ON_GESTURE, gestureId, 0).sendToTarget();
1993 }
1994
1995 private void notifyGestureInternal(int gestureId) {
1996 IAccessibilityServiceClient listener = mServiceInterface;
1997 if (listener != null) {
1998 try {
1999 listener.onGesture(gestureId);
2000 } catch (RemoteException re) {
2001 Slog.e(LOG_TAG, "Error during sending gesture " + gestureId
2002 + " to " + mService, re);
2003 }
2004 }
2005 }
2006
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07002007 private void sendDownAndUpKeyEvents(int keyCode) {
2008 final long token = Binder.clearCallingIdentity();
2009
2010 // Inject down.
2011 final long downTime = SystemClock.uptimeMillis();
2012 KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
2013 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2014 InputDevice.SOURCE_KEYBOARD, null);
2015 InputManager.getInstance().injectInputEvent(down,
2016 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2017 down.recycle();
2018
2019 // Inject up.
2020 final long upTime = SystemClock.uptimeMillis();
2021 KeyEvent up = KeyEvent.obtain(downTime, upTime, KeyEvent.ACTION_UP, keyCode, 0, 0,
2022 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
2023 InputDevice.SOURCE_KEYBOARD, null);
2024 InputManager.getInstance().injectInputEvent(up,
2025 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
2026 up.recycle();
2027
2028 Binder.restoreCallingIdentity(token);
2029 }
2030
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002031 private void expandNotifications() {
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002032 final long token = Binder.clearCallingIdentity();
2033
2034 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2035 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002036 statusBarManager.expandNotificationsPanel();
Svetoslav Ganove20a1772012-09-25 16:07:46 -07002037
2038 Binder.restoreCallingIdentity(token);
2039 }
2040
2041 private void expandQuickSettings() {
2042 final long token = Binder.clearCallingIdentity();
2043
2044 StatusBarManager statusBarManager = (StatusBarManager) mContext.getSystemService(
2045 android.app.Service.STATUS_BAR_SERVICE);
Daniel Sandler11cf1782012-09-27 14:03:08 -04002046 statusBarManager.expandSettingsPanel();
Svetoslav Ganov5c89f442012-05-15 13:28:09 -07002047
2048 Binder.restoreCallingIdentity(token);
2049 }
2050
Svetoslav Ganovc682fc92012-06-04 14:02:09 -07002051 private void openRecents() {
2052 final long token = Binder.clearCallingIdentity();
2053
2054 IStatusBarService statusBarService = IStatusBarService.Stub.asInterface(
2055 ServiceManager.getService("statusbar"));
2056 try {
2057 statusBarService.toggleRecentApps();
2058 } catch (RemoteException e) {
2059 Slog.e(LOG_TAG, "Error toggling recent apps.");
2060 }
2061
2062 Binder.restoreCallingIdentity(token);
2063 }
2064
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002065 private IAccessibilityInteractionConnection getConnectionLocked(int windowId) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002066 if (DEBUG) {
2067 Slog.i(LOG_TAG, "Trying to get interaction connection to windowId: " + windowId);
2068 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002069 AccessibilityConnectionWrapper wrapper = mGlobalInteractionConnections.get(windowId);
2070 if (wrapper == null) {
2071 wrapper = getCurrentUserStateLocked().mInteractionConnections.get(windowId);
2072 }
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002073 if (wrapper != null && wrapper.mConnection != null) {
2074 return wrapper.mConnection;
2075 }
2076 if (DEBUG) {
2077 Slog.e(LOG_TAG, "No interaction connection to window: " + windowId);
2078 }
2079 return null;
2080 }
2081
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002082 private int resolveAccessibilityWindowIdLocked(int accessibilityWindowId) {
Svetoslav Ganov0d04e242012-02-21 13:46:36 -08002083 if (accessibilityWindowId == AccessibilityNodeInfo.ACTIVE_WINDOW_ID) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002084 return mSecurityPolicy.mActiveWindowId;
Svetoslav Ganov79311c42012-01-17 20:24:26 -08002085 }
2086 return accessibilityWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002087 }
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002088
Svetoslav Ganov8bd69612011-08-23 13:40:30 -07002089 private float getCompatibilityScale(int windowId) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002090 try {
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002091 IBinder windowToken = mGlobalWindowTokens.get(windowId);
2092 if (windowToken != null) {
2093 return mWindowManagerService.getWindowCompatibilityScale(windowToken);
2094 }
2095 windowToken = getCurrentUserStateLocked().mWindowTokens.get(windowId);
2096 if (windowToken != null) {
2097 return mWindowManagerService.getWindowCompatibilityScale(windowToken);
2098 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002099 } catch (RemoteException re) {
2100 /* ignore */
2101 }
2102 return 1.0f;
Svetoslav Ganov7961be72011-06-21 12:31:56 -07002103 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002104 }
2105
2106 final class SecurityPolicy {
Svetoslav Ganov42138042012-03-20 11:51:39 -07002107 private static final int VALID_ACTIONS =
2108 AccessibilityNodeInfo.ACTION_CLICK
Svetoslav Ganov76f287e2012-04-23 11:02:36 -07002109 | AccessibilityNodeInfo.ACTION_LONG_CLICK
Svetoslav Ganov42138042012-03-20 11:51:39 -07002110 | AccessibilityNodeInfo.ACTION_FOCUS
2111 | AccessibilityNodeInfo.ACTION_CLEAR_FOCUS
2112 | AccessibilityNodeInfo.ACTION_SELECT
2113 | AccessibilityNodeInfo.ACTION_CLEAR_SELECTION
2114 | AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07002115 | AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07002116 | AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
2117 | AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002118 | AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT
Svetoslav Ganova1dc7612012-05-10 04:14:53 -07002119 | AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT
2120 | AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
2121 | AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002122
2123 private static final int RETRIEVAL_ALLOWING_EVENT_TYPES =
Svetoslav Ganov42138042012-03-20 11:51:39 -07002124 AccessibilityEvent.TYPE_VIEW_CLICKED
2125 | AccessibilityEvent.TYPE_VIEW_FOCUSED
2126 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
2127 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
2128 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
2129 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
2130 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
2131 | AccessibilityEvent.TYPE_VIEW_SELECTED
Svetoslav Ganova0156172011-06-26 17:55:44 -07002132 | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED
2133 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07002134 | AccessibilityEvent.TYPE_VIEW_SCROLLED
2135 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
2136 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002137
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002138 private int mActiveWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002139
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002140 private boolean canDispatchAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov58fd9f82012-10-03 18:10:27 -07002141 final int eventType = event.getEventType();
2142 switch (eventType) {
2143 // All events that are for changes in a global window
2144 // state should *always* be dispatched.
2145 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
2146 case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
2147 // All events generated by the user touching the
2148 // screen should *always* be dispatched.
2149 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
2150 case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
2151 case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
2152 case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
2153 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
2154 case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
2155 // These will change the active window, so dispatch.
2156 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
2157 case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT: {
2158 return true;
2159 }
2160 // All events for changes in window content should be
2161 // dispatched *only* if this window is the active one.
2162 default:
2163 return event.getWindowId() == mActiveWindowId;
2164 }
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07002165 }
2166
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002167 public void updateEventSourceLocked(AccessibilityEvent event) {
2168 if ((event.getEventType() & RETRIEVAL_ALLOWING_EVENT_TYPES) == 0) {
2169 event.setSource(null);
2170 }
2171 }
2172
2173 public void updateActiveWindow(int windowId, int eventType) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002174 // The active window is either the window that has input focus or
2175 // the window that the user is currently touching. If the user is
2176 // touching a window that does not have input focus as soon as the
2177 // the user stops touching that window the focused window becomes
2178 // the active one.
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002179 switch (eventType) {
2180 case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: {
2181 if (getFocusedWindowId() == windowId) {
2182 mActiveWindowId = windowId;
2183 }
2184 } break;
Svetoslav Ganovf772cba2012-10-05 18:49:17 -07002185 case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER: {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002186 mActiveWindowId = windowId;
2187 } break;
Svetoslav Ganov4e2a7622011-07-26 20:08:46 -07002188 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002189 }
2190
Svetoslav Ganovf772cba2012-10-05 18:49:17 -07002191 public void onTouchInteractionEnd() {
2192 // We want to set the active window to be current immediately
2193 // after the user has stopped touching the screen since if the
2194 // user types with the IME he should get a feedback for the
2195 // letter typed in the text view which is in the input focused
2196 // window. Note that we always deliver hover accessibility events
2197 // (they are a result of user touching the screen) so change of
2198 // the active window before all hover accessibility events from
2199 // the touched window are delivered is fine.
2200 mActiveWindowId = getFocusedWindowId();
2201 }
2202
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002203 public int getRetrievalAllowingWindowLocked() {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002204 return mActiveWindowId;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002205 }
2206
2207 public boolean canGetAccessibilityNodeInfoLocked(Service service, int windowId) {
2208 return canRetrieveWindowContent(service) && isRetrievalAllowingWindow(windowId);
2209 }
2210
Svetoslav Ganovb7ff3252012-04-24 18:40:07 -07002211 public boolean canPerformActionLocked(Service service, int windowId, int action,
2212 Bundle arguments) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002213 return canRetrieveWindowContent(service)
2214 && isRetrievalAllowingWindow(windowId)
Svetoslav Ganovafe8cf22012-04-28 13:26:57 -07002215 && isActionPermitted(action);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002216 }
2217
2218 public boolean canRetrieveWindowContent(Service service) {
2219 return service.mCanRetrieveScreenContent;
2220 }
2221
Svetoslav Ganov4a49d9f2011-07-17 12:22:08 -07002222 public void enforceCanRetrieveWindowContent(Service service) throws RemoteException {
2223 // This happens due to incorrect registration so make it apparent.
2224 if (!canRetrieveWindowContent(service)) {
2225 Slog.e(LOG_TAG, "Accessibility serivce " + service.mComponentName + " does not " +
2226 "declare android:canRetrieveWindowContent.");
2227 throw new RemoteException();
2228 }
2229 }
2230
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002231 public int resolveCallingUserIdEnforcingPermissionsLocked(int userId) {
2232 final int callingUid = Binder.getCallingUid();
2233 if (callingUid == Process.SYSTEM_UID
2234 || callingUid == Process.SHELL_UID) {
2235 return mCurrentUserId;
2236 }
2237 final int callingUserId = UserHandle.getUserId(callingUid);
2238 if (callingUserId == userId) {
2239 return userId;
2240 }
2241 if (!hasPermission(Manifest.permission.INTERACT_ACROSS_USERS)
2242 && !hasPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL)) {
2243 throw new SecurityException("Call from user " + callingUserId + " as user "
2244 + userId + " without permission INTERACT_ACROSS_USERS or "
2245 + "INTERACT_ACROSS_USERS_FULL not allowed.");
2246 }
2247 if (userId == UserHandle.USER_CURRENT
2248 || userId == UserHandle.USER_CURRENT_OR_SELF) {
2249 return mCurrentUserId;
2250 }
2251 throw new IllegalArgumentException("Calling user can be changed to only "
2252 + "UserHandle.USER_CURRENT or UserHandle.USER_CURRENT_OR_SELF.");
2253 }
2254
2255 public boolean isCallerInteractingAcrossUsers(int userId) {
2256 final int callingUid = Binder.getCallingUid();
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002257 return (Binder.getCallingPid() == android.os.Process.myPid()
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002258 || callingUid == Process.SHELL_UID
2259 || userId == UserHandle.USER_CURRENT
2260 || userId == UserHandle.USER_CURRENT_OR_SELF);
2261 }
2262
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002263 private boolean isRetrievalAllowingWindow(int windowId) {
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002264 return (mActiveWindowId == windowId);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002265 }
2266
2267 private boolean isActionPermitted(int action) {
2268 return (VALID_ACTIONS & action) != 0;
2269 }
2270
2271 private void enforceCallingPermission(String permission, String function) {
2272 if (OWN_PROCESS_ID == Binder.getCallingPid()) {
2273 return;
2274 }
Svetoslav Ganov9371a0a2012-09-21 18:20:37 -07002275 if (!hasPermission(permission)) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002276 throw new SecurityException("You do not have " + permission
2277 + " required to call " + function);
2278 }
2279 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002280
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002281 private boolean hasPermission(String permission) {
2282 return mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED;
2283 }
2284
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002285 private int getFocusedWindowId() {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002286 try {
2287 // We call this only on window focus change or after touch
2288 // exploration gesture end and the shown windows are not that
2289 // many, so the linear look up is just fine.
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002290 IBinder token = mWindowManagerService.getFocusedWindowToken();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002291 if (token != null) {
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002292 synchronized (mLock) {
2293 int windowId = getFocusedWindowIdLocked(token, mGlobalWindowTokens);
2294 if (windowId < 0) {
2295 windowId = getFocusedWindowIdLocked(token,
2296 getCurrentUserStateLocked().mWindowTokens);
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002297 }
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002298 return windowId;
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002299 }
2300 }
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002301 } catch (RemoteException re) {
2302 /* ignore */
Svetoslav Ganova8afa692012-09-25 13:03:18 -07002303 }
2304 return -1;
2305 }
2306
2307 private int getFocusedWindowIdLocked(IBinder token, SparseArray<IBinder> windows) {
2308 final int windowCount = windows.size();
2309 for (int i = 0; i < windowCount; i++) {
2310 if (windows.valueAt(i) == token) {
2311 return windows.keyAt(i);
2312 }
Svetoslav Ganove15ccb92012-05-16 15:48:55 -07002313 }
2314 return -1;
2315 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07002316 }
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002317
2318 private class UserState {
2319 public final int mUserId;
2320
2321 public final CopyOnWriteArrayList<Service> mServices = new CopyOnWriteArrayList<Service>();
2322
2323 public final RemoteCallbackList<IAccessibilityManagerClient> mClients =
2324 new RemoteCallbackList<IAccessibilityManagerClient>();
2325
2326 public final Map<ComponentName, Service> mComponentNameToServiceMap =
2327 new HashMap<ComponentName, Service>();
2328
2329 public final List<AccessibilityServiceInfo> mInstalledServices =
2330 new ArrayList<AccessibilityServiceInfo>();
2331
2332 public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>();
2333
2334 public final Set<ComponentName> mTouchExplorationGrantedServices =
2335 new HashSet<ComponentName>();
2336
2337 public final SparseArray<AccessibilityConnectionWrapper>
2338 mInteractionConnections =
2339 new SparseArray<AccessibilityConnectionWrapper>();
2340
2341 public final SparseArray<IBinder> mWindowTokens = new SparseArray<IBinder>();
2342
2343 public int mHandledFeedbackTypes = 0;
2344
2345 public boolean mIsAccessibilityEnabled;
2346 public boolean mIsTouchExplorationEnabled;
2347 public boolean mIsDisplayMagnificationEnabled;
2348
2349 public UserState(int userId) {
2350 mUserId = userId;
2351 }
2352 }
2353
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07002354 private class TempUserStateChangeMemento {
2355 public int mUserId = UserHandle.USER_NULL;
2356 public boolean mIsAccessibilityEnabled;
2357 public boolean mIsTouchExplorationEnabled;
2358 public boolean mIsDisplayMagnificationEnabled;
2359 public final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>();
2360 public final Set<ComponentName> mTouchExplorationGrantedServices =
2361 new HashSet<ComponentName>();
2362
2363 public void initialize(int userId, UserState userState) {
2364 mUserId = userId;
2365 mIsAccessibilityEnabled = userState.mIsAccessibilityEnabled;
2366 mIsTouchExplorationEnabled = userState.mIsTouchExplorationEnabled;
2367 mIsDisplayMagnificationEnabled = userState.mIsDisplayMagnificationEnabled;
2368 mEnabledServices.clear();
2369 mEnabledServices.addAll(userState.mEnabledServices);
2370 mTouchExplorationGrantedServices.clear();
2371 mTouchExplorationGrantedServices.addAll(userState.mTouchExplorationGrantedServices);
2372 }
2373
2374 public void applyTo(UserState userState) {
2375 userState.mIsAccessibilityEnabled = mIsAccessibilityEnabled;
2376 userState.mIsTouchExplorationEnabled = mIsTouchExplorationEnabled;
2377 userState.mIsDisplayMagnificationEnabled = mIsDisplayMagnificationEnabled;
2378 userState.mEnabledServices.clear();
2379 userState.mEnabledServices.addAll(mEnabledServices);
2380 userState.mTouchExplorationGrantedServices.clear();
2381 userState.mTouchExplorationGrantedServices.addAll(mTouchExplorationGrantedServices);
2382 }
2383
2384 public void clear() {
2385 mUserId = UserHandle.USER_NULL;
2386 mIsAccessibilityEnabled = false;
2387 mIsTouchExplorationEnabled = false;
2388 mIsDisplayMagnificationEnabled = false;
2389 mEnabledServices.clear();
2390 mTouchExplorationGrantedServices.clear();
2391 }
2392 }
2393
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002394 private final class AccessibilityContentObserver extends ContentObserver {
2395
2396 private final Uri mAccessibilityEnabledUri = Settings.Secure.getUriFor(
2397 Settings.Secure.ACCESSIBILITY_ENABLED);
2398
2399 private final Uri mTouchExplorationEnabledUri = Settings.Secure.getUriFor(
2400 Settings.Secure.TOUCH_EXPLORATION_ENABLED);
2401
2402 private final Uri mDisplayMagnificationEnabledUri = Settings.Secure.getUriFor(
2403 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED);
2404
2405 private final Uri mEnabledAccessibilityServicesUri = Settings.Secure.getUriFor(
2406 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
2407
2408 private final Uri mTouchExplorationGrantedAccessibilityServicesUri = Settings.Secure
2409 .getUriFor(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES);
2410
2411 public AccessibilityContentObserver(Handler handler) {
2412 super(handler);
2413 }
2414
2415 public void register(ContentResolver contentResolver) {
2416 contentResolver.registerContentObserver(mAccessibilityEnabledUri,
2417 false, this, UserHandle.USER_ALL);
2418 contentResolver.registerContentObserver(mTouchExplorationEnabledUri,
2419 false, this, UserHandle.USER_ALL);
2420 contentResolver.registerContentObserver(mDisplayMagnificationEnabledUri,
2421 false, this, UserHandle.USER_ALL);
2422 contentResolver.registerContentObserver(mEnabledAccessibilityServicesUri,
2423 false, this, UserHandle.USER_ALL);
2424 contentResolver.registerContentObserver(
2425 mTouchExplorationGrantedAccessibilityServicesUri,
2426 false, this, UserHandle.USER_ALL);
2427 }
2428
2429 @Override
2430 public void onChange(boolean selfChange, Uri uri) {
2431 if (mAccessibilityEnabledUri.equals(uri)) {
2432 synchronized (mLock) {
2433 // We will update when the automation service dies.
2434 if (mUiAutomationService == null) {
2435 UserState userState = getCurrentUserStateLocked();
2436 handleAccessibilityEnabledSettingChangedLocked(userState);
Svetoslav Ganov7befb7d2012-09-27 16:49:23 -07002437 performServiceManagementLocked(userState);
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07002438 updateInputFilterLocked(userState);
2439 scheduleSendStateToClientsLocked(userState);
2440 }
2441 }
2442 } else if (mTouchExplorationEnabledUri.equals(uri)) {
2443 synchronized (mLock) {
2444 // We will update when the automation service dies.
2445 if (mUiAutomationService == null) {
2446 UserState userState = getCurrentUserStateLocked();
2447 handleTouchExplorationEnabledSettingChangedLocked(userState);
2448 updateInputFilterLocked(userState);
2449 scheduleSendStateToClientsLocked(userState);
2450 }
2451 }
2452 } else if (mDisplayMagnificationEnabledUri.equals(uri)) {
2453 synchronized (mLock) {
2454 // We will update when the automation service dies.
2455 if (mUiAutomationService == null) {
2456 UserState userState = getCurrentUserStateLocked();
2457 handleDisplayMagnificationEnabledSettingChangedLocked(userState);
2458 updateInputFilterLocked(userState);
2459 scheduleSendStateToClientsLocked(userState);
2460 }
2461 }
2462 } else if (mEnabledAccessibilityServicesUri.equals(uri)) {
2463 synchronized (mLock) {
2464 // We will update when the automation service dies.
2465 if (mUiAutomationService == null) {
2466 UserState userState = getCurrentUserStateLocked();
2467 populateEnabledAccessibilityServicesLocked(userState);
2468 manageServicesLocked(userState);
2469 }
2470 }
2471 } else if (mTouchExplorationGrantedAccessibilityServicesUri.equals(uri)) {
2472 synchronized (mLock) {
2473 // We will update when the automation service dies.
2474 if (mUiAutomationService == null) {
2475 UserState userState = getCurrentUserStateLocked();
2476 populateTouchExplorationGrantedAccessibilityServicesLocked(userState);
2477 handleTouchExplorationGrantedAccessibilityServicesChangedLocked(userState);
2478 }
2479 }
2480 }
2481 }
2482 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002483}