From 6656f1a030dc0066a83954b5c1498eda478d679b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20Gia=20B=E1=BA=A3o?= <70064328+YT-Advanced@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:28:37 +0700 Subject: [PATCH] fix(Default Video Quality): Video quality is not changeable if default ones is already set https://github.com/YT-Advanced/RVX_BUG_CENTER/issues/16 https://github.com/YT-Advanced/ReX-patches/issues/80 --- .../patches/video/VideoQualityPatch.java | 53 ++++++++++++++----- .../youtube/utils/VideoHelpers.java | 2 +- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/video/VideoQualityPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/video/VideoQualityPatch.java index 5dac8f9b8a..bfee3b7585 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/video/VideoQualityPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/video/VideoQualityPatch.java @@ -3,6 +3,7 @@ import static app.revanced.integrations.youtube.utils.StringRef.str; import static app.revanced.integrations.youtube.utils.VideoHelpers.getCurrentQuality; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.lang.reflect.Field; @@ -18,6 +19,20 @@ public class VideoQualityPatch { private static final SettingsEnum mobileQualitySetting = SettingsEnum.DEFAULT_VIDEO_QUALITY_MOBILE; private static final SettingsEnum wifiQualitySetting = SettingsEnum.DEFAULT_VIDEO_QUALITY_WIFI; + private static Boolean useCustomQuality = false; + @Nullable + private static int customQuality; + + /** + * Injection point. + * + * @param ignoredVideoId id of the current video + */ + public static void initialize(@NonNull String ignoredVideoId) { + useCustomQuality = false; + customQuality = 0; + } + /** * The available qualities of the current video in human readable form: [1080, 720, 480] */ @@ -25,6 +40,9 @@ public class VideoQualityPatch { private static List videoQualities; private static void changeDefaultQuality(final int defaultQuality) { + // When user manual change the quality, force this quality until video ended. + overrideCustomQuality(defaultQuality); + if (!SettingsEnum.ENABLE_SAVE_VIDEO_QUALITY.getBoolean()) return; @@ -42,6 +60,27 @@ private static void changeDefaultQuality(final int defaultQuality) { ReVancedUtils.showToastShort(str("revanced_save_video_quality_" + networkType.getName(), defaultQuality + "p")); } + public static void overrideCustomQuality(final int defaultQuality) { + customQuality = defaultQuality; + useCustomQuality = true; + overrideQuality(defaultQuality); + } + + public static void overideDefaultVideoQuality() { + final int preferredQuality = + ReVancedUtils.getNetworkType() == ReVancedUtils.NetworkType.MOBILE + ? mobileQualitySetting.getInt() + : wifiQualitySetting.getInt(); + + if (preferredQuality == -2) + return; + + ReVancedUtils.runOnMainThreadDelayed(() -> + setVideoQuality((useCustomQuality) ? customQuality + : preferredQuality) + , 300); + } + /** * Injection point. * @@ -88,18 +127,6 @@ private static void setVideoQuality(int preferredQuality) { overrideQuality(preferredQuality); } - public static void forceVideoQuality() { - final int preferredQuality = - ReVancedUtils.getNetworkType() == ReVancedUtils.NetworkType.MOBILE - ? mobileQualitySetting.getInt() - : wifiQualitySetting.getInt(); - - if (preferredQuality == -2) - return; - - ReVancedUtils.runOnMainThreadDelayed(() -> setVideoQuality(preferredQuality), 300); - } - /** * Injection point. New quality menu. * @@ -123,4 +150,4 @@ public static void userChangedQualityIndex(final int selectedQualityIndex) { changeDefaultQuality(videoQualities.get(selectedQualityIndex)); } -} \ No newline at end of file +} diff --git a/app/src/main/java/app/revanced/integrations/youtube/utils/VideoHelpers.java b/app/src/main/java/app/revanced/integrations/youtube/utils/VideoHelpers.java index 0c58a7acaf..49f7eebd62 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/utils/VideoHelpers.java +++ b/app/src/main/java/app/revanced/integrations/youtube/utils/VideoHelpers.java @@ -46,7 +46,7 @@ public static void onQualityChanges(String quality) { if (currentQuality == quality) return; currentQuality = quality; - VideoQualityPatch.forceVideoQuality(); + VideoQualityPatch.overideDefaultVideoQuality(); } public static void copyUrl(boolean withTimestamp) {