Notify VrListenerService when VR activity changes.

Bug: 27536964
Bug: 22855417

Change-Id: I67e1f8e6595332b3d768a99735bbd5fd38dffdc9
diff --git a/services/core/java/com/android/server/utils/ManagedApplicationService.java b/services/core/java/com/android/server/utils/ManagedApplicationService.java
index a645701..ad8acef0 100644
--- a/services/core/java/com/android/server/utils/ManagedApplicationService.java
+++ b/services/core/java/com/android/server/utils/ManagedApplicationService.java
@@ -60,6 +60,8 @@
     private ServiceConnection mPendingConnection;
     private ServiceConnection mConnection;
     private IInterface mBoundInterface;
+    private PendingEvent mPendingEvent;
+
 
 
     private ManagedApplicationService(final Context context, final ComponentName component,
@@ -82,6 +84,13 @@
     }
 
     /**
+     * Implement to call IInterface methods after service is connected.
+     */
+    public interface PendingEvent {
+         void runEvent(IInterface service) throws RemoteException;
+    }
+
+    /**
      * Create a new ManagedApplicationService object but do not yet bind to the user service.
      *
      * @param context a Context to use for binding the application service.
@@ -131,6 +140,30 @@
         return true;
     }
 
+
+  /**
+   * Send an event to run as soon as the binder interface is available.
+   *
+   * @param event a {@link PendingEvent} to send.
+   */
+  public void sendEvent(@NonNull PendingEvent event) {
+        IInterface iface;
+        synchronized (mLock) {
+            iface = mBoundInterface;
+            if (iface == null) {
+                mPendingEvent = event;
+            }
+        }
+
+        if (iface != null) {
+            try {
+                event.runEvent(iface);
+            } catch (RuntimeException | RemoteException ex) {
+                Slog.e(TAG, "Received exception from user service: ", ex);
+            }
+        }
+    }
+
     /**
      * Asynchronously unbind from the application service if bound.
      */
@@ -168,6 +201,8 @@
             final ServiceConnection serviceConnection = new ServiceConnection() {
                 @Override
                 public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
+                    IInterface iface = null;
+                    PendingEvent pendingEvent = null;
                     synchronized (mLock) {
                         if (mPendingConnection == this) {
                             // No longer pending, remove from pending connection
@@ -186,12 +221,22 @@
                                 mContext.unbindService(this);
                                 mBoundInterface = null;
                             }
+                            iface = mBoundInterface;
+                            pendingEvent = mPendingEvent;
+                            mPendingEvent = null;
                         } catch (RemoteException e) {
                             // DOA
                             Slog.w(TAG, "Unable to bind service: " + intent, e);
                             mBoundInterface = null;
                         }
                     }
+                    if (iface != null && pendingEvent != null) {
+                        try {
+                            pendingEvent.runEvent(iface);
+                        } catch (RuntimeException | RemoteException ex) {
+                            Slog.e(TAG, "Received exception from user service: ", ex);
+                        }
+                    }
                 }
 
                 @Override