From dbd6e4d11f43e5b4cf69a7db18d11f1605a7cc32 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 19 Mar 2023 22:55:37 +0100 Subject: [PATCH] fix: sonarcloud lint --- .../newpipe/player/PlayQueueActivity.java | 28 +++++++++---------- .../org/schabi/newpipe/util/ListHelper.java | 5 ++-- .../schabi/newpipe/util/ListHelperTest.java | 6 ++++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java index fd2128216..bf0dc4a56 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java @@ -657,23 +657,21 @@ public final class PlayQueueActivity extends AppCompatActivity * @param itemId index of the selected item */ private void onAudioTrackClick(final int itemId) { - @Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata(); - if (currentMetadata == null || currentMetadata.getMaybeAudioTrack().isEmpty()) { + if (player.getCurrentMetadata() == null) { return; } + player.getCurrentMetadata().getMaybeAudioTrack().ifPresent(audioTrack -> { + final List availableStreams = audioTrack.getAudioStreams(); + final int selectedStreamIndex = audioTrack.getSelectedAudioStreamIndex(); + if (selectedStreamIndex == itemId || availableStreams.size() <= itemId) { + return; + } - final MediaItemTag.AudioTrack audioTrack = - currentMetadata.getMaybeAudioTrack().get(); - final List availableStreams = audioTrack.getAudioStreams(); - final int selectedStreamIndex = audioTrack.getSelectedAudioStreamIndex(); - if (selectedStreamIndex == itemId || availableStreams.size() <= itemId) { - return; - } - - player.saveStreamProgressState(); - final String newAudioTrack = availableStreams.get(itemId).getAudioTrackId(); - player.setRecovery(); - player.setAudioTrack(newAudioTrack); - player.reloadPlayQueueManager(); + player.saveStreamProgressState(); + final String newAudioTrack = availableStreams.get(itemId).getAudioTrackId(); + player.setRecovery(); + player.setAudioTrack(newAudioTrack); + player.reloadPlayQueueManager(); + }); } } diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index 002a8014e..68e50b9c1 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -239,13 +239,12 @@ public final class ListHelper { } // Filter unknown audio tracks if there are multiple tracks - java.util.stream.Stream cs = collectedStreams.values().stream(); if (collectedStreams.size() > 1) { - cs = cs.filter(s -> s.getAudioTrackId() != null); + collectedStreams.remove(""); } // Sort collected streams by name - return cs.sorted(Comparator.comparing(audioStream -> + return collectedStreams.values().stream().sorted(Comparator.comparing(audioStream -> Localization.audioTrackName(context, audioStream))).collect(Collectors.toList()); } diff --git a/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java b/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java index 173892e5a..4619f8ad7 100644 --- a/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java +++ b/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java @@ -390,6 +390,12 @@ public class ListHelperTest { stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( null, AUDIO_TRACKS_TEST_LIST, cmp)); assertEquals("en.ds", stream.getId()); + + // Japanese language, fall back to original + cmp = ListHelper.getAudioStreamComparator(Locale.JAPANESE, true, false, false); + stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( + null, AUDIO_TRACKS_TEST_LIST, cmp)); + assertEquals("en.or", stream.getId()); } @Test