Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / build-logic / src / main / kotlin / LauncherIconGen.kt
LauncherIconGen.kt172 lines · 7.1 KiBRaw
| 1 | import org.gradle.api.DefaultTask |
| 2 | import org.gradle.api.GradleException |
| 3 | import org.gradle.api.file.DirectoryProperty |
| 4 | import org.gradle.api.provider.Property |
| 5 | import org.gradle.api.tasks.Input |
| 6 | import org.gradle.api.tasks.Internal |
| 7 | import org.gradle.api.tasks.Optional |
| 8 | import org.gradle.api.tasks.OutputDirectory |
| 9 | import org.gradle.api.tasks.TaskAction |
| 10 | import java.net.URI |
| 11 | |
| 12 | // DSL for declaring which Material Symbol an app's launcher icon is built from: |
| 13 | // |
| 14 | // launcherIcon { |
| 15 | // symbol = "calendar_month" |
| 16 | // } |
| 17 | // |
| 18 | // `common-conventions-app` reads this and generates the app's adaptive-icon |
| 19 | // foreground vector at build time, so no ic_launcher_foreground.xml is committed. |
| 20 | interface LauncherIconExtension { |
| 21 | /** Material Symbols name, e.g. "calendar_month" (the `materialsymbolsoutlined` style). */ |
| 22 | val symbol: Property<String> |
| 23 | |
| 24 | /** |
| 25 | * Fraction of the 960 viewport the symbol occupies within the adaptive-icon |
| 26 | * safe zone. Defaults to 0.58; lower values render a smaller glyph with more |
| 27 | * padding. The translate is derived so the glyph stays centered. |
| 28 | */ |
| 29 | val scale: Property<Double> |
| 30 | } |
| 31 | |
| 32 | // Downloads the outlined Material Symbol at [ref], wraps its path in the standard |
| 33 | // adaptive-icon foreground vector, and writes it into an AGP-managed generated res |
| 34 | // directory. Raw symbol XML is cached under [cacheDir] so `clean` builds and |
| 35 | // offline rebuilds don't re-hit the network. |
| 36 | abstract class GenerateLauncherIconTask : DefaultTask() { |
| 37 | @get:Input |
| 38 | abstract val symbol: Property<String> |
| 39 | |
| 40 | @get:Input |
| 41 | abstract val ref: Property<String> |
| 42 | |
| 43 | @get:Input |
| 44 | abstract val scale: Property<Double> |
| 45 | |
| 46 | /** |
| 47 | * True only for app modules (set by `common-conventions-app`): also emit the |
| 48 | * per-app `Theme.General` splash override (see [SPLASH_THEME]). Wear modules |
| 49 | * leave this unset — they do not depend on `:library`, so the parent style |
| 50 | * would not resolve there. |
| 51 | */ |
| 52 | @get:Input |
| 53 | @get:Optional |
| 54 | abstract val splashTheme: Property<Boolean> |
| 55 | |
| 56 | @get:Internal |
| 57 | abstract val cacheDir: DirectoryProperty |
| 58 | |
| 59 | @get:OutputDirectory |
| 60 | abstract val outputDir: DirectoryProperty |
| 61 | |
| 62 | @TaskAction |
| 63 | fun generate() { |
| 64 | val sym = symbol.orNull |
| 65 | ?: throw GradleException( |
| 66 | "No launcher icon symbol set for ${path.substringBeforeLast(':')}. " + |
| 67 | "Declare one in build.gradle.kts:\n\n launcherIcon {\n symbol = \"calendar_month\"\n }\n" |
| 68 | ) |
| 69 | val gitRef = ref.get() |
| 70 | val raw = fetchRawSymbol(sym, gitRef) |
| 71 | val pathData = PATH_DATA.find(raw)?.groupValues?.get(1) |
| 72 | ?: throw GradleException("Could not find android:pathData in Material Symbol '$sym' (ref $gitRef)") |
| 73 | |
| 74 | val drawableDir = outputDir.get().asFile.resolve("drawable").apply { mkdirs() } |
| 75 | drawableDir.resolve("ic_launcher_foreground.xml").writeText(wrap(pathData)) |
| 76 | |
| 77 | // Emit the adaptive-icon wrapper too, so no ic_launcher.xml is committed per app. |
| 78 | // It references the generated foreground, the standard monochrome layer, and the |
| 79 | // per-app @color/ic_launcher_background (still declared in each app's values). |
| 80 | val mipmapDir = outputDir.get().asFile.resolve("mipmap-anydpi-v26").apply { mkdirs() } |
| 81 | mipmapDir.resolve("ic_launcher.xml").writeText(ADAPTIVE_ICON) |
| 82 | mipmapDir.resolve("ic_launcher_round.xml").writeText(ADAPTIVE_ICON) |
| 83 | |
| 84 | // Per-app splash theme: :library's Theme.General.Base cannot name |
| 85 | // @mipmap/ic_launcher (a library link has no app icon), so the app |
| 86 | // module — where the generated icon above does resolve — defines |
| 87 | // Theme.General itself as Base plus its own splash icon. One values |
| 88 | // file is enough: Base resolves per configuration (night/v33) through |
| 89 | // the parent chain. |
| 90 | if (splashTheme.orNull == true) { |
| 91 | val valuesDir = outputDir.get().asFile.resolve("values").apply { mkdirs() } |
| 92 | valuesDir.resolve("generated_splash_theme.xml").writeText(SPLASH_THEME) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private fun fetchRawSymbol(sym: String, gitRef: String): String { |
| 97 | val cached = cacheDir.get().asFile.resolve(gitRef).resolve("${sym}_24px.xml") |
| 98 | if (cached.isFile && cached.length() > 0) return cached.readText() |
| 99 | |
| 100 | val url = |
| 101 | "https://raw.githubusercontent.com/google/material-design-icons/$gitRef" + |
| 102 | "/symbols/android/$sym/materialsymbolsoutlined/${sym}_24px.xml" |
| 103 | val text = try { |
| 104 | URI(url).toURL().openStream().bufferedReader().use { it.readText() } |
| 105 | } catch (e: Exception) { |
| 106 | throw GradleException( |
| 107 | "Failed to download Material Symbol '$sym' from $url. " + |
| 108 | "Check the symbol name and network access.", |
| 109 | e, |
| 110 | ) |
| 111 | } |
| 112 | if (!text.contains("pathData")) { |
| 113 | throw GradleException("Material Symbol '$sym' not found at ref $gitRef (url: $url)") |
| 114 | } |
| 115 | cached.parentFile.mkdirs() |
| 116 | cached.writeText(text) |
| 117 | return text |
| 118 | } |
| 119 | |
| 120 | private fun wrap(pathData: String): String { |
| 121 | val s = scale.get() |
| 122 | val translate = (VIEWPORT - VIEWPORT * s) / 2.0 |
| 123 | return """ |
| 124 | |<vector xmlns:android="http://schemas.android.com/apk/res/android" |
| 125 | | android:width="108dp" |
| 126 | | android:height="108dp" |
| 127 | | android:viewportWidth="960" |
| 128 | | android:viewportHeight="960" |
| 129 | | android:tint="#000000"> |
| 130 | | <group android:scaleX="${fmt(s)}" |
| 131 | | android:scaleY="${fmt(s)}" |
| 132 | | android:translateX="${fmt(translate)}" |
| 133 | | android:translateY="${fmt(translate)}"> |
| 134 | | <path |
| 135 | | android:fillColor="@android:color/white" |
| 136 | | android:pathData="$pathData"/> |
| 137 | | </group> |
| 138 | |</vector> |
| 139 | | |
| 140 | """.trimMargin() |
| 141 | } |
| 142 | |
| 143 | private fun fmt(v: Double): String { |
| 144 | val s = v.toString() |
| 145 | return if (s.endsWith(".0")) s.dropLast(2) else s |
| 146 | } |
| 147 | |
| 148 | private companion object { |
| 149 | const val VIEWPORT = 960.0 |
| 150 | val PATH_DATA = Regex("""android:pathData="([^"]*)"""") |
| 151 | val ADAPTIVE_ICON = """ |
| 152 | |<?xml version="1.0" encoding="utf-8"?> |
| 153 | |<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> |
| 154 | | <background android:drawable="@color/ic_launcher_background"/> |
| 155 | | <foreground android:drawable="@drawable/ic_launcher_foreground"/> |
| 156 | | <monochrome android:drawable="@drawable/ic_launcher_foreground"/> |
| 157 | |</adaptive-icon> |
| 158 | | |
| 159 | """.trimMargin() |
| 160 | val SPLASH_THEME = """ |
| 161 | |<?xml version="1.0" encoding="utf-8"?> |
| 162 | |<!-- Generated per app: this app's launcher icon as the Android 12+ splash |
| 163 | | icon, over the shared splash background from Theme.General.Base. --> |
| 164 | |<resources> |
| 165 | | <style name="Theme.General" parent="Theme.General.Base"> |
| 166 | | <item name="android:windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item> |
| 167 | | </style> |
| 168 | |</resources> |
| 169 | | |
| 170 | """.trimMargin() |
| 171 | } |
| 172 | } |