|
| 1 | +package app.revanced.extension.youtube.patches; |
| 2 | + |
| 3 | +import static app.revanced.extension.youtube.shared.NavigationBar.NavigationButton; |
| 4 | + |
| 5 | +import android.app.Activity; |
| 6 | +import android.content.Intent; |
| 7 | +import android.net.Uri; |
| 8 | + |
| 9 | +import java.lang.ref.WeakReference; |
| 10 | + |
| 11 | +import app.revanced.extension.shared.Logger; |
| 12 | +import app.revanced.extension.youtube.settings.Settings; |
| 13 | + |
| 14 | +@SuppressWarnings("unused") |
| 15 | +public class OpenShortsInRegularPlayerPatch { |
| 16 | + |
| 17 | + public enum ShortsPlayerType { |
| 18 | + SHORTS_PLAYER, |
| 19 | + REGULAR_PLAYER, |
| 20 | + REGULAR_PLAYER_FULLSCREEN |
| 21 | + } |
| 22 | + |
| 23 | + static { |
| 24 | + if (!VersionCheckPatch.IS_19_46_OR_GREATER |
| 25 | + && Settings.SHORTS_PLAYER_TYPE.get() == ShortsPlayerType.REGULAR_PLAYER_FULLSCREEN) { |
| 26 | + // User imported newer settings to an older app target. |
| 27 | + Logger.printInfo(() -> "Resetting " + Settings.SHORTS_PLAYER_TYPE); |
| 28 | + Settings.SHORTS_PLAYER_TYPE.resetToDefault(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static WeakReference<Activity> mainActivityRef = new WeakReference<>(null); |
| 33 | + |
| 34 | + /** |
| 35 | + * Injection point. |
| 36 | + */ |
| 37 | + public static void setMainActivity(Activity activity) { |
| 38 | + mainActivityRef = new WeakReference<>(activity); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Injection point. |
| 43 | + */ |
| 44 | + public static boolean openShort(String videoID) { |
| 45 | + try { |
| 46 | + ShortsPlayerType type = Settings.SHORTS_PLAYER_TYPE.get(); |
| 47 | + if (type == ShortsPlayerType.SHORTS_PLAYER) { |
| 48 | + return false; // Default unpatched behavior. |
| 49 | + } |
| 50 | + |
| 51 | + if (videoID.isEmpty()) { |
| 52 | + // Shorts was opened using launcher app shortcut. |
| 53 | + // |
| 54 | + // This check will not detect if the Shorts app shortcut is used |
| 55 | + // while the app is running in the background (instead the regular player is opened). |
| 56 | + // To detect that the hooked method map parameter can be checked |
| 57 | + // if integer key 'com.google.android.apps.youtube.app.endpoint.flags' |
| 58 | + // has bitmask 16 set. |
| 59 | + // |
| 60 | + // This use case seems unlikely if the user has the Shorts |
| 61 | + // set to open in the regular player, so it's ignored as |
| 62 | + // checking the map makes the patch more complicated. |
| 63 | + Logger.printDebug(() -> "Ignoring Short with no videoId"); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + if (NavigationButton.getSelectedNavigationButton() == NavigationButton.SHORTS) { |
| 68 | + return false; // Always use Shorts player for the Shorts nav button. |
| 69 | + } |
| 70 | + |
| 71 | + final boolean forceFullScreen = (type == ShortsPlayerType.REGULAR_PLAYER_FULLSCREEN); |
| 72 | + OpenVideosFullscreenHookPatch.setOpenNextVideoFullscreen(forceFullScreen); |
| 73 | + |
| 74 | + // Can use the application context and add intent flags of |
| 75 | + // FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP |
| 76 | + // But the activity context seems to fix random app crashes |
| 77 | + // if Shorts urls are opened outside the app. |
| 78 | + var context = mainActivityRef.get(); |
| 79 | + |
| 80 | + Intent videoPlayerIntent = new Intent( |
| 81 | + Intent.ACTION_VIEW, |
| 82 | + Uri.parse("https://youtube.com/watch?v=" + videoID) |
| 83 | + ); |
| 84 | + videoPlayerIntent.setPackage(context.getPackageName()); |
| 85 | + |
| 86 | + context.startActivity(videoPlayerIntent); |
| 87 | + return true; |
| 88 | + } catch (Exception ex) { |
| 89 | + OpenVideosFullscreenHookPatch.setOpenNextVideoFullscreen(null); |
| 90 | + Logger.printException(() -> "openShort failure", ex); |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments