feat(YouTube - Shorts components): add Change Shorts background repeat state setting (YouTube 19.34.42+)

This commit is contained in:
inotia00
2024-12-16 21:45:47 +09:00
parent 254c766347
commit 7f065547eb
8 changed files with 145 additions and 35 deletions

View File

@ -56,20 +56,6 @@ public class ShortsPatch {
NAVIGATION_BAR_HEIGHT_PERCENTAGE = heightPercentage / 100d;
}
public static Enum<?> repeat;
public static Enum<?> singlePlay;
public static Enum<?> endScreen;
public static Enum<?> changeShortsRepeatState(Enum<?> currentState) {
switch (Settings.CHANGE_SHORTS_REPEAT_STATE.get()) {
case 1 -> currentState = repeat;
case 2 -> currentState = singlePlay;
case 3 -> currentState = endScreen;
}
return currentState;
}
public static boolean disableResumingStartupShortsPlayer() {
return Settings.DISABLE_RESUMING_SHORTS_PLAYER.get();
}

View File

@ -0,0 +1,101 @@
package app.revanced.extension.youtube.patches.shorts;
import android.app.Activity;
import java.lang.ref.WeakReference;
import java.util.Objects;
import app.revanced.extension.shared.utils.Logger;
import app.revanced.extension.youtube.settings.Settings;
import app.revanced.extension.youtube.utils.ExtendedUtils;
@SuppressWarnings("unused")
public class ShortsRepeatStatePatch {
public enum ShortsLoopBehavior {
UNKNOWN,
/**
* Repeat the same Short forever!
*/
REPEAT,
/**
* Play once, then advanced to the next Short.
*/
SINGLE_PLAY,
/**
* Pause playback after 1 play.
*/
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.
*/
private Enum<?> ytEnumValue;
}
private static WeakReference<Activity> mainActivityRef = new WeakReference<>(null);
public static void setMainActivity(Activity activity) {
mainActivityRef = new WeakReference<>(activity);
}
/**
* @return If the app is currently in background PiP mode.
*/
private static boolean isAppInBackgroundPiPMode() {
Activity activity = mainActivityRef.get();
return activity != null && activity.isInPictureInPictureMode();
}
/**
* Injection point.
*/
public static void setYTShortsRepeatEnum(Enum<?> ytEnum) {
try {
for (Enum<?> ytBehavior : Objects.requireNonNull(ytEnum.getClass().getEnumConstants())) {
ShortsLoopBehavior.setYTEnumValue(ytBehavior);
}
} catch (Exception ex) {
Logger.printException(() -> "setYTShortsRepeatEnum failure", ex);
}
}
/**
* Injection point.
*/
public static Enum<?> changeShortsRepeatBehavior(Enum<?> original) {
try {
final ShortsLoopBehavior behavior = ExtendedUtils.IS_19_34_OR_GREATER &&
isAppInBackgroundPiPMode()
? Settings.CHANGE_SHORTS_BACKGROUND_REPEAT_STATE.get()
: Settings.CHANGE_SHORTS_REPEAT_STATE.get();
if (behavior != ShortsLoopBehavior.UNKNOWN && behavior.ytEnumValue != null) {
Logger.printDebug(() -> behavior.ytEnumValue == original
? "Changing Shorts repeat behavior from: " + original.name() + " to: " + behavior.ytEnumValue
: "Behavior setting is same as original. Using original: " + original.name()
);
return behavior.ytEnumValue;
}
} catch (Exception ex) {
Logger.printException(() -> "changeShortsRepeatState failure", ex);
}
return original;
}
}

View File

@ -39,6 +39,7 @@ import app.revanced.extension.youtube.patches.general.MiniplayerPatch;
import app.revanced.extension.youtube.patches.general.YouTubeMusicActionsPatch;
import app.revanced.extension.youtube.patches.misc.WatchHistoryPatch.WatchHistoryType;
import app.revanced.extension.youtube.patches.shorts.AnimationFeedbackPatch.AnimationType;
import app.revanced.extension.youtube.patches.shorts.ShortsRepeatStatePatch.ShortsLoopBehavior;
import app.revanced.extension.youtube.patches.utils.PatchStatus;
import app.revanced.extension.youtube.shared.PlaylistIdPrefix;
import app.revanced.extension.youtube.sponsorblock.SponsorBlockSettings;
@ -426,7 +427,8 @@ public class Settings extends BaseSettings {
public static final BooleanSetting HIDE_SHORTS_SHELF_SUBSCRIPTIONS = new BooleanSetting("revanced_hide_shorts_shelf_subscriptions", TRUE);
public static final BooleanSetting HIDE_SHORTS_SHELF_SEARCH = new BooleanSetting("revanced_hide_shorts_shelf_search", TRUE);
public static final BooleanSetting HIDE_SHORTS_SHELF_HISTORY = new BooleanSetting("revanced_hide_shorts_shelf_history", TRUE);
public static final IntegerSetting CHANGE_SHORTS_REPEAT_STATE = new IntegerSetting("revanced_change_shorts_repeat_state", 0);
public static final EnumSetting<ShortsLoopBehavior> CHANGE_SHORTS_REPEAT_STATE = new EnumSetting<>("revanced_change_shorts_repeat_state", ShortsLoopBehavior.UNKNOWN);
public static final EnumSetting<ShortsLoopBehavior> CHANGE_SHORTS_BACKGROUND_REPEAT_STATE = new EnumSetting<>("revanced_change_shorts_background_repeat_state", ShortsLoopBehavior.UNKNOWN);
// PreferenceScreen: Shorts - Shorts player components
public static final BooleanSetting HIDE_SHORTS_JOIN_BUTTON = new BooleanSetting("revanced_hide_shorts_join_button", TRUE);