blob: 83ce3e338d3465c387b225fa431b142c8f499265 [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
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
svetoslavganov75986cf2009-05-14 22:28:01 -070020import com.android.internal.os.HandlerCaller;
21import com.android.internal.os.HandlerCaller.SomeArgs;
22
23import android.accessibilityservice.AccessibilityService;
24import android.accessibilityservice.AccessibilityServiceInfo;
25import android.accessibilityservice.IAccessibilityServiceConnection;
26import android.accessibilityservice.IEventListener;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070027import android.app.PendingIntent;
svetoslavganov75986cf2009-05-14 22:28:01 -070028import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
34import android.content.ServiceConnection;
35import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.content.pm.ServiceInfo;
38import android.database.ContentObserver;
39import android.net.Uri;
40import android.os.Binder;
41import android.os.DeadObjectException;
42import android.os.Handler;
43import android.os.IBinder;
44import android.os.Message;
45import android.os.RemoteException;
46import android.provider.Settings;
47import android.text.TextUtils;
48import android.text.TextUtils.SimpleStringSplitter;
Svetoslav Ganov714cff02010-02-17 19:36:28 -080049import android.util.Config;
Joe Onorato8a9b2202010-02-26 18:56:32 -080050import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.util.SparseArray;
52import android.view.accessibility.AccessibilityEvent;
53import android.view.accessibility.IAccessibilityManager;
54import android.view.accessibility.IAccessibilityManagerClient;
55
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.HashMap;
59import java.util.HashSet;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080060import java.util.Iterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import java.util.List;
62import java.util.Map;
63import 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 */
73public 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();
svetoslavganov75986cf2009-05-14 22:28:01 -0700141 }
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 Hackborn21f1bd12010-02-19 17:02:21 -0800150 PackageMonitor monitor = new PackageMonitor() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700151 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800152 public void onSomePackagesChanged() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700153 synchronized (mLock) {
154 populateAccessibilityServiceListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800155 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();
svetoslavganov75986cf2009-05-14 22:28:01 -0700201
Svetoslav Ganov714cff02010-02-17 19:36:28 -0800202 // get the accessibility enabled setting on boot
svetoslavganov75986cf2009-05-14 22:28:01 -0700203 mIsEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
204 Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;
Svetoslav Ganov714cff02010-02-17 19:36:28 -0800205
206 // if accessibility is enabled inform our clients we are on
207 if (mIsEnabled) {
208 updateClientsLocked();
209 }
Svetoslav Ganov714cff02010-02-17 19:36:28 -0800210
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800211 manageServicesLocked();
212 }
213
214 return;
svetoslavganov75986cf2009-05-14 22:28:01 -0700215 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800216
217 super.onReceive(context, intent);
svetoslavganov75986cf2009-05-14 22:28:01 -0700218 }
219 };
220
221 // package changes
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800222 monitor.register(context, true);
svetoslavganov75986cf2009-05-14 22:28:01 -0700223
224 // boot completed
225 IntentFilter bootFiler = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800226 mContext.registerReceiver(monitor, bootFiler);
svetoslavganov75986cf2009-05-14 22:28:01 -0700227 }
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 Ganovdd64a9b2010-04-13 18:41:17 -0700272 public boolean addClient(IAccessibilityManagerClient client) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700273 synchronized (mLock) {
Svetoslav Ganovdd64a9b2010-04-13 18:41:17 -0700274 mClients.add(client);
275 return mIsEnabled;
svetoslavganov75986cf2009-05-14 22:28:01 -0700276 }
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 Onorato8a9b2202010-02-26 18:56:32 -0800308 Slog.w(LOG_TAG, "Dead " + service.mService + ". Cleaning up.");
svetoslavganov75986cf2009-05-14 22:28:01 -0700309 if (removeDeadServiceLocked(service)) {
310 count--;
311 i--;
312 }
313 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800314 Slog.e(LOG_TAG, "Error during sending interrupt request to "
svetoslavganov75986cf2009-05-14 22:28:01 -0700315 + 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 Onorato8a9b2202010-02-26 18:56:32 -0800342 Slog.w(LOG_TAG, "Unknown message type: " + message.what);
svetoslavganov75986cf2009-05-14 22:28:01 -0700343 }
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 Chen85b598b2009-07-29 17:23:50 -0700369 try {
370 for (int i = 0, count = mServices.size(); i < count; i++) {
371 Service service = mServices.get(i);
svetoslavganov75986cf2009-05-14 22:28:01 -0700372
Charles Chen85b598b2009-07-29 17:23:50 -0700373 if (service.mIsDefault == isDefault) {
374 if (canDispathEventLocked(service, event, mHandledFeedbackTypes)) {
375 mHandledFeedbackTypes |= service.mFeedbackType;
376 notifyAccessibilityServiceDelayedLocked(service, event);
377 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700378 }
379 }
Charles Chen85b598b2009-07-29 17:23:50 -0700380 } 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;
svetoslavganov75986cf2009-05-14 22:28:01 -0700385 }
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 Chenbbc19342009-07-24 16:06:09 -0700421 if (event == null) {
422 return;
423 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700424 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 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700434 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 Ganov714cff02010-02-17 19:36:28 -0800449 if (Config.DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800450 Slog.i(LOG_TAG, "Event " + event + " sent to " + listener);
svetoslavganov75986cf2009-05-14 22:28:01 -0700451 }
452 } catch (RemoteException re) {
453 if (re instanceof DeadObjectException) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800454 Slog.w(LOG_TAG, "Dead " + service.mService + ". Cleaning up.");
svetoslavganov75986cf2009-05-14 22:28:01 -0700455 synchronized (mLock) {
456 removeDeadServiceLocked(service);
457 }
458 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800459 Slog.e(LOG_TAG, "Error during sending " + event + " to " + service.mService, re);
svetoslavganov75986cf2009-05-14 22:28:01 -0700460 }
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 Ganov714cff02010-02-17 19:36:28 -0800474 if (Config.DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800475 Slog.i(LOG_TAG, "Dead service " + service.mService + " removed");
svetoslavganov75986cf2009-05-14 22:28:01 -0700476 }
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 Hackborn21f1bd12010-02-19 17:02:21 -0800569 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 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700577 }
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 Ganov714cff02010-02-17 19:36:28 -0800593 boolean isEnabled = mIsEnabled;
svetoslavganov75986cf2009-05-14 22:28:01 -0700594
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 Ganov714cff02010-02-17 19:36:28 -0800601 if (isEnabled && enabledServices.contains(componentName)) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700602 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 Ganovfb606da2010-02-18 10:54:36 -0800625 i--;
svetoslavganov75986cf2009-05-14 22:28:01 -0700626 }
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 Hackborndd9b82c2009-09-03 00:18:47 -0700669 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));
svetoslavganov75986cf2009-05-14 22:28:01 -0700673 }
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 Onorato8a9b2202010-02-26 18:56:32 -0800721 Slog.w(LOG_TAG, "Error while setting Controller for service: " + service, re);
svetoslavganov75986cf2009-05-14 22:28:01 -0700722 }
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}