Trying to unregister a semi connected accessibility service.

    When an accessibility service connects we get a callback in
    which we either add the service, if this service is in the list
    of connecting services (we still want the service to connect),
    or we unbind and clear the state, if the service is no longer in
    the list of connecting services (we do not want this service to
    connect because something change between the bind request and
    the connection callback).

    The problem is that when the service connects and it is not in
    the list of connecting services on service connected we called
    the clean up code before the connection was complete. However,
    the clean up code expects fully configured services. Now we
    fully connect the service and in case there is a problem -
    disconnect it.

    bug:8232627

Change-Id: I939e544e31ffc1406035265a012c180f2ca95d7c
diff --git a/services/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
index 9c518a1..fd5e79a 100644
--- a/services/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -934,12 +934,10 @@
      * @param service The service.
      * @return True if the service was removed, false otherwise.
      */
-    private void removeServiceLocked(Service service) {
-        UserState userState = getUserStateLocked(service.mUserId);
+    private void removeServiceLocked(Service service, UserState userState) {
         userState.mBoundServices.remove(service);
         userState.mComponentNameToServiceMap.remove(service.mComponentName);
         service.unlinkToOwnDeath();
-        service.dispose();
     }
 
     /**
@@ -1672,11 +1670,9 @@
         public boolean bindLocked() {
             UserState userState = getUserStateLocked(mUserId);
             if (!mIsAutomation) {
-                if (mService == null) {
-                    if (mContext.bindServiceAsUser(mIntent, this, Context.BIND_AUTO_CREATE,
-                            new UserHandle(mUserId))) {
-                        userState.mBindingServices.add(mComponentName);
-                    }
+                if (mService == null && mContext.bindServiceAsUser(
+                        mIntent, this, Context.BIND_AUTO_CREATE, new UserHandle(mUserId))) {
+                    userState.mBindingServices.add(mComponentName);
                 }
             } else {
                 userState.mBindingServices.add(mComponentName);
@@ -1697,14 +1693,15 @@
             if (mService == null) {
                 return false;
             }
+            UserState userState = getUserStateLocked(mUserId);
             if (!mIsAutomation) {
                 mContext.unbindService(this);
             } else {
-                UserState userState = getUserStateLocked(mUserId);
                 userState.mUiAutomationService = null;
                 userState.mUiAutomationServiceClient = null;
             }
-            removeServiceLocked(this);
+            removeServiceLocked(this, userState);
+            dispose();
             return true;
         }
 
@@ -1750,11 +1747,11 @@
                 mService = service;
                 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
                 UserState userState = getUserStateLocked(mUserId);
+                addServiceLocked(this, userState);
                 if (!userState.mBindingServices.contains(mComponentName)) {
                     binderDied();
                 } else {
                     userState.mBindingServices.remove(mComponentName);
-                    addServiceLocked(this, userState);
                     onUserStateChangedLocked(userState);
                 }
             }
@@ -2106,9 +2103,10 @@
 
         public void binderDied() {
             synchronized (mLock) {
-                // The death recipient is unregistered in tryRemoveServiceLocked
-                removeServiceLocked(this);
                 UserState userState = getUserStateLocked(mUserId);
+                // The death recipient is unregistered in removeServiceLocked
+                removeServiceLocked(this, userState);
+                dispose();
                 if (mIsAutomation) {
                     // We no longer have an automation service, so restore
                     // the state based on values in the settings database.