From e5bd37c6a5bbf250c711193b1c5952217e272dd4 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 08:09:39 -0400 Subject: [PATCH 01/12] feat(YouTube): Add `Change Shorts repeat` patch --- .../patches/ChangeShortsRepeatPatch.java | 128 ++++++++++++++++++ .../youtube/settings/Settings.java | 4 + 2 files changed, 132 insertions(+) create mode 100644 app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java new file mode 100644 index 0000000000..9a2e49398b --- /dev/null +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -0,0 +1,128 @@ +package app.revanced.integrations.youtube.patches; + +import android.app.Activity; +import android.os.Build; + +import androidx.annotation.RequiresApi; + +import java.lang.ref.WeakReference; +import java.util.Objects; + +import app.revanced.integrations.shared.Logger; +import app.revanced.integrations.youtube.settings.Settings; + +@SuppressWarnings("unused") +public class ChangeShortsRepeatPatch { + + public enum ShortsLoopBehavior { + /** + * Handled as the default and not to change whatever YT uses. + */ + UNKNOWN, + /** + * Repeat the same Short forever! + */ + SINGLE_PLAY, + /** + * Play once, then advanced to the next Short. + */ + REPEAT, + /** + * Pause playback after 1 play. + */ + END_SCREEN; + + /** + * YouTube enum value of the obfuscated enum type. + */ + private Enum ytEnumValue; + } + + private static WeakReference mainActivityRef = new WeakReference<>(null); + + + public static void setMainActivity(Activity activity) { + mainActivityRef = new WeakReference<>(activity); + } + + /** + * @return If the app currently is in background PiP mode. + */ + @RequiresApi(api = Build.VERSION_CODES.N) + private static boolean isAppInBackgroundPiPMode() { + Activity mainActivity = mainActivityRef.get(); + + if (mainActivity == null) { + // Should never happen, since the context is the main activity. + Logger.printException(() -> "Activity is null, cannot determine repeat behavior"); + return false; + } + + final boolean isPiP = mainActivity.isInPictureInPictureMode(); + if (isPiP) { + Logger.printDebug(() -> "Short is in background PiP mode"); + } + + return isPiP; + } + + /** + * Injection point. + */ + public static void setYTShortsRepeatEnum(Enum ytEnum) { + try { + for (Enum ytBehavior : Objects.requireNonNull(ytEnum.getClass().getEnumConstants())) { + for (ShortsLoopBehavior rvBehavior : ShortsLoopBehavior.values()) { + if (ytBehavior.name().endsWith(rvBehavior.name())) { + rvBehavior.ytEnumValue = ytBehavior; + + Logger.printDebug(() -> rvBehavior + " set to YT enum: " + ytBehavior.name()); + } + } + } + } catch (Exception ex) { + Logger.printException(() -> "setYTShortsRepeatEnum failure", ex); + } + } + + /** + * Injection point. + */ + @RequiresApi(api = Build.VERSION_CODES.N) + public static Enum changeShortsRepeatState(Enum originalState) { + try { + ShortsLoopBehavior behavior = isAppInBackgroundPiPMode() + ? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get() + : Settings.CHANGE_SHORTS_REPEAT_STATE.get(); + + String originalStateName = originalState.name(); + + if (behavior == ShortsLoopBehavior.UNKNOWN) { + Logger.printDebug(() -> "Behavior setting is default. " + + "Using original: " + originalStateName); + return originalState; + } + + if (behavior.ytEnumValue != null) { + if (behavior.ytEnumValue == originalState) { + Logger.printDebug(() -> "Behavior setting is same as original. " + + "Using original: " + originalStateName); + return originalState; + } + + Logger.printDebug(() -> "Changing Shorts repeat behavior from: " + originalStateName + + " to: " + behavior.name()); + return behavior.ytEnumValue; + } + + // Somehow during setting the YT enum it failed. + // Either the hook was not called or an unknown enum was encountered. + Logger.printException(() -> "Cannot change Short repeat behavior as " + + "no enum is assigned to: " + originalStateName); + } catch (Exception ex) { + Logger.printException(() -> "changeShortsRepeatState failure", ex); + } + + return originalState; + } +} diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index cdf05b9178..4e68b0ca80 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -8,6 +8,8 @@ import static app.revanced.integrations.youtube.patches.MiniplayerPatch.MiniplayerType; import static app.revanced.integrations.youtube.patches.MiniplayerPatch.MiniplayerType.*; import static app.revanced.integrations.youtube.sponsorblock.objects.CategoryBehaviour.*; +import static app.revanced.integrations.youtube.patches.ChangeShortsRepeatPatch.ShortsLoopBehavior; +import static app.revanced.integrations.youtube.patches.ChangeShortsRepeatPatch.ShortsLoopBehavior.*; import java.util.Arrays; import java.util.HashSet; @@ -247,6 +249,8 @@ public class Settings extends BaseSettings { public static final BooleanSetting HIDE_SHORTS_SOUND_METADATA_LABEL = new BooleanSetting("revanced_hide_shorts_sound_metadata_label", FALSE); public static final BooleanSetting HIDE_SHORTS_FULL_VIDEO_LINK_LABEL = new BooleanSetting("revanced_hide_shorts_full_video_link_label", FALSE); public static final BooleanSetting HIDE_SHORTS_NAVIGATION_BAR = new BooleanSetting("revanced_hide_shorts_navigation_bar", FALSE, true); + public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", SINGLE_PLAY); + public static final EnumSetting CHANGE_SHORTS_BACKGROUND_REPEAT_STATE = new EnumSetting<>("revanced_shorts_background_repeat_behavior", SINGLE_PLAY); // Seekbar public static final BooleanSetting DISABLE_PRECISE_SEEKING_GESTURE = new BooleanSetting("revanced_disable_precise_seeking_gesture", TRUE); From 1a94a8f6b68502aec92806c7f22fcdf619486c7f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 08:09:53 -0400 Subject: [PATCH 02/12] logging --- .../youtube/patches/theme/SeekbarColorPatch.java | 5 +---- .../youtube/sponsorblock/SegmentPlaybackController.java | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/theme/SeekbarColorPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/theme/SeekbarColorPatch.java index 2e97a27d86..a4db2ffd38 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/theme/SeekbarColorPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/theme/SeekbarColorPatch.java @@ -73,10 +73,7 @@ public static int getSeekbarColor() { } public static boolean playerSeekbarGradientEnabled(boolean original) { - if (original) { - Logger.printDebug(() -> "playerSeekbarGradientEnabled original: " + true); - if (SEEKBAR_CUSTOM_COLOR_ENABLED) return false; - } + if (SEEKBAR_CUSTOM_COLOR_ENABLED) return false; return original; } diff --git a/app/src/main/java/app/revanced/integrations/youtube/sponsorblock/SegmentPlaybackController.java b/app/src/main/java/app/revanced/integrations/youtube/sponsorblock/SegmentPlaybackController.java index ad02eec8b8..ddf4ae16ac 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/sponsorblock/SegmentPlaybackController.java +++ b/app/src/main/java/app/revanced/integrations/youtube/sponsorblock/SegmentPlaybackController.java @@ -662,10 +662,7 @@ private static void setSponsorBarAbsoluteRight(Rect rect) { */ @SuppressWarnings("unused") public static void setSponsorBarThickness(int thickness) { - if (sponsorBarThickness != thickness) { - Logger.printDebug(() -> "setSponsorBarThickness: " + thickness); - sponsorBarThickness = thickness; - } + sponsorBarThickness = thickness; } /** From 61682244374c2f2be3aba395dcf166abba26df08 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 08:21:49 -0400 Subject: [PATCH 03/12] Comments --- .../youtube/patches/ChangeShortsRepeatPatch.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java index 9a2e49398b..773068d612 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -22,11 +22,11 @@ public enum ShortsLoopBehavior { /** * Repeat the same Short forever! */ - SINGLE_PLAY, + REPEAT, /** * Play once, then advanced to the next Short. */ - REPEAT, + SINGLE_PLAY, /** * Pause playback after 1 play. */ @@ -46,7 +46,7 @@ public static void setMainActivity(Activity activity) { } /** - * @return If the app currently is in background PiP mode. + * @return If the app is currently in background PiP mode. */ @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isAppInBackgroundPiPMode() { From e28c825ae40a43fdba2116322764623a85d9a4e4 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:18:35 -0400 Subject: [PATCH 04/12] fix: Restrict changing Shorts PiP behavior to 19.35+ --- .../youtube/patches/ChangeShortsRepeatPatch.java | 6 +++++- .../integrations/youtube/patches/VersionCheckPatch.java | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java index 773068d612..3fd7db98bb 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -91,7 +91,11 @@ public static void setYTShortsRepeatEnum(Enum ytEnum) { @RequiresApi(api = Build.VERSION_CODES.N) public static Enum changeShortsRepeatState(Enum originalState) { try { - ShortsLoopBehavior behavior = isAppInBackgroundPiPMode() + // 19.34 and earlier cannot change Shorts repeat behavior for background pip. + final boolean inPiPModeAndCanChangeBehavior = + VersionCheckPatch.IS_19_35_OR_GREATER && isAppInBackgroundPiPMode(); + + ShortsLoopBehavior behavior = inPiPModeAndCanChangeBehavior ? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get() : Settings.CHANGE_SHORTS_REPEAT_STATE.get(); diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java index 4ede010158..af5207cb3d 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java @@ -7,4 +7,5 @@ public class VersionCheckPatch { public static final boolean IS_19_21_OR_GREATER = Utils.getAppVersionName().compareTo("19.21.00") >= 0; public static final boolean IS_19_26_OR_GREATER = Utils.getAppVersionName().compareTo("19.26.00") >= 0; public static final boolean IS_19_29_OR_GREATER = Utils.getAppVersionName().compareTo("19.29.00") >= 0; + public static final boolean IS_19_35_OR_GREATER = Utils.getAppVersionName().compareTo("19.35.00") >= 0; } From 651421fe755772d2fef3b176e229e913d4bb82d9 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:23:11 -0400 Subject: [PATCH 05/12] fix: Use correct defaults --- .../app/revanced/integrations/youtube/settings/Settings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 4e68b0ca80..6330b37fec 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -249,7 +249,7 @@ public class Settings extends BaseSettings { public static final BooleanSetting HIDE_SHORTS_SOUND_METADATA_LABEL = new BooleanSetting("revanced_hide_shorts_sound_metadata_label", FALSE); public static final BooleanSetting HIDE_SHORTS_FULL_VIDEO_LINK_LABEL = new BooleanSetting("revanced_hide_shorts_full_video_link_label", FALSE); public static final BooleanSetting HIDE_SHORTS_NAVIGATION_BAR = new BooleanSetting("revanced_hide_shorts_navigation_bar", FALSE, true); - public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", SINGLE_PLAY); + public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", UNKNOWN); public static final EnumSetting CHANGE_SHORTS_BACKGROUND_REPEAT_STATE = new EnumSetting<>("revanced_shorts_background_repeat_behavior", SINGLE_PLAY); // Seekbar From bd93ba7c5e18f1d184868a10adf05af4bcd1752f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 09:39:25 -0400 Subject: [PATCH 06/12] fix: 19.34 works with background PiP autoplay --- .../integrations/youtube/patches/ChangeShortsRepeatPatch.java | 4 ++-- .../integrations/youtube/patches/VersionCheckPatch.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java index 3fd7db98bb..7fcba02f61 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -91,9 +91,9 @@ public static void setYTShortsRepeatEnum(Enum ytEnum) { @RequiresApi(api = Build.VERSION_CODES.N) public static Enum changeShortsRepeatState(Enum originalState) { try { - // 19.34 and earlier cannot change Shorts repeat behavior for background pip. + // 19.34+ is required to set background play behavior. final boolean inPiPModeAndCanChangeBehavior = - VersionCheckPatch.IS_19_35_OR_GREATER && isAppInBackgroundPiPMode(); + VersionCheckPatch.IS_19_34_OR_GREATER && isAppInBackgroundPiPMode(); ShortsLoopBehavior behavior = inPiPModeAndCanChangeBehavior ? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get() diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java index af5207cb3d..d977a32f5d 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/VersionCheckPatch.java @@ -7,5 +7,5 @@ public class VersionCheckPatch { public static final boolean IS_19_21_OR_GREATER = Utils.getAppVersionName().compareTo("19.21.00") >= 0; public static final boolean IS_19_26_OR_GREATER = Utils.getAppVersionName().compareTo("19.26.00") >= 0; public static final boolean IS_19_29_OR_GREATER = Utils.getAppVersionName().compareTo("19.29.00") >= 0; - public static final boolean IS_19_35_OR_GREATER = Utils.getAppVersionName().compareTo("19.35.00") >= 0; + public static final boolean IS_19_34_OR_GREATER = Utils.getAppVersionName().compareTo("19.34.00") >= 0; } From 643b99ba563aebef3afbe7ea07f7dac9f4574012 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 12:45:55 -0400 Subject: [PATCH 07/12] refactor --- .../patches/ChangeShortsRepeatPatch.java | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java index 7fcba02f61..13215d4351 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -15,9 +15,6 @@ public class ChangeShortsRepeatPatch { public enum ShortsLoopBehavior { - /** - * Handled as the default and not to change whatever YT uses. - */ UNKNOWN, /** * Repeat the same Short forever! @@ -72,13 +69,20 @@ private static boolean isAppInBackgroundPiPMode() { public static void setYTShortsRepeatEnum(Enum ytEnum) { try { for (Enum ytBehavior : Objects.requireNonNull(ytEnum.getClass().getEnumConstants())) { + + boolean foundBehavior = false; for (ShortsLoopBehavior rvBehavior : ShortsLoopBehavior.values()) { if (ytBehavior.name().endsWith(rvBehavior.name())) { rvBehavior.ytEnumValue = ytBehavior; Logger.printDebug(() -> rvBehavior + " set to YT enum: " + ytBehavior.name()); + foundBehavior = true; } } + + if (!foundBehavior) { + Logger.printException(() -> "Unknown Shorts loop behavior: " + ytBehavior.name()); + } } } catch (Exception ex) { Logger.printException(() -> "setYTShortsRepeatEnum failure", ex); @@ -89,7 +93,7 @@ public static void setYTShortsRepeatEnum(Enum ytEnum) { * Injection point. */ @RequiresApi(api = Build.VERSION_CODES.N) - public static Enum changeShortsRepeatState(Enum originalState) { + public static Enum changeShortsRepeatBehavior(Enum original) { try { // 19.34+ is required to set background play behavior. final boolean inPiPModeAndCanChangeBehavior = @@ -99,34 +103,27 @@ public static Enum changeShortsRepeatState(Enum originalState) { ? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get() : Settings.CHANGE_SHORTS_REPEAT_STATE.get(); - String originalStateName = originalState.name(); - if (behavior == ShortsLoopBehavior.UNKNOWN) { Logger.printDebug(() -> "Behavior setting is default. " - + "Using original: " + originalStateName); - return originalState; + + "Using original: " + original.name()); + return original; } - if (behavior.ytEnumValue != null) { - if (behavior.ytEnumValue == originalState) { - Logger.printDebug(() -> "Behavior setting is same as original. " - + "Using original: " + originalStateName); - return originalState; - } + if (behavior.ytEnumValue == original) { + Logger.printDebug(() -> "Behavior setting is same as original. " + + "Using original: " + original.name()); + return original; + } - Logger.printDebug(() -> "Changing Shorts repeat behavior from: " + originalStateName - + " to: " + behavior.name()); + if (behavior.ytEnumValue != null) { + Logger.printDebug(() -> "Changing Shorts repeat behavior from: " + + original.name() + " to: " + behavior.name()); return behavior.ytEnumValue; } - - // Somehow during setting the YT enum it failed. - // Either the hook was not called or an unknown enum was encountered. - Logger.printException(() -> "Cannot change Short repeat behavior as " + - "no enum is assigned to: " + originalStateName); } catch (Exception ex) { Logger.printException(() -> "changeShortsRepeatState failure", ex); } - return originalState; + return original; } } From a7c830ea347386b5ecd6176293ff089cf63ac254 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 12:46:50 -0400 Subject: [PATCH 08/12] fix default --- .../app/revanced/integrations/youtube/settings/Settings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 6330b37fec..8216cbf6db 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -249,7 +249,7 @@ public class Settings extends BaseSettings { public static final BooleanSetting HIDE_SHORTS_SOUND_METADATA_LABEL = new BooleanSetting("revanced_hide_shorts_sound_metadata_label", FALSE); public static final BooleanSetting HIDE_SHORTS_FULL_VIDEO_LINK_LABEL = new BooleanSetting("revanced_hide_shorts_full_video_link_label", FALSE); public static final BooleanSetting HIDE_SHORTS_NAVIGATION_BAR = new BooleanSetting("revanced_hide_shorts_navigation_bar", FALSE, true); - public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", UNKNOWN); + public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", REPEAT); public static final EnumSetting CHANGE_SHORTS_BACKGROUND_REPEAT_STATE = new EnumSetting<>("revanced_shorts_background_repeat_behavior", SINGLE_PLAY); // Seekbar From 432f04fcec6c5cd46f2f9997be8123aa0a8fe292 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 12:49:44 -0400 Subject: [PATCH 09/12] refactor --- .../youtube/patches/ChangeShortsRepeatPatch.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java index 13215d4351..b4306587e3 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java @@ -109,15 +109,12 @@ public static Enum changeShortsRepeatBehavior(Enum original) { return original; } - if (behavior.ytEnumValue == original) { - Logger.printDebug(() -> "Behavior setting is same as original. " - + "Using original: " + original.name()); - return original; - } - if (behavior.ytEnumValue != null) { - Logger.printDebug(() -> "Changing Shorts repeat behavior from: " - + original.name() + " to: " + behavior.name()); + Logger.printDebug(() -> behavior.ytEnumValue == original + ? "Changing Shorts repeat behavior from: " + original.name() + " to: " + behavior.name() + : "Behavior setting is same as original. Using original: " + original.name() + ); + return behavior.ytEnumValue; } } catch (Exception ex) { From fc754657eb19c437ce596a9bdc05dc2d26ba531c Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 14:27:09 -0400 Subject: [PATCH 10/12] refactor: Use switch instead of list preference --- ...eatPatch.java => ShortsAutoplayPatch.java} | 35 +++++++++++-------- .../youtube/settings/Settings.java | 6 ++-- 2 files changed, 22 insertions(+), 19 deletions(-) rename app/src/main/java/app/revanced/integrations/youtube/patches/{ChangeShortsRepeatPatch.java => ShortsAutoplayPatch.java} (80%) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java similarity index 80% rename from app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java rename to app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java index b4306587e3..953ff9df86 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ChangeShortsRepeatPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java @@ -12,9 +12,9 @@ import app.revanced.integrations.youtube.settings.Settings; @SuppressWarnings("unused") -public class ChangeShortsRepeatPatch { +public class ShortsAutoplayPatch { - public enum ShortsLoopBehavior { + private enum ShortsLoopBehavior { UNKNOWN, /** * Repeat the same Short forever! @@ -95,23 +95,28 @@ public static void setYTShortsRepeatEnum(Enum ytEnum) { @RequiresApi(api = Build.VERSION_CODES.N) public static Enum changeShortsRepeatBehavior(Enum original) { try { - // 19.34+ is required to set background play behavior. - final boolean inPiPModeAndCanChangeBehavior = - VersionCheckPatch.IS_19_34_OR_GREATER && isAppInBackgroundPiPMode(); - - ShortsLoopBehavior behavior = inPiPModeAndCanChangeBehavior - ? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get() - : Settings.CHANGE_SHORTS_REPEAT_STATE.get(); - - if (behavior == ShortsLoopBehavior.UNKNOWN) { - Logger.printDebug(() -> "Behavior setting is default. " - + "Using original: " + original.name()); - return original; + final boolean autoplay; + + if (isAppInBackgroundPiPMode()) { + if (!VersionCheckPatch.IS_19_34_OR_GREATER) { + // 19.34+ is required to set background play behavior. + Logger.printDebug(() -> "PiP Shorts not supported, using original repeat behavior"); + + return original; + } + + autoplay = Settings.SHORTS_AUTOPLAY_BACKGROUND.get(); + } else { + autoplay = Settings.SHORTS_AUTOPLAY.get(); } + final ShortsLoopBehavior behavior = autoplay + ? ShortsLoopBehavior.SINGLE_PLAY + : ShortsLoopBehavior.REPEAT; + if (behavior.ytEnumValue != null) { Logger.printDebug(() -> behavior.ytEnumValue == original - ? "Changing Shorts repeat behavior from: " + original.name() + " to: " + behavior.name() + ? "Changing Shorts repeat behavior from: " + original.name() + " to: " + behavior.ytEnumValue : "Behavior setting is same as original. Using original: " + original.name() ); diff --git a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java index 8216cbf6db..e02f482033 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java +++ b/app/src/main/java/app/revanced/integrations/youtube/settings/Settings.java @@ -8,8 +8,6 @@ import static app.revanced.integrations.youtube.patches.MiniplayerPatch.MiniplayerType; import static app.revanced.integrations.youtube.patches.MiniplayerPatch.MiniplayerType.*; import static app.revanced.integrations.youtube.sponsorblock.objects.CategoryBehaviour.*; -import static app.revanced.integrations.youtube.patches.ChangeShortsRepeatPatch.ShortsLoopBehavior; -import static app.revanced.integrations.youtube.patches.ChangeShortsRepeatPatch.ShortsLoopBehavior.*; import java.util.Arrays; import java.util.HashSet; @@ -249,8 +247,8 @@ public class Settings extends BaseSettings { public static final BooleanSetting HIDE_SHORTS_SOUND_METADATA_LABEL = new BooleanSetting("revanced_hide_shorts_sound_metadata_label", FALSE); public static final BooleanSetting HIDE_SHORTS_FULL_VIDEO_LINK_LABEL = new BooleanSetting("revanced_hide_shorts_full_video_link_label", FALSE); public static final BooleanSetting HIDE_SHORTS_NAVIGATION_BAR = new BooleanSetting("revanced_hide_shorts_navigation_bar", FALSE, true); - public static final EnumSetting CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_shorts_repeat_behavior", REPEAT); - public static final EnumSetting CHANGE_SHORTS_BACKGROUND_REPEAT_STATE = new EnumSetting<>("revanced_shorts_background_repeat_behavior", SINGLE_PLAY); + public static final BooleanSetting SHORTS_AUTOPLAY = new BooleanSetting("revanced_shorts_autoplay", FALSE); + public static final BooleanSetting SHORTS_AUTOPLAY_BACKGROUND = new BooleanSetting("revanced_shorts_autoplay_background", TRUE); // Seekbar public static final BooleanSetting DISABLE_PRECISE_SEEKING_GESTURE = new BooleanSetting("revanced_disable_precise_seeking_gesture", TRUE); From d1f3e33a19d9f352da13ef6be88bc96f984b4549 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:02:56 -0400 Subject: [PATCH 11/12] Cleanup --- .../youtube/patches/ShortsAutoplayPatch.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java index 953ff9df86..605234e847 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java @@ -47,20 +47,7 @@ public static void setMainActivity(Activity activity) { */ @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isAppInBackgroundPiPMode() { - Activity mainActivity = mainActivityRef.get(); - - if (mainActivity == null) { - // Should never happen, since the context is the main activity. - Logger.printException(() -> "Activity is null, cannot determine repeat behavior"); - return false; - } - - final boolean isPiP = mainActivity.isInPictureInPictureMode(); - if (isPiP) { - Logger.printDebug(() -> "Short is in background PiP mode"); - } - - return isPiP; + return mainActivityRef.get().isInPictureInPictureMode(); } /** From 1d606995fb86baad319ae4726fce5cf2f0cdcb6f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Mon, 21 Oct 2024 02:51:23 -0400 Subject: [PATCH 12/12] refactor --- .../youtube/patches/ShortsAutoplayPatch.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java index 605234e847..412cb51e68 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java @@ -29,6 +29,19 @@ private enum ShortsLoopBehavior { */ END_SCREEN; + static void setYTEnumValue(Enum ytBehavior) { + for (ShortsLoopBehavior rvBehavior : values()) { + if (ytBehavior.name().endsWith(rvBehavior.name())) { + rvBehavior.ytEnumValue = ytBehavior; + + Logger.printDebug(() -> rvBehavior + " set to YT enum: " + ytBehavior.name()); + return; + } + } + + Logger.printException(() -> "Unknown Shorts loop behavior: " + ytBehavior.name()); + } + /** * YouTube enum value of the obfuscated enum type. */ @@ -47,7 +60,8 @@ public static void setMainActivity(Activity activity) { */ @RequiresApi(api = Build.VERSION_CODES.N) private static boolean isAppInBackgroundPiPMode() { - return mainActivityRef.get().isInPictureInPictureMode(); + Activity activity = mainActivityRef.get(); + return activity != null && activity.isInPictureInPictureMode(); } /** @@ -56,20 +70,7 @@ private static boolean isAppInBackgroundPiPMode() { public static void setYTShortsRepeatEnum(Enum ytEnum) { try { for (Enum ytBehavior : Objects.requireNonNull(ytEnum.getClass().getEnumConstants())) { - - boolean foundBehavior = false; - for (ShortsLoopBehavior rvBehavior : ShortsLoopBehavior.values()) { - if (ytBehavior.name().endsWith(rvBehavior.name())) { - rvBehavior.ytEnumValue = ytBehavior; - - Logger.printDebug(() -> rvBehavior + " set to YT enum: " + ytBehavior.name()); - foundBehavior = true; - } - } - - if (!foundBehavior) { - Logger.printException(() -> "Unknown Shorts loop behavior: " + ytBehavior.name()); - } + ShortsLoopBehavior.setYTEnumValue(ytBehavior); } } catch (Exception ex) { Logger.printException(() -> "setYTShortsRepeatEnum failure", ex);