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

Commit c4218061 authored by Chienyuan's avatar Chienyuan
Browse files

Add null check for AclStateChangedHandler

When we receive ACL state change and can't find corresponding
CachedBluetoothDevice, we will trigger onAclConnectStateChanged with
null activeDevice and cause NullPointerException.
Add a null check in AclStateChangedHandler to prevent it.

Bug: 122049350
Test: RunSettingsLibRoboTests
Change-Id: Iaabccc8c4ad60dbae14bbb0f6af655be06bc18ea
parent 46d40a2b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -484,6 +484,10 @@ public class BluetoothEventManager {
                return;
            }
            final CachedBluetoothDevice activeDevice = mDeviceManager.findDevice(device);
            if (activeDevice == null) {
                Log.w(TAG, "AclStateChangedHandler: activeDevice is null");
                return;
            }
            final int state;
            switch (action) {
                case BluetoothDevice.ACTION_ACL_CONNECTED:
+13 −0
Original line number Diff line number Diff line
@@ -181,4 +181,17 @@ public class BluetoothEventManagerTest {
        verify(mBluetoothCallback, never()).onAclConnectionStateChanged(mCachedBluetoothDevice,
                BluetoothAdapter.STATE_CONNECTED);
    }

    @Test
    public void dispatchAclConnectionStateChanged_findDeviceReturnNull_shouldNotDispatchCallback() {
        when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(null);
        mBluetoothEventManager.registerCallback(mBluetoothCallback);
        mIntent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
        mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);

        mContext.sendBroadcast(mIntent);

        verify(mBluetoothCallback, never()).onAclConnectionStateChanged(mCachedBluetoothDevice,
                BluetoothAdapter.STATE_CONNECTED);
    }
}