Skip to content

Commit

Permalink
feat(youtube/sponsorblock): skip segments once automatically (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebestnom committed Oct 29, 2022
1 parent d745e29 commit 06bebd7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Expand Up @@ -183,7 +183,7 @@ public void run() {
continue;

// we are in the segment!
if (segment.category.behaviour.skip) {
if (segment.category.behaviour.skip && !(segment.category.behaviour.key.equals("skip-once") && segment.didAutoSkipped)) {
sendViewRequestAsync(millis, segment);
skipSegment(segment, false);
break;
Expand Down Expand Up @@ -350,26 +350,26 @@ public static void skipRelativeMilliseconds(int millisRelative) {
skipToMillisecond(lastKnownVideoTime + millisRelative);
}

public static void skipToMillisecond(long millisecond) {
public static boolean skipToMillisecond(long millisecond) {
// in 15.x if sponsor clip hits the end, then it crashes the app, because of too many function invocations
// I put this block so that skip can be made only once per some time
long now = System.currentTimeMillis();
if (now < allowNextSkipRequestTime) {
LogHelper.debug(PlayerController.class, "skipToMillisecond: to fast, slow down, because you'll fail");
return;
return false;
}
allowNextSkipRequestTime = now + 100;

if (setMillisecondMethod == null) {
LogHelper.printException(PlayerController.class, "setMillisecondMethod is null");
return;
return false;
}


final Object currentObj = currentPlayerController.get();
if (currentObj == null) {
LogHelper.printException(PlayerController.class, "currentObj is null (might have been collected by GC)");
return;
return false;
}

LogHelper.debug(PlayerController.class, String.format("Requesting skip to millis=%d on thread %s", millisecond, Thread.currentThread().toString()));
Expand All @@ -385,6 +385,7 @@ public static void skipToMillisecond(long millisecond) {
LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e);
}
});
return true;
}


Expand All @@ -402,7 +403,7 @@ private static void findAndSkipSegment(boolean wasClicked) {
continue;

SkipSegmentView.show();
if (!(segment.category.behaviour.skip || wasClicked))
if (!((segment.category.behaviour.skip && !(segment.category.behaviour.key.equals("skip-once") && segment.didAutoSkipped)) || wasClicked))
return;

sendViewRequestAsync(millis, segment);
Expand All @@ -421,7 +422,10 @@ private static void skipSegment(SponsorSegment segment, boolean wasClicked) {
if (SettingsEnum.SB_SHOW_TOAST_WHEN_SKIP.getBoolean() && !wasClicked)
SkipSegmentView.notifySkipped(segment);

skipToMillisecond(segment.end + 2);
boolean didSucceed = skipToMillisecond(segment.end + 2);
if(didSucceed && !wasClicked) {
segment.didAutoSkipped = true;
}
SkipSegmentView.hide();
if (segment.category == SponsorBlockSettings.SegmentInfo.UNSUBMITTED) {
SponsorSegment[] newSegments = new SponsorSegment[sponsorSegmentsOfCurrentVideo.length - 1];
Expand Down
Expand Up @@ -96,6 +96,7 @@ public static void update(Context context) {
}

public enum SegmentBehaviour {
SKIP_AUTOMATICALLY_ONCE("skip-once", 3, sf("skip_automatically_once"), true, true),
SKIP_AUTOMATICALLY("skip", 2, sf("skip_automatically"), true, true),
MANUAL_SKIP("manual-skip", 1, sf("skip_showbutton"), false, true),
IGNORE("ignore", -1, sf("skip_ignore"), false, false);
Expand Down
@@ -1,5 +1,9 @@
package app.revanced.integrations.sponsorblock.objects;

import androidx.annotation.NonNull;

import java.text.MessageFormat;

import app.revanced.integrations.sponsorblock.SponsorBlockSettings;

public class SponsorSegment implements Comparable<SponsorSegment> {
Expand All @@ -8,6 +12,7 @@ public class SponsorSegment implements Comparable<SponsorSegment> {
public final SponsorBlockSettings.SegmentInfo category;
public final String UUID;
public final boolean isLocked;
public boolean didAutoSkipped = false;

public SponsorSegment(long start, long end, SponsorBlockSettings.SegmentInfo category, String UUID, boolean isLocked) {
this.start = start;
Expand All @@ -17,14 +22,10 @@ public SponsorSegment(long start, long end, SponsorBlockSettings.SegmentInfo cat
this.isLocked = isLocked;
}

@NonNull
@Override
public String toString() {
return "SegmentInfo{" +
"start=" + start +
", end=" + end +
", category='" + category + '\'' +
", locked=" + isLocked +
'}';
return MessageFormat.format("SegmentInfo'{'start={0}, end={1}, category=''{2}'', locked={3}'}'", start, end, category, isLocked);
}

@Override
Expand Down

0 comments on commit 06bebd7

Please sign in to comment.