Skip to content

Commit 5f2f61a

Browse files
Allow custom icon to be only for specific resolutions (same behavior as change header patch)
1 parent 123d152 commit 5f2f61a

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

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

Lines changed: 28 additions & 22 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",
@@ -83,16 +84,13 @@ internal fun baseCustomBrandingPatch(
8384
description = """
8485
The icon to apply to the app.
8586
86-
If a path to a folder is provided, the folder must contain the following folders:
87-
87+
If a path to a folder is provided, the folder must contain one or more of the following folders:
8888
${formatResourceFileList(mipmapDirectories)}
8989
9090
Each of these folders must contain the following files:
91-
9291
${formatResourceFileList((iconResourceFileNamesPng + legacyIconResourceFileNamesPng))}
9392
9493
Optionally, a 'drawable' folder with the monochrome icon files:
95-
9694
${formatResourceFileList(monochromeIconFileNames)}
9795
""".trimIndentMultiline(),
9896
)
@@ -127,40 +125,48 @@ internal fun baseCustomBrandingPatch(
127125
ResourceGroup("mipmap-anydpi", *adaptiveIconFileNames)
128126
)
129127
} else {
130-
val mipmapIconResourceGroups = mipmapDirectories.map { directory ->
131-
ResourceGroup(
132-
directory,
133-
*iconResourceFileNamesPng,
134-
)
135-
}
136-
137128
val filePath = File(iconPathTrimmed)
138129
val resourceDirectory = get("res")
130+
var replacedResources = false
139131

140132
// Replace mipmap icons.
141-
mipmapIconResourceGroups.forEach { groupResources ->
133+
mipmapDirectories.map { directory ->
134+
ResourceGroup(
135+
directory,
136+
*iconResourceFileNamesPng,
137+
)
138+
}.forEach { groupResources ->
142139
val groupResourceDirectoryName = groupResources.resourceDirectoryName
143140
val fromDirectory = filePath.resolve(groupResourceDirectoryName)
144141
val toDirectory = resourceDirectory.resolve(groupResourceDirectoryName)
145142

146143
groupResources.resources.forEach { iconFileName ->
147-
Files.write(
148-
toDirectory.resolve(iconFileName).toPath(),
149-
fromDirectory.resolve(iconFileName).readBytes(),
150-
)
144+
val replacement = fromDirectory.resolve(iconFileName)
145+
if (replacement.exists()) {
146+
Files.write(
147+
toDirectory.resolve(iconFileName).toPath(),
148+
replacement.readBytes(),
149+
)
150+
replacedResources = true
151+
}
151152
}
152153
}
153154

154155
// Replace monochrome icons if provided.
155-
monochromeIconFileNames.forEach { fileName ->
156-
val replacementMonochrome = filePath.resolve("drawable").resolve(fileName)
157-
if (replacementMonochrome.exists()) {
156+
monochromeIconFileNames.forEach { iconFileName ->
157+
val replacement = filePath.resolve("drawable").resolve(iconFileName)
158+
if (replacement.exists()) {
158159
Files.write(
159-
resourceDirectory.resolve("drawable").resolve(fileName).toPath(),
160-
replacementMonochrome.readBytes(),
160+
resourceDirectory.resolve("drawable").resolve(iconFileName).toPath(),
161+
replacement.readBytes(),
161162
)
163+
replacedResources = true
162164
}
163165
}
166+
167+
if (!replacedResources) {
168+
throw PatchException("Could not find any replacement images in patch option path: $iconPathTrimmed")
169+
}
164170
}
165171

166172
// Change the app name.

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

Lines changed: 28 additions & 10 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(
@@ -29,8 +30,8 @@ val customBrandingPatch = baseCustomBrandingPatch(
2930
"ringo2_adaptive_monochrome_ic_youtube_launcher.xml"
3031
),
3132
adaptiveIconFileNames = arrayOf(
32-
ADAPTIVE_BACKGROUND_RESOURCE_FILE_NAME,
33-
ADAPTIVE_FOREGROUND_RESOURCE_FILE_NAME,
33+
ADAPTIVE_BACKGROUND_RESOURCE_NAME,
34+
ADAPTIVE_FOREGROUND_RESOURCE_NAME,
3435
),
3536
legacyIconResourceFileNames = arrayOf(
3637
"ic_launcher",
@@ -49,17 +50,34 @@ val customBrandingPatch = baseCustomBrandingPatch(
4950
},
5051

5152
executeBlock = {
52-
val resourceDirectory = get("res/mipmap-anydpi")
53+
val resourceDirectory = get("res")
5354

5455
// Copy adaptive icon to secondary adaptive file.
5556
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",
57+
ADAPTIVE_BACKGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_background_color_108",
58+
ADAPTIVE_FOREGROUND_RESOURCE_NAME to "adaptiveproduct_youtube_2024_q4_foreground_color_108",
5859
).forEach { (old, new) ->
59-
val oldFile = resourceDirectory.resolve(old)
60-
val newFile = resourceDirectory.resolve(new)
60+
val newFile = resourceDirectory.resolve("/mipmap-anydpi/$new.xml")
61+
if (newFile.exists()) {
62+
val oldFile = resourceDirectory.resolve("$old.xml")
63+
Files.write(oldFile.toPath(), newFile.readBytes())
64+
}
65+
}
66+
67+
// Copy mipmaps to secondary files.
68+
mipmapDirectories.forEach { directory ->
69+
val targetDirectory = resourceDirectory.resolve(directory)
6170

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

0 commit comments

Comments
 (0)