Skip to content

Commit e272302

Browse files
inotia00anddea
authored andcommitted
feat(YouTube Music - Custom branding icon): Add patch option RestoreOldSplashIcon
1 parent 37373c9 commit e272302

13 files changed

Lines changed: 73 additions & 22 deletions

File tree

src/main/kotlin/app/revanced/patches/music/layout/branding/icon/CustomBrandingIconPatch.kt

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import app.revanced.patches.music.utils.settings.ResourceUtils.setIconType
99
import app.revanced.util.ResourceGroup
1010
import app.revanced.util.Utils.trimIndentMultiline
1111
import app.revanced.util.copyResources
12+
import app.revanced.util.getResourceGroup
1213
import app.revanced.util.patch.BaseResourcePatch
1314
import app.revanced.util.underBarOrThrow
15+
import org.w3c.dom.Element
1416
import java.io.File
1517
import java.nio.file.Files
1618

@@ -84,23 +86,14 @@ object CustomBrandingIconPatch : BaseResourcePatch(
8486
"record"
8587
).map { "$it.png" }.toTypedArray()
8688

87-
private val headerIconResourceGroups = drawableDirectories.map { directory ->
88-
ResourceGroup(
89-
directory, *headerIconResourceFileNames
90-
)
91-
}
89+
private val headerIconResourceGroups =
90+
drawableDirectories.getResourceGroup(headerIconResourceFileNames)
9291

93-
private val launcherIconResourceGroups = mipmapDirectories.map { directory ->
94-
ResourceGroup(
95-
directory, *launcherIconResourceFileNames
96-
)
97-
}
92+
private val launcherIconResourceGroups =
93+
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)
9894

99-
private val splashIconResourceGroups = largeDrawableDirectories.map { directory ->
100-
ResourceGroup(
101-
directory, *splashIconResourceFileNames
102-
)
103-
}
95+
private val splashIconResourceGroups =
96+
largeDrawableDirectories.getResourceGroup(splashIconResourceFileNames)
10497

10598
val AppIcon = stringPatchOption(
10699
key = "AppIcon",
@@ -137,13 +130,28 @@ object CustomBrandingIconPatch : BaseResourcePatch(
137130
required = true
138131
)
139132

133+
private val RestoreOldSplashIcon by booleanPatchOption(
134+
key = "RestoreOldSplashIcon",
135+
default = false,
136+
title = "Restore old splash icon",
137+
description = """
138+
Restore the old style splash icon.
139+
140+
If you enable both the old style splash icon and the Cairo splash animation,
141+
142+
Old style splash icon will appear first and then the Cairo splash animation will start.
143+
""".trimIndentMultiline(),
144+
required = true
145+
)
146+
140147
override fun execute(context: ResourceContext) {
141148

142149
// Check patch options first.
143150
val appIcon = AppIcon
144151
.underBarOrThrow()
145152

146153
val appIconResourcePath = "music/branding/$appIcon"
154+
val youtubeMusicIconResourcePath = "music/branding/youtube_music"
147155

148156
// Check if a custom path is used in the patch options.
149157
if (!availableIcon.containsValue(appIcon)) {
@@ -196,6 +204,39 @@ object CustomBrandingIconPatch : BaseResourcePatch(
196204
}
197205
}
198206

207+
// Change splash icon.
208+
if (RestoreOldSplashIcon == true) {
209+
var oldSplashIconNotExists: Boolean
210+
211+
context.xmlEditor["res/drawable/splash_screen.xml"].use { editor ->
212+
editor.file.apply {
213+
val node = getElementsByTagName("layer-list").item(0)
214+
oldSplashIconNotExists = (node as Element)
215+
.getElementsByTagName("item")
216+
.length == 1
217+
218+
if (oldSplashIconNotExists) {
219+
createElement("item").also { itemNode ->
220+
itemNode.appendChild(
221+
createElement("bitmap").also { bitmapNode ->
222+
bitmapNode.setAttribute("android:gravity", "center")
223+
bitmapNode.setAttribute("android:src", "@drawable/record")
224+
}
225+
)
226+
node.appendChild(itemNode)
227+
}
228+
}
229+
}
230+
}
231+
if (oldSplashIconNotExists) {
232+
splashIconResourceGroups.let { resourceGroups ->
233+
resourceGroups.forEach {
234+
context.copyResources("$youtubeMusicIconResourcePath/splash", it, createDirectoryIfNotExist = true)
235+
}
236+
}
237+
}
238+
}
239+
199240
// Change splash icon.
200241
if (ChangeSplashIcon == true) {
201242
// Some resources have been removed in the latest YouTube Music.

src/main/kotlin/app/revanced/patches/youtube/layout/branding/icon/CustomBrandingIconPatch.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import app.revanced.util.Utils.trimIndentMultiline
1111
import app.revanced.util.copyFile
1212
import app.revanced.util.copyResources
1313
import app.revanced.util.copyXmlNode
14+
import app.revanced.util.getResourceGroup
1415
import app.revanced.util.patch.BaseResourcePatch
1516
import app.revanced.util.underBarOrThrow
1617

@@ -99,12 +100,6 @@ object CustomBrandingIconPatch : BaseResourcePatch(
99100
"avd_anim"
100101
).map { "$it.xml" }.toTypedArray()
101102

102-
private fun List<String>.getResourceGroup(fileNames: Array<String>) = map { directory ->
103-
ResourceGroup(
104-
directory, *fileNames
105-
)
106-
}
107-
108103
private val headerIconResourceGroups =
109104
drawableDirectories.getResourceGroup(headerIconResourceFileNames)
110105

src/main/kotlin/app/revanced/util/ResourceUtils.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ fun String.startsWithAny(vararg prefixes: String): Boolean {
6363
return false
6464
}
6565

66+
fun List<String>.getResourceGroup(fileNames: Array<String>) = map { directory ->
67+
ResourceGroup(
68+
directory, *fileNames
69+
)
70+
}
71+
6672
fun ResourceContext.copyFile(
6773
resourceGroup: List<ResourceGroup>,
6874
path: String,
@@ -98,16 +104,25 @@ fun ResourceContext.copyFile(
98104
*
99105
* @param sourceResourceDirectory The source resource directory name.
100106
* @param resources The resources to copy.
107+
* @param createDirectoryIfNotExist Whether to create a new directory if it does not exist.
101108
*/
102109
fun ResourceContext.copyResources(
103110
sourceResourceDirectory: String,
104111
vararg resources: ResourceGroup,
112+
createDirectoryIfNotExist: Boolean = false,
105113
) {
106114
val targetResourceDirectory = this["res"]
107115

108116
for (resourceGroup in resources) {
109117
resourceGroup.resources.forEach { resource ->
110-
val resourceFile = "${resourceGroup.resourceDirectoryName}/$resource"
118+
val resourceDirectoryName = resourceGroup.resourceDirectoryName
119+
120+
if (createDirectoryIfNotExist) {
121+
val targetDirectory = targetResourceDirectory.resolve(resourceDirectoryName)
122+
if (!targetDirectory.isDirectory) Files.createDirectories(targetDirectory.toPath())
123+
}
124+
125+
val resourceFile = "$resourceDirectoryName/$resource"
111126

112127
inputStreamFromBundledResource(
113128
sourceResourceDirectory,
1.94 KB
Loading
1.79 KB
Loading
2.64 KB
Loading
1.79 KB
Loading
3.49 KB
Loading
1.25 KB
Loading
2.34 KB
Loading

0 commit comments

Comments
 (0)