Skip to content

Commit ac90528

Browse files
allow only specific resolutions for custom icons
1 parent b34a1ab commit ac90528

File tree

3 files changed

+79
-62
lines changed

3 files changed

+79
-62
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ val customBrandingPatch = baseCustomBrandingPatch(
6363
),
6464
resourceFolder = "custom-branding/music",
6565
iconResourceFileNames = arrayOf(
66-
"adaptiveproduct_youtube_music_2024_q4_background_color_108",
67-
"adaptiveproduct_youtube_music_2024_q4_foreground_color_108",
68-
"ic_launcher_release",
66+
"adaptiveproduct_youtube_music_2024_q4_background_color_108.png",
67+
"adaptiveproduct_youtube_music_2024_q4_foreground_color_108.png"
6968
),
7069
monochromeIconFileNames = arrayOf("ic_app_icons_themed_youtube_music.xml"),
7170
adaptiveIconFileNames = arrayOf(
7271
"adaptiveproduct_youtube_music_2024_q4_background_color_108.xml",
73-
"adaptiveproduct_youtube_music_2024_q4_foreground_color_108.xml",
72+
"adaptiveproduct_youtube_music_2024_q4_foreground_color_108.xml"
7473
),
75-
legacyIconResourceFileNames = arrayOf("ic_launcher_release"),
74+
legacyIconResourceFileNames = arrayOf("ic_launcher_release.png"),
7675

