app: suggest recovery images with FirmwareCrawler

Add FirmwareCrawler in app:core to provide an extensible API for
crawling and suggesting firmware candidate download URLs in the
install flow.

Implement GooglebookProvider to query production_recovery.json via
NetworkService, filtering by matching Build.DEVICE or Build.MODEL to
suggest the latest official recovery image on supported devices.

Refactor InstallViewModel and InstallDialog to use the generic
FirmwareCrawler API without hardcoding device-specific logic,
presenting candidate suggestions alongside custom URL input.

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 3ebcc3440d
commit 93e4023b8b
11 changed files with 252 additions and 36 deletions
@@ -2,7 +2,6 @@ package com.topjohnwu.magisk.core
import android.os.Build
import android.os.Process
import com.topjohnwu.magisk.core.BuildConfig.APP_VERSION_CODE
@Suppress("DEPRECATION")
object Const {
@@ -45,6 +44,8 @@ object Const {
const val GITHUB_API_URL = "https://api.github.com/"
const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk-files/"
const val INVALID_URL = "https://example.com/"
const val GOOGLEBOOK_RECOVERY_URL =
"https://dl.google.com/dl/edgedl/device/recovery/production_recovery.json"
}
object Key {
@@ -60,6 +60,9 @@ object Info {
|| getProperty("ro.kernel.qemu", "0") == "1"
|| getProperty("ro.boot.qemu", "0") == "1"
val isGooglebook get() =
AppContext.packageManager.hasSystemFeature("com.google.desktop.gms")
val isHeadlessSystemUser by lazy {
getProperty("ro.fw.mu.headless_system_user", "false").toBoolean()
}
@@ -1,6 +1,7 @@
package com.topjohnwu.magisk.core.data
import com.topjohnwu.magisk.core.model.ModuleJson
import com.topjohnwu.magisk.core.model.GooglebookRecoveryJson
import com.topjohnwu.magisk.core.model.Release
import com.topjohnwu.magisk.core.model.UpdateJson
import okhttp3.ResponseBody
@@ -26,6 +27,9 @@ interface RawUrl {
@GET
suspend fun fetchUpdateJson(@Url url: String): UpdateJson
@GET
suspend fun fetchGooglebookRecoveryJson(@Url url: String): List<GooglebookRecoveryJson>
}
interface GithubApiServices {
@@ -11,6 +11,7 @@ import com.topjohnwu.magisk.core.data.magiskdb.PolicyDao
import com.topjohnwu.magisk.core.data.magiskdb.SettingsDao
import com.topjohnwu.magisk.core.data.magiskdb.StringDao
import com.topjohnwu.magisk.core.ktx.deviceProtectedContext
import com.topjohnwu.magisk.core.repository.FirmwareCrawler
import com.topjohnwu.magisk.core.repository.LogRepository
import com.topjohnwu.magisk.core.repository.NetworkService
import io.noties.markwon.Markwon
@@ -38,6 +39,7 @@ object ServiceLocator {
createApiService(retrofit, Const.Url.GITHUB_API_URL),
)
}
val firmwareCrawler by lazy { FirmwareCrawler(networkService) }
}
private fun createSuLogDatabase(context: Context) =
@@ -0,0 +1,23 @@
package com.topjohnwu.magisk.core.model
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class GooglebookRecoveryJson(
val channel: String = "",
val device: String = "",
val file: String = "",
val filesize: Long = 0L,
val manufacturer: String = "",
val md5: String = "",
val model: String = "",
val name: String = "",
val url: String = "",
val zipfilesize: Long = 0L,
val buildexploreruri: String = "",
val buildidentifier: String = "",
val target: String = "",
val branch: String = "",
val dateupdated: String = "",
val previousVersionGeneration: Long = 0L,
)
@@ -0,0 +1,74 @@
package com.topjohnwu.magisk.core.repository
import android.os.Build
import com.topjohnwu.magisk.core.AppContext
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.GooglebookRecoveryJson
import timber.log.Timber
import com.topjohnwu.magisk.core.R as CoreR
class FirmwareCrawler(
private val svc: NetworkService,
) {
private val providers: Map<() -> Boolean, Provider> = mapOf(
{ Info.isGooglebook } to GooglebookProvider,
)
data class Candidate(
val url: String,
val title: String,
val description: String = "",
)
fun interface Provider {
suspend fun crawl(svc: NetworkService): Candidate?
}
suspend fun crawl(): Candidate? {
for ((predicate, provider) in providers) {
try {
if (predicate()) {
val candidate = provider.crawl(svc)
if (candidate != null) return candidate
}
} catch (e: Exception) {
Timber.e(e)
}
}
return null
}
private object GooglebookProvider : Provider {
override suspend fun crawl(svc: NetworkService): Candidate? {
val list = svc.fetchGooglebookRecovery() ?: return null
val matching = list.filter {
it.device.equals(Build.DEVICE, ignoreCase = true) ||
it.model.equals(Build.MODEL, ignoreCase = true)
}
val recovery = matching.maxWithOrNull(
compareBy<GooglebookRecoveryJson> { it.dateupdated }
.thenBy { it.previousVersionGeneration }
) ?: return null
if (recovery.url.isEmpty()) return null
val details = buildString {
if (recovery.model.isNotEmpty()) {
append(recovery.model)
} else if (recovery.name.isNotEmpty()) {
append(recovery.name)
}
if (recovery.buildidentifier.isNotEmpty()) {
if (isNotEmpty()) append(" • ")
append(recovery.buildidentifier)
}
}
return Candidate(
url = recovery.url,
title = AppContext.getString(CoreR.string.download_googlebook_recovery_image),
description = details,
)
}
}
}
@@ -7,10 +7,12 @@ import com.topjohnwu.magisk.core.Config.Value.CUSTOM_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.DEBUG_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.DEFAULT_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.data.GithubApiServices
import com.topjohnwu.magisk.core.data.RawUrl
import com.topjohnwu.magisk.core.ktx.dateFormat
import com.topjohnwu.magisk.core.model.GooglebookRecoveryJson
import com.topjohnwu.magisk.core.model.Release
import com.topjohnwu.magisk.core.model.ReleaseAssets
import com.topjohnwu.magisk.core.model.UpdateInfo
@@ -45,6 +47,10 @@ class NetworkService(
findRelease { it.versionCode == version }.asInfo()
}
suspend fun fetchGooglebookRecovery() = safe {
raw.fetchGooglebookRecoveryJson(Const.Url.GOOGLEBOOK_RECOVERY_URL)
}
// Keep going through all release pages until we find a match
private suspend inline fun findRelease(predicate: (Release) -> Boolean): Release? {
var page = 1
+1
View File
@@ -50,6 +50,7 @@
<string name="download_patch_file">Download and patch a file</string>
<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="reboot_delay_toast">Rebooting in 5 seconds…</string>
<string name="flash_screen_title">Installation</string>