From ddb73e857d7c26fd27ea995a27f53f5660d3f71c Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 9 Nov 2024 22:53:01 +0400 Subject: [PATCH] fix(YouTube - Remember video quality): Correctly set default quality when changing from a low quality video (#3879) --- .../patches/components/ShortsFilter.java | 1 - .../quality/RememberVideoQualityPatch.java | 50 +++++++++---------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/components/ShortsFilter.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/components/ShortsFilter.java index e774733969..ee7b789522 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/components/ShortsFilter.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/components/ShortsFilter.java @@ -416,7 +416,6 @@ public static void hideShortsShareButton(final View shareButtonView) { // endregion public static void setNavigationBar(PivotBar view) { - Logger.printDebug(() -> "Setting navigation bar"); pivotBarRef = new WeakReference<>(view); } diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/quality/RememberVideoQualityPatch.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/quality/RememberVideoQualityPatch.java index 785603895e..f55b36d617 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/quality/RememberVideoQualityPatch.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/quality/RememberVideoQualityPatch.java @@ -62,19 +62,12 @@ private static void changeDefaultQuality(int defaultQuality) { */ public static int setVideoQuality(Object[] qualities, final int originalQualityIndex, Object qInterface, String qIndexMethod) { try { - if (!(qualityNeedsUpdating || userChangedDefaultQuality) || qInterface == null) { - return originalQualityIndex; - } - qualityNeedsUpdating = false; + final int preferredQuality = Utils.getNetworkType() == NetworkType.MOBILE + ? mobileQualitySetting.get() + : wifiQualitySetting.get(); - final int preferredQuality; - if (Utils.getNetworkType() == NetworkType.MOBILE) { - preferredQuality = mobileQualitySetting.get(); - } else { - preferredQuality = wifiQualitySetting.get(); - } if (!userChangedDefaultQuality && preferredQuality == AUTOMATIC_VIDEO_QUALITY_VALUE) { - return originalQualityIndex; // nothing to do + return originalQualityIndex; // Nothing to do. } if (videoQualities == null || videoQualities.size() != qualities.length) { @@ -87,7 +80,11 @@ public static int setVideoQuality(Object[] qualities, final int originalQualityI } } } - Logger.printDebug(() -> "videoQualities: " + videoQualities); + + // After changing videos the qualities can initially be for the prior video. + // So if the qualities have changed an update is needed. + qualityNeedsUpdating = true; + Logger.printDebug(() -> "VideoQualities: " + videoQualities); } if (userChangedDefaultQuality) { @@ -98,7 +95,12 @@ public static int setVideoQuality(Object[] qualities, final int originalQualityI return userSelectedQualityIndex; } - // find the highest quality that is equal to or less than the preferred + if (!qualityNeedsUpdating) { + return originalQualityIndex; + } + qualityNeedsUpdating = false; + + // Find the highest quality that is equal to or less than the preferred. int qualityToUse = videoQualities.get(0); // first element is automatic mode int qualityIndexToUse = 0; int i = 0; @@ -112,20 +114,18 @@ public static int setVideoQuality(Object[] qualities, final int originalQualityI // If the desired quality index is equal to the original index, // then the video is already set to the desired default quality. - // - // The method could return here, but the UI video quality flyout will still - // show 'Auto' (ie: Auto (480p)) - // It appears that "Auto" picks the resolution on video load, - // and it does not appear to change the resolution during playback. - // - // To prevent confusion, set the video index anyways (even if it matches the existing index) - // As that will force the UI picker to not display "Auto" which may confuse the user. + final int qualityToUseFinal = qualityToUse; if (qualityIndexToUse == originalQualityIndex) { - Logger.printDebug(() -> "Video is already preferred quality: " + preferredQuality); + // On first load of a new video, if the UI video quality flyout menu + // is not updated then it will still show 'Auto' (ie: Auto (480p)), + // even though it's already set to the desired resolution. + // + // To prevent confusion, set the video index anyways (even if it matches the existing index) + // as that will force the UI picker to not display "Auto". + Logger.printDebug(() -> "Video is already preferred quality: " + qualityToUseFinal); } else { - final int qualityToUseLog = qualityToUse; - Logger.printDebug(() -> "Quality changed from: " - + videoQualities.get(originalQualityIndex) + " to: " + qualityToUseLog); + Logger.printDebug(() -> "Changing video quality from: " + + videoQualities.get(originalQualityIndex) + " to: " + qualityToUseFinal); } Method m = qInterface.getClass().getMethod(qIndexMethod, Integer.TYPE);