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

Commit 0ec9db92 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

mcp: Fix NPE from getMediaStateChar

Bug: 208754844
Tag: #feature
Test: atest MediaControlGattServiceTest
Change-Id: I9fdce1acef19a95545aea26a352985ccd7f64b46
parent fd0d1bd9
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -995,14 +995,20 @@ public class MediaControlGattService implements MediaControlGattServiceInterface
        }
    }

    private int getMediaStateChar() {
    @VisibleForTesting
    int getMediaStateChar() {
        if (!isFeatureSupported(ServiceFeature.MEDIA_STATE)) return MediaState.INACTIVE.getValue();

        BluetoothGattCharacteristic stateChar =
                mCharacteristics.get(CharId.MEDIA_STATE);

        if (stateChar.getValue() != null) {
            return stateChar.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        }

        return MediaState.INACTIVE.getValue();
    }

    @VisibleForTesting
    void updateMediaStateChar(int state) {
        if (DBG) {
+21 −0
Original line number Diff line number Diff line
@@ -1158,4 +1158,25 @@ public class MediaControlGattServiceTest {
        state_map.put(PlayerStateField.PLAYER_NAME, player_name);
        mMcpService.updatePlayerState(state_map);
    }

    @Test
    public void testUpdatePlayerStateFromNullStateChar() {
        BluetoothGattService service = initAllFeaturesGattService();

        BluetoothGattCharacteristic characteristic =
                service.getCharacteristic(MediaControlGattService.UUID_MEDIA_STATE);
        Assert.assertNotNull(characteristic);
        byte[] nullBytes = null;
        characteristic.setValue(nullBytes);

        prepareConnectedDevice();
        doReturn(BluetoothDevice.ACCESS_ALLOWED)
                .when(mMockMcpService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        Map<PlayerStateField, Object> state_map = new HashMap<>();
        MediaState playback_state = MediaState.SEEKING;
        state_map.put(PlayerStateField.PLAYBACK_STATE, playback_state);
        mMcpService.updatePlayerState(state_map);
    }
}