From 06bebd7017980880765a5735b1dc0502a01c5037 Mon Sep 17 00:00:00 2001 From: thebestnom Date: Sun, 30 Oct 2022 00:14:20 +0300 Subject: [PATCH] feat(youtube/sponsorblock): skip segments once automatically (#190) --- .../sponsorblock/PlayerController.java | 18 +++++++++++------- .../sponsorblock/SponsorBlockSettings.java | 1 + .../sponsorblock/objects/SponsorSegment.java | 13 +++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java b/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java index 4c9e356e..053ec187 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java @@ -183,7 +183,7 @@ public class PlayerController { 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; @@ -350,26 +350,26 @@ public class PlayerController { 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())); @@ -385,6 +385,7 @@ public class PlayerController { LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e); } }); + return true; } @@ -402,7 +403,7 @@ public class PlayerController { 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); @@ -421,7 +422,10 @@ public class PlayerController { 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]; diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockSettings.java b/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockSettings.java index 7e5f05de..c4f7cf76 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockSettings.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/SponsorBlockSettings.java @@ -96,6 +96,7 @@ public class SponsorBlockSettings { } 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); diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java b/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java index d39961b2..585cd7ae 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java @@ -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 { @@ -8,6 +12,7 @@ public class SponsorSegment implements Comparable { 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; @@ -17,14 +22,10 @@ public class SponsorSegment implements Comparable { 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