This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(YouTube): Add
Shorts autoplay
patch (#719)
- Loading branch information
1 parent
1c9d371
commit 14fb894
Showing
5 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
app/src/main/java/app/revanced/integrations/youtube/patches/ShortsAutoplayPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
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 ShortsAutoplayPatch { | ||
|
||
private 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. | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
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. | ||
*/ | ||
@RequiresApi(api = Build.VERSION_CODES.N) | ||
public static Enum<?> changeShortsRepeatBehavior(Enum<?> original) { | ||
try { | ||
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.ytEnumValue | ||
: "Behavior setting is same as original. Using original: " + original.name() | ||
); | ||
|
||
return behavior.ytEnumValue; | ||
} | ||
} catch (Exception ex) { | ||
Logger.printException(() -> "changeShortsRepeatState failure", ex); | ||
} | ||
|
||
return original; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters