Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/revanced-patches.api
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,7 @@ public final class app/revanced/patches/youtube/misc/minimizedplayback/Minimized

public final class app/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch : app/revanced/patcher/patch/ResourcePatch, java/io/Closeable {
public static final field INSTANCE Lapp/revanced/patches/youtube/misc/playercontrols/BottomControlsResourcePatch;
public final fun addControls (Ljava/lang/String;)V
public fun close ()V
public synthetic fun execute (Lapp/revanced/patcher/data/Context;)V
public fun execute (Lapp/revanced/patcher/data/ResourceContext;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,39 @@ internal object CopyVideoUrlResourcePatch : ResourcePatch() {
SwitchPreference(
"revanced_copy_video_url",
StringResource("revanced_copy_video_url_title", "Show copy video URL button"),
StringResource("revanced_copy_video_url_summary_on", "Button is shown. Tap to copy video URL. Tap and hold to copy video URL with timestamp"),
StringResource(
"revanced_copy_video_url_summary_on",
"Button is shown. Tap to copy video URL. Tap and hold to copy video URL with timestamp"
),
StringResource("revanced_copy_video_url_summary_off", "Button is not shown")
),
SwitchPreference(
"revanced_copy_video_url_timestamp",
StringResource("revanced_copy_video_url_timestamp_title", "Show copy timestamp URL button"),
StringResource("revanced_copy_video_url_timestamp_summary_on", "Button is shown. Tap to copy video URL with timestamp. Tap and hold to copy video without timestamp"),
StringResource(
"revanced_copy_video_url_timestamp_summary_on",
"Button is shown. Tap to copy video URL with timestamp. Tap and hold to copy video without timestamp"
),
StringResource("revanced_copy_video_url_timestamp_summary_off", "Button is not shown")
)
),
StringResource("revanced_copy_video_url_preference_screen_summary", "Settings related to copy URL buttons in video player")
StringResource(
"revanced_copy_video_url_preference_screen_summary",
"Settings related to copy URL buttons in video player"
)
)
)

context.copyResources("copyvideourl", ResourceGroup(
resourceDirectoryName = "drawable",
"revanced_yt_copy.xml",
"revanced_yt_copy_timestamp.xml"
)
context.copyResources(
"copyvideourl", ResourceGroup(
resourceDirectoryName = "drawable",
"revanced_yt_copy.xml",
"revanced_yt_copy_timestamp.xml"
)
)

// merge strings
context.mergeStrings("copyvideourl/host/values/strings.xml")

BottomControlsResourcePatch.addControls("copyvideourl/host/layout/${BottomControlsResourcePatch.TARGET_RESOURCE_NAME}")
BottomControlsResourcePatch.addControls("copyvideourl")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,35 @@ internal object ExternalDownloadsResourcePatch : ResourcePatch() {
"revanced_external_downloader",
StringResource("revanced_external_downloader_title", "Show external download button"),
StringResource("revanced_external_downloader_summary_on", "Download button shown in player"),
StringResource("revanced_external_downloader_summary_off", "Download button not shown in player")
StringResource(
"revanced_external_downloader_summary_off",
"Download button not shown in player"
)
),
TextPreference(
"revanced_external_downloader_name",
StringResource("revanced_external_downloader_name_title", "Downloader package name"),
StringResource("revanced_external_downloader_name_summary", "Package name of your installed external downloader app, such as NewPipe or Seal"),
StringResource(
"revanced_external_downloader_name_summary",
"Package name of your installed external downloader app, such as NewPipe or Seal"
),
InputType.TEXT
)
),
StringResource("revanced_external_downloader_preference_screen_summary", "Settings for using an external downloader")
StringResource(
"revanced_external_downloader_preference_screen_summary",
"Settings for using an external downloader"
)
)
)


// Copy strings
context.mergeStrings("downloads/host/values/strings.xml")

// Copy resources
context.copyResources("downloads", ResourceGroup("drawable", "revanced_yt_download_button.xml"))
context.copyResources(
"downloads",
ResourceGroup("drawable", "revanced_yt_download_button.xml")
)

// Add download button node
BottomControlsResourcePatch.addControls("downloads/host/layout/${BottomControlsResourcePatch.TARGET_RESOURCE_NAME}")
BottomControlsResourcePatch.addControls("downloads")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
}