Merge "MediaBrowserService: Clean up ConnectionRecord when browser is dead" into oc-mr1-dev am: bbf55bb7ae
am: 86baa82d01

Change-Id: I509f5d8ae4c8224ee1e7b8ceffd2a65f1da1254c
diff --git a/media/java/android/service/media/MediaBrowserService.java b/media/java/android/service/media/MediaBrowserService.java
index 4df645d..4fc43ea 100644
--- a/media/java/android/service/media/MediaBrowserService.java
+++ b/media/java/android/service/media/MediaBrowserService.java
@@ -110,12 +110,22 @@
     /**
      * All the info about a connection.
      */
-    private class ConnectionRecord {
+    private class ConnectionRecord implements IBinder.DeathRecipient {
         String pkg;
         Bundle rootHints;
         IMediaBrowserServiceCallbacks callbacks;
         BrowserRoot root;
         HashMap<String, List<Pair<IBinder, Bundle>>> subscriptions = new HashMap<>();
+
+        @Override
+        public void binderDied() {
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    mConnections.remove(callbacks.asBinder());
+                }
+            });
+        }
     }
 
     /**
@@ -207,7 +217,6 @@
                         connection.pkg = pkg;
                         connection.rootHints = rootHints;
                         connection.callbacks = callbacks;
-
                         connection.root = MediaBrowserService.this.onGetRoot(pkg, uid, rootHints);
 
                         // If they didn't return something, don't allow this client.
@@ -223,6 +232,7 @@
                         } else {
                             try {
                                 mConnections.put(b, connection);
+                                b.linkToDeath(connection, 0);
                                 if (mSession != null) {
                                     callbacks.onConnect(connection.root.getRootId(),
                                             mSession, connection.root.getExtras());
@@ -248,6 +258,7 @@
                         final ConnectionRecord old = mConnections.remove(b);
                         if (old != null) {
                             // TODO
+                            old.callbacks.asBinder().unlinkToDeath(old, 0);
                         }
                     }
                 });