7776
block = {
7877
dependsOn(disableSplashAnimationPatch)

patches/src/main/kotlin/app/revanced/patches/shared/layout/branding/BaseCustomBrandingPatch.kt

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package app.revanced.patches.shared.layout.branding
22

3+
import app.revanced.patcher.patch.PatchException
34
import app.revanced.patcher.patch.ResourcePatch
45
import app.revanced.patcher.patch.ResourcePatchBuilder
56
import app.revanced.patcher.patch.ResourcePatchContext
@@ -14,7 +15,7 @@ import java.util.logging.Logger
1415

1516
private const val REVANCED_ICON = "ReVanced*Logo" // Can never be a valid path.
1617

17-
private val mipmapDirectories = arrayOf(
18+
internal val mipmapDirectories = arrayOf(
1819
// Target app does not have ldpi icons.
1920
"mdpi",
2021
"hdpi",
@@ -63,10 +64,6 @@ internal fun baseCustomBrandingPatch(
6364
description = "Applies a custom app name and icon. Defaults to \"$defaultAppName\" and the ReVanced logo.",
6465
use = false,
6566
) {
66-
fun Array<String>.addPngExtension() = this.map { "$it.png" }.toTypedArray<String>()
67-
val iconResourceFileNamesPng = iconResourceFileNames.addPngExtension()
68-
val legacyIconResourceFileNamesPng = legacyIconResourceFileNames.addPngExtension()
69-
7067
val appName by stringOption(
7168
key = "appName",
7269
default = defaultAppName,
@@ -83,16 +80,13 @@ internal fun baseCustomBrandingPatch(
8380
description = """
8481
The icon to apply to the app.
8582
86-
If a path to a folder is provided, the folder must contain the following folders:
87-
83+
If a path to a folder is provided, the folder must contain one or more of the following folders:
8884
${formatResourceFileList(mipmapDirectories)}
8985
9086
Each of these folders must contain the following files:
91-
92-
${formatResourceFileList((iconResourceFileNamesPng + legacyIconResourceFileNamesPng))}
87+
${formatResourceFileList((iconResourceFileNames + legacyIconResourceFileNames))}
9388
9489
Optionally, a 'drawable' folder with the monochrome icon files:
95-
9690
${formatResourceFileList(monochromeIconFileNames)}
9791
""".trimIndentMultiline(),
9892
)
@@ -103,64 +97,71 @@ internal fun baseCustomBrandingPatch(
10397
val iconPathTrimmed = iconPath!!.trim()
10498

10599
if (iconPathTrimmed == REVANCED_ICON) {
106-
val mipmapIconResourceGroups = mipmapDirectories.map { directory ->
107-
ResourceGroup(
108-
directory,
109-
*legacyIconResourceFileNamesPng,
110-
)
111-
}
112-
113-
// Copy monochrome icons.
100+
// Copy adaptive icons.
114101
copyResources(
115102
resourceFolder,
116-
ResourceGroup("drawable", *monochromeIconFileNames)
103+
ResourceGroup("mipmap-anydpi", *adaptiveIconFileNames)
117104
)
118105

119106
// Copy legacy icons.
120-
mipmapIconResourceGroups.forEach { groupResources ->
107+
mipmapDirectories.map { directory ->
108+
ResourceGroup(
109+
directory,
110+
*legacyIconResourceFileNames,
111+
)
112+
}.forEach { groupResources ->
121113
copyResources(resourceFolder, groupResources)
122114
}
123115

124-
// Copy adaptive icons.
116+
// Copy monochrome icons.
125117
copyResources(
126118
resourceFolder,
127-
ResourceGroup("mipmap-anydpi", *adaptiveIconFileNames)
119+
ResourceGroup("drawable", *monochromeIconFileNames)
128120
)
129121
} else {
130-
val mipmapIconResourceGroups = mipmapDirectories.map { directory ->
131-
ResourceGroup(
132-
directory,
133-
*iconResourceFileNamesPng,
134-
)
135-
}
136-
137-
val filePath = File(iconPathTrimmed)
122+
val iconPathFile = File(iconPathTrimmed)
138123
val resourceDirectory = get("res")
124+
var replacedResources = false
139125

140126
// Replace mipmap icons.
141-
mipmapIconResourceGroups.forEach { groupResources ->
127+
mipmapDirectories.map { directory ->
128+
ResourceGroup(
129+
directory,
130+
*iconResourceFileNames,
131+
)
132+
}.forEach { groupResources ->
142133
val groupResourceDirectoryName = groupResources.resourceDirectoryName
143-
val fromDirectory = filePath.resolve(groupResourceDirectoryName)
134+
val fromDirectory = iconPathFile.resolve(groupResourceDirectoryName)
144135
val toDirectory = resourceDirectory.resolve(groupResourceDirectoryName)
145136

146137
groupResources.resources.forEach { iconFileName ->
147-
Files.write(
148-
toDirectory.resolve(iconFileName).toPath(),
149-
fromDirectory.resolve(iconFileName).readBytes(),
150-
)
138+
val replacement = fromDirectory.resolve(iconFileName)
139+
if (replacement.exists()) {
140+
Files.write(
141+
toDirectory.resolve(iconFileName).toPath(),
142+
replacement.readBytes(),
143+
)
144+
replacedResources = true
145+
}
151146
}
152147
}
153148

154149
// Replace monochrome icons if provided.
155-
monochromeIconFileNames.forEach { fileName ->
156-
val replacementMonochrome = filePath.resolve("drawable").resolve(fileName)
157-
if (replacementMonochrome.exists()) {
150+
monochromeIconFileNames.forEach { iconFileName ->
151+
val resourceType = "drawable"
152+
val replacement = iconPathFile.resolve(resourceType).resolve(iconFileName)
153+
if (replacement.exists()) {
158154
Files.write(
159-
resourceDirectory.resolve("drawable").resolve(fileName).toPath(),
160-
replacementMonochrome.readBytes(),
155+
resourceDirectory.resolve(resourceType).resolve(iconFileName).toPath(),
156+
replacement.readBytes(),
161157
)
158+
replacedResources = true
162159
}
163160
}
161+
162+
if (!replacedResources) {
163+
throw PatchException("Could not find any replacement images in patch option path: $iconPathTrimmed")
164+
}
164165
}
165166

166167
// Change the app name.
Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package app.revanced.patches.youtube.layout.branding
22

33
import app.revanced.patches.shared.layout.branding.baseCustomBrandingPatch
4+
import app.revanced.patches.shared.layout.branding.mipmapDirectories
45
import java.nio.file.Files
56

67
private const val APP_NAME = "YouTube ReVanced"
78

8-
private const val ADAPTIVE_BACKGROUND_RESOURCE_FILE_NAME = "adaptiveproduct_youtube_background_color_108.xml"
9-
private const val ADAPTIVE_FOREGROUND_RESOURCE_FILE_NAME = "adaptiveproduct_youtube_foreground_color_108.xml"
9+
private const val ADAPTIVE_BACKGROUND_RESOURCE_NAME = "adaptiveproduct_youtube_background_color_108"
10+
private const val ADAPTIVE_FOREGROUND_RESOURCE_NAME = "adaptiveproduct_youtube_foreground_color_108"
1011

1112
@Suppress("unused")
1213
val customBrandingPatch = baseCustomBrandingPatch(
@@ -19,22 +20,20 @@ val customBrandingPatch = baseCustomBrandingPatch(
1920
),
2021
resourceFolder = "custom-branding/youtube",
2122
iconResourceFileNames = arrayOf(
22-
"adaptiveproduct_youtube_background_color_108",
23-
"adaptiveproduct_youtube_foreground_color_108",
24-
"ic_launcher",
25-
"ic_launcher_round",
23+
"$ADAPTIVE_BACKGROUND_RESOURCE_NAME.png",
24+
"$ADAPTIVE_FOREGROUND_RESOURCE_NAME.png",
2625
),
2726
monochromeIconFileNames = arrayOf(
2827
"adaptive_monochrome_ic_youtube_launcher.xml",
2928
"ringo2_adaptive_monochrome_ic_youtube_launcher.xml"
3029
),
3130
adaptiveIconFileNames = arrayOf(
32-
ADAPTIVE_BACKGROUND_RESOURCE_FILE_NAME,
33-
ADAPTIVE_FOREGROUND_RESOURCE_FILE_NAME,
31+
"$ADAPTIVE_BACKGROUND_RESOURCE_NAME.xml",
32+
"$ADAPTIVE_FOREGROUND_RESOURCE_NAME.xml",
3433
),
3534
legacyIconResourceFileNames = arrayOf(
36-
"ic_launcher",
37-
// "ic_launcher_round" also exists in 19.34, but was removed in later targets.
35+
"ic_launcher.png",
36+
// "ic_launcher_round" exists in 19.34, but was removed in later targets.
3837
),
3938

4039
block = {
@@ -49,17 +48,35 @@ val customBrandingPatch = baseCustomBrandingPatch(
4948
},
5049

5150
executeBlock = {
52-
val resourceDirectory = get("res/mipmap-anydpi")
51+
val resourceDirectory = get("res")
5352

5453
// Copy adaptive icon to secondary adaptive file.
5554
arrayOf(
56-
ADAPTIVE_BACKGROUND_RESOURCE_FILE_NAME to "adaptiveproduct_youtube_2024_q4_background_color_108.xml",
57-
ADAPTIVE_FOREGROUND_RESOURCE_FILE_NAME to "adaptiveproduct_youtube_2024_q4_foreground_color_108.xml",
55+
ADAPTIVE_BACKGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_background_color_108",
56+
ADAPTIVE_FOREGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_foreground_color_108",
5857
).forEach { (old, new) ->
59-
val oldFile = resourceDirectory.resolve(old)
60-
val newFile = resourceDirectory.resolve(new)
58+
var resourceType = "mipmap-anydpi"
59+
val oldFile = resourceDirectory.resolve("$resourceType/$old.xml")
60+
if (oldFile.exists()) {
61+
val newFile = resourceDirectory.resolve("$resourceType/$new.xml")
62+
Files.write(newFile.toPath(), oldFile.readBytes())
63+
}
64+
}
65+
66+
// Copy mipmaps to secondary files.
67+
mipmapDirectories.forEach { directory ->
68+
val targetDirectory = resourceDirectory.resolve(directory)
6169

62-
Files.write(newFile.toPath(), oldFile.readBytes())
70+
arrayOf(
71+
ADAPTIVE_BACKGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_background_color_108",
72+
ADAPTIVE_FOREGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_foreground_color_108",
73+
).forEach { (old, new) ->
74+
val oldFile = targetDirectory.resolve("$old.png")
75+
if (oldFile.exists()) {
76+
val newFile = targetDirectory.resolve("$new.png")
77+
Files.write(newFile.toPath(), oldFile.readBytes())
78+
}
79+
}
6380
}
6481
}
6582
)

0 commit comments

Comments
 (0)