From 9848499496b4264d70b497181173379fca245cde Mon Sep 17 00:00:00 2001 From: inotia00 Date: Sun, 30 Apr 2023 18:32:08 +0900 Subject: [PATCH] add `custom-branding-icon-manual` patch --- .../patch/CustomBrandingIconManualPatch.kt | 61 +++++++++++++++++ .../app/revanced/util/resources/IconHelper.kt | 67 ++++++++++++++++--- .../youtube/settings/values-v21/strings.xml | 1 + 3 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/etc/branding/icon/patch/CustomBrandingIconManualPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/etc/branding/icon/patch/CustomBrandingIconManualPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/etc/branding/icon/patch/CustomBrandingIconManualPatch.kt new file mode 100644 index 000000000..f190b3c8d --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/etc/branding/icon/patch/CustomBrandingIconManualPatch.kt @@ -0,0 +1,61 @@ +package app.revanced.patches.youtube.layout.etc.branding.icon.patch + +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultError +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.ResourcePatch +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.shared.annotation.YouTubeCompatibility +import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch +import app.revanced.util.resources.IconHelper.YOUTUBE_LAUNCHER_ICON_ARRAY +import app.revanced.util.resources.IconHelper.copyFiles +import app.revanced.util.resources.IconHelper.makeDirectoryAndCopyFiles +import app.revanced.util.resources.ResourceHelper.updatePatchStatusIcon +import app.revanced.util.resources.ResourceUtils + + +@Patch(false) +@Name("custom-branding-icon-manual") +@Description("Changes the YouTube Music launcher icon specified in the 'branding' directory.") +@DependsOn([SettingsPatch::class]) +@YouTubeCompatibility +@Version("0.0.1") +class CustomBrandingIconManualPatch : ResourcePatch { + override fun execute(context: ResourceContext): PatchResult { + + fun copyResources(resourceGroups: List) { + val iconPath = "branding" + + try { + context.copyFiles(resourceGroups, iconPath) + } catch (_: Exception) { + context.makeDirectoryAndCopyFiles(resourceGroups, iconPath) + throw PatchResultError("icon not found!") + } + } + + val iconResourceFileNames = + YOUTUBE_LAUNCHER_ICON_ARRAY + .map { "$it.png" } + .toTypedArray() + + fun createGroup(directory: String) = ResourceUtils.ResourceGroup( + directory, *iconResourceFileNames + ) + + // change the app icon + arrayOf("xxxhdpi", "xxhdpi", "xhdpi", "hdpi", "mdpi") + .map { "mipmap-$it" } + .map(::createGroup) + .let(::copyResources) + + context.updatePatchStatusIcon("custom") + + return PatchResultSuccess() + } +} diff --git a/src/main/kotlin/app/revanced/util/resources/IconHelper.kt b/src/main/kotlin/app/revanced/util/resources/IconHelper.kt index 334757201..620fc7443 100644 --- a/src/main/kotlin/app/revanced/util/resources/IconHelper.kt +++ b/src/main/kotlin/app/revanced/util/resources/IconHelper.kt @@ -1,19 +1,74 @@ package app.revanced.util.resources import app.revanced.patcher.data.ResourceContext +import java.io.File import java.nio.file.Files +import java.nio.file.Paths import java.nio.file.StandardCopyOption internal object IconHelper { - - internal fun ResourceContext.customIcon(iconName: String) { - val launchIcon = arrayOf( + internal var YOUTUBE_LAUNCHER_ICON_ARRAY = + arrayOf( "adaptiveproduct_youtube_background_color_108", "adaptiveproduct_youtube_foreground_color_108", "ic_launcher", "ic_launcher_round" ) + internal var YOUTUBE_MUSIC_LAUNCHER_ICON_ARRAY = + arrayOf( + "adaptiveproduct_youtube_music_background_color_108", + "adaptiveproduct_youtube_music_foreground_color_108", + "ic_launcher_release" + ) + + internal fun ResourceContext.copyFiles( + resourceGroups: List, + path: String + ) { + val iconPath = File(Paths.get("").toAbsolutePath().toString()).resolve(path) + val resourceDirectory = this["res"] + + resourceGroups.forEach { group -> + val fromDirectory = iconPath.resolve(group.resourceDirectoryName) + val toDirectory = resourceDirectory.resolve(group.resourceDirectoryName) + + group.resources.forEach { iconFileName -> + Files.write( + toDirectory.resolve(iconFileName).toPath(), + fromDirectory.resolve(iconFileName).readBytes() + ) + } + } + } + + internal fun ResourceContext.makeDirectoryAndCopyFiles( + resourceGroups: List, + path: String + ) { + val newDirectory = Paths.get("").toAbsolutePath().toString() + "/$path" + this[newDirectory].mkdir() + + val iconPath = File(Paths.get("").toAbsolutePath().toString()).resolve(path) + val resourceDirectory = this["res"] + + resourceGroups.forEach { group -> + this[newDirectory + "/${group.resourceDirectoryName}"].mkdir() + val fromDirectory = iconPath.resolve(group.resourceDirectoryName) + val toDirectory = resourceDirectory.resolve(group.resourceDirectoryName) + + group.resources.forEach { iconFileName -> + Files.write( + fromDirectory.resolve(iconFileName).toPath(), + toDirectory.resolve(iconFileName).readBytes() + ) + } + } + } + + internal fun ResourceContext.customIcon(iconName: String) { + val launchIcon = YOUTUBE_LAUNCHER_ICON_ARRAY + val splashIcon = arrayOf( "product_logo_youtube_color_24", "product_logo_youtube_color_36", @@ -49,11 +104,7 @@ internal object IconHelper { } internal fun ResourceContext.customIconMusic(iconName: String) { - val launchIcon = arrayOf( - "adaptiveproduct_youtube_music_background_color_108", - "adaptiveproduct_youtube_music_foreground_color_108", - "ic_launcher_release" - ) + val launchIcon = YOUTUBE_MUSIC_LAUNCHER_ICON_ARRAY copyResources( "music", diff --git a/src/main/resources/youtube/settings/values-v21/strings.xml b/src/main/resources/youtube/settings/values-v21/strings.xml index e4af1d4cb..d67d85860 100644 --- a/src/main/resources/youtube/settings/values-v21/strings.xml +++ b/src/main/resources/youtube/settings/values-v21/strings.xml @@ -51,6 +51,7 @@ afn / blue stock + custom afn / red mmt revancify