feat(youtube/sponsorblock): skip segments once automatically (#190)

This commit is contained in:
thebestnom 2022-10-30 00:14:20 +03:00 committed by GitHub
parent d745e29395
commit 06bebd7017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 13 deletions

View File

@ -183,7 +183,7 @@ public class PlayerController {
continue; continue;
// we are in the segment! // 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); sendViewRequestAsync(millis, segment);
skipSegment(segment, false); skipSegment(segment, false);
break; break;
@ -350,26 +350,26 @@ public class PlayerController {
skipToMillisecond(lastKnownVideoTime + 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 // 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 // I put this block so that skip can be made only once per some time
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (now < allowNextSkipRequestTime) { if (now < allowNextSkipRequestTime) {
LogHelper.debug(PlayerController.class, "skipToMillisecond: to fast, slow down, because you'll fail"); LogHelper.debug(PlayerController.class, "skipToMillisecond: to fast, slow down, because you'll fail");
return; return false;
} }
allowNextSkipRequestTime = now + 100; allowNextSkipRequestTime = now + 100;
if (setMillisecondMethod == null) { if (setMillisecondMethod == null) {
LogHelper.printException(PlayerController.class, "setMillisecondMethod is null"); LogHelper.printException(PlayerController.class, "setMillisecondMethod is null");
return; return false;
} }
final Object currentObj = currentPlayerController.get(); final Object currentObj = currentPlayerController.get();
if (currentObj == null) { if (currentObj == null) {
LogHelper.printException(PlayerController.class, "currentObj is null (might have been collected by GC)"); 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())); LogHelper.debug(PlayerController.class, String.format("Requesting skip to millis=%d on thread %s", millisecond, Thread.currentThread().toString()));
@ -385,6 +385,7 @@ public class PlayerController {
LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e); LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e);
} }
}); });
return true;
} }
@ -402,7 +403,7 @@ public class PlayerController {
continue; continue;
SkipSegmentView.show(); SkipSegmentView.show();
if (!(segment.category.behaviour.skip || wasClicked)) if (!((segment.category.behaviour.skip && !(segment.category.behaviour.key.equals("skip-once") && segment.didAutoSkipped)) || wasClicked))
return; return;
sendViewRequestAsync(millis, segment); sendViewRequestAsync(millis, segment);
@ -421,7 +422,10 @@ public class PlayerController {
if (SettingsEnum.SB_SHOW_TOAST_WHEN_SKIP.getBoolean() && !wasClicked) if (SettingsEnum.SB_SHOW_TOAST_WHEN_SKIP.getBoolean() && !wasClicked)
SkipSegmentView.notifySkipped(segment); SkipSegmentView.notifySkipped(segment);
skipToMillisecond(segment.end + 2); boolean didSucceed = skipToMillisecond(segment.end + 2);
if(didSucceed && !wasClicked) {
segment.didAutoSkipped = true;
}
SkipSegmentView.hide(); SkipSegmentView.hide();
if (segment.category == SponsorBlockSettings.SegmentInfo.UNSUBMITTED) { if (segment.category == SponsorBlockSettings.SegmentInfo.UNSUBMITTED) {
SponsorSegment[] newSegments = new SponsorSegment[sponsorSegmentsOfCurrentVideo.length - 1]; SponsorSegment[] newSegments = new SponsorSegment[sponsorSegmentsOfCurrentVideo.length - 1];

View File

@ -96,6 +96,7 @@ public class SponsorBlockSettings {
} }
public enum SegmentBehaviour { public enum SegmentBehaviour {
SKIP_AUTOMATICALLY_ONCE("skip-once", 3, sf("skip_automatically_once"), true, true),
SKIP_AUTOMATICALLY("skip", 2, sf("skip_automatically"), true, true), SKIP_AUTOMATICALLY("skip", 2, sf("skip_automatically"), true, true),
MANUAL_SKIP("manual-skip", 1, sf("skip_showbutton"), false, true), MANUAL_SKIP("manual-skip", 1, sf("skip_showbutton"), false, true),
IGNORE("ignore", -1, sf("skip_ignore"), false, false); IGNORE("ignore", -1, sf("skip_ignore"), false, false);

View File

@ -1,5 +1,9 @@
package app.revanced.integrations.sponsorblock.objects; package app.revanced.integrations.sponsorblock.objects;
import androidx.annotation.NonNull;
import java.text.MessageFormat;
import app.revanced.integrations.sponsorblock.SponsorBlockSettings; import app.revanced.integrations.sponsorblock.SponsorBlockSettings;
public class SponsorSegment implements Comparable<SponsorSegment> { public class SponsorSegment implements Comparable<SponsorSegment> {
@ -8,6 +12,7 @@ public class SponsorSegment implements Comparable<SponsorSegment> {
public final SponsorBlockSettings.SegmentInfo category; public final SponsorBlockSettings.SegmentInfo category;
public final String UUID; public final String UUID;
public final boolean isLocked; public final boolean isLocked;
public boolean didAutoSkipped = false;
public SponsorSegment(long start, long end, SponsorBlockSettings.SegmentInfo category, String UUID, boolean isLocked) { public SponsorSegment(long start, long end, SponsorBlockSettings.SegmentInfo category, String UUID, boolean isLocked) {
this.start = start; this.start = start;
@ -17,14 +22,10 @@ public class SponsorSegment implements Comparable<SponsorSegment> {
this.isLocked = isLocked; this.isLocked = isLocked;
} }
@NonNull
@Override @Override
public String toString() { public String toString() {
return "SegmentInfo{" + return MessageFormat.format("SegmentInfo'{'start={0}, end={1}, category=''{2}'', locked={3}'}'", start, end, category, isLocked);
"start=" + start +
", end=" + end +
", category='" + category + '\'' +
", locked=" + isLocked +
'}';
} }
@Override @Override