Skip to content

Commit fd55d85

Browse files
Remove SimplifyOptionalCallChains.
1 parent 1bb166a commit fd55d85

6 files changed

Lines changed: 108 additions & 132 deletions

File tree

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,8 @@ public void onServiceConnected(final Player connectedPlayer,
255255
playerUi.ifPresent(MainPlayerUi::toggleFullscreen);
256256
}
257257

258-
//noinspection SimplifyOptionalCallChains
259-
if (playAfterConnect
260-
|| (currentInfo != null
261-
&& isAutoplayEnabled()
262-
&& !playerUi.isPresent())) {
258+
if (playAfterConnect || (currentInfo != null && isAutoplayEnabled()
259+
&& playerUi.isEmpty())) {
263260
autoPlayEnabled = true; // forcefully start playing
264261
openVideoPlayerAutoFullscreen();
265262
}
@@ -1174,16 +1171,15 @@ private void openMainPlayer() {
11741171
* be reused in a few milliseconds and the flickering would be annoying.
11751172
*/
11761173
private void hideMainPlayerOnLoadingNewStream() {
1177-
//noinspection SimplifyOptionalCallChains
1178-
if (!isPlayerServiceAvailable() || !getRoot().isPresent()
1179-
|| !player.videoPlayerSelected()) {
1174+
final var root = getRoot();
1175+
if (!isPlayerServiceAvailable() || root.isEmpty() || !player.videoPlayerSelected()) {
11801176
return;
11811177
}
11821178

11831179
removeVideoPlayerView();
11841180
if (isAutoplayEnabled()) {
11851181
playerService.stopForImmediateReusing();
1186-
getRoot().ifPresent(view -> view.setVisibility(View.GONE));
1182+
root.ifPresent(view -> view.setVisibility(View.GONE));
11871183
} else {
11881184
playerHolder.stopService();
11891185
}
@@ -1887,10 +1883,8 @@ public void onServiceStopped() {
18871883
@Override
18881884
public void onFullscreenStateChanged(final boolean fullscreen) {
18891885
setupBrightness();
1890-
//noinspection SimplifyOptionalCallChains
1891-
if (!isPlayerAndPlayerServiceAvailable()
1892-
|| !player.UIs().get(MainPlayerUi.class).isPresent()
1893-
|| getRoot().map(View::getParent).orElse(null) == null) {
1886+
if (!isPlayerAndPlayerServiceAvailable() || player.UIs().get(MainPlayerUi.class).isEmpty()
1887+
|| getRoot().map(View::getParent).isEmpty()) {
18941888
return;
18951889
}
18961890

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

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,26 +1695,25 @@ private void registerStreamViewed() {
16951695
}
16961696

16971697
private void saveStreamProgressState(final long progressMillis) {
1698-
//noinspection SimplifyOptionalCallChains
1699-
if (!getCurrentStreamInfo().isPresent()
1700-
|| !prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true)) {
1701-
return;
1702-
}
1703-
if (DEBUG) {
1704-
Log.d(TAG, "saveStreamProgressState() called with: progressMillis=" + progressMillis
1705-
+ ", currentMetadata=[" + getCurrentStreamInfo().get().getName() + "]");
1706-
}
1698+
getCurrentStreamInfo().ifPresent(info -> {
1699+
if (!prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true)) {
1700+
return;
1701+
}
1702+
if (DEBUG) {
1703+
Log.d(TAG, "saveStreamProgressState() called with: progressMillis=" + progressMillis
1704+
+ ", currentMetadata=[" + info.getName() + "]");
1705+
}
17071706

1708-
databaseUpdateDisposable
1709-
.add(recordManager.saveStreamState(getCurrentStreamInfo().get(), progressMillis)
1710-
.observeOn(AndroidSchedulers.mainThread())
1711-
.doOnError(e -> {
1712-
if (DEBUG) {
1713-
e.printStackTrace();
1714-
}
1715-
})
1716-
.onErrorComplete()
1717-
.subscribe());
1707+
databaseUpdateDisposable.add(recordManager.saveStreamState(info, progressMillis)
1708+
.observeOn(AndroidSchedulers.mainThread())
1709+
.doOnError(e -> {
1710+
if (DEBUG) {
1711+
e.printStackTrace();
1712+
}
1713+
})
1714+
.onErrorComplete()
1715+
.subscribe());
1716+
});
17181717
}
17191718

17201719
public void saveStreamProgressState() {
@@ -2036,40 +2035,35 @@ public void useVideoSource(final boolean videoEnabled) {
20362035
// in livestreams) so we will be not able to execute the block below.
20372036
// Reload the play queue manager in this case, which is the behavior when we don't know the
20382037
// index of the video renderer or playQueueManagerReloadingNeeded returns true.
2039-
final Optional<StreamInfo> optCurrentStreamInfo = getCurrentStreamInfo();
2040-
if (!optCurrentStreamInfo.isPresent()) {
2041-
reloadPlayQueueManager();
2042-
setRecovery();
2043-
return;
2044-
}
2038+
getCurrentStreamInfo().ifPresentOrElse(info -> {
2039+
// In the case we don't know the source type, fallback to the one with video with audio
2040+
// or audio-only source.
2041+
final SourceType sourceType = videoResolver.getStreamSourceType()
2042+
.orElse(SourceType.VIDEO_WITH_AUDIO_OR_AUDIO_ONLY);
20452043

2046-
final StreamInfo info = optCurrentStreamInfo.get();
2047-
2048-
// In the case we don't know the source type, fallback to the one with video with audio or
2049-
// audio-only source.
2050-
final SourceType sourceType = videoResolver.getStreamSourceType().orElse(
2051-
SourceType.VIDEO_WITH_AUDIO_OR_AUDIO_ONLY);
2052-
2053-
if (playQueueManagerReloadingNeeded(sourceType, info, getVideoRendererIndex())) {
2054-
reloadPlayQueueManager();
2055-
} else {
2056-
if (StreamTypeUtil.isAudio(info.getStreamType())) {
2057-
// Nothing to do more than setting the recovery position
2058-
setRecovery();
2059-
return;
2060-
}
2044+
if (playQueueManagerReloadingNeeded(sourceType, info, getVideoRendererIndex())) {
2045+
reloadPlayQueueManager();
2046+
} else {
2047+
if (StreamTypeUtil.isAudio(info.getStreamType())) {
2048+
// Nothing to do more than setting the recovery position
2049+
setRecovery();
2050+
return;
2051+
}
20612052

2062-
final DefaultTrackSelector.Parameters.Builder parametersBuilder =
2063-
trackSelector.buildUponParameters();
2053+
final var parametersBuilder = trackSelector.buildUponParameters();
20642054

2065-
// Enable/disable the video track and the ability to select subtitles
2066-
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, !videoEnabled);
2067-
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, !videoEnabled);
2055+
// Enable/disable the video track and the ability to select subtitles
2056+
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_TEXT, !videoEnabled);
2057+
parametersBuilder.setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, !videoEnabled);
20682058

2069-
trackSelector.setParameters(parametersBuilder);
2070-
}
2059+
trackSelector.setParameters(parametersBuilder);
2060+
}
20712061

2072-
setRecovery();
2062+
setRecovery();
2063+
}, () -> {
2064+
reloadPlayQueueManager();
2065+
setRecovery();
2066+
});
20732067
}
20742068

20752069
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private void maybeLoadItem(@NonNull final PlayQueueItem item) {
423423
private Single<ManagedMediaSource> getLoadedMediaSource(@NonNull final PlayQueueItem stream) {
424424
return stream.getStream().map(streamInfo -> {
425425
final MediaSource source = playbackListener.sourceOf(stream, streamInfo);
426-
if (source == null || !MediaItemTag.from(source.getMediaItem()).isPresent()) {
426+
if (source == null || MediaItemTag.from(source.getMediaItem()).isEmpty()) {
427427
final String message = "Unable to resolve source from stream info. "
428428
+ "URL: " + stream.getUrl() + ", "
429429
+ "audio count: " + streamInfo.getAudioStreams().size() + ", "

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import androidx.annotation.IntDef;
1010
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
1112
import androidx.core.graphics.BitmapCompat;
1213
import androidx.core.math.MathUtils;
1314
import androidx.preference.PreferenceManager;
@@ -16,7 +17,6 @@
1617
import org.schabi.newpipe.util.DeviceUtils;
1718

1819
import java.lang.annotation.Retention;
19-
import java.util.Optional;
2020
import java.util.function.IntSupplier;
2121

2222
import static java.lang.annotation.RetentionPolicy.SOURCE;
@@ -66,21 +66,19 @@ public static int getSeekbarPreviewThumbnailType(@NonNull final Context context)
6666

6767
public static void tryResizeAndSetSeekbarPreviewThumbnail(
6868
@NonNull final Context context,
69-
@NonNull final Optional<Bitmap> optPreviewThumbnail,
69+
@Nullable final Bitmap previewThumbnail,
7070
@NonNull final ImageView currentSeekbarPreviewThumbnail,
7171
@NonNull final IntSupplier baseViewWidthSupplier) {
72-
73-
if (!optPreviewThumbnail.isPresent()) {
72+
if (previewThumbnail == null) {
7473
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);
7574
return;
7675
}
7776

7877
currentSeekbarPreviewThumbnail.setVisibility(View.VISIBLE);
79-
final Bitmap srcBitmap = optPreviewThumbnail.get();
8078

8179
// Resize original bitmap
8280
try {
83-
final int srcWidth = srcBitmap.getWidth() > 0 ? srcBitmap.getWidth() : 1;
81+
final int srcWidth = previewThumbnail.getWidth() > 0 ? previewThumbnail.getWidth() : 1;
8482
final int newWidth = MathUtils.clamp(
8583
// Use 1/4 of the width for the preview
8684
Math.round(baseViewWidthSupplier.getAsInt() / 4f),
@@ -90,15 +88,16 @@ public static void tryResizeAndSetSeekbarPreviewThumbnail(
9088
Math.round(srcWidth * 2.5f));
9189

9290
final float scaleFactor = (float) newWidth / srcWidth;
93-
final int newHeight = (int) (srcBitmap.getHeight() * scaleFactor);
91+
final int newHeight = (int) (previewThumbnail.getHeight() * scaleFactor);
9492

95-
currentSeekbarPreviewThumbnail.setImageBitmap(BitmapCompat.createScaledBitmap(srcBitmap,
96-
newWidth, newHeight, null, true));
93+
currentSeekbarPreviewThumbnail.setImageBitmap(
94+
BitmapCompat.createScaledBitmap(previewThumbnail, newWidth, newHeight, null,
95+
true));
9796
} catch (final Exception ex) {
9897
Log.e(TAG, "Failed to resize and set seekbar preview thumbnail", ex);
9998
currentSeekbarPreviewThumbnail.setVisibility(View.GONE);
10099
} finally {
101-
srcBitmap.recycle();
100+
previewThumbnail.recycle();
102101
}
103102
}
104103
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.schabi.newpipe.util.external_communication.KoreUtils;
7575
import org.schabi.newpipe.util.external_communication.ShareUtils;
7676

77+
import java.util.Collections;
7778
import java.util.List;
7879
import java.util.Objects;
7980
import java.util.Optional;
@@ -746,15 +747,10 @@ public void onItemLongClick(@NonNull final StreamSegmentItem item, final int sec
746747
}
747748

748749
private int getNearestStreamSegmentPosition(final long playbackPosition) {
749-
//noinspection SimplifyOptionalCallChains
750-
if (!player.getCurrentStreamInfo().isPresent()) {
751-
return 0;
752-
}
753-
754750
int nearestPosition = 0;
755751
final List<StreamSegment> segments = player.getCurrentStreamInfo()
756-
.get()
757-
.getStreamSegments();
752+
.map(StreamInfo::getStreamSegments)
753+
.orElse(Collections.emptyList());
758754

759755
for (int i = 0; i < segments.size(); i++) {
760756
if (segments.get(i).getStartTimeSeconds() * 1000L > playbackPosition) {

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

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public void onProgressChanged(final SeekBar seekBar, final int progress,
566566
SeekbarPreviewThumbnailHelper
567567
.tryResizeAndSetSeekbarPreviewThumbnail(
568568
player.getContext(),
569-
seekbarPreviewThumbnailHolder.getBitmapAt(progress),
569+
seekbarPreviewThumbnailHolder.getBitmapAt(progress).orElse(null),
570570
binding.currentSeekbarPreviewThumbnail,
571571
binding.subtitleView::getWidth);
572572

@@ -982,61 +982,56 @@ public void onMetadataChanged(@NonNull final StreamInfo info) {
982982
}
983983

984984
private void updateStreamRelatedViews() {
985-
//noinspection SimplifyOptionalCallChains
986-
if (!player.getCurrentStreamInfo().isPresent()) {
987-
return;
988-
}
989-
final StreamInfo info = player.getCurrentStreamInfo().get();
990-
991-
binding.qualityTextView.setVisibility(View.GONE);
992-
binding.playbackSpeed.setVisibility(View.GONE);
985+
player.getCurrentStreamInfo().ifPresent(info -> {
986+
binding.qualityTextView.setVisibility(View.GONE);
987+
binding.playbackSpeed.setVisibility(View.GONE);
988+
989+
binding.playbackEndTime.setVisibility(View.GONE);
990+
binding.playbackLiveSync.setVisibility(View.GONE);
991+
992+
switch (info.getStreamType()) {
993+
case AUDIO_STREAM:
994+
case POST_LIVE_AUDIO_STREAM:
995+
binding.surfaceView.setVisibility(View.GONE);
996+
binding.endScreen.setVisibility(View.VISIBLE);
997+
binding.playbackEndTime.setVisibility(View.VISIBLE);
998+
break;
993999

994-
binding.playbackEndTime.setVisibility(View.GONE);
995-
binding.playbackLiveSync.setVisibility(View.GONE);
1000+
case AUDIO_LIVE_STREAM:
1001+
binding.surfaceView.setVisibility(View.GONE);
1002+
binding.endScreen.setVisibility(View.VISIBLE);
1003+
binding.playbackLiveSync.setVisibility(View.VISIBLE);
1004+
break;
9961005

997-
switch (info.getStreamType()) {
998-
case AUDIO_STREAM:
999-
case POST_LIVE_AUDIO_STREAM:
1000-
binding.surfaceView.setVisibility(View.GONE);
1001-
binding.endScreen.setVisibility(View.VISIBLE);
1002-
binding.playbackEndTime.setVisibility(View.VISIBLE);
1003-
break;
1006+
case LIVE_STREAM:
1007+
binding.surfaceView.setVisibility(View.VISIBLE);
1008+
binding.endScreen.setVisibility(View.GONE);
1009+
binding.playbackLiveSync.setVisibility(View.VISIBLE);
1010+
break;
10041011

1005-
case AUDIO_LIVE_STREAM:
1006-
binding.surfaceView.setVisibility(View.GONE);
1007-
binding.endScreen.setVisibility(View.VISIBLE);
1008-
binding.playbackLiveSync.setVisibility(View.VISIBLE);
1009-
break;
1012+
case VIDEO_STREAM:
1013+
case POST_LIVE_STREAM:
1014+
if (player.getCurrentMetadata() != null
1015+
&& player.getCurrentMetadata().getMaybeQuality().isEmpty()
1016+
|| (info.getVideoStreams().isEmpty()
1017+
&& info.getVideoOnlyStreams().isEmpty())) {
1018+
break;
1019+
}
10101020

1011-
case LIVE_STREAM:
1012-
binding.surfaceView.setVisibility(View.VISIBLE);
1013-
binding.endScreen.setVisibility(View.GONE);
1014-
binding.playbackLiveSync.setVisibility(View.VISIBLE);
1015-
break;
1021+
buildQualityMenu();
10161022

1017-
case VIDEO_STREAM:
1018-
case POST_LIVE_STREAM:
1019-
//noinspection SimplifyOptionalCallChains
1020-
if (player.getCurrentMetadata() != null
1021-
&& !player.getCurrentMetadata().getMaybeQuality().isPresent()
1022-
|| (info.getVideoStreams().isEmpty()
1023-
&& info.getVideoOnlyStreams().isEmpty())) {
1023+
binding.qualityTextView.setVisibility(View.VISIBLE);
1024+
binding.surfaceView.setVisibility(View.VISIBLE);
1025+
// fallthrough
1026+
default:
1027+
binding.endScreen.setVisibility(View.GONE);
1028+
binding.playbackEndTime.setVisibility(View.VISIBLE);
10241029
break;
1025-
}
1026-
1027-
buildQualityMenu();
1028-
1029-
binding.qualityTextView.setVisibility(View.VISIBLE);
1030-
binding.surfaceView.setVisibility(View.VISIBLE);
1031-
// fallthrough
1032-
default:
1033-
binding.endScreen.setVisibility(View.GONE);
1034-
binding.playbackEndTime.setVisibility(View.VISIBLE);
1035-
break;
1036-
}
1030+
}
10371031

1038-
buildPlaybackSpeedMenu();
1039-
binding.playbackSpeed.setVisibility(View.VISIBLE);
1032+
buildPlaybackSpeedMenu();
1033+
binding.playbackSpeed.setVisibility(View.VISIBLE);
1034+
});
10401035
}
10411036
//endregion
10421037

@@ -1198,8 +1193,7 @@ public boolean onMenuItemClick(@NonNull final MenuItem menuItem) {
11981193
if (menuItem.getGroupId() == POPUP_MENU_ID_QUALITY) {
11991194
final int menuItemIndex = menuItem.getItemId();
12001195
@Nullable final MediaItemTag currentMetadata = player.getCurrentMetadata();
1201-
//noinspection SimplifyOptionalCallChains
1202-
if (currentMetadata == null || !currentMetadata.getMaybeQuality().isPresent()) {
1196+
if (currentMetadata == null || currentMetadata.getMaybeQuality().isEmpty()) {
12031197
return true;
12041198
}
12051199

@@ -1300,9 +1294,8 @@ public void onTextTracksChanged(@NonNull final Tracks currentTracks) {
13001294

13011295
// Build UI
13021296
buildCaptionMenu(availableLanguages);
1303-
//noinspection SimplifyOptionalCallChains
13041297
if (player.getTrackSelector().getParameters().getRendererDisabled(
1305-
player.getCaptionRendererIndex()) || !selectedTracks.isPresent()) {
1298+
player.getCaptionRendererIndex()) || selectedTracks.isEmpty()) {
13061299
binding.captionTextView.setText(R.string.caption_none);
13071300
} else {
13081301
binding.captionTextView.setText(selectedTracks.get().language);

0 commit comments

Comments
 (0)