fix(youtube/theme): use custom light/dark colors for launch splash screen (#2337)
This commit is contained in:
parent
fbc48a7b4b
commit
8adc05a174
|
@ -45,14 +45,5 @@ class ThemeBytecodePatch : BytecodePatch() {
|
||||||
description = "The background color of the light theme. Can be a hex color or a resource reference.",
|
description = "The background color of the light theme. Can be a hex color or a resource reference.",
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
var splashScreenBackgroundColor: String? by option(
|
|
||||||
PatchOption.StringOption(
|
|
||||||
key = "splashScreenBackgroundColor",
|
|
||||||
default = "?android:attr/colorBackground",
|
|
||||||
title = "Background color for the splash screen",
|
|
||||||
description = "The background color of the splash screen. Can be a hex color or a resource reference.",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package app.revanced.patches.youtube.layout.theme.resource
|
||||||
|
|
||||||
import app.revanced.patcher.data.ResourceContext
|
import app.revanced.patcher.data.ResourceContext
|
||||||
import app.revanced.patcher.patch.PatchResult
|
import app.revanced.patcher.patch.PatchResult
|
||||||
|
import app.revanced.patcher.patch.PatchResultError
|
||||||
import app.revanced.patcher.patch.PatchResultSuccess
|
import app.revanced.patcher.patch.PatchResultSuccess
|
||||||
import app.revanced.patcher.patch.ResourcePatch
|
import app.revanced.patcher.patch.ResourcePatch
|
||||||
import app.revanced.patcher.patch.annotations.DependsOn
|
import app.revanced.patcher.patch.annotations.DependsOn
|
||||||
|
@ -12,7 +13,6 @@ import app.revanced.patches.shared.settings.preference.impl.TextPreference
|
||||||
import app.revanced.patches.youtube.layout.seekbar.resource.SeekbarPreferencesPatch
|
import app.revanced.patches.youtube.layout.seekbar.resource.SeekbarPreferencesPatch
|
||||||
import app.revanced.patches.youtube.layout.theme.bytecode.patch.ThemeBytecodePatch.Companion.darkThemeBackgroundColor
|
import app.revanced.patches.youtube.layout.theme.bytecode.patch.ThemeBytecodePatch.Companion.darkThemeBackgroundColor
|
||||||
import app.revanced.patches.youtube.layout.theme.bytecode.patch.ThemeBytecodePatch.Companion.lightThemeBackgroundColor
|
import app.revanced.patches.youtube.layout.theme.bytecode.patch.ThemeBytecodePatch.Companion.lightThemeBackgroundColor
|
||||||
import app.revanced.patches.youtube.layout.theme.bytecode.patch.ThemeBytecodePatch.Companion.splashScreenBackgroundColor
|
|
||||||
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
|
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
|
||||||
import org.w3c.dom.Element
|
import org.w3c.dom.Element
|
||||||
|
|
||||||
|
@ -50,58 +50,55 @@ class ThemeResourcePatch : ResourcePatch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
splashScreenBackgroundColor ?: return PatchResultSuccess()
|
// Add a dynamic background color to the colors.xml file.
|
||||||
|
addResourceColor(context, "res/values/colors.xml",
|
||||||
|
SPLASH_BACKGROUND_COLOR, lightThemeBackgroundColor!!)
|
||||||
|
addResourceColor(context, "res/values-night/colors.xml",
|
||||||
|
SPLASH_BACKGROUND_COLOR, darkThemeBackgroundColor!!)
|
||||||
|
|
||||||
// Edit splash screen background color for Android 11 and below.
|
// Edit splash screen files and change the background color.
|
||||||
context.xmlEditor["res/values/styles.xml"].use {
|
val splashScreenResourceFiles = listOf(
|
||||||
val resourcesNode = it.file.getElementsByTagName("resources").item(0) as Element
|
"res/drawable/quantum_launchscreen_youtube.xml",
|
||||||
|
"res/drawable-sw600dp/quantum_launchscreen_youtube.xml")
|
||||||
|
|
||||||
val children = resourcesNode.childNodes
|
splashScreenResourceFiles.forEach editSplashScreen@ { resourceFile ->
|
||||||
for (i in 0 until children.length) {
|
context.xmlEditor[resourceFile].use {
|
||||||
val node = children.item(i) as? Element ?: continue
|
val layerList = it.file.getElementsByTagName("layer-list").item(0) as Element
|
||||||
|
|
||||||
if (node.tagName != "style") continue
|
val childNodes = layerList.childNodes
|
||||||
|
for (i in 0 until childNodes.length) {
|
||||||
val name = node.getAttribute("name")
|
val node = childNodes.item(i)
|
||||||
if (name != LAUNCHER_STYLE_NAME) continue
|
if (node is Element && node.hasAttribute("android:drawable")) {
|
||||||
|
node.setAttribute("android:drawable", "@color/$SPLASH_BACKGROUND_COLOR")
|
||||||
it.file.createElement("item").apply {
|
return@editSplashScreen
|
||||||
setAttribute("name", "android:windowSplashScreenBackground")
|
|
||||||
textContent = splashScreenBackgroundColor
|
|
||||||
}.also(node::appendChild)
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return PatchResultError("Failed to modify launch screen")
|
||||||
// Edit splash screen background color for Android 12+.
|
|
||||||
|
|
||||||
// Add the splash screen background color to the colors.xml file.
|
|
||||||
context.xmlEditor["res/values/colors.xml"].use {
|
|
||||||
val resourcesNode = it.file.getElementsByTagName("resources").item(0) as Element
|
|
||||||
|
|
||||||
it.file.createElement("color").apply {
|
|
||||||
setAttribute("name", COLOR_NAME)
|
|
||||||
setAttribute("category", "color")
|
|
||||||
textContent = splashScreenBackgroundColor
|
|
||||||
}.also(resourcesNode::appendChild)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Point to the splash screen background color.
|
|
||||||
context.xmlEditor["res/drawable/quantum_launchscreen_youtube.xml"].use {
|
|
||||||
val node = it.file.getElementsByTagName("layer-list").item(0) as Element
|
|
||||||
|
|
||||||
val backgroundColorItem = node.childNodes.item(1) as Element
|
|
||||||
backgroundColorItem.apply {
|
|
||||||
setAttribute("android:drawable", "@color/$COLOR_NAME")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return PatchResultSuccess()
|
return PatchResultSuccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun addResourceColor(
|
||||||
|
context: ResourceContext,
|
||||||
|
resourceFile: String,
|
||||||
|
colorName: String,
|
||||||
|
colorValue: String
|
||||||
|
) {
|
||||||
|
context.xmlEditor[resourceFile].use {
|
||||||
|
val resourcesNode = it.file.getElementsByTagName("resources").item(0) as Element
|
||||||
|
|
||||||
|
resourcesNode.appendChild(
|
||||||
|
it.file.createElement("color").apply {
|
||||||
|
setAttribute("name", colorName)
|
||||||
|
setAttribute("category", "color")
|
||||||
|
textContent = colorValue
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
private const val LAUNCHER_STYLE_NAME = "Base.Theme.YouTube.Launcher"
|
private const val SPLASH_BACKGROUND_COLOR = "revanced_splash_background_color"
|
||||||
private const val COLOR_NAME = "splash_background_color"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue