Skip to content

Commit

Permalink
feat(GmsCore): add Custom branding icon patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushTNM committed Jun 19, 2024
1 parent ab80dc6 commit 6bd178f
Show file tree
Hide file tree
Showing 78 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package app.revanced.patches.gmscore.branding.icon

import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.options.PatchOption.PatchExtensions.stringPatchOption
import app.revanced.util.ResourceGroup
import app.revanced.util.Utils.trimIndentMultiline
import app.revanced.util.copyFile
import app.revanced.util.copyResources
import app.revanced.patcher.patch.ResourcePatch

import app.revanced.util.underBarOrThrow

//@Suppress("unused")
object CustomBrandingIconPatch : ResourcePatch(
name = "Custom branding icon",
description = "Changes the app icon to the icon specified in options.json.",
compatiblePackages = setOf(
CompatiblePackage("app.revanced.android.gms")
)
) {
private const val DEFAULT_ICON = "revancify_blue"

private val availableIcon = mapOf(
"MMT" to "mmt",
"Revancify Blue" to DEFAULT_ICON,
"Revancify Red" to "revancify_red"
)

private val sizeArray = arrayOf(
"xxxhdpi",
"xxhdpi",
"xhdpi",
"hdpi",
"mdpi",
)

private val mipmapDirectories = sizeArray.map { "mipmap-$it" }

private val launcherIconResourceFileNames = arrayOf(
"ic_app",
"ic_app_round",
"ic_app_foreground",
"ic_app_foreground_mono",
"ic_app_background"
).map { "$it.png" }.toTypedArray()

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

private val iconResourceGroups =
mipmapDirectories.getResourceGroup(launcherIconResourceFileNames)

// region patch option

val AppIcon = stringPatchOption(
key = "AppIcon",
default = DEFAULT_ICON,
values = availableIcon,
title = "App icon",
description = """
The icon to apply to the app.
If a path to a folder is provided, the folder must contain the following folders:
${mipmapDirectories.joinToString("\n") { "- $it" }}
Each of these folders must contain the following files:
${launcherIconResourceFileNames.joinToString("\n") { "- $it" }}
""".trimIndentMultiline(),
required = true
)

// endregion

override fun execute(context: ResourceContext) {

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

val appIconResourcePath = "gmscore/branding/icon/$appIcon"

context.copyResources("gmscore/branding/icon", ResourceGroup("mipmap-anydpi-v26", "ic_app.xml"))
// Check if a custom path is used in the patch options.
if (!availableIcon.containsValue(appIcon)) {
context.copyFile(
iconResourceGroups,
appIcon,
"WARNING: Invalid app icon path: $appIcon. Does not apply patches."
)
} else {
// Change launcher icon.

iconResourceGroups.let { resourceGroups ->
resourceGroups.forEach {
context.copyResources(appIconResourcePath, it)
}
}
}
val manifest = context.get("AndroidManifest.xml", false)
manifest.writeText(
manifest.readText()
.replace(
"\"@mipmap/ic_app_settings\"",
"\"@mipmap/ic_app\"",
).replace(
"android:roundIcon=\"@mipmap/ic_app\"",
"android:roundIcon=\"@mipmap/ic_app_round\""
),
)
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/app/revanced/util/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.revanced.util

internal object Utils {
internal fun String.trimIndentMultiline() =
this.split("\n")
.joinToString("\n") { it.trimIndent() } // Remove the leading whitespace from each line.
.trimIndent() // Remove the leading newline.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon
xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_app_background" />
<foreground android:drawable="@mipmap/ic_app_foreground" />
<monochrome android:drawable="@mipmap/ic_app_foreground_mono" />
</adaptive-icon>
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.
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.
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 6bd178f

Please sign in to comment.