From 7551f0104c463714749acc6abedbea5cd7afb52f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 28 Jan 2023 11:28:42 +0400 Subject: [PATCH] fix(youtube/return-youtube-dislike): do not fetch voting stats when watching shorts (#302) Signed-off-by: oSumAtrIX Co-authored-by: oSumAtrIX --- .../patches/PlayerTypeHookPatch.java | 6 ++++-- .../ReturnYouTubeDislike.java | 20 +++++++++++++------ .../integrations/shared/PlayerType.kt | 2 +- .../sponsorblock/PlayerController.java | 17 ++++++++-------- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/patches/PlayerTypeHookPatch.java b/app/src/main/java/app/revanced/integrations/patches/PlayerTypeHookPatch.java index 5173cf48..3e31d5ed 100644 --- a/app/src/main/java/app/revanced/integrations/patches/PlayerTypeHookPatch.java +++ b/app/src/main/java/app/revanced/integrations/patches/PlayerTypeHookPatch.java @@ -2,8 +2,8 @@ package app.revanced.integrations.patches; import androidx.annotation.Nullable; -import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.shared.PlayerType; +import app.revanced.integrations.utils.LogHelper; /** * Hook receiver class for 'player-type-hook' patch @@ -24,7 +24,9 @@ public class PlayerTypeHookPatch { // update current player type final PlayerType newType = PlayerType.safeParseFromString(type.toString()); - if (newType != null) { + if (newType == null) { + LogHelper.printException(() -> "Unknown PlayerType encountered: " + type); + } else { PlayerType.setCurrent(newType); LogHelper.printDebug(() -> "YouTubePlayerOverlaysLayout player type was updated to " + newType); } diff --git a/app/src/main/java/app/revanced/integrations/returnyoutubedislike/ReturnYouTubeDislike.java b/app/src/main/java/app/revanced/integrations/returnyoutubedislike/ReturnYouTubeDislike.java index 89e2463c..5d45d30b 100644 --- a/app/src/main/java/app/revanced/integrations/returnyoutubedislike/ReturnYouTubeDislike.java +++ b/app/src/main/java/app/revanced/integrations/returnyoutubedislike/ReturnYouTubeDislike.java @@ -2,7 +2,6 @@ package app.revanced.integrations.returnyoutubedislike; import static app.revanced.integrations.sponsorblock.StringRef.str; -import android.content.Context; import android.icu.text.CompactDecimalFormat; import android.os.Build; import android.text.Spannable; @@ -13,7 +12,6 @@ import android.text.style.CharacterStyle; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.ScaleXSpan; -import android.util.DisplayMetrics; import androidx.annotation.GuardedBy; import androidx.annotation.Nullable; @@ -31,6 +29,7 @@ import java.util.concurrent.atomic.AtomicReference; import app.revanced.integrations.returnyoutubedislike.requests.RYDVoteData; import app.revanced.integrations.returnyoutubedislike.requests.ReturnYouTubeDislikeApi; import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.shared.PlayerType; import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.SharedPrefHelper; @@ -117,8 +116,12 @@ public class ReturnYouTubeDislike { if (!isEnabled) return; try { Objects.requireNonNull(videoId); - LogHelper.printDebug(() -> "New video loaded: " + videoId); - + PlayerType currentPlayerType = PlayerType.getCurrent(); + if (currentPlayerType == PlayerType.INLINE_MINIMAL) { + LogHelper.printDebug(() -> "Ignoring inline playback of video: "+ videoId); + return; + } + LogHelper.printDebug(() -> " new video loaded: " + videoId + " playerType: " + currentPlayerType); synchronized (videoIdLockObject) { currentVideoId = videoId; // no need to wrap the call in a try/catch, @@ -175,8 +178,7 @@ public class ReturnYouTubeDislike { if (updateDislike(textRef, isSegmentedButton, votingData)) { LogHelper.printDebug(() -> "Updated dislike span to: " + textRef.get()); } else { - LogHelper.printDebug(() -> "Ignoring dislike span: " + textRef.get() - + " that appears to already show voting data: " + votingData); + LogHelper.printDebug(() -> "Ignoring already updated dislike span: " + textRef.get()); } } catch (Exception ex) { LogHelper.printException(() -> "Error while trying to update dislikes", ex); @@ -187,7 +189,13 @@ public class ReturnYouTubeDislike { if (!isEnabled) return; try { Objects.requireNonNull(vote); + + if (PlayerType.getCurrent() == PlayerType.NONE) { // should occur if shorts is playing + LogHelper.printDebug(() -> "Ignoring vote during Shorts playback"); + return; + } if (SharedPrefHelper.getBoolean(SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true)) { + LogHelper.printDebug(() -> "User is logged out, ignoring voting"); return; } diff --git a/app/src/main/java/app/revanced/integrations/shared/PlayerType.kt b/app/src/main/java/app/revanced/integrations/shared/PlayerType.kt index 2ea15366..561b3840 100644 --- a/app/src/main/java/app/revanced/integrations/shared/PlayerType.kt +++ b/app/src/main/java/app/revanced/integrations/shared/PlayerType.kt @@ -7,7 +7,7 @@ import app.revanced.integrations.utils.Event */ @Suppress("unused") enum class PlayerType { - NONE, + NONE, // this also includes when shorts are playing HIDDEN, WATCH_WHILE_MINIMIZED, WATCH_WHILE_MAXIMIZED, 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 6d275177..81ef5776 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java @@ -3,7 +3,6 @@ package app.revanced.integrations.sponsorblock; import static app.revanced.integrations.sponsorblock.SponsorBlockUtils.timeWithoutSegments; import static app.revanced.integrations.sponsorblock.SponsorBlockUtils.videoHasSegments; -import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; @@ -21,12 +20,12 @@ import java.util.TimerTask; import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.shared.PlayerType; import app.revanced.integrations.sponsorblock.objects.SponsorSegment; import app.revanced.integrations.sponsorblock.requests.SBRequester; import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; -@SuppressLint({"LongLogTag"}) public class PlayerController { private static final Timer sponsorTimer = new Timer("sponsor-skip-timer"); @@ -43,8 +42,6 @@ public class PlayerController { private static float sponsorBarThickness = 2f; private static TimerTask skipSponsorTask = null; - public static boolean shorts_playing = false; - public static String getCurrentVideoId() { return currentVideoId; } @@ -56,13 +53,19 @@ public class PlayerController { return; } + // currently this runs every time a video is loaded (regardless if sponsorblock is turned on or off) + // FIXME: change this so if sponsorblock is disabled, then run this method exactly once and once only SponsorBlockSettings.update(null); if (!SettingsEnum.SB_ENABLED.getBoolean()) { currentVideoId = null; return; } - + if (PlayerType.getCurrent() == PlayerType.NONE) { + LogHelper.printDebug(() -> "ignoring shorts video"); + currentVideoId = null; + return; + } if (videoId.equals(currentVideoId)) return; @@ -90,9 +93,7 @@ public class PlayerController { public static void executeDownloadSegments(String videoId) { videoHasSegments = false; timeWithoutSegments = ""; - if (shorts_playing) { - return; - } + SponsorSegment[] segments = SBRequester.getSegments(videoId); Arrays.sort(segments);