Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0e249a7a authored by jackqdyulei's avatar jackqdyulei
Browse files

Fix crash in CachedBluetoothDevice.

In LocalBluetoothProfileManager, we will update profiles in constructor
by register service callback to bluetooth profiles. So the callback may
return before this constructor, in which case mBluetoothProfileManager
maybe null in LocalBluetoothMangaer.

This cl moves the updateLocalProfiles() out of the constructor, to make
sure we will first construct mBluetoothProfileManger and then connect to
bluetooth profiles.

Change-Id: I93a388cf0598555d26181a6b0da99dcb35f227dd
Fixes: 113290231
Test: RunSettingsRoboTests
parent 890efce1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -75,6 +75,8 @@ public class LocalBluetoothManager {
                mCachedDeviceManager, context);
        mProfileManager = new LocalBluetoothProfileManager(context,
                mLocalAdapter, mCachedDeviceManager, mEventManager);

        mProfileManager.updateLocalProfiles();
        mEventManager.readPairedDevices();
    }

+0 −1
Original line number Diff line number Diff line
@@ -119,7 +119,6 @@ public class LocalBluetoothProfileManager {
        // pass this reference to adapter and event manager (circular dependency)
        adapter.setProfileManager(this);

        updateLocalProfiles();
        if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete");
    }

+19 −18
Original line number Diff line number Diff line
@@ -79,6 +79,8 @@ public class LocalBluetoothProfileManagerTest {
                mContext));
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        when(mDeviceManager.findDevice(mDevice)).thenReturn(mCachedBluetoothDevice);
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
    }

    /**
@@ -88,20 +90,26 @@ public class LocalBluetoothProfileManagerTest {
    public void constructor_initiateHidAndHidDeviceProfile() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HID_HOST, BluetoothProfile.HID_DEVICE}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();

        assertThat(mProfileManager.getHidProfile()).isNotNull();
        assertThat(mProfileManager.getHidDeviceProfile()).isNotNull();
    }

    @Test
    public void constructor_doNotUpdateProfiles() {
        mProfileManager = spy(new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager));

        verify(mProfileManager, never()).updateLocalProfiles();
    }

    /**
     * Verify updateLocalProfiles() for a local A2DP source adds A2dpProfile
     */
    @Test
    public void updateLocalProfiles_addA2dpToLocalProfiles() {
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        assertThat(mProfileManager.getA2dpProfile()).isNull();
        assertThat(mProfileManager.getHeadsetProfile()).isNull();

@@ -120,8 +128,7 @@ public class LocalBluetoothProfileManagerTest {
    public void updateProfiles_addHidProfileForRemoteDevice() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HID_HOST}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        ParcelUuid[] uuids = new ParcelUuid[]{BluetoothUuid.Hid};
        ParcelUuid[] localUuids = new ParcelUuid[]{};
        List<LocalBluetoothProfile> profiles = new ArrayList<>();
@@ -143,8 +150,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receiveA2dpConnectionStateChanged_shouldDispatchCallback() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.A2DP}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);
@@ -167,8 +173,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receiveHeadsetConnectionStateChanged_shouldDispatchCallback() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HEADSET}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);
@@ -191,8 +196,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receiveHAPConnectionStateChanged_shouldDispatchCallback() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.HEARING_AID}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);
@@ -215,8 +219,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receivePanConnectionStateChanged_shouldNotDispatchCallback() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.PAN}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);
@@ -239,8 +242,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receivePanConnectionStateChangedWithoutProfile_shouldNotRefresh
    () {
        mShadowBluetoothAdapter.setSupportedProfiles(null);
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);
@@ -262,8 +264,7 @@ public class LocalBluetoothProfileManagerTest {
    public void stateChangedHandler_receivePanConnectionStateChangedWithProfile_shouldRefresh() {
        mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                new int[] {BluetoothProfile.PAN}));
        mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
                mDeviceManager, mEventManager);
        mProfileManager.updateLocalProfiles();
        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
        // LocalBluetoothProfileManager created.
        mEventManager.setReceiverHandler(null);