mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 00:20:32 +00:00
Use Optional chaining.
This commit is contained in:
parent
fd55d85bbf
commit
e3062d7c66
@ -2423,23 +2423,20 @@ public final class VideoDetailFragment
|
|||||||
|
|
||||||
// helpers to check the state of player and playerService
|
// helpers to check the state of player and playerService
|
||||||
boolean isPlayerAvailable() {
|
boolean isPlayerAvailable() {
|
||||||
return (player != null);
|
return player != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isPlayerServiceAvailable() {
|
boolean isPlayerServiceAvailable() {
|
||||||
return (playerService != null);
|
return playerService != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isPlayerAndPlayerServiceAvailable() {
|
boolean isPlayerAndPlayerServiceAvailable() {
|
||||||
return (player != null && playerService != null);
|
return player != null && playerService != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<View> getRoot() {
|
public Optional<View> getRoot() {
|
||||||
if (player == null) {
|
return Optional.ofNullable(player)
|
||||||
return Optional.empty();
|
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
|
||||||
}
|
|
||||||
|
|
||||||
return player.UIs().get(VideoPlayerUi.class)
|
|
||||||
.map(playerUi -> playerUi.getBinding().getRoot());
|
.map(playerUi -> playerUi.getBinding().getRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1877,21 +1877,16 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public VideoStream getSelectedVideoStream() {
|
public VideoStream getSelectedVideoStream() {
|
||||||
@Nullable final MediaItemTag.Quality quality = Optional.ofNullable(currentMetadata)
|
return Optional.ofNullable(currentMetadata)
|
||||||
.flatMap(MediaItemTag::getMaybeQuality)
|
.flatMap(MediaItemTag::getMaybeQuality)
|
||||||
|
.filter(quality -> {
|
||||||
|
final int selectedStreamIndex = quality.getSelectedVideoStreamIndex();
|
||||||
|
return selectedStreamIndex >= 0
|
||||||
|
&& selectedStreamIndex < quality.getSortedVideoStreams().size();
|
||||||
|
})
|
||||||
|
.map(quality -> quality.getSortedVideoStreams()
|
||||||
|
.get(quality.getSelectedVideoStreamIndex()))
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
if (quality == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<VideoStream> availableStreams = quality.getSortedVideoStreams();
|
|
||||||
final int selectedStreamIndex = quality.getSelectedVideoStreamIndex();
|
|
||||||
|
|
||||||
if (selectedStreamIndex >= 0 && availableStreams.size() > selectedStreamIndex) {
|
|
||||||
return availableStreams.get(selectedStreamIndex);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
@ -61,12 +61,11 @@ public interface MediaItemTag {
|
|||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
|
static Optional<MediaItemTag> from(@Nullable final MediaItem mediaItem) {
|
||||||
if (mediaItem == null || mediaItem.localConfiguration == null
|
return Optional.ofNullable(mediaItem)
|
||||||
|| !(mediaItem.localConfiguration.tag instanceof MediaItemTag)) {
|
.map(item -> item.localConfiguration)
|
||||||
return Optional.empty();
|
.map(localConfiguration -> localConfiguration.tag)
|
||||||
}
|
.filter(MediaItemTag.class::isInstance)
|
||||||
|
.map(MediaItemTag.class::cast);
|
||||||
return Optional.of((MediaItemTag) mediaItem.localConfiguration.tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@ -7,8 +7,6 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.collection.ArraySet;
|
import androidx.collection.ArraySet;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.source.MediaSource;
|
|
||||||
|
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
@ -27,6 +25,7 @@ import org.schabi.newpipe.util.ServiceHelper;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
@ -422,21 +421,24 @@ public class MediaSourceManager {
|
|||||||
|
|
||||||
private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueueItem stream) {
|
private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueueItem stream) {
|
||||||
return stream.getStream().map(streamInfo -> {
|
return stream.getStream().map(streamInfo -> {
|
||||||
final MediaSource source = playbackListener.sourceOf(stream, streamInfo);
|
final var source = playbackListener.sourceOf(stream, streamInfo);
|
||||||
if (source == null || MediaItemTag.from(source.getMediaItem()).isEmpty()) {
|
|
||||||
final String message = "Unable to resolve source from stream info. "
|
|
||||||
+ "URL: " + stream.getUrl() + ", "
|
|
||||||
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "
|
|
||||||
+ "video count: " + streamInfo.getVideoOnlyStreams().size() + ", "
|
|
||||||
+ streamInfo.getVideoStreams().size();
|
|
||||||
return (ManagedMediaSource)
|
|
||||||
FailedMediaSource.of(stream, new MediaSourceResolutionException(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
final MediaItemTag tag = MediaItemTag.from(source.getMediaItem()).get();
|
return Optional.ofNullable(source)
|
||||||
final long expiration = System.currentTimeMillis()
|
.flatMap(source1 -> MediaItemTag.from(source1.getMediaItem()))
|
||||||
+ ServiceHelper.getCacheExpirationMillis(streamInfo.getServiceId());
|
.<ManagedMediaSource>map(tag -> {
|
||||||
return new LoadedMediaSource(source, tag, stream, expiration);
|
final long expiration = System.currentTimeMillis()
|
||||||
|
+ ServiceHelper.getCacheExpirationMillis(streamInfo.getServiceId());
|
||||||
|
return new LoadedMediaSource(source, tag, stream, expiration);
|
||||||
|
})
|
||||||
|
.orElseGet(() -> {
|
||||||
|
final String message = "Unable to resolve source from stream info. "
|
||||||
|
+ "URL: " + stream.getUrl() + ", "
|
||||||
|
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "
|
||||||
|
+ "video count: " + streamInfo.getVideoOnlyStreams().size() + ", "
|
||||||
|
+ streamInfo.getVideoStreams().size();
|
||||||
|
return FailedMediaSource.of(stream, new MediaSourceResolutionException(
|
||||||
|
message));
|
||||||
|
});
|
||||||
}).onErrorReturn(throwable -> {
|
}).onErrorReturn(throwable -> {
|
||||||
if (throwable instanceof ExtractionException) {
|
if (throwable instanceof ExtractionException) {
|
||||||
return FailedMediaSource.of(stream, new StreamInfoLoadException(throwable));
|
return FailedMediaSource.of(stream, new StreamInfoLoadException(throwable));
|
||||||
|
@ -90,9 +90,8 @@ public final class SeekbarPreviewThumbnailHelper {
|
|||||||
final float scaleFactor = (float) newWidth / srcWidth;
|
final float scaleFactor = (float) newWidth / srcWidth;
|
||||||
final int newHeight = (int) (previewThumbnail.getHeight() * scaleFactor);
|
final int newHeight = (int) (previewThumbnail.getHeight() * scaleFactor);
|
||||||
|
|
||||||
currentSeekbarPreviewThumbnail.setImageBitmap(
|
currentSeekbarPreviewThumbnail.setImageBitmap(BitmapCompat
|
||||||
BitmapCompat.createScaledBitmap(previewThumbnail, newWidth, newHeight, null,
|
.createScaledBitmap(previewThumbnail, newWidth, newHeight, null, true));
|
||||||
true));
|
|
||||||
} catch (final Exception ex) {
|
} catch (final Exception ex) {
|
||||||
Log.e(TAG, "Failed to resize and set seekbar preview thumbnail", ex);
|
Log.e(TAG, "Failed to resize and set seekbar preview thumbnail", ex);
|
||||||
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);
|
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);
|
||||||
|
@ -862,14 +862,11 @@ public final class MainPlayerUi extends VideoPlayerUi implements View.OnLayoutCh
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPlaybackSpeedClicked() {
|
protected void onPlaybackSpeedClicked() {
|
||||||
final AppCompatActivity activity = getParentActivity().orElse(null);
|
getParentActivity().ifPresent(activity ->
|
||||||
if (activity == null) {
|
PlaybackParameterDialog.newInstance(player.getPlaybackSpeed(),
|
||||||
return;
|
player.getPlaybackPitch(), player.getPlaybackSkipSilence(),
|
||||||
}
|
player::setPlaybackParameters)
|
||||||
|
.show(activity.getSupportFragmentManager(), null));
|
||||||
PlaybackParameterDialog.newInstance(player.getPlaybackSpeed(), player.getPlaybackPitch(),
|
|
||||||
player.getPlaybackSkipSilence(), player::setPlaybackParameters)
|
|
||||||
.show(activity.getSupportFragmentManager(), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -969,22 +966,22 @@ public final class MainPlayerUi extends VideoPlayerUi implements View.OnLayoutCh
|
|||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
//region Getters
|
//region Getters
|
||||||
|
|
||||||
|
private Optional<Context> getParentContext() {
|
||||||
|
return Optional.ofNullable(binding.getRoot().getParent())
|
||||||
|
.filter(ViewGroup.class::isInstance)
|
||||||
|
.map(parent -> ((ViewGroup) parent).getContext());
|
||||||
|
}
|
||||||
|
|
||||||
public Optional<AppCompatActivity> getParentActivity() {
|
public Optional<AppCompatActivity> getParentActivity() {
|
||||||
final ViewParent rootParent = binding.getRoot().getParent();
|
return getParentContext()
|
||||||
if (rootParent instanceof ViewGroup) {
|
.filter(AppCompatActivity.class::isInstance)
|
||||||
final Context activity = ((ViewGroup) rootParent).getContext();
|
.map(AppCompatActivity.class::cast);
|
||||||
if (activity instanceof AppCompatActivity) {
|
|
||||||
return Optional.of((AppCompatActivity) activity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLandscape() {
|
public boolean isLandscape() {
|
||||||
// DisplayMetrics from activity context knows about MultiWindow feature
|
// DisplayMetrics from activity context knows about MultiWindow feature
|
||||||
// while DisplayMetrics from app context doesn't
|
// while DisplayMetrics from app context doesn't
|
||||||
return DeviceUtils.isLandscape(
|
return DeviceUtils.isLandscape(getParentContext().orElse(player.getService()));
|
||||||
getParentActivity().map(Context.class::cast).orElse(player.getService()));
|
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user