From ddf541fc6efb72bb6d37aa9669dae6adf912edcd Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 16 Nov 2011 12:52:45 -0800 Subject: [PATCH] Unlink the Binder DeathRecipient when removing a Bluetooth service record Change-Id: Ib8a9e254d7c9ebe9d54f5c10b3daa31d062b4df0 --- .../java/android/server/BluetoothService.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index d604a01a759e8..bc239cb50edd8 100755 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -145,7 +145,12 @@ public class BluetoothService extends IBluetooth.Stub { private final ArrayList mUuidIntentTracker; private final HashMap mUuidCallbackTracker; - private final HashMap> mServiceRecordToPid; + private static class ServiceRecordClient { + int pid; + IBinder binder; + IBinder.DeathRecipient death; + } + private final HashMap mServiceRecordToPid; private final HashMap mDeviceProfileState; private final BluetoothProfileState mA2dpProfileState; @@ -221,7 +226,7 @@ public class BluetoothService extends IBluetooth.Stub { mDeviceOobData = new HashMap>(); mUuidIntentTracker = new ArrayList(); mUuidCallbackTracker = new HashMap(); - mServiceRecordToPid = new HashMap>(); + mServiceRecordToPid = new HashMap(); mDeviceProfileState = new HashMap(); mA2dpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.A2DP); mHfpProfileState = new BluetoothProfileState(mContext, BluetoothProfileState.HFP); @@ -1528,11 +1533,17 @@ public class BluetoothService extends IBluetooth.Stub { return -1; } - int pid = Binder.getCallingPid(); - mServiceRecordToPid.put(new Integer(handle), new Pair(pid, b)); + ServiceRecordClient client = new ServiceRecordClient(); + client.pid = Binder.getCallingPid(); + client.binder = b; + client.death = new Reaper(handle, client.pid, RFCOMM_RECORD_REAPER); + mServiceRecordToPid.put(new Integer(handle), client); try { - b.linkToDeath(new Reaper(handle, pid, RFCOMM_RECORD_REAPER), 0); - } catch (RemoteException e) {Log.e(TAG, "", e);} + b.linkToDeath(client.death, 0); + } catch (RemoteException e) { + Log.e(TAG, "", e); + client.death = null; + } return handle; } @@ -1547,10 +1558,15 @@ public class BluetoothService extends IBluetooth.Stub { } private synchronized void checkAndRemoveRecord(int handle, int pid) { - Pair pidPair = mServiceRecordToPid.get(handle); - if (pidPair != null && pid == pidPair.first) { + ServiceRecordClient client = mServiceRecordToPid.get(handle); + if (client != null && pid == client.pid) { if (DBG) Log.d(TAG, "Removing service record " + Integer.toHexString(handle) + " for pid " + pid); + + if (client.death != null) { + client.binder.unlinkToDeath(client.death, 0); + } + mServiceRecordToPid.remove(handle); removeServiceRecordNative(handle); } @@ -1880,7 +1896,7 @@ public class BluetoothService extends IBluetooth.Stub { private void dumpApplicationServiceRecords(PrintWriter pw) { pw.println("\n--Application Service Records--"); for (Integer handle : mServiceRecordToPid.keySet()) { - Integer pid = mServiceRecordToPid.get(handle).first; + Integer pid = mServiceRecordToPid.get(handle).pid; pw.println("\tpid " + pid + " handle " + Integer.toHexString(handle)); } }