Skip to content

Commit e3062d7

Browse files
Use Optional chaining.
1 parent fd55d85 commit e3062d7

6 files changed

Lines changed: 54 additions & 65 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,23 +2423,20 @@ private void setOverlayElementsClickable(final boolean enable) {
24232423

24242424
// helpers to check the state of player and playerService
24252425
boolean isPlayerAvailable() {
2426-
return (player != null);
2426+
return player != null;
24272427
}
24282428

24292429
boolean isPlayerServiceAvailable() {
2430-
return (playerService != null);
2430+
return playerService != null;
24312431
}
24322432

24332433
boolean isPlayerAndPlayerServiceAvailable() {
2434-
return (player != null && playerService != null);
2434+
return player != null && playerService != null;
24352435
}
24362436

24372437
public Optional<View> getRoot() {
2438-
if (player == null) {
2439-
return Optional.empty();
2440-
}
2441-
2442-
return player.UIs().get(VideoPlayerUi.class)
2438+
return Optional.ofNullable(player)
2439+
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
24432440
.map(playerUi -> playerUi.getBinding().getRoot());
24442441
}
24452442

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,21 +1877,16 @@ public void disablePreloadingOfCurrentTrack() {
18771877

18781878
@Nullable
18791879
public VideoStream getSelectedVideoStream() {
1880-
@Nullable final MediaItemTag.Quality quality = Optional.ofNullable(currentMetadata)
1880+
return Optional.ofNullable(currentMetadata)
18811881
.flatMap(MediaItemTag::getMaybeQuality)
1882+
.filter(quality -> {
1883+
final int selectedStreamIndex = quality.getSelectedVideoStreamIndex();
1884+
return selectedStreamIndex >= 0
1885+
&& selectedStreamIndex < quality.getSortedVideoStreams().size();
1886+
})
1887+
.map(quality -> quality.getSortedVideoStreams()
1888+
.get(quality.getSelectedVideoStreamIndex()))
18821889
.orElse(null);
1883-
if (quality == null) {
1884-
return null;
1885-
}
1886-
1887-
final List<VideoStream> availableStreams = quality.getSortedVideoStreams();
1888-
final int selectedStreamIndex = quality.getSelectedVideoStreamIndex();
1889-
1890-
if (selectedStreamIndex >= 0 && availableStreams.size() > selectedStreamIndex) {
1891-
return availableStreams.get(selectedStreamIndex);
1892-
} else {
1893-
return null;
1894-
}
18951890
}
18961891
//endregion
18971892

app/src/main/java/org/schabi/newpipe/player/mediaitem/MediaItemTag.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ default Optional<Quality> getMaybeQuality() {
6161

6262
@NonNull
6363
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
64-
if (mediaItem == null || mediaItem.localConfiguration == null
65-
|| !(mediaItem.localConfiguration.tag instanceof MediaItemTag)) {
66-
return Optional.empty();
67-
}
68-
69-
return Optional.of((MediaItemTag) mediaItem.localConfiguration.tag);
64+
return Optional.ofNullable(mediaItem)
65+
.map(item -> item.localConfiguration)
66+
.map(localConfiguration -> localConfiguration.tag)
67+
.filter(MediaItemTag.class::isInstance)
68+
.map(MediaItemTag.class::cast);
7069
}
7170

7271
@NonNull

app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import androidx.annotation.Nullable;
88
import androidx.collection.ArraySet;
99

10-
import com.google.android.exoplayer2.source.MediaSource;
11-
1210
import org.reactivestreams.Subscriber;
1311
import org.reactivestreams.Subscription;
1412
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -27,6 +25,7 @@
2725

2826
import java.util.Collection;
2927
import java.util.Collections;
28+
import java.util.Optional;
3029
import java.util.Set;
3130
import java.util.concurrent.TimeUnit;
3231
import java.util.concurrent.atomic.AtomicBoolean;
@@ -422,21 +421,24 @@ private void maybeLoadItem(@NonNull final PlayQueueItem item) {
422421

423422
private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueueItem stream) {
424423
return stream.getStream().map(streamInfo -> {
425-
final MediaSource source = playbackListener.sourceOf(stream, streamInfo);
426-
if (source == null || MediaItemTag.from(source.getMediaItem()).isEmpty()) {
427-
final String message = "Unable to resolve source from stream info. "
428-
+ "URL: " + stream.getUrl() + ", "
429-
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "
430-
+ "video count: " + streamInfo.getVideoOnlyStreams().size() + ", "
431-
+ streamInfo.getVideoStreams().size();
432-
return (ManagedMediaSource)
433-
FailedMediaSource.of(stream, new MediaSourceResolutionException(message));
434-
}
435-
436-
final MediaItemTag tag = MediaItemTag.from(source.getMediaItem()).get();
437-
final long expiration = System.currentTimeMillis()
438-
+ ServiceHelper.getCacheExpirationMillis(streamInfo.getServiceId());
439-
return new LoadedMediaSource(source, tag, stream, expiration);
424+
final var source = playbackListener.sourceOf(stream, streamInfo);
425+
426+
return Optional.ofNullable(source)
427+
.flatMap(source1 -> MediaItemTag.from(source1.getMediaItem()))
428+
.<ManagedMediaSource>map(tag -> {
429+
final long expiration = System.currentTimeMillis()
430+
+ ServiceHelper.getCacheExpirationMillis(streamInfo.getServiceId());
431+
return new LoadedMediaSource(source, tag, stream, expiration);
432+
})
433+
.orElseGet(() -> {
434+
final String message = "Unable to resolve source from stream info. "
435+
+ "URL: " + stream.getUrl() + ", "
436+
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "
437+
+ "video count: " + streamInfo.getVideoOnlyStreams().size() + ", "
438+
+ streamInfo.getVideoStreams().size();
439+
return FailedMediaSource.of(stream, new MediaSourceResolutionException(
440+
message));
441+
});
440442
}).onErrorReturn(throwable -> {
441443
if (throwable instanceof ExtractionException) {
442444
return FailedMediaSource.of(stream, new StreamInfoLoadException(throwable));

app/src/main/java/org/schabi/newpipe/player/seekbarpreview/SeekbarPreviewThumbnailHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ public static void tryResizeAndSetSeekbarPreviewThumbnail(
9090
final float scaleFactor = (float) newWidth / srcWidth;
9191
final int newHeight = (int) (previewThumbnail.getHeight() * scaleFactor);
9292

93-
currentSeekbarPreviewThumbnail.setImageBitmap(
94-
BitmapCompat.createScaledBitmap(previewThumbnail, newWidth, newHeight, null,
95-
true));
93+
currentSeekbarPreviewThumbnail.setImageBitmap(BitmapCompat
94+
.createScaledBitmap(previewThumbnail, newWidth, newHeight, null, true));
9695
} catch (final Exception ex) {
9796
Log.e(TAG, "Failed to resize and set seekbar preview thumbnail", ex);
9897
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);

app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,11 @@ public boolean isVerticalVideo() {
862862

863863
@Override
864864
protected void onPlaybackSpeedClicked() {
865-
final AppCompatActivity activity = getParentActivity().orElse(null);
866-
if (activity == null) {
867-
return;
868-
}
869-
870-
PlaybackParameterDialog.newInstance(player.getPlaybackSpeed(), player.getPlaybackPitch(),
871-
player.getPlaybackSkipSilence(), player::setPlaybackParameters)
872-
.show(activity.getSupportFragmentManager(), null);
865+
getParentActivity().ifPresent(activity ->
866+
PlaybackParameterDialog.newInstance(player.getPlaybackSpeed(),
867+
player.getPlaybackPitch(), player.getPlaybackSkipSilence(),
868+
player::setPlaybackParameters)
869+
.show(activity.getSupportFragmentManager(), null));
873870
}
874871

875872
@Override
@@ -969,22 +966,22 @@ public void checkLandscape() {
969966
//////////////////////////////////////////////////////////////////////////*/
970967
//region Getters
971968

969+
private Optional<Context> getParentContext() {
970+
return Optional.ofNullable(binding.getRoot().getParent())
971+
.filter(ViewGroup.class::isInstance)
972+
.map(parent -> ((ViewGroup) parent).getContext());
973+
}
974+
972975
public Optional<AppCompatActivity> getParentActivity() {
973-
final ViewParent rootParent = binding.getRoot().getParent();
974-
if (rootParent instanceof ViewGroup) {
975-
final Context activity = ((ViewGroup) rootParent).getContext();
976-
if (activity instanceof AppCompatActivity) {
977-
return Optional.of((AppCompatActivity) activity);
978-
}
979-
}
980-
return Optional.empty();
976+
return getParentContext()
977+
.filter(AppCompatActivity.class::isInstance)
978+
.map(AppCompatActivity.class::cast);
981979
}
982980

983981
public boolean isLandscape() {
984982
// DisplayMetrics from activity context knows about MultiWindow feature
985983
// while DisplayMetrics from app context doesn't
986-
return DeviceUtils.isLandscape(
987-
getParentActivity().map(Context.class::cast).orElse(player.getService()));
984+
return DeviceUtils.isLandscape(getParentContext().orElse(player.getService()));
988985
}
989986
//endregion
990987
}

0 commit comments

Comments
 (0)