From c97d1b7ee5be6a0f097f2995321608bc74f5822c Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Tue, 9 May 2023 19:50:56 +0400 Subject: [PATCH] fix(youtube/remember-video-quality): fix default video quality/speed being applied when resuming app (#392) --- .../patches/VideoInformation.java | 22 +++++++-------- .../quality/RememberVideoQualityPatch.java | 27 +++---------------- .../speed/RememberPlaybackSpeedPatch.java | 14 +++------- .../SegmentPlaybackController.java | 2 +- 4 files changed, 19 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java b/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java index 13149541..e5822fa7 100644 --- a/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java +++ b/app/src/main/java/app/revanced/integrations/patches/VideoInformation.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import java.lang.ref.WeakReference; import java.lang.reflect.Method; +import java.util.Objects; import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch; import app.revanced.integrations.utils.LogHelper; @@ -16,7 +17,7 @@ public final class VideoInformation { private static final float DEFAULT_YOUTUBE_PLAYBACK_SPEED = 1.0f; private static final String SEEK_METHOD_NAME = "seekTo"; - private static WeakReference playerController; + private static WeakReference playerControllerRef; private static Method seekMethod; @NonNull @@ -30,17 +31,17 @@ public final class VideoInformation { /** * Injection point. - * Sets a reference to the YouTube playback controller. * - * @param thisRef Reference to the player controller object. + * @param playerController player controller object. */ - public static void playerController_onCreateHook(final Object thisRef) { - playerController = new WeakReference<>(thisRef); - videoLength = 0; - videoTime = -1; - + public static void initialize(@NonNull Object playerController) { try { - seekMethod = thisRef.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE); + playerControllerRef = new WeakReference<>(Objects.requireNonNull(playerController)); + videoTime = -1; + videoLength = 0; + playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED; + + seekMethod = playerController.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE); seekMethod.setAccessible(true); } catch (Exception ex) { LogHelper.printException(() -> "Failed to initialize", ex); @@ -56,7 +57,6 @@ public final class VideoInformation { if (!videoId.equals(newlyLoadedVideoId)) { LogHelper.printDebug(() -> "New video id: " + newlyLoadedVideoId); videoId = newlyLoadedVideoId; - playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED; } } @@ -124,7 +124,7 @@ public final class VideoInformation { try { LogHelper.printDebug(() -> "Seeking to " + millisecond); - return (Boolean) seekMethod.invoke(playerController.get(), millisecond); + return (Boolean) seekMethod.invoke(playerControllerRef.get(), millisecond); } catch (Exception ex) { LogHelper.printException(() -> "Failed to seek", ex); return false; diff --git a/app/src/main/java/app/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch.java b/app/src/main/java/app/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch.java index df85317d..8da4a4af 100644 --- a/app/src/main/java/app/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch.java +++ b/app/src/main/java/app/revanced/integrations/patches/playback/quality/RememberVideoQualityPatch.java @@ -2,7 +2,6 @@ package app.revanced.integrations.patches.playback.quality; import static app.revanced.integrations.utils.ReVancedUtils.NetworkType; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.lang.reflect.Field; @@ -20,8 +19,6 @@ public class RememberVideoQualityPatch { private static final SettingsEnum mobileQualitySetting = SettingsEnum.VIDEO_QUALITY_DEFAULT_MOBILE; private static boolean qualityNeedsUpdating; - @Nullable - private static String currentVideoId; /** * If the user selected a new quality from the flyout menu, @@ -91,7 +88,7 @@ public class RememberVideoQualityPatch { } } } - LogHelper.printDebug(() -> "VideoId: " + currentVideoId + " videoQualities: " + videoQualities); + LogHelper.printDebug(() -> "videoQualities: " + videoQualities); } if (userChangedDefaultQuality) { @@ -144,25 +141,9 @@ public class RememberVideoQualityPatch { /** * Injection point. */ - public static void newVideoStarted(@NonNull String videoId) { - // The same videoId can be passed in multiple times for a single video playback. - // Such as closing and opening the app, and sometimes when turning off/on the device screen. - // - // Known limitation, if: - // 1. a default video quality exists, and remember quality is turned off - // 2. user opens a video - // 3. user changes the video quality - // 4. user turns off then on the device screen (or does anything else that triggers the video id hook) - // result: the video quality of the current video will revert back to the saved default - // - // qualityNeedsUpdating could be set only when the videoId changes - // but then if the user closes and re-opens the same video the default video quality will not be applied. - LogHelper.printDebug(() -> "newVideoStarted: " + videoId); + public static void newVideoStarted(Object ignoredPlayerController) { + LogHelper.printDebug(() -> "newVideoStarted"); qualityNeedsUpdating = true; - - if (!videoId.equals(currentVideoId)) { - currentVideoId = videoId; - videoQualities = null; - } + videoQualities = null; } } diff --git a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java index 4ccd457f..5b96940d 100644 --- a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java +++ b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java @@ -2,11 +2,9 @@ package app.revanced.integrations.patches.playback.speed; import android.preference.ListPreference; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; public final class RememberPlaybackSpeedPatch { @@ -16,18 +14,12 @@ public final class RememberPlaybackSpeedPatch { */ private static String[] preferenceListEntries, preferenceListEntryValues; - @Nullable - private static String currentVideoId; /** * Injection point. - * Called when a new video loads. */ - public static void newVideoLoaded(@NonNull String videoId) { - if (videoId.equals(currentVideoId)) { - return; - } - currentVideoId = videoId; + public static void newVideoStarted(Object ignoredPlayerController) { + LogHelper.printDebug(() -> "newVideoStarted"); VideoInformation.overridePlaybackSpeed(SettingsEnum.PLAYBACK_SPEED_DEFAULT.getFloat()); } diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java b/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java index 9a50ec6a..eac791f9 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/SegmentPlaybackController.java @@ -177,7 +177,7 @@ public class SegmentPlaybackController { * Injection point. * Initializes SponsorBlock when the video player starts playing a new video. */ - public static void initialize(Object _o) { + public static void initialize(Object ignoredPlayerController) { try { ReVancedUtils.verifyOnMainThread(); SponsorBlockSettings.initialize();