Skip to content

Commit cdbd349

Browse files
committed
feat(YouTube Music/Custom branding icon for YouTube Music): add patch option RestoreOldSplashIcon inotia00/ReVanced_Extended#2368
1 parent 3d331a7 commit cdbd349

File tree

13 files changed

+71
-17
lines changed

13 files changed

+71
-17
lines changed

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

+54-10
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

@@ -71,17 +73,11 @@ object CustomBrandingIconPatch : BaseResourcePatch(
7173
"record"
7274
).map { "$it.png" }.toTypedArray()
7375

74-
private val launcherIconResourceGroups = mipmapDirectories.map { directory ->
75-
ResourceGroup(
76-
directory, *launcherIconResourceFileNames
77-
)
78-
}
76+
private val launcherIconResourceGroups =
77+
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)
7978

80-
private val splashIconResourceGroups = largeDrawableDirectories.map { directory ->
81-
ResourceGroup(
82-
directory, *splashIconResourceFileNames
83-
)
84-
}
79+
private val splashIconResourceGroups =
80+
largeDrawableDirectories.getResourceGroup(splashIconResourceFileNames)
8581

8682
private val AppIcon = stringPatchOption(
8783
key = "AppIcon",
@@ -110,13 +106,28 @@ object CustomBrandingIconPatch : BaseResourcePatch(
110106
required = true
111107
)
112108

109+
private val RestoreOldSplashIcon by booleanPatchOption(
110+
key = "RestoreOldSplashIcon",
111+
default = false,
112+
title = "Restore old splash icon",
113+
description = """
114+
Restore the old style splash icon.
115+
116+
If you enable both the old style splash icon and the Cairo splash animation,
117+
118+
Old style splash icon will appear first and then the Cairo splash animation will start.
119+
""".trimIndentMultiline(),
120+
required = true
121+
)
122+
113123
override fun execute(context: ResourceContext) {
114124

115125
// Check patch options first.
116126
val appIcon = AppIcon
117127
.underBarOrThrow()
118128

119129
val appIconResourcePath = "music/branding/$appIcon"
130+
val youtubeMusicIconResourcePath = "music/branding/youtube_music"
120131

121132
// Check if a custom path is used in the patch options.
122133
if (!availableIcon.containsValue(appIcon)) {
@@ -160,6 +171,39 @@ object CustomBrandingIconPatch : BaseResourcePatch(
160171
context.copyResources("$appIconResourcePath/monochrome", resourceGroup)
161172
}
162173

174+
// Change splash icon.
175+
if (RestoreOldSplashIcon == true) {
176+
var oldSplashIconNotExists: Boolean
177+
178+
context.xmlEditor["res/drawable/splash_screen.xml"].use { editor ->
179+
editor.file.apply {
180+
val node = getElementsByTagName("layer-list").item(0)
181+
oldSplashIconNotExists = (node as Element)
182+
.getElementsByTagName("item")
183+
.length == 1
184+
185+
if (oldSplashIconNotExists) {
186+
createElement("item").also { itemNode ->
187+
itemNode.appendChild(
188+
createElement("bitmap").also { bitmapNode ->
189+
bitmapNode.setAttribute("android:gravity", "center")
190+
bitmapNode.setAttribute("android:src", "@drawable/record")
191+
}
192+
)
193+
node.appendChild(itemNode)
194+
}
195+
}
196+
}
197+
}
198+
if (oldSplashIconNotExists) {
199+
splashIconResourceGroups.let { resourceGroups ->
200+
resourceGroups.forEach {
201+
context.copyResources("$youtubeMusicIconResourcePath/splash", it, createDirectoryIfNotExist = true)
202+
}
203+
}
204+
}
205+
}
206+
163207
// Change splash icon.
164208
if (ChangeSplashIcon == true) {
165209
// Some resources have been removed in the latest YouTube Music.

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

+1-6
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

@@ -73,12 +74,6 @@ object CustomBrandingIconPatch : BaseResourcePatch(
7374
"avd_anim"
7475
).map { "$it.xml" }.toTypedArray()
7576

76-
private fun List<String>.getResourceGroup(fileNames: Array<String>) = map { directory ->
77-
ResourceGroup(
78-
directory, *fileNames
79-
)
80-
}
81-
8277
private val launcherIconResourceGroups =
8378
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)
8479

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

+16-1
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,
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)