diff --git a/app/src/main/java/net/pipe01/pinepartner/service/BackgroundService.kt b/app/src/main/java/net/pipe01/pinepartner/service/BackgroundService.kt index 6351de1..a25df63 100644 --- a/app/src/main/java/net/pipe01/pinepartner/service/BackgroundService.kt +++ b/app/src/main/java/net/pipe01/pinepartner/service/BackgroundService.kt @@ -47,6 +47,7 @@ import net.pipe01.pinepartner.scripting.PluginManager import net.pipe01.pinepartner.scripting.ScriptDependencies import net.pipe01.pinepartner.utils.runJobThrowing import java.io.ByteArrayInputStream +import java.net.URL import java.time.Duration import java.time.LocalDateTime import java.time.ZoneOffset @@ -282,11 +283,14 @@ class BackgroundService : Service() { Log.d(TAG, "Flashing watch $address with $uri") val device = deviceManager.get(address) ?: throw ServiceException("Device not found") - runJobThrowing(CoroutineScope(Dispatchers.IO), onStart = { transferJobs[jobId] = TransferJob(it, TransferProgress(0f, null, null, false)) }) { - contentResolver.openInputStream(uri)!!.use { stream -> + if (uri.scheme?.startsWith("http") == true) { + URL(uri.toString()).openStream() + } else { + contentResolver.openInputStream(uri)!! + }.use { stream -> device.flashDFU(stream, this) { transferJobs[jobId]?.progress = TransferProgress( it.totalProgress, diff --git a/app/src/main/java/net/pipe01/pinepartner/utils/InfiniTimeReleases.kt b/app/src/main/java/net/pipe01/pinepartner/utils/InfiniTimeReleases.kt index 86ff970..bf891ac 100644 --- a/app/src/main/java/net/pipe01/pinepartner/utils/InfiniTimeReleases.kt +++ b/app/src/main/java/net/pipe01/pinepartner/utils/InfiniTimeReleases.kt @@ -1,2 +1,48 @@ package net.pipe01.pinepartner.utils +import android.net.Uri +import fuel.Fuel +import fuel.get +import kotlinx.serialization.Serializable +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json + +data class InfiniTimeRelease( + val version: String, + val name: String, + val resourcesUri: Uri, + val firmwareUri: Uri, +) + +@Serializable +private data class GitHubRelease( + val tag_name: String, + val name: String, + val assets: List, +) + +@Serializable +private data class GitHubAsset( + val name: String, + val browser_download_url: String, +) + +private val json = Json { ignoreUnknownKeys = true } + +// This endpoint caches the response of https://api.github.com/repos/InfiniTimeOrg/InfiniTime/releases +private const val endpoint = "https://pipe01.net/pinepartner-proxy/releases" + +suspend fun getInfiniTimeReleases(): List { + val resp = Fuel.get(endpoint).body + + val ghReleases = json.decodeFromString>(resp) + + return ghReleases.mapNotNull { + InfiniTimeRelease( + version = it.tag_name, + name = it.name.trim(), + resourcesUri = Uri.parse(it.assets.firstOrNull { it.name.startsWith("infinitime-resources-") }?.browser_download_url ?: return@mapNotNull null), + firmwareUri = Uri.parse(it.assets.firstOrNull { it.name.startsWith("pinetime-mcuboot-app-dfu-") }?.browser_download_url ?: return@mapNotNull null), + ) + } +}