fix: Use deprecated members to ensure backwards compatibility

By migrating to early to new APIs of ReVanced Patcher, if you were to use old versions of ReVanced Patcher, you would get compatibility issues. By using deprecated members until most have updated ReVanced Patcher, we can ensure seamless migration.
This commit is contained in:
oSumAtrIX
2024-02-22 01:10:09 +01:00
parent 1e3356808d
commit a6d8e4210f
24 changed files with 174 additions and 108 deletions
@@ -81,7 +81,7 @@ object CustomBrandingPatch : ResourcePatch() {
}.let { resourceGroups ->
if (icon != REVANCED_ICON) {
val path = File(icon)
val resourceDirectory = context.get("res", false)
val resourceDirectory = context.get("res")
resourceGroups.forEach { group ->
val fromDirectory = path.resolve(group.resourceDirectoryName)
@@ -102,7 +102,7 @@ object CustomBrandingPatch : ResourcePatch() {
appName?.let { name ->
// Change the app name.
val manifest = context.get("AndroidManifest.xml", false)
val manifest = context.get("AndroidManifest.xml")
manifest.writeText(
manifest.readText()
.replace(
@@ -71,7 +71,7 @@ object ChangeHeaderPatch : ResourcePatch() {
override fun execute(context: ResourceContext) {
// The directories to copy the header to.
val targetResourceDirectories = targetResourceDirectoryNames.keys.mapNotNull {
context.get("res", false).resolve(it).takeIf(File::exists)
context.get("res").resolve(it).takeIf(File::exists)
}
// The files to replace in the target directories.
val targetResourceFiles = targetResourceDirectoryNames.keys.map { directoryName ->
@@ -120,7 +120,7 @@ object ChangeHeaderPatch : ResourcePatch() {
// For each source folder, copy the files to the target resource directories.
sourceFolders.forEach { dpiSourceFolder ->
val targetDpiFolder = context.get("res", false).resolve(dpiSourceFolder.name)
val targetDpiFolder = context.get("res").resolve(dpiSourceFolder.name)
if (!targetDpiFolder.exists()) return@forEach
val imgSourceFiles = dpiSourceFolder.listFiles { file -> file.isFile }!!
@@ -37,7 +37,9 @@ object PlayerControlsBackgroundPatch : ResourcePatch() {
private const val RESOURCE_FILE_PATH = "res/drawable/player_button_circle_background.xml"
override fun execute(context: ResourceContext) {
context.document[RESOURCE_FILE_PATH].use { document ->
context.xmlEditor[RESOURCE_FILE_PATH].use { editor ->
val document = editor.file
document.doRecursively node@{ node ->
if (node !is Element) return@node
@@ -29,7 +29,9 @@ internal object SeekbarColorResourcePatch : ResourcePatch() {
findColorResource("inline_time_bar_played_not_highlighted_color")
// Edit the resume playback drawable and replace the progress bar with a custom drawable
context.document["res/drawable/resume_playback_progressbar_drawable.xml"].use { document ->
context.xmlEditor["res/drawable/resume_playback_progressbar_drawable.xml"].use { editor ->
val document = editor.file
val layerList = document.getElementsByTagName("layer-list").item(0) as Element
val progressNode = layerList.getElementsByTagName("item").item(1) as Element
if (!progressNode.getAttributeNode("android:id").value.endsWith("progress")) {
@@ -65,12 +65,14 @@ internal object SponsorBlockResourcePatch : ResourcePatch() {
)!!
var modifiedControlsLayout = false
val targetDocument = context.document["res/layout/youtube_controls_layout.xml"]
val editor = context.xmlEditor["res/layout/youtube_controls_layout.xml"]
"RelativeLayout".copyXmlNode(
context.document[hostingResourceStream],
targetDocument,
context.xmlEditor[hostingResourceStream],
editor,
).also {
val children = targetDocument.getElementsByTagName("RelativeLayout").item(0).childNodes
val document = editor.file
val children = document.getElementsByTagName("RelativeLayout").item(0).childNodes
// Replace the startOf with the voting button view so that the button does not overlap
for (i in 1 until children.length) {
@@ -82,7 +84,7 @@ internal object SponsorBlockResourcePatch : ResourcePatch() {
view.attributes.getNamedItem(
"android:id",
).nodeValue.endsWith("live_chat_overlay_button")
)
)
) {
continue
}
@@ -35,7 +35,9 @@ internal object ThemeResourcePatch : ResourcePatch() {
)
// Edit theme colors via resources.
context.document["res/values/colors.xml"].use { document ->
context.xmlEditor["res/values/colors.xml"].use { editor ->
val document = editor.file
val resourcesNode = document.getElementsByTagName("resources").item(0) as Element
val children = resourcesNode.childNodes
@@ -76,8 +78,10 @@ internal object ThemeResourcePatch : ResourcePatch() {
)
splashScreenResourceFiles.forEach editSplashScreen@{ resourceFile ->
context.document[resourceFile].use {
val layerList = it.getElementsByTagName("layer-list").item(0) as Element
context.xmlEditor[resourceFile].use { editor ->
val document = editor.file
val layerList = document.getElementsByTagName("layer-list").item(0) as Element
val childNodes = layerList.childNodes
for (i in 0 until childNodes.length) {
@@ -99,11 +103,13 @@ internal object ThemeResourcePatch : ResourcePatch() {
colorName: String,
colorValue: String,
) {
context.document[resourceFile].use {
val resourcesNode = it.getElementsByTagName("resources").item(0) as Element
context.xmlEditor[resourceFile].use { editor ->
val document = editor.file
val resourcesNode = document.getElementsByTagName("resources").item(0) as Element
resourcesNode.appendChild(
it.createElement("color").apply {
document.createElement("color").apply {
setAttribute("name", colorName)
setAttribute("category", "color")
textContent = colorValue
@@ -3,7 +3,7 @@ package app.revanced.patches.youtube.misc.playercontrols
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.Document
import app.revanced.patcher.util.DomFileEditor
import app.revanced.patches.shared.misc.mapping.ResourceMappingPatch
import java.io.Closeable
@@ -18,15 +18,14 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable {
private var lastLeftOf = "fullscreen_button"
private lateinit var resourceContext: ResourceContext
private lateinit var targetDocument: Document
private lateinit var targetDocumentEditor: DomFileEditor
override fun execute(context: ResourceContext) {
resourceContext = context
targetDocument = context.document[TARGET_RESOURCE]
targetDocumentEditor = context.xmlEditor[TARGET_RESOURCE]
bottomUiContainerResourceId =
ResourceMappingPatch.resourceMappings
.single { it.type == "id" && it.name == "bottom_ui_container_stub" }.id
bottomUiContainerResourceId = ResourceMappingPatch.resourceMappings
.single { it.type == "id" && it.name == "bottom_ui_container_stub" }.id
}
/**
@@ -35,21 +34,21 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable {
* @param resourceDirectoryName The name of the directory containing the hosting resource.
*/
fun addControls(resourceDirectoryName: String) {
val sourceDocument =
resourceContext.document[
this::class.java.classLoader.getResourceAsStream(
"$resourceDirectoryName/host/layout/$TARGET_RESOURCE_NAME",
)!!,
]
val sourceDocumentEditor = resourceContext.xmlEditor[
this::class.java.classLoader.getResourceAsStream(
"$resourceDirectoryName/host/layout/$TARGET_RESOURCE_NAME",
)!!,
]
val sourceDocument = sourceDocumentEditor.file
val targetDocument = targetDocumentEditor.file
val targetElement = "android.support.constraint.ConstraintLayout"
val targetElementTag = "android.support.constraint.ConstraintLayout"
val hostElements = sourceDocument.getElementsByTagName(targetElement).item(0).childNodes
val sourceElements = sourceDocument.getElementsByTagName(targetElementTag).item(0).childNodes
val targetElement = targetDocument.getElementsByTagName(targetElementTag).item(0)
val destinationElement = targetDocument.getElementsByTagName(targetElement).item(0)
for (index in 1 until hostElements.length) {
val element = hostElements.item(index).cloneNode(true)
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
@@ -65,10 +64,10 @@ object BottomControlsResourcePatch : ResourcePatch(), Closeable {
// Add the element.
targetDocument.adoptNode(element)
destinationElement.appendChild(element)
targetElement.appendChild(element)
}
sourceDocument.close()
sourceDocumentEditor.close()
}
override fun close() = targetDocument.close()
override fun close() = targetDocumentEditor.close()
}
@@ -15,10 +15,10 @@ object SettingsResourcePatch : BaseSettingsResourcePatch(
intent = SettingsPatch.newIntent("revanced_settings_intent"),
) to "settings_fragment",
dependencies =
setOf(
ResourceMappingPatch::class,
AddResourcesPatch::class,
),
setOf(
ResourceMappingPatch::class,
AddResourcesPatch::class,
),
) {
// Used for a fingerprint from SettingsPatch.
internal var appearanceStringId = -1L
@@ -43,7 +43,9 @@ object SettingsResourcePatch : BaseSettingsResourcePatch(
// Modify the manifest and add a data intent filter to the LicenseActivity.
// Some devices freak out if undeclared data is passed to an intent,
// and this change appears to fix the issue.
context.document["AndroidManifest.xml"].use { document ->
context.xmlEditor["AndroidManifest.xml"].use { editor ->
val document = editor.file
// A xml regular-expression would probably work better than this manual searching.
val manifestNodes = document.getElementsByTagName("manifest").item(0).childNodes
for (i in 0..manifestNodes.length) {