55import androidx .room .Entity ;
66import androidx .room .ForeignKey ;
77
8- import java .util .concurrent .TimeUnit ;
9-
108import static androidx .room .ForeignKey .CASCADE ;
119import static org .schabi .newpipe .database .stream .model .StreamStateEntity .JOIN_STREAM_ID ;
1210import static org .schabi .newpipe .database .stream .model .StreamStateEntity .STREAM_STATE_TABLE ;
@@ -30,11 +28,13 @@ public class StreamStateEntity {
3028 /**
3129 * Playback state will not be saved, if playback time is less than this threshold.
3230 */
33- private static final int PLAYBACK_SAVE_THRESHOLD_START_SECONDS = 5 ;
31+ private static final long PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS = 5000 ; // 5000ms = 5s
32+
3433 /**
35- * Playback state will not be saved, if time left is less than this threshold.
34+ * @see #isFinished(long)
35+ * @see org.schabi.newpipe.database.feed.dao.FeedDAO#getLiveOrNotPlayedStreams()
3636 */
37- private static final int PLAYBACK_SAVE_THRESHOLD_END_SECONDS = 10 ;
37+ public static final long PLAYBACK_FINISHED_END_MILLISECONDS = 60000 ; // 60000ms = 60s
3838
3939 @ ColumnInfo (name = JOIN_STREAM_ID )
4040 private long streamUid ;
@@ -63,10 +63,27 @@ public void setProgressTime(final long progressTime) {
6363 this .progressTime = progressTime ;
6464 }
6565
66- public boolean isValid (final int durationInSeconds ) {
67- final int seconds = (int ) TimeUnit .MILLISECONDS .toSeconds (progressTime );
68- return seconds > PLAYBACK_SAVE_THRESHOLD_START_SECONDS
69- && seconds < durationInSeconds - PLAYBACK_SAVE_THRESHOLD_END_SECONDS ;
66+ /**
67+ * The state will be considered valid, and thus be saved, if the progress is more than {@link
68+ * #PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS}.
69+ * @return whether this stream state entity should be saved or not
70+ */
71+ public boolean isValid () {
72+ return progressTime > PLAYBACK_SAVE_THRESHOLD_START_MILLISECONDS ;
73+ }
74+
75+ /**
76+ * The video will be considered as finished, if the time left is less than {@link
77+ * #PLAYBACK_FINISHED_END_MILLISECONDS} and the progress is at least 3/4 of the video length.
78+ * The state will be saved anyway, so that it can be shown under stream info items, but the
79+ * player will not resume if a state is considered as finished. Finished streams are also the
80+ * ones that can be filtered out in the feed fragment.
81+ * @param durationInSeconds the duration of the stream connected with this state, in seconds
82+ * @return whether the stream is finished or not
83+ */
84+ public boolean isFinished (final long durationInSeconds ) {
85+ return progressTime >= durationInSeconds * 1000 - PLAYBACK_FINISHED_END_MILLISECONDS
86+ && progressTime >= durationInSeconds * 1000 * 3 / 4 ;
7087 }
7188
7289 @ Override
0 commit comments