From 497739e8ce6933c1f1ea46edffc102e56b985623 Mon Sep 17 00:00:00 2001
From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com>
Date: Fri, 15 Nov 2024 07:56:36 +0400
Subject: [PATCH] fix(YouTube - Playback speed): Add 'Auto' speed. Always
override speed if default is set to 1.0x (#3914)
---
.../speed/CustomPlaybackSpeedPatch.java | 29 ++++++++++++-------
.../speed/RememberPlaybackSpeedPatch.java | 2 +-
.../extension/youtube/settings/Settings.java | 2 +-
.../remember/RememberPlaybackSpeedPatch.kt | 6 ++--
.../resources/addresources/values/strings.xml | 1 +
5 files changed, 25 insertions(+), 15 deletions(-)
diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java
index d015c192a..7e754407d 100644
--- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java
+++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java
@@ -1,5 +1,6 @@
package app.revanced.extension.youtube.patches.playback.speed;
+import static app.revanced.extension.shared.StringRef.sf;
import static app.revanced.extension.shared.StringRef.str;
import android.preference.ListPreference;
@@ -10,15 +11,18 @@ import android.view.ViewParent;
import androidx.annotation.NonNull;
-import app.revanced.extension.youtube.patches.components.PlaybackSpeedMenuFilterPatch;
-import app.revanced.extension.youtube.settings.Settings;
+import java.util.Arrays;
+
import app.revanced.extension.shared.Logger;
import app.revanced.extension.shared.Utils;
-
-import java.util.Arrays;
+import app.revanced.extension.youtube.patches.components.PlaybackSpeedMenuFilterPatch;
+import app.revanced.extension.youtube.settings.Settings;
@SuppressWarnings("unused")
public class CustomPlaybackSpeedPatch {
+
+ private static final float PLAYBACK_SPEED_AUTO = Settings.PLAYBACK_SPEED_DEFAULT.defaultValue;
+
/**
* Maximum playback speed, exclusive value. Custom speeds must be less than this value.
*
@@ -26,7 +30,7 @@ public class CustomPlaybackSpeedPatch {
* and the UI selector starts flickering and acting weird.
* Over 10x and the speeds show up out of order in the UI selector.
*/
- public static final float MAXIMUM_PLAYBACK_SPEED = 8;
+ public static final float PLAYBACK_SPEED_MAXIMUM = 8;
/**
* Custom playback speeds.
@@ -69,8 +73,8 @@ public class CustomPlaybackSpeedPatch {
throw new IllegalArgumentException();
}
- if (speedFloat >= MAXIMUM_PLAYBACK_SPEED) {
- resetCustomSpeeds(str("revanced_custom_playback_speeds_invalid", MAXIMUM_PLAYBACK_SPEED));
+ if (speedFloat >= PLAYBACK_SPEED_MAXIMUM) {
+ resetCustomSpeeds(str("revanced_custom_playback_speeds_invalid", PLAYBACK_SPEED_MAXIMUM));
loadCustomSpeeds();
return;
}
@@ -98,10 +102,15 @@ public class CustomPlaybackSpeedPatch {
@SuppressWarnings("deprecation")
public static void initializeListPreference(ListPreference preference) {
if (preferenceListEntries == null) {
- preferenceListEntries = new String[customPlaybackSpeeds.length];
- preferenceListEntryValues = new String[customPlaybackSpeeds.length];
+ final int numberOfEntries = customPlaybackSpeeds.length + 1;
+ preferenceListEntries = new String[numberOfEntries];
+ preferenceListEntryValues = new String[numberOfEntries];
- int i = 0;
+ // Auto speed (same behavior as unpatched).
+ preferenceListEntries[0] = sf("revanced_custom_playback_speeds_auto").toString();
+ preferenceListEntryValues[0] = String.valueOf(PLAYBACK_SPEED_AUTO);
+
+ int i = 1;
for (float speed : customPlaybackSpeeds) {
String speedString = String.valueOf(speed);
preferenceListEntries[i] = speedString + "x";
diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java
index 87237ae6f..3c504df5d 100644
--- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java
+++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/playback/speed/RememberPlaybackSpeedPatch.java
@@ -33,7 +33,7 @@ public final class RememberPlaybackSpeedPatch {
// With the 0.05x menu, if the speed is set by integrations to higher than 2.0x
// then the menu will allow increasing without bounds but the max speed is
// still capped to under 8.0x.
- playbackSpeed = Math.min(playbackSpeed, CustomPlaybackSpeedPatch.MAXIMUM_PLAYBACK_SPEED - 0.05f);
+ playbackSpeed = Math.min(playbackSpeed, CustomPlaybackSpeedPatch.PLAYBACK_SPEED_MAXIMUM - 0.05f);
// Prevent toast spamming if using the 0.05x adjustments.
// Show exactly one toast after the user stops interacting with the speed menu.
diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/Settings.java
index aeca9ddfd..96155e653 100644
--- a/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/Settings.java
+++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/settings/Settings.java
@@ -33,7 +33,7 @@ public class Settings extends BaseSettings {
// Speed
public static final BooleanSetting REMEMBER_PLAYBACK_SPEED_LAST_SELECTED = new BooleanSetting("revanced_remember_playback_speed_last_selected", FALSE);
public static final BooleanSetting CUSTOM_SPEED_MENU = new BooleanSetting("revanced_custom_speed_menu", TRUE);
- public static final FloatSetting PLAYBACK_SPEED_DEFAULT = new FloatSetting("revanced_playback_speed_default", 1.0f);
+ public static final FloatSetting PLAYBACK_SPEED_DEFAULT = new FloatSetting("revanced_playback_speed_default", -2.0f);
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);
diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/video/speed/remember/RememberPlaybackSpeedPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/video/speed/remember/RememberPlaybackSpeedPatch.kt
index 3c51559ea..75fcf392a 100644
--- a/patches/src/main/kotlin/app/revanced/patches/youtube/video/speed/remember/RememberPlaybackSpeedPatch.kt
+++ b/patches/src/main/kotlin/app/revanced/patches/youtube/video/speed/remember/RememberPlaybackSpeedPatch.kt
@@ -61,10 +61,10 @@ internal val rememberPlaybackSpeedPatch = bytecodePatch {
invoke-static { }, $EXTENSION_CLASS_DESCRIPTOR->getPlaybackSpeedOverride()F
move-result v0
- # Check if the playback speed is not 1.0x.
- const/high16 v1, 1.0f
+ # Check if the playback speed is not auto (-2.0f)
+ const/4 v1, 0x0
cmpg-float v1, v0, v1
- if-eqz v1, :do_not_override
+ if-lez v1, :do_not_override
# Get the instance of the class which has the container class field below.
iget-object v1, p0, $onItemClickListenerClassFieldReference
diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml
index b933029cf..240f0578c 100644
--- a/patches/src/main/resources/addresources/values/strings.xml
+++ b/patches/src/main/resources/addresources/values/strings.xml
@@ -1189,6 +1189,7 @@ This is because Crowdin requires temporarily flattening this file and removing t
Add or change the custom playback speeds
Custom speeds must be less than %s. Using default values.
Invalid custom playback speeds. Using default values.
+ Auto
Remember playback speed changes