From 8adc05a17445dede5f35ee27426d20b750a2a1d3 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Fri, 2 Jun 2023 22:15:13 +0400 Subject: [PATCH] fix(youtube/theme): use custom light/dark colors for launch splash screen (#2337) --- .../bytecode/patch/ThemeBytecodePatch.kt | 9 -- .../theme/resource/ThemeResourcePatch.kt | 85 +++++++++---------- 2 files changed, 41 insertions(+), 53 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/theme/bytecode/patch/ThemeBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/theme/bytecode/patch/ThemeBytecodePatch.kt index 25c3a3ec..ecc2d12e 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/theme/bytecode/patch/ThemeBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/theme/bytecode/patch/ThemeBytecodePatch.kt @@ -45,14 +45,5 @@ class ThemeBytecodePatch : BytecodePatch() { 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.", - ) - ) } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/theme/resource/ThemeResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/theme/resource/ThemeResourcePatch.kt index 83ca11e2..44bc6667 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/theme/resource/ThemeResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/theme/resource/ThemeResourcePatch.kt @@ -2,6 +2,7 @@ package app.revanced.patches.youtube.layout.theme.resource import app.revanced.patcher.data.ResourceContext import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.ResourcePatch 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.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.splashScreenBackgroundColor import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch 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. - context.xmlEditor["res/values/styles.xml"].use { - val resourcesNode = it.file.getElementsByTagName("resources").item(0) as Element + // Edit splash screen files and change the background color. + val splashScreenResourceFiles = listOf( + "res/drawable/quantum_launchscreen_youtube.xml", + "res/drawable-sw600dp/quantum_launchscreen_youtube.xml") - val children = resourcesNode.childNodes - for (i in 0 until children.length) { - val node = children.item(i) as? Element ?: continue + splashScreenResourceFiles.forEach editSplashScreen@ { resourceFile -> + context.xmlEditor[resourceFile].use { + val layerList = it.file.getElementsByTagName("layer-list").item(0) as Element - if (node.tagName != "style") continue - - val name = node.getAttribute("name") - if (name != LAUNCHER_STYLE_NAME) continue - - it.file.createElement("item").apply { - setAttribute("name", "android:windowSplashScreenBackground") - textContent = splashScreenBackgroundColor - }.also(node::appendChild) - - break - } - } - - // 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") + val childNodes = layerList.childNodes + for (i in 0 until childNodes.length) { + val node = childNodes.item(i) + if (node is Element && node.hasAttribute("android:drawable")) { + node.setAttribute("android:drawable", "@color/$SPLASH_BACKGROUND_COLOR") + return@editSplashScreen + } + } + return PatchResultError("Failed to modify launch screen") } } 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 const val LAUNCHER_STYLE_NAME = "Base.Theme.YouTube.Launcher" - private const val COLOR_NAME = "splash_background_color" + private const val SPLASH_BACKGROUND_COLOR = "revanced_splash_background_color" } } \ No newline at end of file