From f727e6d58742a2e0ccb4cda6471a2a14f5c93a04 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:08:29 -0400 Subject: [PATCH 01/19] fix: Show audio track button without layout overlap issues --- .../BottomControlsResourcePatch.kt | 45 +++++++++---------- .../kotlin/app/revanced/util/ResourceUtils.kt | 22 +++++++++ .../youtube_controls_bottom_ui_container.xml | 4 +- .../youtube_controls_bottom_ui_container.xml | 2 +- .../youtube_controls_bottom_ui_container.xml | 2 +- 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt index 474ccf4798..6c2ed33600 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt @@ -1,10 +1,13 @@ package app.revanced.patches.youtube.misc.playercontrols import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchException import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patcher.util.DomFileEditor import app.revanced.patches.shared.misc.mapping.ResourceMappingPatch +import app.revanced.util.findElementById +import org.w3c.dom.Element import java.io.Closeable @Patch(dependencies = [ResourceMappingPatch::class]) @@ -14,17 +17,25 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable { private const val TARGET_RESOURCE_NAME = "youtube_controls_bottom_ui_container.xml" private const val TARGET_RESOURCE = "res/layout/$TARGET_RESOURCE_NAME" - // The element to the left of the element being added. - private var lastLeftOf = "fullscreen_button" - private lateinit var resourceContext: ResourceContext private lateinit var targetDocumentEditor: DomFileEditor + private lateinit var targetElement : Element override fun execute(context: ResourceContext) { + bottomUiContainerResourceId = ResourceMappingPatch["id", "bottom_ui_container_stub"] + resourceContext = context targetDocumentEditor = context.xmlEditor[TARGET_RESOURCE] - bottomUiContainerResourceId = ResourceMappingPatch["id", "bottom_ui_container_stub"] + // Add all buttons to an inner layout, to prevent audio track and other + // YT buttons from being inserted into the middle. + targetElement = targetDocumentEditor.file.createElement("LinearLayout") + targetElement.setAttribute("android:layout_height", "wrap_content") + targetElement.setAttribute("android:layout_width", "wrap_content") + + val bottomContainer = targetDocumentEditor.file.childNodes.findElementById("@id/bottom_end_container") + ?: throw PatchException("Could not find target element") + bottomContainer.appendChild(targetElement) } /** @@ -38,33 +49,19 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable { "$resourceDirectoryName/host/layout/$TARGET_RESOURCE_NAME", )!!, ] - val sourceDocument = sourceDocumentEditor.file - val targetDocument = targetDocumentEditor.file - val targetElementTag = "android.support.constraint.ConstraintLayout" - - val sourceElements = sourceDocument.getElementsByTagName(targetElementTag).item(0).childNodes - val targetElement = targetDocument.getElementsByTagName(targetElementTag).item(0) + val sourceElements = sourceDocumentEditor.file.getElementsByTagName( + "android.support.constraint.ConstraintLayout" + ).item(0).childNodes + // Copy the patch layout xml into the target layout file. for (index in 1 until sourceElements.length) { val element = sourceElements.item(index).cloneNode(true) - // If the element has no attributes there's no point to adding it to the destination. - if (!element.hasAttributes()) continue - - // Set the elements lastLeftOf attribute to the lastLeftOf value. - val namespace = "@+id" - element.attributes.getNamedItem("yt:layout_constraintRight_toLeftOf").nodeValue = - "$namespace/$lastLeftOf" - - // Set lastLeftOf attribute to the current element. - val nameSpaceLength = 5 - lastLeftOf = element.attributes.getNamedItem("android:id").nodeValue.substring(nameSpaceLength) - - // Add the element. - targetDocument.adoptNode(element) + targetDocumentEditor.file.adoptNode(element) targetElement.appendChild(element) } + sourceDocumentEditor.close() } diff --git a/src/main/kotlin/app/revanced/util/ResourceUtils.kt b/src/main/kotlin/app/revanced/util/ResourceUtils.kt index 56076078d3..c1465d0c95 100644 --- a/src/main/kotlin/app/revanced/util/ResourceUtils.kt +++ b/src/main/kotlin/app/revanced/util/ResourceUtils.kt @@ -3,6 +3,7 @@ package app.revanced.util import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.util.DomFileEditor import app.revanced.util.resource.BaseResource +import org.w3c.dom.Element import org.w3c.dom.Node import org.w3c.dom.NodeList import java.io.InputStream @@ -164,3 +165,24 @@ internal fun Node.addResource( } internal fun org.w3c.dom.Document.getNode(tagName: String) = this.getElementsByTagName(tagName).item(0) + +internal fun NodeList.findElementById(id: String): Element? { + for (i in 0 until length) { + val node = item(i) + if (node.nodeType == Node.ELEMENT_NODE) { + val element = node as Element + + if (element.getAttribute("android:id") == id) { + return element + } + + // Recursively search. + val found = element.childNodes.findElementById(id) + if (found != null) { + return found + } + } + } + + return null +} diff --git a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml index 3a7cdd3efa..374882bce9 100644 --- a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,5 +1,5 @@ - - + + diff --git a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml index b38a30b976..0885a38666 100644 --- a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + diff --git a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml index ddd8a125c8..9b6a8fd659 100644 --- a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + From 03bdecd10d08e0a3e481463f641dc07e5ec1ae41 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 21 Sep 2024 20:09:53 -0400 Subject: [PATCH 02/19] refactor --- .../misc/playercontrols/BottomControlsResourcePatch.kt | 9 ++++++--- src/main/kotlin/app/revanced/util/ResourceUtils.kt | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt index 6c2ed33600..ac374a1f3a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt @@ -6,7 +6,7 @@ import app.revanced.patcher.patch.ResourcePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patcher.util.DomFileEditor import app.revanced.patches.shared.misc.mapping.ResourceMappingPatch -import app.revanced.util.findElementById +import app.revanced.util.findElementByAttributeValue import org.w3c.dom.Element import java.io.Closeable @@ -33,8 +33,11 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable { targetElement.setAttribute("android:layout_height", "wrap_content") targetElement.setAttribute("android:layout_width", "wrap_content") - val bottomContainer = targetDocumentEditor.file.childNodes.findElementById("@id/bottom_end_container") - ?: throw PatchException("Could not find target element") + val bottomContainer = targetDocumentEditor.file.childNodes.findElementByAttributeValue( + "android:id", + "@id/bottom_end_container" + ) ?: throw PatchException("Could not find target element") + bottomContainer.appendChild(targetElement) } diff --git a/src/main/kotlin/app/revanced/util/ResourceUtils.kt b/src/main/kotlin/app/revanced/util/ResourceUtils.kt index c1465d0c95..fde3752f2d 100644 --- a/src/main/kotlin/app/revanced/util/ResourceUtils.kt +++ b/src/main/kotlin/app/revanced/util/ResourceUtils.kt @@ -50,7 +50,7 @@ fun ResourceContext.copyResources( sourceResourceDirectory: String, vararg resources: ResourceGroup, ) { - val targetResourceDirectory = this.get("res") + val targetResourceDirectory = this["res", false] for (resourceGroup in resources) { resourceGroup.resources.forEach { resource -> @@ -166,18 +166,18 @@ internal fun Node.addResource( internal fun org.w3c.dom.Document.getNode(tagName: String) = this.getElementsByTagName(tagName).item(0) -internal fun NodeList.findElementById(id: String): Element? { +internal fun NodeList.findElementByAttributeValue(attributeName: String, value: String): Element? { for (i in 0 until length) { val node = item(i) if (node.nodeType == Node.ELEMENT_NODE) { val element = node as Element - if (element.getAttribute("android:id") == id) { + if (element.getAttribute(attributeName) == value) { return element } // Recursively search. - val found = element.childNodes.findElementById(id) + val found = element.childNodes.findElementByAttributeValue(attributeName, value) if (found != null) { return found } From e108f225b9543404c927a28ce8e977171c65c902 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:36:34 -0400 Subject: [PATCH 03/19] fix: Fix vertical alignment in some situations --- .../playercontrols/BottomControlsResourcePatch.kt | 11 ++++++++--- .../layout/youtube_controls_bottom_ui_container.xml | 4 ++-- .../layout/youtube_controls_bottom_ui_container.xml | 2 +- .../layout/youtube_controls_bottom_ui_container.xml | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt index ac374a1f3a..74b28cdcdc 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch.kt @@ -27,11 +27,16 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable { resourceContext = context targetDocumentEditor = context.xmlEditor[TARGET_RESOURCE] - // Add all buttons to an inner layout, to prevent audio track and other - // YT buttons from being inserted into the middle. + // Add all buttons to an inner layout, to prevent + // cardboard VR from being inserted into the middle. targetElement = targetDocumentEditor.file.createElement("LinearLayout") + targetElement.setAttribute("android:layoutDirection", "ltr") + targetElement.setAttribute("android:layout_width", "match_parent") targetElement.setAttribute("android:layout_height", "wrap_content") - targetElement.setAttribute("android:layout_width", "wrap_content") + targetElement.setAttribute("android:paddingTop", "0dip") + targetElement.setAttribute("android:paddingBottom", "1dip") + targetElement.setAttribute("android:orientation", "horizontal") + targetElement.setAttribute("android:gravity", "center_vertical") val bottomContainer = targetDocumentEditor.file.childNodes.findElementByAttributeValue( "android:id", diff --git a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml index 374882bce9..a519b020b5 100644 --- a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,5 +1,5 @@ - - + + diff --git a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml index 0885a38666..befd56fb23 100644 --- a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + diff --git a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml index 9b6a8fd659..67b2633881 100644 --- a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + From 65c265aba25978c9d2a16c8f1496bf2c73860b7e Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:17:33 -0400 Subject: [PATCH 04/19] fix: Remove empty attributes --- .../host/layout/youtube_controls_bottom_ui_container.xml | 4 ++-- .../host/layout/youtube_controls_bottom_ui_container.xml | 2 +- .../host/layout/youtube_controls_bottom_ui_container.xml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml index a519b020b5..0d6ac2bfff 100644 --- a/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/copyvideourl/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,5 +1,5 @@ - - + + diff --git a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml index befd56fb23..d79ee466e2 100644 --- a/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/downloads/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + diff --git a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml index 67b2633881..a91ecc2f3b 100644 --- a/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml +++ b/src/main/resources/speedbutton/host/layout/youtube_controls_bottom_ui_container.xml @@ -1,4 +1,4 @@ - + From 945ea3a7d32385bc6962a32f277d76f34070c629 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:17:50 -0400 Subject: [PATCH 05/19] Comments --- src/main/resources/copyvideourl/drawable/revanced_yt_copy.xml | 2 +- .../copyvideourl/drawable/revanced_yt_copy_timestamp.xml | 2 +- .../downloads/drawable/revanced_yt_download_button.xml | 2 +- .../drawable/revanced_playback_speed_dialog_button.xml | 2 +- src/main/resources/sponsorblock/drawable/revanced_sb_adjust.xml | 2 +- .../resources/sponsorblock/drawable/revanced_sb_backward.xml | 2 +- .../resources/sponsorblock/drawable/revanced_sb_compare.xml | 2 +- src/main/resources/sponsorblock/drawable/revanced_sb_edit.xml | 2 +- .../resources/sponsorblock/drawable/revanced_sb_forward.xml | 2 +- src/main/resources/sponsorblock/drawable/revanced_sb_logo.xml | 2 +- .../resources/sponsorblock/drawable/revanced_sb_publish.xml | 2 +- src/main/resources/sponsorblock/drawable/revanced_sb_voting.xml | 2 +- .../swipecontrols/drawable/revanced_ic_sc_brightness_auto.xml | 2 +- .../swipecontrols/drawable/revanced_ic_sc_brightness_manual.xml | 2 +- .../swipecontrols/drawable/revanced_ic_sc_volume_mute.xml | 2 +- .../swipecontrols/drawable/revanced_ic_sc_volume_normal.xml | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/resources/copyvideourl/drawable/revanced_yt_copy.xml b/src/main/resources/copyvideourl/drawable/revanced_yt_copy.xml index 008393c30c..2a5bbc872a 100644 --- a/src/main/resources/copyvideourl/drawable/revanced_yt_copy.xml +++ b/src/main/resources/copyvideourl/drawable/revanced_yt_copy.xml @@ -1,6 +1,6 @@