Skip to content

Commit

Permalink
feat(YouTube Music/Custom branding icon for YouTube Music): add patch…
Browse files Browse the repository at this point in the history
… option `RestoreOldSplashIcon` inotia00/ReVanced_Extended#2368
  • Loading branch information
inotia00 committed Sep 6, 2024
1 parent 3d331a7 commit cdbd349
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import app.revanced.patches.music.utils.settings.ResourceUtils.setIconType
import app.revanced.util.ResourceGroup
import app.revanced.util.Utils.trimIndentMultiline
import app.revanced.util.copyResources
import app.revanced.util.getResourceGroup
import app.revanced.util.patch.BaseResourcePatch
import app.revanced.util.underBarOrThrow
import org.w3c.dom.Element
import java.io.File
import java.nio.file.Files

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

private val launcherIconResourceGroups = mipmapDirectories.map { directory ->
ResourceGroup(
directory, *launcherIconResourceFileNames
)
}
private val launcherIconResourceGroups =
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)

private val splashIconResourceGroups = largeDrawableDirectories.map { directory ->
ResourceGroup(
directory, *splashIconResourceFileNames
)
}
private val splashIconResourceGroups =
largeDrawableDirectories.getResourceGroup(splashIconResourceFileNames)

private val AppIcon = stringPatchOption(
key = "AppIcon",
Expand Down Expand Up @@ -110,13 +106,28 @@ object CustomBrandingIconPatch : BaseResourcePatch(
required = true
)

private val RestoreOldSplashIcon by booleanPatchOption(
key = "RestoreOldSplashIcon",
default = false,
title = "Restore old splash icon",
description = """
Restore the old style splash icon.
If you enable both the old style splash icon and the Cairo splash animation,
Old style splash icon will appear first and then the Cairo splash animation will start.
""".trimIndentMultiline(),
required = true
)

override fun execute(context: ResourceContext) {

// Check patch options first.
val appIcon = AppIcon
.underBarOrThrow()

val appIconResourcePath = "music/branding/$appIcon"
val youtubeMusicIconResourcePath = "music/branding/youtube_music"

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

// Change splash icon.
if (RestoreOldSplashIcon == true) {
var oldSplashIconNotExists: Boolean

context.xmlEditor["res/drawable/splash_screen.xml"].use { editor ->
editor.file.apply {
val node = getElementsByTagName("layer-list").item(0)
oldSplashIconNotExists = (node as Element)
.getElementsByTagName("item")
.length == 1

if (oldSplashIconNotExists) {
createElement("item").also { itemNode ->
itemNode.appendChild(
createElement("bitmap").also { bitmapNode ->
bitmapNode.setAttribute("android:gravity", "center")
bitmapNode.setAttribute("android:src", "@drawable/record")
}
)
node.appendChild(itemNode)
}
}
}
}
if (oldSplashIconNotExists) {
splashIconResourceGroups.let { resourceGroups ->
resourceGroups.forEach {
context.copyResources("$youtubeMusicIconResourcePath/splash", it, createDirectoryIfNotExist = true)
}
}
}
}

// Change splash icon.
if (ChangeSplashIcon == true) {
// Some resources have been removed in the latest YouTube Music.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.revanced.util.Utils.trimIndentMultiline
import app.revanced.util.copyFile
import app.revanced.util.copyResources
import app.revanced.util.copyXmlNode
import app.revanced.util.getResourceGroup
import app.revanced.util.patch.BaseResourcePatch
import app.revanced.util.underBarOrThrow

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

private fun List<String>.getResourceGroup(fileNames: Array<String>) = map { directory ->
ResourceGroup(
directory, *fileNames
)
}

private val launcherIconResourceGroups =
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)

Expand Down
17 changes: 16 additions & 1 deletion src/main/kotlin/app/revanced/util/ResourceUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ fun String.startsWithAny(vararg prefixes: String): Boolean {
return false
}

fun List<String>.getResourceGroup(fileNames: Array<String>) = map { directory ->
ResourceGroup(
directory, *fileNames
)
}

fun ResourceContext.copyFile(
resourceGroup: List<ResourceGroup>,
path: String,
Expand Down Expand Up @@ -98,16 +104,25 @@ fun ResourceContext.copyFile(
*
* @param sourceResourceDirectory The source resource directory name.
* @param resources The resources to copy.
* @param createDirectoryIfNotExist Whether to create a new directory if it does not exist.
*/
fun ResourceContext.copyResources(
sourceResourceDirectory: String,
vararg resources: ResourceGroup,
createDirectoryIfNotExist: Boolean = false,
) {
val targetResourceDirectory = this["res"]

for (resourceGroup in resources) {
resourceGroup.resources.forEach { resource ->
val resourceFile = "${resourceGroup.resourceDirectoryName}/$resource"
val resourceDirectoryName = resourceGroup.resourceDirectoryName

if (createDirectoryIfNotExist) {
val targetDirectory = targetResourceDirectory.resolve(resourceDirectoryName)
if (!targetDirectory.isDirectory) Files.createDirectories(targetDirectory.toPath())
}

val resourceFile = "$resourceDirectoryName/$resource"

inputStreamFromBundledResource(
sourceResourceDirectory,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cdbd349

Please sign in to comment.