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

Commit cd9ee08b authored by Hansong Zhang's avatar Hansong Zhang Committed by android-build-merger
Browse files

Prevent NPE when profile is null

am: c3ea0d3f

Change-Id: I7db11fad7b6d40e56dbcd029eb960c139c3a3b0d
parents 7f126376 c3ea0d3f
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -1189,7 +1189,8 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
     * @return {@code true} if {@code cachedBluetoothDevice} is a2dp device
     */
    public boolean isA2dpDevice() {
        return mProfileManager.getA2dpProfile().getConnectionStatus(mDevice) ==
        A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
        return a2dpProfile != null && a2dpProfile.getConnectionStatus(mDevice) ==
                BluetoothProfile.STATE_CONNECTED;
    }

@@ -1197,7 +1198,17 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
     * @return {@code true} if {@code cachedBluetoothDevice} is HFP device
     */
    public boolean isHfpDevice() {
        return mProfileManager.getHeadsetProfile().getConnectionStatus(mDevice) ==
        HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile();
        return headsetProfile != null && headsetProfile.getConnectionStatus(mDevice) ==
                BluetoothProfile.STATE_CONNECTED;
    }

    /**
     * @return {@code true} if {@code cachedBluetoothDevice} is Hearing Aid device
     */
    public boolean isConnectedHearingAidDevice() {
        HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile();
        return hearingAidProfile != null && hearingAidProfile.getConnectionStatus(mDevice) ==
                BluetoothProfile.STATE_CONNECTED;
    }
}
+39 −0
Original line number Diff line number Diff line
@@ -572,4 +572,43 @@ public class CachedBluetoothDeviceTest {

        assertThat(mCachedDevice.isHfpDevice()).isFalse();
    }

    @Test
    public void isConnectedHearingAidDevice_connected_returnTrue() {
        when(mProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
        when(mHearingAidProfile.getConnectionStatus(mDevice)).
                thenReturn(BluetoothProfile.STATE_CONNECTED);

        assertThat(mCachedDevice.isConnectedHearingAidDevice()).isTrue();
    }

    @Test
    public void isConnectedHearingAidDevice_disconnected_returnFalse() {
        when(mProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile);
        when(mHearingAidProfile.getConnectionStatus(mDevice)).
                thenReturn(BluetoothProfile.STATE_DISCONNECTED);

        assertThat(mCachedDevice.isConnectedHearingAidDevice()).isFalse();
    }

    @Test
    public void isConnectedHfpDevice_profileIsNull_returnFalse() {
        when(mProfileManager.getHeadsetProfile()).thenReturn(null);

        assertThat(mCachedDevice.isHfpDevice()).isFalse();
    }

    @Test
    public void isConnectedA2dpDevice_profileIsNull_returnFalse() {
        when(mProfileManager.getA2dpProfile()).thenReturn(null);

        assertThat(mCachedDevice.isA2dpDevice()).isFalse();
    }

    @Test
    public void isConnectedHearingAidDevice_profileIsNull_returnFalse() {
        when(mProfileManager.getHearingAidProfile()).thenReturn(null);

        assertThat(mCachedDevice.isConnectedHearingAidDevice()).isFalse();
    }
}