Skip to content

Commit

Permalink
fix(youtube/theme): use custom light/dark colors for launch splash sc…
Browse files Browse the repository at this point in the history
…reen (ReVanced#2337)
  • Loading branch information
LisoUseInAIKyrios committed Jun 2, 2023
1 parent fbc48a7 commit 8adc05a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -50,58 +50,55 @@ class ThemeResourcePatch : ResourcePatch {
}
}

splashScreenBackgroundColor ?: return PatchResultSuccess()

// 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

val children = resourcesNode.childNodes
for (i in 0 until children.length) {
val node = children.item(i) as? Element ?: continue

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
// 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 files and change the background color.
val splashScreenResourceFiles = listOf(
"res/drawable/quantum_launchscreen_youtube.xml",
"res/drawable-sw600dp/quantum_launchscreen_youtube.xml")

splashScreenResourceFiles.forEach editSplashScreen@ { resourceFile ->
context.xmlEditor[resourceFile].use {
val layerList = it.file.getElementsByTagName("layer-list").item(0) as Element

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")
}
}

// Edit splash screen background color for Android 12+.
return PatchResultSuccess()
}

// Add the splash screen background color to the colors.xml file.
context.xmlEditor["res/values/colors.xml"].use {
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

it.file.createElement("color").apply {
setAttribute("name", COLOR_NAME)
setAttribute("category", "color")
textContent = splashScreenBackgroundColor
}.also(resourcesNode::appendChild)
resourcesNode.appendChild(
it.file.createElement("color").apply {
setAttribute("name", colorName)
setAttribute("category", "color")
textContent = colorValue
})
}

// 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()
}

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"
}
}

0 comments on commit 8adc05a

Please sign in to comment.