app: add PixelProvider for FirmwareCrawler

Add PixelProvider in FirmwareCrawler to crawl and suggest official
factory images for Google Pixel devices.

Fetch factory images from developers.google.com/android/images via
NetworkService with DevSite's terms acknowledgment cookie, parse the
target device table, and suggest the latest factory image (or matching
Build.ID) as candidate download in the install flow.

Add unit tests in AppTest verifying device matching, build ID
selection, Nexus device filtering, and unknown device rejection.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-08 10:35:49 -07:00
committed by John Wu
parent 93e4023b8b
commit 4d169b84b6
7 changed files with 91 additions and 0 deletions
@@ -46,6 +46,8 @@ object Const {
const val INVALID_URL = "https://example.com/"
const val GOOGLEBOOK_RECOVERY_URL =
"https://dl.google.com/dl/edgedl/device/recovery/production_recovery.json"
const val PIXEL_FACTORY_IMAGES_URL =
"https://developers.google.com/android/images"
}
object Key {
@@ -63,6 +63,9 @@ object Info {
val isGooglebook get() =
AppContext.packageManager.hasSystemFeature("com.google.desktop.gms")
val isPixel get() =
Build.BRAND.equals("google", ignoreCase = true) && !isGooglebook && !isEmulator
val isHeadlessSystemUser by lazy {
getProperty("ro.fw.mu.headless_system_user", "false").toBoolean()
}
@@ -30,6 +30,10 @@ interface RawUrl {
@GET
suspend fun fetchGooglebookRecoveryJson(@Url url: String): List<GooglebookRecoveryJson>
@GET
@Headers("Cookie: devsite_wall_acks=nexus-image-tos")
suspend fun fetchPixelFactoryImages(@Url url: String): String
}
interface GithubApiServices {
@@ -13,6 +13,7 @@ class FirmwareCrawler(
private val providers: Map<() -> Boolean, Provider> = mapOf(
{ Info.isGooglebook } to GooglebookProvider,
{ Info.isPixel } to PixelProvider,
)
data class Candidate(
@@ -71,4 +72,52 @@ class FirmwareCrawler(
)
}
}
internal object PixelProvider : Provider {
override suspend fun crawl(svc: NetworkService): Candidate? =
crawl(svc, Build.DEVICE, Build.MODEL, Build.ID)
suspend fun crawl(
svc: NetworkService,
device: String,
model: String = Build.MODEL,
buildId: String = "",
): Candidate? {
val html = svc.fetchPixelFactoryImages() ?: return null
val sectionRegex = Regex(
"""<h2 id="${Regex.escape(device)}"[^>]*>(.*?)</h2>\s*<table>(.*?)</table>""",
setOf(RegexOption.DOT_MATCHES_ALL, RegexOption.IGNORE_CASE)
)
val match = sectionRegex.find(html) ?: return null
val heading = match.groupValues[1]
val table = match.groupValues[2]
val parsedModel = Regex("""for\s+([^<]+)$""").find(heading)
?.groupValues?.get(1)?.trim()?.trim('"', '\'') ?: model
if (!parsedModel.startsWith("Pixel", ignoreCase = true)) return null
val rowRegex = Regex(
"""<tr[^>]*>\s*<td>([^<]+)</td>.*?href="(https://dl\.google\.com/dl/android/aosp/[^"]+)"""",
RegexOption.DOT_MATCHES_ALL
)
val rows = rowRegex.findAll(table).toList()
if (rows.isEmpty()) return null
val target = if (buildId.isNotEmpty()) {
rows.findLast { it.value.contains(buildId, ignoreCase = true) } ?: rows.last()
} else {
rows.last()
}
val version = target.groupValues[1].trim()
.replace("&amp;", "&")
.replace("&quot;", "\"")
return Candidate(
url = target.groupValues[2],
title = AppContext.getString(CoreR.string.download_pixel_factory_image),
description = "$parsedModel • $version",
)
}
}
}
@@ -51,6 +51,10 @@ class NetworkService(
raw.fetchGooglebookRecoveryJson(Const.Url.GOOGLEBOOK_RECOVERY_URL)
}
suspend fun fetchPixelFactoryImages() = safe {
raw.fetchPixelFactoryImages(Const.Url.PIXEL_FACTORY_IMAGES_URL)
}
// Keep going through all release pages until we find a match
private suspend inline fun findRelease(predicate: (Release) -> Boolean): Release? {
var page = 1
@@ -3,6 +3,7 @@ package com.topjohnwu.magisk.test
import androidx.annotation.Keep
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.topjohnwu.magisk.core.di.ServiceLocator
import com.topjohnwu.magisk.core.repository.FirmwareCrawler
import com.topjohnwu.magisk.core.tasks.OtaPayloadExtractor
import com.topjohnwu.magisk.core.tasks.PatchFileClassifier
import com.topjohnwu.magisk.core.tasks.RecoveryGptProcessor
@@ -362,6 +363,33 @@ class AppTest : TestCommon {
}
}
@Test
fun testPixelProvider() = runBlocking(Dispatchers.IO) {
val svc = ServiceLocator.networkService
// Match latest
val candidate = FirmwareCrawler.PixelProvider.crawl(svc, "shiba", "Pixel 8")
assertNotNull(candidate)
assertTrue(candidate!!.url.startsWith("https://dl.google.com/dl/android/aosp/"))
assertTrue(candidate.url.endsWith(".zip"))
assertTrue(candidate.description.startsWith("Pixel 8 •"))
// Match specific build ID
val candidateSpecific =
FirmwareCrawler.PixelProvider.crawl(svc, "shiba", "Pixel 8", "UD1A.230803.022.A5")
assertNotNull(candidateSpecific)
assertTrue(candidateSpecific!!.url.contains("shiba-ud1a.230803.022.a5"))
assertTrue(candidateSpecific.description.contains("UD1A.230803.022.A5"))
// Non-pixel device should return null
val candidateNexus = FirmwareCrawler.PixelProvider.crawl(svc, "angler", "Nexus 6P")
assertNull(candidateNexus)
// Unknown device should return null
val candidateUnknown = FirmwareCrawler.PixelProvider.crawl(svc, "unknown", "Unknown")
assertNull(candidateUnknown)
}
private class ZipItem(val name: String, val data: ByteArray, val method: Int)
private fun buildZip(vararg items: ZipItem): ByteArray {
+1
View File
@@ -51,6 +51,7 @@
<string name="download_dialog_title">Enter image URL</string>
<string name="download_dialog_msg">Full OTA image or factory image</string>
<string name="download_googlebook_recovery_image">Download latest Googlebook recovery image</string>
<string name="download_pixel_factory_image">Download latest Pixel factory image</string>
<string name="reboot_delay_toast">Rebooting in 5 seconds…</string>
<string name="flash_screen_title">Installation</string>