From 029aee8023f096413fc80a2c583b4fe55ecb10ac Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Thu, 26 Dec 2024 13:58:29 +0400 Subject: [PATCH] fix(YouTube - Force original audio): If stream spoofing to Android then show a summary text why force audio is not available (#4220) --- .../shared/spoof/SpoofVideoStreamsPatch.java | 18 ++++------ .../patches/ForceOriginalAudioPatch.java | 16 +++++++++ .../extension/youtube/settings/Settings.java | 4 +-- .../ForceOriginalAudioSwitchPreference.java | 36 +++++++++++++++++++ .../video/audio/ForceOriginalAudioPatch.kt | 5 ++- .../resources/addresources/values/strings.xml | 1 + 6 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ForceOriginalAudioSwitchPreference.java diff --git a/extensions/shared/library/src/main/java/app/revanced/extension/shared/spoof/SpoofVideoStreamsPatch.java b/extensions/shared/library/src/main/java/app/revanced/extension/shared/spoof/SpoofVideoStreamsPatch.java index 2595ca166..ef11885da 100644 --- a/extensions/shared/library/src/main/java/app/revanced/extension/shared/spoof/SpoofVideoStreamsPatch.java +++ b/extensions/shared/library/src/main/java/app/revanced/extension/shared/spoof/SpoofVideoStreamsPatch.java @@ -34,6 +34,12 @@ public class SpoofVideoStreamsPatch { return false; // Modified during patching. } + public static boolean notSpoofingToAndroid() { + return !isPatchIncluded() + || !BaseSettings.SPOOF_VIDEO_STREAMS.get() + || BaseSettings.SPOOF_VIDEO_STREAMS_CLIENT_TYPE.get() == ClientType.IOS_UNPLUGGED; + } + /** * Injection point. * Blocks /get_watch requests by returning an unreachable URI. @@ -206,18 +212,6 @@ public class SpoofVideoStreamsPatch { return videoFormat; } - public static final class NotSpoofingAndroidAvailability implements Setting.Availability { - @Override - public boolean isAvailable() { - if (SpoofVideoStreamsPatch.isPatchIncluded()) { - return !BaseSettings.SPOOF_VIDEO_STREAMS.get() - || BaseSettings.SPOOF_VIDEO_STREAMS_CLIENT_TYPE.get() == ClientType.IOS_UNPLUGGED; - } - - return true; - } - } - public static final class AudioStreamLanguageOverrideAvailability implements Setting.Availability { @Override public boolean isAvailable() { diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/ForceOriginalAudioPatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/ForceOriginalAudioPatch.java index 91b2fe67a..7a713d4f4 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/ForceOriginalAudioPatch.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/ForceOriginalAudioPatch.java @@ -1,6 +1,8 @@ package app.revanced.extension.youtube.patches; import app.revanced.extension.shared.Logger; +import app.revanced.extension.shared.settings.Setting; +import app.revanced.extension.shared.spoof.SpoofVideoStreamsPatch; import app.revanced.extension.youtube.settings.Settings; @SuppressWarnings("unused") @@ -8,6 +10,20 @@ public class ForceOriginalAudioPatch { private static final String DEFAULT_AUDIO_TRACKS_SUFFIX = ".4"; + /** + * If the conditions to use this patch were present when the app launched. + */ + public static boolean PATCH_AVAILABLE = SpoofVideoStreamsPatch.notSpoofingToAndroid(); + + public static final class ForceOriginalAudioAvailability implements Setting.Availability { + @Override + public boolean isAvailable() { + // Check conditions of launch and now. Otherwise if spoofing is changed + // without a restart the setting will show as available when it's not. + return PATCH_AVAILABLE && SpoofVideoStreamsPatch.notSpoofingToAndroid(); + } + } + /** * Injection point. */ diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index 2d0eeef27..765f03f9a 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -7,8 +7,8 @@ import static app.revanced.extension.shared.settings.Setting.migrateFromOldPrefe import static app.revanced.extension.shared.settings.Setting.migrateOldSettingToNew; import static app.revanced.extension.shared.settings.Setting.parent; import static app.revanced.extension.shared.settings.Setting.parentsAny; -import static app.revanced.extension.shared.spoof.SpoofVideoStreamsPatch.NotSpoofingAndroidAvailability; import static app.revanced.extension.youtube.patches.ChangeStartPagePatch.StartPage; +import static app.revanced.extension.youtube.patches.ForceOriginalAudioPatch.ForceOriginalAudioAvailability; import static app.revanced.extension.youtube.patches.MiniplayerPatch.MiniplayerHideExpandCloseAvailability; import static app.revanced.extension.youtube.patches.MiniplayerPatch.MiniplayerHorizontalDragAvailability; import static app.revanced.extension.youtube.patches.MiniplayerPatch.MiniplayerType; @@ -54,7 +54,7 @@ public class Settings extends BaseSettings { public static final StringSetting CUSTOM_PLAYBACK_SPEEDS = new StringSetting("revanced_custom_playback_speeds", "0.25\n0.5\n0.75\n0.9\n0.95\n1.0\n1.05\n1.1\n1.25\n1.5\n1.75\n2.0\n3.0\n4.0\n5.0", true); // Audio - public static final BooleanSetting FORCE_ORIGINAL_AUDIO = new BooleanSetting("revanced_force_original_audio", FALSE, new NotSpoofingAndroidAvailability()); + public static final BooleanSetting FORCE_ORIGINAL_AUDIO = new BooleanSetting("revanced_force_original_audio", FALSE, new ForceOriginalAudioAvailability()); // Ads public static final BooleanSetting HIDE_BUTTONED_ADS = new BooleanSetting("revanced_hide_buttoned_ads", TRUE); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ForceOriginalAudioSwitchPreference.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ForceOriginalAudioSwitchPreference.java new file mode 100644 index 000000000..6a8c3af36 --- /dev/null +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ForceOriginalAudioSwitchPreference.java @@ -0,0 +1,36 @@ +package app.revanced.extension.youtube.settings.preference; + +import static app.revanced.extension.shared.StringRef.str; + +import android.content.Context; +import android.preference.SwitchPreference; +import android.util.AttributeSet; + +import app.revanced.extension.youtube.patches.ForceOriginalAudioPatch; + +@SuppressWarnings({"deprecation", "unused"}) +public class ForceOriginalAudioSwitchPreference extends SwitchPreference { + + { + if (!ForceOriginalAudioPatch.PATCH_AVAILABLE) { + // Show why force audio is not available. + String summary = str("revanced_force_original_audio_not_available"); + setSummary(summary); + setSummaryOn(summary); + setSummaryOff(summary); + } + } + + public ForceOriginalAudioSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + public ForceOriginalAudioSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + public ForceOriginalAudioSwitchPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + public ForceOriginalAudioSwitchPreference(Context context) { + super(context); + } +} diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/video/audio/ForceOriginalAudioPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/video/audio/ForceOriginalAudioPatch.kt index 4a4f00ecf..80b91f139 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/video/audio/ForceOriginalAudioPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/video/audio/ForceOriginalAudioPatch.kt @@ -57,7 +57,10 @@ val forceOriginalAudioPatch = bytecodePatch( addResources("youtube", "video.audio.forceOriginalAudioPatch") PreferenceScreen.VIDEO.addPreferences( - SwitchPreference("revanced_force_original_audio") + SwitchPreference( + key = "revanced_force_original_audio", + tag = "app.revanced.extension.youtube.settings.preference.ForceOriginalAudioSwitchPreference" + ) ) fun Method.firstFormatStreamingModelCall( diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index cc2e0a6c1..83737d8c1 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -1246,6 +1246,7 @@ Enabling this can unlock higher video qualities" Force original audio Using original audio Using default audio + To use this feature, change stream spoofing to iOS client type