add CarAppContextManager

- This replaces CarMessageManager and take care of
  app state sharing like active navigation or voice
  command.
- Apps can change the state and such change is
  notified to other apps.
- The informaiton is sent down to vehicle HAL as well
  to use that information for setting audio priority.
- added event count to VNS dump
- cleaned up unnecessary init/release in VNS's handler
- removed init from handler thread in car service vehicle hal:
  this just brings race when things start queickly.

bug: 25261670

(cherry picked from commit 53729bb2d3600bc23a26553f1cc3cda7d1ea0b2c)

Change-Id: I48a0215c6c8b73b5db3a2b1890febb1320096b63
diff --git a/service/src/com/android/car/BinderInterfaceContainer.java b/service/src/com/android/car/BinderInterfaceContainer.java
index e3bbba4..c233afa 100644
--- a/service/src/com/android/car/BinderInterfaceContainer.java
+++ b/service/src/com/android/car/BinderInterfaceContainer.java
@@ -31,20 +31,21 @@
 
     public static class BinderInterface<T extends IInterface>
             implements IBinder.DeathRecipient {
-        public final T binderInterface;
         public final int version;
+        public final T binderInterface;
         private final BinderInterfaceContainer<T> mContainer;
 
-        private BinderInterface(BinderInterfaceContainer<T> container, T binderInterface,
-                int version) {
+        public BinderInterface(BinderInterfaceContainer<T> container, int version,
+                T binderInterface) {
             mContainer = container;
-            this.binderInterface = binderInterface;
             this.version = version;
+            this.binderInterface = binderInterface;
         }
 
         @Override
         public void binderDied() {
             binderInterface.asBinder().unlinkToDeath(this, 0);
+            mContainer.handleBinderDeath(this);
         }
     }
 
@@ -62,11 +63,11 @@
     public void addBinder(int version, T binderInterface) {
         IBinder binder = binderInterface.asBinder();
         synchronized (this) {
-            BinderInterface bInterface = mBinders.get(binder);
+            BinderInterface<T> bInterface = mBinders.get(binder);
             if (bInterface != null) {
                 return;
             }
-            bInterface = new BinderInterface(this, binderInterface, version);
+            bInterface = new BinderInterface<T>(this, version, binderInterface);
             try {
                 binder.linkToDeath(bInterface, 0);
             } catch (RemoteException e) {
@@ -79,7 +80,7 @@
     public void removeBinder(T binderInterface) {
         IBinder binder = binderInterface.asBinder();
         synchronized(this) {
-            BinderInterface bInterface = mBinders.get(binder);
+            BinderInterface<T> bInterface = mBinders.get(binder);
             if (bInterface != null) {
                 return;
             }
@@ -88,13 +89,32 @@
         }
     }
 
+    public BinderInterface<T> getBinderInterface(T binderInterface) {
+        IBinder binder = binderInterface.asBinder();
+        synchronized (this) {
+            return mBinders.get(binder);
+        }
+    }
+
+    public void addBinderInterface(BinderInterface<T> bInterface) {
+        IBinder binder = bInterface.binderInterface.asBinder();
+        synchronized (this) {
+            try {
+                binder.linkToDeath(bInterface, 0);
+            } catch (RemoteException e) {
+                throw new IllegalArgumentException(e);
+            }
+            mBinders.put(binder, bInterface);
+        }
+    }
+
     public Collection<BinderInterface<T>> getInterfaces() {
         synchronized (this) {
             return mBinders.values();
         }
     }
 
-    public synchronized void release() {
+    public synchronized void clear() {
         Collection<BinderInterface<T>> interfaces = getInterfaces();
         for (BinderInterface<T> bInterface : interfaces) {
             removeBinder(bInterface.binderInterface);