| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 20 | import com.android.internal.os.HandlerCaller; |
| 21 | import com.android.internal.os.HandlerCaller.SomeArgs; |
| 22 | |
| 23 | import android.accessibilityservice.AccessibilityService; |
| 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 25 | import android.accessibilityservice.IAccessibilityServiceConnection; |
| 26 | import android.accessibilityservice.IEventListener; |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 27 | import android.app.PendingIntent; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.ComponentName; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
| 34 | import android.content.ServiceConnection; |
| 35 | import android.content.pm.PackageManager; |
| 36 | import android.content.pm.ResolveInfo; |
| 37 | import android.content.pm.ServiceInfo; |
| 38 | import android.database.ContentObserver; |
| 39 | import android.net.Uri; |
| 40 | import android.os.Binder; |
| 41 | import android.os.DeadObjectException; |
| 42 | import android.os.Handler; |
| 43 | import android.os.IBinder; |
| 44 | import android.os.Message; |
| 45 | import android.os.RemoteException; |
| 46 | import android.provider.Settings; |
| 47 | import android.text.TextUtils; |
| 48 | import android.text.TextUtils.SimpleStringSplitter; |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 49 | import android.util.Config; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 50 | import android.util.Slog; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 51 | import android.util.SparseArray; |
| 52 | import android.view.accessibility.AccessibilityEvent; |
| 53 | import android.view.accessibility.IAccessibilityManager; |
| 54 | import android.view.accessibility.IAccessibilityManagerClient; |
| 55 | |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Arrays; |
| 58 | import java.util.HashMap; |
| 59 | import java.util.HashSet; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 60 | import java.util.Iterator; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import java.util.List; |
| 62 | import java.util.Map; |
| 63 | import java.util.Set; |
| 64 | |
| 65 | /** |
| 66 | * This class is instantiated by the system as a system level service and can be |
| 67 | * accessed only by the system. The task of this service is to be a centralized |
| 68 | * event dispatch for {@link AccessibilityEvent}s generated across all processes |
| 69 | * on the device. Events are dispatched to {@link AccessibilityService}s. |
| 70 | * |
| 71 | * @hide |
| 72 | */ |
| 73 | public class AccessibilityManagerService extends IAccessibilityManager.Stub |
| 74 | implements HandlerCaller.Callback { |
| 75 | |
| 76 | private static final String LOG_TAG = "AccessibilityManagerService"; |
| 77 | |
| 78 | private static int sIdCounter = 0; |
| 79 | |
| 80 | private static final int OWN_PROCESS_ID = android.os.Process.myPid(); |
| 81 | |
| 82 | private static final int DO_SET_SERVICE_INFO = 10; |
| 83 | |
| 84 | final HandlerCaller mCaller; |
| 85 | |
| 86 | final Context mContext; |
| 87 | |
| 88 | final Object mLock = new Object(); |
| 89 | |
| 90 | final List<Service> mServices = new ArrayList<Service>(); |
| 91 | |
| 92 | final List<IAccessibilityManagerClient> mClients = |
| 93 | new ArrayList<IAccessibilityManagerClient>(); |
| 94 | |
| 95 | final Map<ComponentName, Service> mComponentNameToServiceMap = |
| 96 | new HashMap<ComponentName, Service>(); |
| 97 | |
| 98 | private final List<ServiceInfo> mInstalledServices = new ArrayList<ServiceInfo>(); |
| 99 | |
| 100 | private final Set<ComponentName> mEnabledServices = new HashSet<ComponentName>(); |
| 101 | |
| 102 | private final SimpleStringSplitter mStringColonSplitter = new SimpleStringSplitter(':'); |
| 103 | |
| 104 | private PackageManager mPackageManager; |
| 105 | |
| 106 | private int mHandledFeedbackTypes = 0; |
| 107 | |
| 108 | private boolean mIsEnabled; |
| 109 | |
| 110 | /** |
| 111 | * Handler for delayed event dispatch. |
| 112 | */ |
| 113 | private Handler mHandler = new Handler() { |
| 114 | |
| 115 | @Override |
| 116 | public void handleMessage(Message message) { |
| 117 | Service service = (Service) message.obj; |
| 118 | int eventType = message.arg1; |
| 119 | |
| 120 | synchronized (mLock) { |
| 121 | notifyEventListenerLocked(service, eventType); |
| 122 | AccessibilityEvent oldEvent = service.mPendingEvents.get(eventType); |
| 123 | service.mPendingEvents.remove(eventType); |
| 124 | tryRecycleLocked(oldEvent); |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | /** |
| 130 | * Creates a new instance. |
| 131 | * |
| 132 | * @param context A {@link Context} instance. |
| 133 | */ |
| 134 | AccessibilityManagerService(Context context) { |
| 135 | mContext = context; |
| 136 | mPackageManager = mContext.getPackageManager(); |
| 137 | mCaller = new HandlerCaller(context, this); |
| 138 | |
| 139 | registerPackageChangeAndBootCompletedBroadcastReceiver(); |
| 140 | registerSettingsContentObservers(); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Registers a {@link BroadcastReceiver} for the events of |
| 145 | * adding/changing/removing/restarting a package and boot completion. |
| 146 | */ |
| 147 | private void registerPackageChangeAndBootCompletedBroadcastReceiver() { |
| 148 | Context context = mContext; |
| 149 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 150 | PackageMonitor monitor = new PackageMonitor() { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 151 | @Override |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 152 | public void onSomePackagesChanged() { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 153 | synchronized (mLock) { |
| 154 | populateAccessibilityServiceListLocked(); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 155 | manageServicesLocked(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public boolean onHandleForceStop(Intent intent, String[] packages, |
| 161 | int uid, boolean doit) { |
| 162 | synchronized (mLock) { |
| 163 | boolean changed = false; |
| 164 | Iterator<ComponentName> it = mEnabledServices.iterator(); |
| 165 | while (it.hasNext()) { |
| 166 | ComponentName comp = it.next(); |
| 167 | String compPkg = comp.getPackageName(); |
| 168 | for (String pkg : packages) { |
| 169 | if (compPkg.equals(pkg)) { |
| 170 | if (!doit) { |
| 171 | return true; |
| 172 | } |
| 173 | it.remove(); |
| 174 | changed = true; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | if (changed) { |
| 179 | it = mEnabledServices.iterator(); |
| 180 | StringBuilder str = new StringBuilder(); |
| 181 | while (it.hasNext()) { |
| 182 | if (str.length() > 0) { |
| 183 | str.append(':'); |
| 184 | } |
| 185 | str.append(it.next().flattenToShortString()); |
| 186 | } |
| 187 | Settings.Secure.putString(mContext.getContentResolver(), |
| 188 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, |
| 189 | str.toString()); |
| 190 | manageServicesLocked(); |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onReceive(Context context, Intent intent) { |
| 198 | if (intent.getAction() == Intent.ACTION_BOOT_COMPLETED) { |
| 199 | synchronized (mLock) { |
| 200 | populateAccessibilityServiceListLocked(); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 201 | |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 202 | // get the accessibility enabled setting on boot |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 203 | mIsEnabled = Settings.Secure.getInt(mContext.getContentResolver(), |
| 204 | Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1; |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 205 | |
| 206 | // if accessibility is enabled inform our clients we are on |
| 207 | if (mIsEnabled) { |
| 208 | updateClientsLocked(); |
| 209 | } |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 210 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 211 | manageServicesLocked(); |
| 212 | } |
| 213 | |
| 214 | return; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 215 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 216 | |
| 217 | super.onReceive(context, intent); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 218 | } |
| 219 | }; |
| 220 | |
| 221 | // package changes |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 222 | monitor.register(context, true); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 223 | |
| 224 | // boot completed |
| 225 | IntentFilter bootFiler = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 226 | mContext.registerReceiver(monitor, bootFiler); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * {@link ContentObserver}s for {@link Settings.Secure#ACCESSIBILITY_ENABLED} |
| 231 | * and {@link Settings.Secure#ENABLED_ACCESSIBILITY_SERVICES} settings. |
| 232 | */ |
| 233 | private void registerSettingsContentObservers() { |
| 234 | ContentResolver contentResolver = mContext.getContentResolver(); |
| 235 | |
| 236 | Uri enabledUri = Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_ENABLED); |
| 237 | contentResolver.registerContentObserver(enabledUri, false, |
| 238 | new ContentObserver(new Handler()) { |
| 239 | @Override |
| 240 | public void onChange(boolean selfChange) { |
| 241 | super.onChange(selfChange); |
| 242 | |
| 243 | mIsEnabled = Settings.Secure.getInt(mContext.getContentResolver(), |
| 244 | Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1; |
| 245 | |
| 246 | synchronized (mLock) { |
| 247 | if (mIsEnabled) { |
| 248 | manageServicesLocked(); |
| 249 | } else { |
| 250 | unbindAllServicesLocked(); |
| 251 | } |
| 252 | updateClientsLocked(); |
| 253 | } |
| 254 | } |
| 255 | }); |
| 256 | |
| 257 | Uri providersUri = |
| 258 | Settings.Secure.getUriFor(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); |
| 259 | contentResolver.registerContentObserver(providersUri, false, |
| 260 | new ContentObserver(new Handler()) { |
| 261 | @Override |
| 262 | public void onChange(boolean selfChange) { |
| 263 | super.onChange(selfChange); |
| 264 | |
| 265 | synchronized (mLock) { |
| 266 | manageServicesLocked(); |
| 267 | } |
| 268 | } |
| 269 | }); |
| 270 | } |
| 271 | |
| Svetoslav Ganov | dd64a9b | 2010-04-13 18:41:17 -0700 | [diff] [blame] | 272 | public boolean addClient(IAccessibilityManagerClient client) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 273 | synchronized (mLock) { |
| Svetoslav Ganov | dd64a9b | 2010-04-13 18:41:17 -0700 | [diff] [blame] | 274 | mClients.add(client); |
| 275 | return mIsEnabled; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | public boolean sendAccessibilityEvent(AccessibilityEvent event) { |
| 280 | synchronized (mLock) { |
| 281 | notifyAccessibilityServicesDelayedLocked(event, false); |
| 282 | notifyAccessibilityServicesDelayedLocked(event, true); |
| 283 | } |
| 284 | // event not scheduled for dispatch => recycle |
| 285 | if (mHandledFeedbackTypes == 0) { |
| 286 | event.recycle(); |
| 287 | } else { |
| 288 | mHandledFeedbackTypes = 0; |
| 289 | } |
| 290 | |
| 291 | return (OWN_PROCESS_ID != Binder.getCallingPid()); |
| 292 | } |
| 293 | |
| 294 | public List<ServiceInfo> getAccessibilityServiceList() { |
| 295 | synchronized (mLock) { |
| 296 | return mInstalledServices; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public void interrupt() { |
| 301 | synchronized (mLock) { |
| 302 | for (int i = 0, count = mServices.size(); i < count; i++) { |
| 303 | Service service = mServices.get(i); |
| 304 | try { |
| 305 | service.mServiceInterface.onInterrupt(); |
| 306 | } catch (RemoteException re) { |
| 307 | if (re instanceof DeadObjectException) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 308 | Slog.w(LOG_TAG, "Dead " + service.mService + ". Cleaning up."); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 309 | if (removeDeadServiceLocked(service)) { |
| 310 | count--; |
| 311 | i--; |
| 312 | } |
| 313 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 314 | Slog.e(LOG_TAG, "Error during sending interrupt request to " |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 315 | + service.mService, re); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public void executeMessage(Message message) { |
| 323 | switch (message.what) { |
| 324 | case DO_SET_SERVICE_INFO: |
| 325 | SomeArgs arguments = ((SomeArgs) message.obj); |
| 326 | |
| 327 | AccessibilityServiceInfo info = (AccessibilityServiceInfo) arguments.arg1; |
| 328 | Service service = (Service) arguments.arg2; |
| 329 | |
| 330 | synchronized (mLock) { |
| 331 | service.mEventTypes = info.eventTypes; |
| 332 | service.mFeedbackType = info.feedbackType; |
| 333 | String[] packageNames = info.packageNames; |
| 334 | if (packageNames != null) { |
| 335 | service.mPackageNames.addAll(Arrays.asList(packageNames)); |
| 336 | } |
| 337 | service.mNotificationTimeout = info.notificationTimeout; |
| 338 | service.mIsDefault = (info.flags & AccessibilityServiceInfo.DEFAULT) != 0; |
| 339 | } |
| 340 | return; |
| 341 | default: |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 342 | Slog.w(LOG_TAG, "Unknown message type: " + message.what); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Populates the cached list of installed {@link AccessibilityService}s. |
| 348 | */ |
| 349 | private void populateAccessibilityServiceListLocked() { |
| 350 | mInstalledServices.clear(); |
| 351 | |
| 352 | List<ResolveInfo> installedServices = mPackageManager.queryIntentServices( |
| 353 | new Intent(AccessibilityService.SERVICE_INTERFACE), PackageManager.GET_SERVICES); |
| 354 | |
| 355 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 356 | mInstalledServices.add(installedServices.get(i).serviceInfo); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Performs {@link AccessibilityService}s delayed notification. The delay is configurable |
| 362 | * and denotes the period after the last event before notifying the service. |
| 363 | * |
| 364 | * @param event The event. |
| 365 | * @param isDefault True to notify default listeners, not default services. |
| 366 | */ |
| 367 | private void notifyAccessibilityServicesDelayedLocked(AccessibilityEvent event, |
| 368 | boolean isDefault) { |
| Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 369 | try { |
| 370 | for (int i = 0, count = mServices.size(); i < count; i++) { |
| 371 | Service service = mServices.get(i); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 372 | |
| Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 373 | if (service.mIsDefault == isDefault) { |
| 374 | if (canDispathEventLocked(service, event, mHandledFeedbackTypes)) { |
| 375 | mHandledFeedbackTypes |= service.mFeedbackType; |
| 376 | notifyAccessibilityServiceDelayedLocked(service, event); |
| 377 | } |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| Charles Chen | 85b598b | 2009-07-29 17:23:50 -0700 | [diff] [blame] | 380 | } catch (IndexOutOfBoundsException oobe) { |
| 381 | // An out of bounds exception can happen if services are going away |
| 382 | // as the for loop is running. If that happens, just bail because |
| 383 | // there are no more services to notify. |
| 384 | return; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Performs an {@link AccessibilityService} delayed notification. The delay is configurable |
| 390 | * and denotes the period after the last event before notifying the service. |
| 391 | * |
| 392 | * @param service The service. |
| 393 | * @param event The event. |
| 394 | */ |
| 395 | private void notifyAccessibilityServiceDelayedLocked(Service service, |
| 396 | AccessibilityEvent event) { |
| 397 | synchronized (mLock) { |
| 398 | int eventType = event.getEventType(); |
| 399 | AccessibilityEvent oldEvent = service.mPendingEvents.get(eventType); |
| 400 | service.mPendingEvents.put(eventType, event); |
| 401 | |
| 402 | int what = eventType | (service.mId << 16); |
| 403 | if (oldEvent != null) { |
| 404 | mHandler.removeMessages(what); |
| 405 | tryRecycleLocked(oldEvent); |
| 406 | } |
| 407 | |
| 408 | Message message = mHandler.obtainMessage(what, service); |
| 409 | message.arg1 = event.getEventType(); |
| 410 | mHandler.sendMessageDelayed(message, service.mNotificationTimeout); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Recycles an event if it can be safely recycled. The condition is that no |
| 416 | * not notified service is interested in the event. |
| 417 | * |
| 418 | * @param event The event. |
| 419 | */ |
| 420 | private void tryRecycleLocked(AccessibilityEvent event) { |
| Charles Chen | bbc1934 | 2009-07-24 16:06:09 -0700 | [diff] [blame] | 421 | if (event == null) { |
| 422 | return; |
| 423 | } |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 424 | int eventType = event.getEventType(); |
| 425 | List<Service> services = mServices; |
| 426 | |
| 427 | // linear in the number of service which is not large |
| 428 | for (int i = 0, count = services.size(); i < count; i++) { |
| 429 | Service service = services.get(i); |
| 430 | if (service.mPendingEvents.get(eventType) == event) { |
| 431 | return; |
| 432 | } |
| 433 | } |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 434 | event.recycle(); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Notifies a service for a scheduled event given the event type. |
| 439 | * |
| 440 | * @param service The service. |
| 441 | * @param eventType The type of the event to dispatch. |
| 442 | */ |
| 443 | private void notifyEventListenerLocked(Service service, int eventType) { |
| 444 | IEventListener listener = service.mServiceInterface; |
| 445 | AccessibilityEvent event = service.mPendingEvents.get(eventType); |
| 446 | |
| 447 | try { |
| 448 | listener.onAccessibilityEvent(event); |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 449 | if (Config.DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 450 | Slog.i(LOG_TAG, "Event " + event + " sent to " + listener); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 451 | } |
| 452 | } catch (RemoteException re) { |
| 453 | if (re instanceof DeadObjectException) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 454 | Slog.w(LOG_TAG, "Dead " + service.mService + ". Cleaning up."); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 455 | synchronized (mLock) { |
| 456 | removeDeadServiceLocked(service); |
| 457 | } |
| 458 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 459 | Slog.e(LOG_TAG, "Error during sending " + event + " to " + service.mService, re); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Removes a dead service. |
| 466 | * |
| 467 | * @param service The service. |
| 468 | * @return True if the service was removed, false otherwise. |
| 469 | */ |
| 470 | private boolean removeDeadServiceLocked(Service service) { |
| 471 | mServices.remove(service); |
| 472 | mHandler.removeMessages(service.mId); |
| 473 | |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 474 | if (Config.DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 475 | Slog.i(LOG_TAG, "Dead service " + service.mService + " removed"); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if (mServices.isEmpty()) { |
| 479 | mIsEnabled = false; |
| 480 | updateClientsLocked(); |
| 481 | } |
| 482 | |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Determines if given event can be dispatched to a service based on the package of the |
| 488 | * event source and already notified services for that event type. Specifically, a |
| 489 | * service is notified if it is interested in events from the package and no other service |
| 490 | * providing the same feedback type has been notified. Exception are services the |
| 491 | * provide generic feedback (feedback type left as a safety net for unforeseen feedback |
| 492 | * types) which are always notified. |
| 493 | * |
| 494 | * @param service The potential receiver. |
| 495 | * @param event The event. |
| 496 | * @param handledFeedbackTypes The feedback types for which services have been notified. |
| 497 | * @return True if the listener should be notified, false otherwise. |
| 498 | */ |
| 499 | private boolean canDispathEventLocked(Service service, AccessibilityEvent event, |
| 500 | int handledFeedbackTypes) { |
| 501 | |
| 502 | if (!service.isConfigured()) { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | if (!service.mService.isBinderAlive()) { |
| 507 | removeDeadServiceLocked(service); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | int eventType = event.getEventType(); |
| 512 | if ((service.mEventTypes & eventType) != eventType) { |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | Set<String> packageNames = service.mPackageNames; |
| 517 | CharSequence packageName = event.getPackageName(); |
| 518 | |
| 519 | if (packageNames.isEmpty() || packageNames.contains(packageName)) { |
| 520 | int feedbackType = service.mFeedbackType; |
| 521 | if ((handledFeedbackTypes & feedbackType) != feedbackType |
| 522 | || feedbackType == AccessibilityServiceInfo.FEEDBACK_GENERIC) { |
| 523 | return true; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Manages services by starting enabled ones and stopping disabled ones. |
| 532 | */ |
| 533 | private void manageServicesLocked() { |
| 534 | populateEnabledServicesLocked(mEnabledServices); |
| 535 | updateServicesStateLocked(mInstalledServices, mEnabledServices); |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Unbinds all bound services. |
| 540 | */ |
| 541 | private void unbindAllServicesLocked() { |
| 542 | List<Service> services = mServices; |
| 543 | |
| 544 | for (int i = 0, count = services.size(); i < count; i++) { |
| 545 | Service service = services.get(i); |
| 546 | |
| 547 | service.unbind(); |
| 548 | mComponentNameToServiceMap.remove(service.mComponentName); |
| 549 | } |
| 550 | services.clear(); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Populates a list with the {@link ComponentName}s of all enabled |
| 555 | * {@link AccessibilityService}s. |
| 556 | * |
| 557 | * @param enabledServices The list. |
| 558 | */ |
| 559 | private void populateEnabledServicesLocked(Set<ComponentName> enabledServices) { |
| 560 | enabledServices.clear(); |
| 561 | |
| 562 | String servicesValue = Settings.Secure.getString(mContext.getContentResolver(), |
| 563 | Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); |
| 564 | |
| 565 | if (servicesValue != null) { |
| 566 | TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 567 | splitter.setString(servicesValue); |
| 568 | while (splitter.hasNext()) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 569 | String str = splitter.next(); |
| 570 | if (str == null || str.length() <= 0) { |
| 571 | continue; |
| 572 | } |
| 573 | ComponentName enabledService = ComponentName.unflattenFromString(str); |
| 574 | if (enabledService != null) { |
| 575 | enabledServices.add(enabledService); |
| 576 | } |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Updates the state of each service by starting (or keeping running) enabled ones and |
| 583 | * stopping the rest. |
| 584 | * |
| 585 | * @param installedServices All installed {@link AccessibilityService}s. |
| 586 | * @param enabledServices The {@link ComponentName}s of the enabled services. |
| 587 | */ |
| 588 | private void updateServicesStateLocked(List<ServiceInfo> installedServices, |
| 589 | Set<ComponentName> enabledServices) { |
| 590 | |
| 591 | Map<ComponentName, Service> componentNameToServiceMap = mComponentNameToServiceMap; |
| 592 | List<Service> services = mServices; |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 593 | boolean isEnabled = mIsEnabled; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 594 | |
| 595 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 596 | ServiceInfo intalledService = installedServices.get(i); |
| 597 | ComponentName componentName = new ComponentName(intalledService.packageName, |
| 598 | intalledService.name); |
| 599 | Service service = componentNameToServiceMap.get(componentName); |
| 600 | |
| Svetoslav Ganov | 714cff0 | 2010-02-17 19:36:28 -0800 | [diff] [blame] | 601 | if (isEnabled && enabledServices.contains(componentName)) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 602 | if (service == null) { |
| 603 | new Service(componentName).bind(); |
| 604 | } |
| 605 | } else { |
| 606 | if (service != null) { |
| 607 | service.unbind(); |
| 608 | componentNameToServiceMap.remove(componentName); |
| 609 | services.remove(service); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Updates the state of {@link android.view.accessibility.AccessibilityManager} clients. |
| 617 | */ |
| 618 | private void updateClientsLocked() { |
| 619 | for (int i = 0, count = mClients.size(); i < count; i++) { |
| 620 | try { |
| 621 | mClients.get(i).setEnabled(mIsEnabled); |
| 622 | } catch (RemoteException re) { |
| 623 | mClients.remove(i); |
| 624 | count--; |
| Svetoslav Ganov | fb606da | 2010-02-18 10:54:36 -0800 | [diff] [blame] | 625 | i--; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * This class represents an accessibility service. It stores all per service |
| 632 | * data required for the service management, provides API for starting/stopping the |
| 633 | * service and is responsible for adding/removing the service in the data structures |
| 634 | * for service management. The class also exposes configuration interface that is |
| 635 | * passed to the service it represents as soon it is bound. It also serves as the |
| 636 | * connection for the service. |
| 637 | */ |
| 638 | class Service extends IAccessibilityServiceConnection.Stub implements ServiceConnection { |
| 639 | int mId = 0; |
| 640 | |
| 641 | IBinder mService; |
| 642 | |
| 643 | IEventListener mServiceInterface; |
| 644 | |
| 645 | int mEventTypes; |
| 646 | |
| 647 | int mFeedbackType; |
| 648 | |
| 649 | Set<String> mPackageNames = new HashSet<String>(); |
| 650 | |
| 651 | boolean mIsDefault; |
| 652 | |
| 653 | long mNotificationTimeout; |
| 654 | |
| 655 | boolean mIsActive; |
| 656 | |
| 657 | ComponentName mComponentName; |
| 658 | |
| 659 | Intent mIntent; |
| 660 | |
| 661 | // the events pending events to be dispatched to this service |
| 662 | final SparseArray<AccessibilityEvent> mPendingEvents = |
| 663 | new SparseArray<AccessibilityEvent>(); |
| 664 | |
| 665 | Service(ComponentName componentName) { |
| 666 | mId = sIdCounter++; |
| 667 | mComponentName = componentName; |
| 668 | mIntent = new Intent().setComponent(mComponentName); |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 669 | mIntent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 670 | com.android.internal.R.string.accessibility_binding_label); |
| 671 | mIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 672 | mContext, 0, new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 0)); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Binds to the accessibility service. |
| 677 | */ |
| 678 | public void bind() { |
| 679 | if (mService == null) { |
| 680 | mContext.bindService(mIntent, this, Context.BIND_AUTO_CREATE); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Unbinds form the accessibility service and removes it from the data |
| 686 | * structures for service management. |
| 687 | */ |
| 688 | public void unbind() { |
| 689 | if (mService != null) { |
| 690 | mContext.unbindService(this); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Returns if the service is configured i.e. at least event types of interest |
| 696 | * and feedback type must be set. |
| 697 | * |
| 698 | * @return True if the service is configured, false otherwise. |
| 699 | */ |
| 700 | public boolean isConfigured() { |
| 701 | return (mEventTypes != 0 && mFeedbackType != 0); |
| 702 | } |
| 703 | |
| 704 | public void setServiceInfo(AccessibilityServiceInfo info) { |
| 705 | mCaller.obtainMessageOO(DO_SET_SERVICE_INFO, info, this).sendToTarget(); |
| 706 | } |
| 707 | |
| 708 | public void onServiceConnected(ComponentName componentName, IBinder service) { |
| 709 | mService = service; |
| 710 | mServiceInterface = IEventListener.Stub.asInterface(service); |
| 711 | |
| 712 | try { |
| 713 | mServiceInterface.setConnection(this); |
| 714 | synchronized (mLock) { |
| 715 | if (!mServices.contains(this)) { |
| 716 | mServices.add(this); |
| 717 | mComponentNameToServiceMap.put(componentName, this); |
| 718 | } |
| 719 | } |
| 720 | } catch (RemoteException re) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 721 | Slog.w(LOG_TAG, "Error while setting Controller for service: " + service, re); |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
| 725 | public void onServiceDisconnected(ComponentName componentName) { |
| 726 | synchronized (mLock) { |
| 727 | Service service = mComponentNameToServiceMap.remove(componentName); |
| 728 | mServices.remove(service); |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |