From 9efb1343a018c479ff57d70e8058658faa8a926d Mon Sep 17 00:00:00 2001 From: Jaikumar Ganesh Date: Fri, 11 Feb 2011 18:10:31 -0800 Subject: [PATCH] Fix ANR in Settings and Phone app. When we have a lot of devices paired and try to enable bluetooth, loadBondState will take a some time. During this all UUIDs would have been registered and we would send out the Bluetooth On intent. When Settings apps or phone app gets the Bluetooth On intent, they try to initialize state. However, the enable Thread in Bluetooth Service is still initializing internal state in loadBondState leading to ANRs and deadlocks. So register SDP records as the last step of enable Thread. Change-Id: Iaa0a773e31b9d269f4c56c4f975a0e2973e02d6e --- .../java/android/server/BluetoothService.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index a61f685f2b6a4..a295de5660f63 100644 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -561,21 +561,16 @@ public class BluetoothService extends IBluetooth.Stub { persistBluetoothOnSetting(true); } - updateSdpRecords(); - mIsDiscovering = false; mBondState.readAutoPairingData(); mBondState.loadBondState(); initProfileState(); - // Log bluetooth on to battery stats. - long ident = Binder.clearCallingIdentity(); - try { - mBatteryStats.noteBluetoothOn(); - } catch (RemoteException e) { - } finally { - Binder.restoreCallingIdentity(ident); - } + // This should be the last step of the the enable thread. + // Because this adds SDP records which asynchronously + // broadcasts the Bluetooth On State in updateBluetoothState. + // So we want all internal state setup before this. + updateSdpRecords(); } else { setBluetoothState(BluetoothAdapter.STATE_OFF); } @@ -623,6 +618,11 @@ public class BluetoothService extends IBluetooth.Stub { } } + /** + * This function is called from Bluetooth Event Loop when onPropertyChanged + * for adapter comes in with UUID property. + * @param uuidsThe uuids of adapter as reported by Bluez. + */ synchronized void updateBluetoothState(String uuids) { if (mBluetoothState == BluetoothAdapter.STATE_TURNING_ON) { ParcelUuid[] adapterUuids = convertStringToParcelUuid(uuids); @@ -633,6 +633,15 @@ public class BluetoothService extends IBluetooth.Stub { String[] propVal = {"Pairable", getProperty("Pairable")}; mEventLoop.onPropertyChanged(propVal); + // Log bluetooth on to battery stats. + long ident = Binder.clearCallingIdentity(); + try { + mBatteryStats.noteBluetoothOn(); + } catch (RemoteException e) { + } finally { + Binder.restoreCallingIdentity(ident); + } + if (mIsAirplaneSensitive && isAirplaneModeOn() && !mIsAirplaneToggleable) { disable(false); }