From 484867a8ed3d46fbb8df493c77f2e2d46a0db654 Mon Sep 17 00:00:00 2001 From: Matthew Xie Date: Thu, 25 Aug 2011 16:45:58 -0700 Subject: [PATCH] Move Bluetooth remove service record handler to the app main thread Move Bluetooth remove service record handler to the app main thread. This removes the dependency of caller thread that constructs the BluetoothAdapter singleton instance. The caller thread could stop while BluetoothAdapter singleton exists but lose the handler context. Make the BluetoothService.removeServiceRecord return quickly without blocking on native call. bug 4999443 Change-Id: I302534baa381f4aca8192e1e4a9ea21793b07ba6 --- .../android/bluetooth/BluetoothAdapter.java | 31 ++++++++++++------- .../java/android/server/BluetoothService.java | 9 +++++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 223692859d8a8..254c98ff8cf0b 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -22,6 +22,7 @@ import android.content.Context; import android.os.Binder; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.os.ParcelUuid; import android.os.RemoteException; @@ -348,6 +349,8 @@ public final class BluetoothAdapter { private final IBluetooth mService; + private Handler mServiceRecordHandler; + /** * Get a handle to the default local Bluetooth adapter. *

Currently Android only supports one Bluetooth adapter, but the API @@ -376,6 +379,7 @@ public final class BluetoothAdapter { throw new IllegalArgumentException("service is null"); } mService = service; + mServiceRecordHandler = null; } /** @@ -1011,7 +1015,21 @@ public final class BluetoothAdapter { } catch (IOException e) {} throw new IOException("Not able to register SDP record for " + name); } - socket.setCloseHandler(mHandler, handle); + + if (mServiceRecordHandler == null) { + mServiceRecordHandler = new Handler(Looper.getMainLooper()) { + public void handleMessage(Message msg) { + /* handle socket closing */ + int handle = msg.what; + try { + if (DBG) Log.d(TAG, "Removing service record " + + Integer.toHexString(handle)); + mService.removeServiceRecord(handle); + } catch (RemoteException e) {Log.e(TAG, "", e);} + } + }; + } + socket.setCloseHandler(mServiceRecordHandler, handle); return socket; } @@ -1243,17 +1261,6 @@ public final class BluetoothAdapter { return Collections.unmodifiableSet(devices); } - private Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - /* handle socket closing */ - int handle = msg.what; - try { - if (DBG) Log.d(TAG, "Removing service record " + Integer.toHexString(handle)); - mService.removeServiceRecord(handle); - } catch (RemoteException e) {Log.e(TAG, "", e);} - } - }; - /** * Validate a Bluetooth address, such as "00:43:A8:23:10:F0" *

Alphabetic characters must be uppercase to be valid. diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index e942969e13722..55a06246a3b4b 100755 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -117,6 +117,7 @@ public class BluetoothService extends IBluetooth.Stub { private static final int MESSAGE_UUID_INTENT = 1; private static final int MESSAGE_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 2; + private static final int MESSAGE_REMOVE_SERVICE_RECORD = 3; private static final int RFCOMM_RECORD_REAPER = 10; private static final int STATE_CHANGE_REAPER = 11; @@ -537,6 +538,10 @@ public class BluetoothService extends IBluetooth.Stub { } if (attempt > 0) mBondState.clearPinAttempts(address); break; + case MESSAGE_REMOVE_SERVICE_RECORD: + Pair pair = (Pair) msg.obj; + checkAndRemoveRecord(pair.first, pair.second); + break; } } }; @@ -1542,7 +1547,9 @@ public class BluetoothService extends IBluetooth.Stub { public void removeServiceRecord(int handle) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - checkAndRemoveRecord(handle, Binder.getCallingPid()); + Message message = mHandler.obtainMessage(MESSAGE_REMOVE_SERVICE_RECORD); + message.obj = new Pair(handle, Binder.getCallingPid()); + mHandler.sendMessage(message); } private synchronized void checkAndRemoveRecord(int handle, int pid) {