Skip to content

Commit 73855ca

Browse files
committed
Use StreamTypeUtil where possible and add isAudio and isVideo to this utility class
1 parent 8dad6d7 commit 73855ca

6 files changed

Lines changed: 44 additions & 35 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import org.schabi.newpipe.database.BasicDAO
1212
import org.schabi.newpipe.database.stream.model.StreamEntity
1313
import org.schabi.newpipe.database.stream.model.StreamEntity.Companion.STREAM_ID
1414
import org.schabi.newpipe.extractor.stream.StreamType
15-
import org.schabi.newpipe.extractor.stream.StreamType.AUDIO_LIVE_STREAM
16-
import org.schabi.newpipe.extractor.stream.StreamType.LIVE_STREAM
15+
import org.schabi.newpipe.util.StreamTypeUtil
1716
import java.time.OffsetDateTime
1817

1918
@Dao
@@ -91,8 +90,7 @@ abstract class StreamDAO : BasicDAO<StreamEntity> {
9190
?: throw IllegalStateException("Stream cannot be null just after insertion.")
9291
newerStream.uid = existentMinimalStream.uid
9392

94-
val isNewerStreamLive = newerStream.streamType == AUDIO_LIVE_STREAM || newerStream.streamType == LIVE_STREAM
95-
if (!isNewerStreamLive) {
93+
if (!StreamTypeUtil.isLiveStream(newerStream.streamType)) {
9694

9795
// Use the existent upload date if the newer stream does not have a better precision
9896
// (i.e. is an approximation). This is done to prevent unnecessary changes.

app/src/main/java/org/schabi/newpipe/info_list/dialog/InfoItemDialog.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2525
import org.schabi.newpipe.extractor.stream.StreamType;
2626
import org.schabi.newpipe.player.helper.PlayerHolder;
27+
import org.schabi.newpipe.util.StreamTypeUtil;
2728
import org.schabi.newpipe.util.external_communication.KoreUtils;
2829

2930
import java.util.ArrayList;
@@ -269,8 +270,7 @@ public Builder addEnqueueEntriesIfNeeded() {
269270
*/
270271
public Builder addStartHereEntries() {
271272
addEntry(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND);
272-
if (infoItem.getStreamType() != StreamType.AUDIO_STREAM
273-
&& infoItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
273+
if (!StreamTypeUtil.isAudio(infoItem.getStreamType())) {
274274
addEntry(StreamDialogDefaultEntry.START_HERE_ON_POPUP);
275275
}
276276
return this;
@@ -285,9 +285,7 @@ public Builder addMarkAsWatchedEntryIfNeeded() {
285285
final boolean isWatchHistoryEnabled = PreferenceManager
286286
.getDefaultSharedPreferences(context)
287287
.getBoolean(context.getString(R.string.enable_watch_history_key), false);
288-
if (isWatchHistoryEnabled
289-
&& infoItem.getStreamType() != StreamType.LIVE_STREAM
290-
&& infoItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
288+
if (isWatchHistoryEnabled && !StreamTypeUtil.isLiveStream(infoItem.getStreamType())) {
291289
addEntry(StreamDialogDefaultEntry.MARK_AS_WATCHED);
292290
}
293291
return this;

app/src/main/java/org/schabi/newpipe/info_list/holder/StreamMiniInfoItemHolder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import org.schabi.newpipe.database.stream.model.StreamStateEntity;
1212
import org.schabi.newpipe.extractor.InfoItem;
1313
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
14-
import org.schabi.newpipe.extractor.stream.StreamType;
1514
import org.schabi.newpipe.info_list.InfoItemBuilder;
1615
import org.schabi.newpipe.ktx.ViewUtils;
1716
import org.schabi.newpipe.local.history.HistoryRecordManager;
1817
import org.schabi.newpipe.util.PicassoHelper;
1918
import org.schabi.newpipe.util.Localization;
19+
import org.schabi.newpipe.util.StreamTypeUtil;
2020
import org.schabi.newpipe.views.AnimatedProgressBar;
2121

2222
import java.util.concurrent.TimeUnit;
@@ -70,8 +70,7 @@ public void updateFromItem(final InfoItem infoItem,
7070
} else {
7171
itemProgressView.setVisibility(View.GONE);
7272
}
73-
} else if (item.getStreamType() == StreamType.LIVE_STREAM
74-
|| item.getStreamType() == StreamType.AUDIO_LIVE_STREAM) {
73+
} else if (StreamTypeUtil.isLiveStream(item.getStreamType())) {
7574
itemDurationView.setText(R.string.duration_live);
7675
itemDurationView.setBackgroundColor(ContextCompat.getColor(itemBuilder.getContext(),
7776
R.color.live_duration_background_color));
@@ -115,8 +114,7 @@ public void updateState(final InfoItem infoItem,
115114
final StreamStateEntity state
116115
= historyRecordManager.loadStreamState(infoItem).blockingGet()[0];
117116
if (state != null && item.getDuration() > 0
118-
&& item.getStreamType() != StreamType.LIVE_STREAM
119-
&& item.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
117+
&& !StreamTypeUtil.isLiveStream(item.getStreamType())) {
120118
itemProgressView.setMax((int) item.getDuration());
121119
if (itemProgressView.getVisibility() == View.VISIBLE) {
122120
itemProgressView.setProgressAnimated((int) TimeUnit.MILLISECONDS

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4234,10 +4234,7 @@ private void useVideoSource(final boolean videoEnabled) {
42344234
if (playQueueManagerReloadingNeeded(sourceType, info, getVideoRendererIndex())) {
42354235
reloadPlayQueueManager();
42364236
} else {
4237-
final StreamType streamType = info.getStreamType();
4238-
if (streamType == StreamType.AUDIO_STREAM
4239-
|| streamType == StreamType.AUDIO_LIVE_STREAM
4240-
|| streamType == StreamType.POST_LIVE_AUDIO_STREAM) {
4237+
if (StreamTypeUtil.isAudio(info.getStreamType())) {
42414238
// Nothing to do more than setting the recovery position
42424239
setRecovery();
42434240
return;
@@ -4296,21 +4293,17 @@ private boolean playQueueManagerReloadingNeeded(final SourceType sourceType,
42964293
@NonNull final StreamInfo streamInfo,
42974294
final int videoRendererIndex) {
42984295
final StreamType streamType = streamInfo.getStreamType();
4296+
final boolean isStreamTypeAudio = StreamTypeUtil.isAudio(streamType);
42994297

4300-
if (videoRendererIndex == RENDERER_UNAVAILABLE && streamType != StreamType.AUDIO_STREAM
4301-
&& streamType != StreamType.AUDIO_LIVE_STREAM
4302-
&& streamType != StreamType.POST_LIVE_AUDIO_STREAM) {
4298+
if (videoRendererIndex == RENDERER_UNAVAILABLE && !isStreamTypeAudio) {
43034299
return true;
43044300
}
43054301

43064302
// The content is an audio stream, an audio live stream, or a live stream with a live
43074303
// source: it's not needed to reload the play queue manager because the stream source will
43084304
// be the same
4309-
if ((streamType == StreamType.AUDIO_STREAM
4310-
|| streamType == StreamType.POST_LIVE_AUDIO_STREAM
4311-
|| streamType == StreamType.AUDIO_LIVE_STREAM)
4312-
|| (streamType == StreamType.LIVE_STREAM
4313-
&& sourceType == SourceType.LIVE_STREAM)) {
4305+
if (isStreamTypeAudio || (streamType == StreamType.LIVE_STREAM
4306+
&& sourceType == SourceType.LIVE_STREAM)) {
43144307
return false;
43154308
}
43164309

@@ -4324,9 +4317,7 @@ private boolean playQueueManagerReloadingNeeded(final SourceType sourceType,
43244317
&& isNullOrEmpty(streamInfo.getAudioStreams()))) {
43254318
// It's not needed to reload the play queue manager only if the content's stream type
43264319
// is a video stream, a live stream or an ended live stream
4327-
return streamType != StreamType.VIDEO_STREAM
4328-
&& streamType != StreamType.LIVE_STREAM
4329-
&& streamType != StreamType.POST_LIVE_STREAM;
4320+
return !StreamTypeUtil.isVideo(streamType);
43304321
}
43314322

43324323
// Other cases: the play queue manager reload is needed

app/src/main/java/org/schabi/newpipe/util/SparseItemUtil.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.schabi.newpipe.util;
22

3-
import static org.schabi.newpipe.extractor.stream.StreamType.AUDIO_LIVE_STREAM;
4-
import static org.schabi.newpipe.extractor.stream.StreamType.LIVE_STREAM;
53
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
64

75
import android.content.Context;
@@ -49,8 +47,8 @@ private SparseItemUtil() {
4947
public static void fetchItemInfoIfSparse(@NonNull final Context context,
5048
@NonNull final StreamInfoItem item,
5149
@NonNull final Consumer<SinglePlayQueue> callback) {
52-
if (((item.getStreamType() == LIVE_STREAM || item.getStreamType() == AUDIO_LIVE_STREAM)
53-
|| item.getDuration() >= 0) && !isNullOrEmpty(item.getUploaderUrl())) {
50+
if ((StreamTypeUtil.isLiveStream(item.getStreamType()) || item.getDuration() >= 0)
51+
&& !isNullOrEmpty(item.getUploaderUrl())) {
5452
// if the duration is >= 0 (provided that the item is not a livestream) and there is an
5553
// uploader url, probably all info is already there, so there is no need to fetch it
5654
callback.accept(new SinglePlayQueue(item));

app/src/main/java/org/schabi/newpipe/util/StreamTypeUtil.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,34 @@ private StreamTypeUtil() {
1414
* Check if the {@link StreamType} of a stream is a livestream.
1515
*
1616
* @param streamType the stream type of the stream
17-
* @return <code>true</code> if the streamType is a
18-
* {@link StreamType#LIVE_STREAM} or {@link StreamType#AUDIO_LIVE_STREAM}
17+
* @return whether the stream type is {@link StreamType#AUDIO_STREAM},
18+
* {@link StreamType#AUDIO_LIVE_STREAM} or {@link StreamType#POST_LIVE_AUDIO_STREAM}
19+
*/
20+
public static boolean isAudio(final StreamType streamType) {
21+
return streamType == StreamType.AUDIO_STREAM
22+
|| streamType == StreamType.AUDIO_LIVE_STREAM
23+
|| streamType == StreamType.POST_LIVE_AUDIO_STREAM;
24+
}
25+
26+
/**
27+
* Check if the {@link StreamType} of a stream is a livestream.
28+
*
29+
* @param streamType the stream type of the stream
30+
* @return whether the stream type is {@link StreamType#VIDEO_STREAM},
31+
* {@link StreamType#LIVE_STREAM} or {@link StreamType#POST_LIVE_STREAM}
32+
*/
33+
public static boolean isVideo(final StreamType streamType) {
34+
return streamType == StreamType.VIDEO_STREAM
35+
|| streamType == StreamType.LIVE_STREAM
36+
|| streamType == StreamType.POST_LIVE_STREAM;
37+
}
38+
39+
/**
40+
* Check if the {@link StreamType} of a stream is a livestream.
41+
*
42+
* @param streamType the stream type of the stream
43+
* @return whether the stream type is {@link StreamType#LIVE_STREAM} or
44+
* {@link StreamType#AUDIO_LIVE_STREAM}
1945
*/
2046
public static boolean isLiveStream(final StreamType streamType) {
2147
return streamType == StreamType.LIVE_STREAM

0 commit comments

Comments
 (0)