feat(YouTube): Add Override 'Open in YouTube Music' button patch

Co-authored-by: ILoveOpenSourceApplications <117499019+iloveopensourceapplications@users.noreply.github.com>
This commit is contained in:
oSumAtrIX 2026-03-08 22:39:22 +01:00
parent bc08ecf785
commit 8c2445f92f
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
6 changed files with 230 additions and 4 deletions

View file

@ -0,0 +1,18 @@
package app.revanced.patches.youtube.layout.buttons.music
import app.revanced.patcher.accessFlags
import app.revanced.patcher.definingClass
import app.revanced.patcher.gettingFirstMethodDeclaratively
import app.revanced.patcher.name
import app.revanced.patcher.parameterTypes
import app.revanced.patcher.patch.BytecodePatchContext
import app.revanced.patcher.returnType
import com.android.tools.smali.dexlib2.AccessFlags
internal val BytecodePatchContext.getOverridePackageNameMethod by gettingFirstMethodDeclaratively {
name("getOverridePackageName")
definingClass(EXTENSION_CLASS_DESCRIPTOR)
accessFlags(AccessFlags.PUBLIC, AccessFlags.STATIC)
returnType("Ljava/lang/String;")
parameterTypes()
}

View file

@ -0,0 +1,122 @@
package app.revanced.patches.youtube.layout.buttons.music
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.extensions.methodReference
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patcher.patch.resourcePatch
import app.revanced.patcher.patch.stringOption
import app.revanced.patches.all.misc.resources.addResources
import app.revanced.patches.all.misc.resources.addResourcesPatch
import app.revanced.patches.shared.misc.settings.preference.PreferenceCategory
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
import app.revanced.patches.youtube.misc.settings.PreferenceScreen
import app.revanced.patches.youtube.misc.settings.settingsPatch
import app.revanced.util.forEachInstructionAsSequence
import app.revanced.util.returnEarly
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.RegisterRangeInstruction
import org.w3c.dom.Element
internal const val EXTENSION_CLASS_DESCRIPTOR =
"Lapp/revanced/extension/youtube/patches/OverrideOpenInYouTubeMusicButtonPatch;"
val packageNameOption = stringOption(
name = "YouTube Music package name",
description = "The package name of the YouTube Music app to open when clicking the 'Open in YouTube Music' button.",
default = "app.revanced.android.apps.youtube.music",
values = mapOf(
"Original package name" to "com.google.android.apps.youtube.music",
"ReVanced default package name" to "app.revanced.android.apps.youtube.music"
),
required = true,
)
private val overrideOpenInYouTubeMusicManifestResourcePatch = resourcePatch {
apply {
val packageName by packageNameOption
document("AndroidManifest.xml").use { document ->
val queriesList = document.getElementsByTagName("queries")
val queries = if (queriesList.length > 0) queriesList.item(0) as Element
else document.createElement("queries").also(document::appendChild)
document.createElement("package").apply {
setAttribute("android:name", packageName)
}.let(queries::appendChild)
}
}
}
@Suppress("unused")
val overrideOpenInYouTubeMusicButtonPatch = bytecodePatch(
name = "Override 'Open in YouTube Music' button",
description = "Overrides the button to open YouTube Music under a different package name. " +
"By default, it overrides to the ReVanced default package name of YouTube Music.",
) {
dependsOn(
settingsPatch,
addResourcesPatch,
overrideOpenInYouTubeMusicManifestResourcePatch
)
compatibleWith(
"com.google.android.youtube"(
"20.14.43",
"20.21.37",
"20.26.46",
"20.31.42",
"20.37.48",
"20.40.45"
),
)
val packageName by packageNameOption()
apply {
addResources("youtube", "layout.buttons.music.overrideOpenInYouTubeMusicButtonPatch")
PreferenceScreen.GENERAL.addPreferences(
PreferenceCategory(
titleKey = null,
tag = "app.revanced.extension.shared.settings.preference.NoTitlePreferenceCategory",
preferences = setOf(SwitchPreference(key = "revanced_override_open_in_youtube_music_button"))
)
)
getOverridePackageNameMethod.returnEarly(packageName!!)
forEachInstructionAsSequence({ _, _, instruction, index ->
if (instruction.opcode != Opcode.INVOKE_VIRTUAL) return@forEachInstructionAsSequence null
val reference = instruction.methodReference ?: return@forEachInstructionAsSequence null
if (reference.definingClass != "Landroid/content/Intent;") return@forEachInstructionAsSequence null
when (reference.name) {
"setPackage" if reference.parameterTypes == listOf("Ljava/lang/String;") ->
index to "overrideSetPackage(Landroid/content/Intent;Ljava/lang/String;)Landroid/content/Intent;"
"setData" if reference.parameterTypes == listOf("Landroid/net/Uri;") ->
index to "overrideSetData(Landroid/content/Intent;Landroid/net/Uri;)Landroid/content/Intent;"
else -> null
}
}) { method, (index, methodDescriptor) ->
val invokeString = when (val instruction = method.getInstruction(index)) {
is RegisterRangeInstruction ->
"invoke-static/range { v${instruction.startRegister} .. v${instruction.startRegister + instruction.registerCount - 1} }"
is FiveRegisterInstruction ->
"invoke-static { v${instruction.registerC}, v${instruction.registerD} }"
else -> return@forEachInstructionAsSequence
}
method.replaceInstruction(
index,
"$invokeString, $EXTENSION_CLASS_DESCRIPTOR->$methodDescriptor"
)
}
}
}

View file

@ -1111,10 +1111,7 @@ To show the Audio track menu, change \'Spoof video streams\' to \'Android No SDK
</patch>
<patch id="layout.hide.endscreensuggestedvideo.hideEndScreenSuggestedVideoPatch">
<string name="revanced_end_screen_suggested_video_title">Hide end screen suggested video</string>
<string name="revanced_end_screen_suggested_video_summary_on">"End screen suggested video is hidden when autoplay is turned off
Autoplay can be changed in YouTube settings:
Settings → Playback → Autoplay next video"</string>
<string name="revanced_end_screen_suggested_video_summary_on">"End screen suggested video is hidden</string>
<string name="revanced_end_screen_suggested_video_summary_off">End screen suggested video is shown</string>
</patch>
<patch id="layout.hide.relatedvideooverlay.hideRelatedVideoOverlayPatch">
@ -1442,6 +1439,11 @@ If later turned off, it is recommended to clear the app data to prevent UI bugs.
<string name="revanced_spoof_app_version_target_entry_1">20.13.41 - Restore non collapsed video action bar</string>
<string name="revanced_spoof_app_version_target_entry_2">20.05.46 - Restore transcript functionality</string>
</patch>
<patch id="layout.buttons.music.overrideOpenInYouTubeMusicButtonPatch">
<string name="revanced_override_open_in_youtube_music_button_title">Override \'Open in YouTube Music\' button</string>
<string name="revanced_override_open_in_youtube_music_button_summary_on">\'Open in YouTube Music\' button opens your target music app</string>
<string name="revanced_override_open_in_youtube_music_button_summary_off">\'Open in YouTube Music\' button opens the original app</string>
</patch>
<patch id="layout.startpage.changeStartPagePatch">
<string name="revanced_change_start_page_title">Change start page</string>
<string name="revanced_change_start_page_entry_default">Default</string>