generated from ReVanced/revanced-patches-template
-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Open Shorts in regular player
patch (#4153)
- Loading branch information
1 parent
a4db3f1
commit c7c5e5b
Showing
18 changed files
with
463 additions
and
62 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
.../src/main/java/app/revanced/extension/youtube/patches/OpenShortsInRegularPlayerPatch.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,94 @@ | ||
package app.revanced.extension.youtube.patches; | ||
|
||
import static app.revanced.extension.youtube.shared.NavigationBar.NavigationButton; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
|
||
import java.lang.ref.WeakReference; | ||
|
||
import app.revanced.extension.shared.Logger; | ||
import app.revanced.extension.youtube.settings.Settings; | ||
|
||
@SuppressWarnings("unused") | ||
public class OpenShortsInRegularPlayerPatch { | ||
|
||
public enum ShortsPlayerType { | ||
SHORTS_PLAYER, | ||
REGULAR_PLAYER, | ||
REGULAR_PLAYER_FULLSCREEN | ||
} | ||
|
||
static { | ||
if (!VersionCheckPatch.IS_19_46_OR_GREATER | ||
&& Settings.SHORTS_PLAYER_TYPE.get() == ShortsPlayerType.REGULAR_PLAYER_FULLSCREEN) { | ||
// User imported newer settings to an older app target. | ||
Logger.printInfo(() -> "Resetting " + Settings.SHORTS_PLAYER_TYPE); | ||
Settings.SHORTS_PLAYER_TYPE.resetToDefault(); | ||
} | ||
} | ||
|
||
private static WeakReference<Activity> mainActivityRef = new WeakReference<>(null); | ||
|
||
/** | ||
* Injection point. | ||
*/ | ||
public static void setMainActivity(Activity activity) { | ||
mainActivityRef = new WeakReference<>(activity); | ||
} | ||
|
||
/** | ||
* Injection point. | ||
*/ | ||
public static boolean openShort(String videoID) { | ||
try { | ||
ShortsPlayerType type = Settings.SHORTS_PLAYER_TYPE.get(); | ||
if (type == ShortsPlayerType.SHORTS_PLAYER) { | ||
return false; // Default unpatched behavior. | ||
} | ||
|
||
if (videoID.isEmpty()) { | ||
// Shorts was opened using launcher app shortcut. | ||
// | ||
// This check will not detect if the Shorts app shortcut is used | ||
// while the app is running in the background (instead the regular player is opened). | ||
// To detect that the hooked method map parameter can be checked | ||
// if integer key 'com.google.android.apps.youtube.app.endpoint.flags' | ||
// has bitmask 16 set. | ||
// | ||
// This use case seems unlikely if the user has the Shorts | ||
// set to open in the regular player, so it's ignored as | ||
// checking the map makes the patch more complicated. | ||
Logger.printDebug(() -> "Ignoring Short with no videoId"); | ||
return false; | ||
} | ||
|
||
if (NavigationButton.getSelectedNavigationButton() == NavigationButton.SHORTS) { | ||
return false; // Always use Shorts player for the Shorts nav button. | ||
} | ||
|
||
final boolean forceFullScreen = (type == ShortsPlayerType.REGULAR_PLAYER_FULLSCREEN); | ||
OpenVideosFullscreenHookPatch.setOpenNextVideoFullscreen(forceFullScreen); | ||
|
||
// Can use the application context and add intent flags of | ||
// FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP | ||
// But the activity context seems to fix random app crashes | ||
// if Shorts urls are opened outside the app. | ||
var context = mainActivityRef.get(); | ||
|
||
Intent videoPlayerIntent = new Intent( | ||
Intent.ACTION_VIEW, | ||
Uri.parse("https://youtube.com/watch?v=" + videoID) | ||
); | ||
videoPlayerIntent.setPackage(context.getPackageName()); | ||
|
||
context.startActivity(videoPlayerIntent); | ||
return true; | ||
} catch (Exception ex) { | ||
OpenVideosFullscreenHookPatch.setOpenNextVideoFullscreen(null); | ||
Logger.printException(() -> "openShort failure", ex); | ||
return false; | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
...ns/youtube/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreen.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
...e/src/main/java/app/revanced/extension/youtube/patches/OpenVideosFullscreenHookPatch.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,41 @@ | ||
package app.revanced.extension.youtube.patches; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import app.revanced.extension.youtube.settings.Settings; | ||
|
||
@SuppressWarnings("unused") | ||
public class OpenVideosFullscreenHookPatch { | ||
|
||
@Nullable | ||
private static volatile Boolean openNextVideoFullscreen; | ||
|
||
public static void setOpenNextVideoFullscreen(@Nullable Boolean forceFullScreen) { | ||
openNextVideoFullscreen = forceFullScreen; | ||
} | ||
|
||
/** | ||
* Changed during patching since this class is also | ||
* used by {@link OpenVideosFullscreenHookPatch}. | ||
*/ | ||
private static boolean isFullScreenPatchIncluded() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Injection point. | ||
*/ | ||
public static boolean openVideoFullscreenPortrait(boolean original) { | ||
Boolean openFullscreen = openNextVideoFullscreen; | ||
if (openFullscreen != null) { | ||
openNextVideoFullscreen = null; | ||
return openFullscreen; | ||
} | ||
|
||
if (!isFullScreenPatchIncluded()) { | ||
return false; | ||
} | ||
|
||
return Settings.OPEN_VIDEOS_FULLSCREEN_PORTRAIT.get(); | ||
} | ||
} |
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
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
45 changes: 4 additions & 41 deletions
45
...main/kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreen.kt
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 |
---|---|---|
@@ -1,46 +1,9 @@ | ||
package app.revanced.patches.youtube.layout.player.fullscreen | ||
|
||
import app.revanced.patcher.patch.bytecodePatch | ||
import app.revanced.patches.all.misc.resources.addResources | ||
import app.revanced.patches.all.misc.resources.addResourcesPatch | ||
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference | ||
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch | ||
import app.revanced.patches.youtube.misc.settings.PreferenceScreen | ||
import app.revanced.patches.youtube.misc.settings.settingsPatch | ||
import app.revanced.util.insertFeatureFlagBooleanOverride | ||
|
||
private const val EXTENSION_CLASS_DESCRIPTOR = | ||
"Lapp/revanced/extension/youtube/patches/OpenVideosFullscreen;" | ||
|
||
@Suppress("unused") | ||
val openVideosFullscreenPatch = bytecodePatch( | ||
name = "Open videos fullscreen", | ||
description = "Adds an option to open videos in full screen portrait mode.", | ||
) { | ||
dependsOn( | ||
sharedExtensionPatch, | ||
settingsPatch, | ||
addResourcesPatch, | ||
) | ||
|
||
compatibleWith( | ||
"com.google.android.youtube"( | ||
"19.46.42", | ||
) | ||
) | ||
|
||
execute { | ||
openVideosFullscreenPortraitFingerprint.method.insertFeatureFlagBooleanOverride( | ||
OPEN_VIDEOS_FULLSCREEN_PORTRAIT_FEATURE_FLAG, | ||
"$EXTENSION_CLASS_DESCRIPTOR->openVideoFullscreenPortrait(Z)Z" | ||
) | ||
|
||
// Add resources and setting last, in case the user force patches an old incompatible version. | ||
|
||
addResources("youtube", "layout.player.fullscreen.openVideosFullscreen") | ||
|
||
PreferenceScreen.PLAYER.addPreferences( | ||
SwitchPreference("revanced_open_videos_fullscreen_portrait") | ||
) | ||
} | ||
} | ||
@Deprecated("Renamed to openVideosFullscreenPatch", ReplaceWith("openVideosFullscreenPatch")) | ||
val openVideosFullscreen = bytecodePatch{ | ||
dependsOn(openVideosFullscreenPatch) | ||
} |
32 changes: 32 additions & 0 deletions
32
...in/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreenHookPatch.kt
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,32 @@ | ||
package app.revanced.patches.youtube.layout.player.fullscreen | ||
|
||
import app.revanced.patcher.patch.bytecodePatch | ||
import app.revanced.patches.youtube.layout.shortsplayer.openShortsInRegularPlayerPatch | ||
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch | ||
import app.revanced.patches.youtube.misc.playservice.is_19_46_or_greater | ||
import app.revanced.patches.youtube.misc.playservice.versionCheckPatch | ||
import app.revanced.util.insertFeatureFlagBooleanOverride | ||
|
||
internal const val EXTENSION_CLASS_DESCRIPTOR = | ||
"Lapp/revanced/extension/youtube/patches/OpenVideosFullscreenHookPatch;" | ||
|
||
/** | ||
* Used by both [openVideosFullscreenPatch] and [openShortsInRegularPlayerPatch]. | ||
*/ | ||
internal val openVideosFullscreenHookPatch = bytecodePatch { | ||
dependsOn( | ||
sharedExtensionPatch, | ||
versionCheckPatch | ||
) | ||
|
||
execute { | ||
if (!is_19_46_or_greater) { | ||
return@execute | ||
} | ||
|
||
openVideosFullscreenPortraitFingerprint.method.insertFeatureFlagBooleanOverride( | ||
OPEN_VIDEOS_FULLSCREEN_PORTRAIT_FEATURE_FLAG, | ||
"$EXTENSION_CLASS_DESCRIPTOR->openVideoFullscreenPortrait(Z)Z" | ||
) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...kotlin/app/revanced/patches/youtube/layout/player/fullscreen/OpenVideosFullscreenPatch.kt
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,46 @@ | ||
package app.revanced.patches.youtube.layout.player.fullscreen | ||
|
||
import app.revanced.patcher.patch.PatchException | ||
import app.revanced.patcher.patch.bytecodePatch | ||
import app.revanced.patches.all.misc.resources.addResources | ||
import app.revanced.patches.all.misc.resources.addResourcesPatch | ||
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference | ||
import app.revanced.patches.youtube.misc.playservice.is_19_46_or_greater | ||
import app.revanced.patches.youtube.misc.playservice.versionCheckPatch | ||
import app.revanced.patches.youtube.misc.settings.PreferenceScreen | ||
import app.revanced.patches.youtube.misc.settings.settingsPatch | ||
import app.revanced.util.returnEarly | ||
|
||
@Suppress("unused") | ||
val openVideosFullscreenPatch = bytecodePatch( | ||
name = "Open videos fullscreen", | ||
description = "Adds an option to open videos in full screen portrait mode.", | ||
) { | ||
dependsOn( | ||
openVideosFullscreenHookPatch, | ||
settingsPatch, | ||
addResourcesPatch, | ||
versionCheckPatch | ||
) | ||
|
||
compatibleWith( | ||
"com.google.android.youtube"( | ||
"19.46.42", | ||
) | ||
) | ||
|
||
execute { | ||
if (!is_19_46_or_greater) { | ||
throw PatchException("'Open videos fullscreen' requires 19.46.42 or greater") | ||
} | ||
|
||
addResources("youtube", "layout.player.fullscreen.openVideosFullscreen") | ||
|
||
PreferenceScreen.PLAYER.addPreferences( | ||
SwitchPreference("revanced_open_videos_fullscreen_portrait") | ||
) | ||
|
||
// Enable the logic for the user Setting to open regular videos fullscreen. | ||
openVideosFullscreenHookPatchExtensionFingerprint.method.returnEarly(true) | ||
} | ||
} |
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
Oops, something went wrong.