From 7627e5d057fdfb5847b2406b609634489efd3cbf Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:00:34 +0400 Subject: [PATCH] fix(youtube/remember-playback-speed): allow to not remember playback speed (#338) Co-authored-by: oSumAtrIX --- .../speed/RememberPlaybackRatePatch.java | 32 ------- .../speed/RememberPlaybackSpeedPatch.java | 83 +++++++++++++++++++ .../integrations/settings/SettingsEnum.java | 6 +- 3 files changed, 86 insertions(+), 35 deletions(-) delete mode 100644 app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackRatePatch.java create mode 100644 app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java diff --git a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackRatePatch.java b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackRatePatch.java deleted file mode 100644 index b68ff784..00000000 --- a/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackRatePatch.java +++ /dev/null @@ -1,32 +0,0 @@ -package app.revanced.integrations.patches.playback.speed; - -import static app.revanced.integrations.utils.SharedPrefHelper.SharedPrefNames.REVANCED_PREFS; -import static app.revanced.integrations.utils.SharedPrefHelper.getFloat; -import static app.revanced.integrations.utils.SharedPrefHelper.saveFloat; - -import android.widget.Toast; - -import app.revanced.integrations.settings.SettingsEnum; -import app.revanced.integrations.utils.LogHelper; -import app.revanced.integrations.utils.ReVancedUtils; - - -public final class RememberPlaybackRatePatch { - private static final String REMEMBERED_PLAYBACK_RATE_PREFERENCE_KEY = "revanced_remember_playback_rate_last_value"; - - public static void rememberPlaybackRate(final float selectedPlaybackRate) { - if (!SettingsEnum.REMEMBER_PLAYBACK_RATE_SELECTED.getBoolean()) return; - - Toast.makeText(ReVancedUtils.getContext(), "Playback rate will be remembered", Toast.LENGTH_SHORT).show(); - - LogHelper.printDebug(() -> "Remembering playback rate: " + selectedPlaybackRate); - saveFloat(REVANCED_PREFS, REMEMBERED_PLAYBACK_RATE_PREFERENCE_KEY, selectedPlaybackRate); - } - - public static float getRememberedPlaybackRate() { - final var playbackRateOverride = getFloat(REVANCED_PREFS, REMEMBERED_PLAYBACK_RATE_PREFERENCE_KEY, -2f); - - LogHelper.printDebug(() -> "Overriding playback rate: " + playbackRateOverride); - return playbackRateOverride; - } -} 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 new file mode 100644 index 00000000..c1d4a8ad --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/patches/playback/speed/RememberPlaybackSpeedPatch.java @@ -0,0 +1,83 @@ +package app.revanced.integrations.patches.playback.speed; + +import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import app.revanced.integrations.settings.SettingsEnum; +import app.revanced.integrations.utils.LogHelper; +import app.revanced.integrations.utils.ReVancedUtils; + +public final class RememberPlaybackSpeedPatch { + + /** + * The current playback speed + */ + private static float currentPlaybackSpeed = getLastRememberedPlaybackSpeed(); + + private final static float DEFAULT_PLAYBACK_SPEED = (float) SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getDefaultValue(); + + @Nullable + private static String currentVideoId; + + private static void showToast(final String message) { + Toast.makeText(ReVancedUtils.getContext(), message, Toast.LENGTH_LONG).show(); + } + + private static float getLastRememberedPlaybackSpeed() { + return SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.getFloat(); + } + + private static void rememberPlaybackSpeed() { + SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE.saveValue(currentPlaybackSpeed); + } + + private static boolean rememberLastSelectedPlaybackSpeed() { + return SettingsEnum.REMEMBER_PLAYBACK_SPEED_LAST_SELECTED.getBoolean(); + } + + /** + * Injection point. + * Called when a new video loads. + */ + public static void newVideoLoaded(@NonNull String videoId) { + if (videoId.equals(currentVideoId)) { + return; + } + + currentVideoId = videoId; + currentPlaybackSpeed = getLastRememberedPlaybackSpeed(); + } + + /** + * Injection point. + * Called when a playback speed is selected. + * + * @param playbackSpeed The playback speed to set. + */ + public static void setPlaybackSpeed(final float playbackSpeed) { + LogHelper.printDebug(() -> "Playback speed changed to: " + playbackSpeed); + + currentPlaybackSpeed = playbackSpeed; + + if (rememberLastSelectedPlaybackSpeed()) { + rememberPlaybackSpeed(); + + showToast("Remembering playback speed: " + playbackSpeed + "x"); + } else { + if (getLastRememberedPlaybackSpeed() == DEFAULT_PLAYBACK_SPEED) return; + + showToast("Applying playback speed: " + playbackSpeed + "x"); + } + } + + /** + * Injection point. + * Called when playback first starts, + * and also called immediately after the user selects a new video speed. + * + * @return The currently set playback speed. + */ + public static float getCurrentPlaybackSpeed() { + return currentPlaybackSpeed; + } +} diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index f1e1892f..e2cac092 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -18,11 +18,11 @@ public enum SettingsEnum { // Video settings OLD_STYLE_VIDEO_QUALITY_PLAYER_SETTINGS("revanced_use_old_style_quality_settings", true, ReturnType.BOOLEAN), REMEMBER_VIDEO_QUALITY_LAST_SELECTED("revanced_remember_video_quality_last_selected", true, ReturnType.BOOLEAN), - REMEMBER_PLAYBACK_RATE_SELECTED("revanced_remember_playback_rate_selected", true, ReturnType.BOOLEAN), - + REMEMBER_PLAYBACK_SPEED_LAST_SELECTED("revanced_remember_playback_speed_last_selected", true, ReturnType.BOOLEAN), + REMEMBER_PLAYBACK_SPEED_LAST_SELECTED_VALUE("revanced_remember_playback_speed_last_selected_value", 1.0f, ReturnType.FLOAT), + // TODO: Unused currently // Whitelist settings - //ToDo: Not used atm, Patch missing ENABLE_WHITELIST("revanced_whitelist_ads_enabled", false, ReturnType.BOOLEAN), // Ad settings