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

Commit d0575ab8 authored by Sandeep Samdaria's avatar Sandeep Samdaria
Browse files

Handle NPE in HFPClientConnectionService

Problem: Telecom manager can return null phone account from a phone account
handle. Hfpclient service tries to retrieve the device from phone account's
address. Since phone account is null, it results in NPE.

Solution: Add a null check.

Bug: 221861375
Test: Added new unit-test to ensure Hfp service handles null data from
telecom manager.
Tag: #stability

Change-Id: I915ada921c40b6d9398ecad1f52f62016977b262
parent aca1d986
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -325,6 +325,9 @@ public class HfpClientConnectionService extends ConnectionService {
    private BluetoothDevice getDevice(PhoneAccountHandle handle) {
        BluetoothAdapter adapter = getSystemService(BluetoothManager.class).getAdapter();
        PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
        if (account == null) {
            return null;
        }
        String btAddr = account.getAddress().getSchemeSpecificPart();
        return adapter.getRemoteDevice(btAddr);
    }
+80 −0
Original line number Diff line number Diff line
@@ -282,4 +282,84 @@ public class HfpClientConnectionServiceTest {
        assertThat(((HfpClientConnection) connection).getDevice()).isEqualTo(TEST_DEVICE);
        assertThat(((HfpClientConnection) connection).getUUID()).isEqualTo(call.getUUID());
    }

    @Test
    public void onCreateIncomingConnection_phoneAccountIsNull_returnsNull() throws Exception{
        doReturn(null).when(mMockTelecomManager).getPhoneAccount(any());
        createService();
        setupDeviceConnection(TEST_DEVICE);

        HfpClientCall call = new HfpClientCall(TEST_DEVICE, /* id= */0,
                HfpClientCall.CALL_STATE_ACTIVE, /* number= */ TEST_NUMBER,
                /* multiParty= */ false, /* outgoing= */false, /* inBandRing= */true);

        Bundle extras = new Bundle();
        extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS,
                new ParcelUuid(call.getUUID()));
        ConnectionRequest connectionRequest = new ConnectionRequest.Builder().setExtras(
                extras).build();

        HfpClientConnectionService.onCallChanged(TEST_DEVICE, call);

        Connection connection = mHfpClientConnectionService.onCreateIncomingConnection(
                getPhoneAccountHandle(TEST_DEVICE),
                connectionRequest);

        assertThat(connection).isNull();
    }


    @Test
    public void onCreateOutgoingConnection_phoneAccountIsNull_returnsNull() throws Exception{
        doReturn(null).when(mMockTelecomManager).getPhoneAccount(any());
        createService();
        setupDeviceConnection(TEST_DEVICE);

        HfpClientCall call = new HfpClientCall(TEST_DEVICE, /* id= */0,
                HfpClientCall.CALL_STATE_ACTIVE, /* number= */ TEST_NUMBER,
                /* multiParty= */ false, /* outgoing= */true, /* inBandRing= */true);

        doReturn(call).when(mMockHeadsetClientService).dial(TEST_DEVICE, TEST_NUMBER);

        Bundle extras = new Bundle();
        extras.putParcelable(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS,
                new ParcelUuid(call.getUUID()));
        ConnectionRequest connectionRequest = new ConnectionRequest.Builder().setExtras(
                extras).setAddress(Uri.fromParts(
                PhoneAccount.SCHEME_TEL, TEST_NUMBER, null)).build();

        Connection connection = mHfpClientConnectionService.onCreateOutgoingConnection(
                getPhoneAccountHandle(TEST_DEVICE),
                connectionRequest);

        assertThat(connection).isNull();
    }


    @Test
    public void onCreateUnknownConnection_phoneAccountIsNull_returnsNull() throws Exception{
        doReturn(null).when(mMockTelecomManager).getPhoneAccount(any());
        createService();
        setupDeviceConnection(TEST_DEVICE);

        HfpClientCall call = new HfpClientCall(TEST_DEVICE, /* id= */0,
                HfpClientCall.CALL_STATE_ACTIVE, /* number= */ TEST_NUMBER,
                /* multiParty= */ false, /* outgoing= */true, /* inBandRing= */true);

        Bundle extras = new Bundle();
        extras.putParcelable(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS,
                new ParcelUuid(call.getUUID()));
        ConnectionRequest connectionRequest = new ConnectionRequest.Builder().setExtras(
                extras).setAddress(Uri.fromParts(
                PhoneAccount.SCHEME_TEL, TEST_NUMBER, null)).build();

        HfpClientConnectionService.onCallChanged(TEST_DEVICE, call);

        Connection connection = mHfpClientConnectionService.onCreateUnknownConnection(
                getPhoneAccountHandle(TEST_DEVICE),
                connectionRequest);

        assertThat(connection).isNull();
    }

}