feat(Public API): Make BottomControlsResource#addControls public (#2514)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
Aunali321
2023-12-18 20:06:20 +01:00
committed by GitHub
co-authored by oSumAtrIX
parent 5a14b17338
commit 26565fdb91
4 changed files with 56 additions and 40 deletions
@@ -11,15 +11,15 @@ import java.io.Closeable
object BottomControlsResourcePatch : ResourcePatch(), Closeable {
internal var bottomUiContainerResourceId: Long = -1
internal const val TARGET_RESOURCE_NAME = "youtube_controls_bottom_ui_container.xml"
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 targetXmlEditor: DomFileEditor
// The element to which to add the new elements to
private var lastLeftOf = "fullscreen_button"
override fun execute(context: ResourceContext) {
resourceContext = context
targetXmlEditor = context.xmlEditor[TARGET_RESOURCE]
@@ -30,46 +30,44 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable {
/**
* Add new controls to the bottom of the YouTube player.
* @param hostYouTubeControlsBottomUiResourceName The hosting resource name containing the elements.
*
* @param resourceDirectoryName The name of the directory containing the hosting resource.
*/
internal fun addControls(hostYouTubeControlsBottomUiResourceName: String) {
val sourceXmlEditor =
resourceContext.xmlEditor[this::class.java.classLoader.getResourceAsStream(
hostYouTubeControlsBottomUiResourceName
)!!]
fun addControls(resourceDirectoryName: String) {
val sourceXmlEditor = resourceContext.xmlEditor[
this::class.java.classLoader.getResourceAsStream(
"$resourceDirectoryName/host/layout/$TARGET_RESOURCE_NAME"
)!!
]
val targetElement =
"android.support.constraint.ConstraintLayout"
val targetElement = "android.support.constraint.ConstraintLayout"
val hostElements = sourceXmlEditor.file.getElementsByTagName(targetElement).item(0).childNodes
val destinationResourceFile = targetXmlEditor.file
val destinationElement =
destinationResourceFile.getElementsByTagName(targetElement).item(0)
val destinationElement = destinationResourceFile.getElementsByTagName(targetElement).item(0)
for (index in 1 until hostElements.length) {
val element = hostElements.item(index).cloneNode(true)
// if the element has no attributes theres no point to adding it to the destination
// 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
// 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
// Set lastLeftOf attribute to the current element.
val nameSpaceLength = 5
lastLeftOf = element.attributes.getNamedItem("android:id").nodeValue.substring(nameSpaceLength)
// copy the element
// Add the element.
destinationResourceFile.adoptNode(element)
destinationElement.appendChild(element)
}
sourceXmlEditor.close()
}
override fun close() {
targetXmlEditor.close()
}
override fun close() = targetXmlEditor.close()
}