From 1e85df5fc5489e0c98ff4b25256dc9b8585a0189 Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:44:35 +0900 Subject: [PATCH] feat(YouTube Music): split `Custom header for YouTube Music` patch from the `Custom branding icon for YouTube Music` patch --- .../branding/icon/CustomBrandingIconPatch.kt | 32 +--- .../music/layout/header/ChangeHeaderPatch.kt | 139 ++++++++++++++++++ .../music/utils/settings/ResourceUtils.kt | 7 + 3 files changed, 149 insertions(+), 29 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/music/layout/header/ChangeHeaderPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/music/layout/branding/icon/CustomBrandingIconPatch.kt b/src/main/kotlin/app/revanced/patches/music/layout/branding/icon/CustomBrandingIconPatch.kt index 9aea48045..0c2c6dc09 100644 --- a/src/main/kotlin/app/revanced/patches/music/layout/branding/icon/CustomBrandingIconPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/layout/branding/icon/CustomBrandingIconPatch.kt @@ -5,6 +5,7 @@ import app.revanced.patcher.patch.PatchException import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.booleanPatchOption import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE +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 @@ -56,12 +57,6 @@ object CustomBrandingIconPatch : BaseResourcePatch( private val mipmapDirectories = sizeArray.map { "mipmap-$it" } - private val headerIconResourceFileNames = arrayOf( - "action_bar_logo", - "logo_music", - "ytm_logo" - ).map { "$it.png" }.toTypedArray() - private val launcherIconResourceFileNames = arrayOf( "adaptiveproduct_youtube_music_background_color_108", "adaptiveproduct_youtube_music_foreground_color_108", @@ -76,12 +71,6 @@ object CustomBrandingIconPatch : BaseResourcePatch( "record" ).map { "$it.png" }.toTypedArray() - private val headerIconResourceGroups = drawableDirectories.map { directory -> - ResourceGroup( - directory, *headerIconResourceFileNames - ) - } - private val launcherIconResourceGroups = mipmapDirectories.map { directory -> ResourceGroup( directory, *launcherIconResourceFileNames @@ -113,14 +102,6 @@ object CustomBrandingIconPatch : BaseResourcePatch( required = true ) - private val ChangeHeader by booleanPatchOption( - key = "ChangeHeader", - default = false, - title = "Change header", - description = "Apply the custom branding icon to the header.", - required = true - ) - private val ChangeSplashIcon by booleanPatchOption( key = "ChangeSplashIcon", default = true, @@ -179,15 +160,6 @@ object CustomBrandingIconPatch : BaseResourcePatch( context.copyResources("$appIconResourcePath/monochrome", resourceGroup) } - // Change header. - if (ChangeHeader == true) { - headerIconResourceGroups.let { resourceGroups -> - resourceGroups.forEach { - context.copyResources("$appIconResourcePath/header", it) - } - } - } - // Change splash icon. if (ChangeSplashIcon == true) { splashIconResourceGroups.let { resourceGroups -> @@ -196,6 +168,8 @@ object CustomBrandingIconPatch : BaseResourcePatch( } } } + + setIconType(appIcon) } } } diff --git a/src/main/kotlin/app/revanced/patches/music/layout/header/ChangeHeaderPatch.kt b/src/main/kotlin/app/revanced/patches/music/layout/header/ChangeHeaderPatch.kt new file mode 100644 index 000000000..bc519e7d7 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/layout/header/ChangeHeaderPatch.kt @@ -0,0 +1,139 @@ +package app.revanced.patches.music.layout.header + +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption +import app.revanced.patches.music.utils.compatibility.Constants +import app.revanced.patches.music.utils.settings.ResourceUtils +import app.revanced.util.ResourceGroup +import app.revanced.util.Utils.trimIndentMultiline +import app.revanced.util.copyFile +import app.revanced.util.copyResources +import app.revanced.util.patch.BaseResourcePatch +import app.revanced.util.underBarOrThrow + +@Suppress("unused") +object ChangeHeaderPatch : BaseResourcePatch( + name = "Custom header for YouTube Music", + description = "Applies a custom header in the top left corner within the app.", + compatiblePackages = Constants.COMPATIBLE_PACKAGE, + use = false, +) { + private const val DEFAULT_HEADER_KEY = "Custom branding icon" + private const val DEFAULT_HEADER_VALUE = "custom_branding_icon" + + private val actionBarLogoResourceDirectoryNames = mapOf( + "xxxhdpi" to "320px x 96px", + "xxhdpi" to "240px x 72px", + "xhdpi" to "160px x 48px", + "hdpi" to "121px x 36px", + "mdpi" to "80px x 24px", + ).map { (dpi, dim) -> + "drawable-$dpi" to dim + }.toMap() + + private val logoMusicResourceDirectoryNames = mapOf( + "xxxhdpi" to "576px x 200px", + "xxhdpi" to "432px x 150px", + "xhdpi" to "288px x 100px", + "hdpi" to "217px x 76px", + "mdpi" to "144px x 50px", + ).map { (dpi, dim) -> + "drawable-$dpi" to dim + }.toMap() + + private val ytmMusicLogoResourceDirectoryNames = mapOf( + "xxxhdpi" to "412px x 144px", + "xxhdpi" to "309px x 108px", + "xhdpi" to "206px x 72px", + "hdpi" to "155px x 54px", + "mdpi" to "103px x 36px", + ).map { (dpi, dim) -> + "drawable-$dpi" to dim + }.toMap() + + private val headerIconResourceFileNames = arrayOf( + "action_bar_logo", + "logo_music", + "ytm_logo" + ).map { "$it.png" }.toTypedArray() + + private val headerIconResourceGroups = + actionBarLogoResourceDirectoryNames.keys.map { directory -> + ResourceGroup( + directory, *headerIconResourceFileNames + ) + } + + private val getDescription = { + var descriptionBody = """ + The header to apply to the app. + + Patch option '$DEFAULT_HEADER_KEY' applies only when: + + 1. Patch 'Custom branding icon for YouTube Music' is included. + 2. Patch option for 'Custom branding icon for YouTube Music' is selected from the preset. + + If a path to a folder is provided, the folder must contain one or more of the following folders, depending on the DPI of the device: + + ${actionBarLogoResourceDirectoryNames.keys.joinToString("\n") { "- $it" }} + + Each of the folders must contain all of the following files: + + ${headerIconResourceFileNames.joinToString("\n") { "- $it" }} + """ + + mapOf( + "action_bar_logo.png" to actionBarLogoResourceDirectoryNames, + "logo_music.png" to logoMusicResourceDirectoryNames, + "ytm_logo.png" to ytmMusicLogoResourceDirectoryNames + ).forEach { (images, directoryNames) -> + descriptionBody += """ + The image '$images' dimensions must be as follows: + + ${directoryNames.map { (dpi, dim) -> "- $dpi: $dim" }.joinToString("\n")} + """ + } + + descriptionBody.trimIndentMultiline() + } + + private val CustomHeader = stringPatchOption( + key = "CustomHeader", + default = DEFAULT_HEADER_VALUE, + values = mapOf( + DEFAULT_HEADER_KEY to DEFAULT_HEADER_VALUE + ), + title = "Custom header", + description = getDescription(), + required = true, + ) + + override fun execute(context: ResourceContext) { + + // Check patch options first. + val customHeader = CustomHeader + .underBarOrThrow() + + val customBrandingIconType = ResourceUtils.getIconType() + val customBrandingIconIncluded = customBrandingIconType != "default" + + val warnings = "WARNING: Invalid header path: $customHeader. Does not apply patches." + + if (customHeader != DEFAULT_HEADER_VALUE) { + context.copyFile( + headerIconResourceGroups, + customHeader, + warnings + ) + } else if (customBrandingIconIncluded) { + headerIconResourceGroups.let { resourceGroups -> + resourceGroups.forEach { + context.copyResources("music/branding/$customBrandingIconType/header", it) + } + } + } else { + println(warnings) + } + + } +} diff --git a/src/main/kotlin/app/revanced/patches/music/utils/settings/ResourceUtils.kt b/src/main/kotlin/app/revanced/patches/music/utils/settings/ResourceUtils.kt index f5b6e8998..a135b694e 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/settings/ResourceUtils.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/settings/ResourceUtils.kt @@ -28,6 +28,13 @@ object ResourceUtils { var musicPackageName = "com.google.android.apps.youtube.music" + private var iconType = "default" + fun getIconType() = iconType + + fun setIconType(iconName: String) { + iconType = iconName + } + private fun isIncludedCategory(category: String): Boolean { CategoryType.entries.forEach { preference -> if (category == preference.value)