2020-10-06 13:58:46 +02:00
|
|
|
package com.topjohnwu.magisk.data.repository
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.core.Config
|
|
|
|
import com.topjohnwu.magisk.core.Config.Value.BETA_CHANNEL
|
|
|
|
import com.topjohnwu.magisk.core.Config.Value.CANARY_CHANNEL
|
|
|
|
import com.topjohnwu.magisk.core.Config.Value.CUSTOM_CHANNEL
|
|
|
|
import com.topjohnwu.magisk.core.Config.Value.DEFAULT_CHANNEL
|
|
|
|
import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL
|
2020-10-07 10:51:05 +02:00
|
|
|
import com.topjohnwu.magisk.core.Const
|
2020-10-06 13:58:46 +02:00
|
|
|
import com.topjohnwu.magisk.core.Info
|
2021-01-24 14:14:46 +01:00
|
|
|
import com.topjohnwu.magisk.core.model.MagiskJson
|
|
|
|
import com.topjohnwu.magisk.core.model.StubJson
|
|
|
|
import com.topjohnwu.magisk.core.model.UpdateInfo
|
2020-10-07 10:51:05 +02:00
|
|
|
import com.topjohnwu.magisk.data.network.*
|
2020-10-06 13:58:46 +02:00
|
|
|
import retrofit2.HttpException
|
|
|
|
import timber.log.Timber
|
|
|
|
import java.io.IOException
|
|
|
|
|
|
|
|
class NetworkService(
|
|
|
|
private val pages: GithubPageServices,
|
2020-10-10 23:31:30 +02:00
|
|
|
private val raw: RawServices,
|
2020-10-06 13:58:46 +02:00
|
|
|
private val jsd: JSDelivrServices,
|
|
|
|
private val api: GithubApiServices
|
|
|
|
) {
|
2020-10-20 16:36:39 +02:00
|
|
|
suspend fun fetchUpdate() = safe {
|
2020-10-06 13:58:46 +02:00
|
|
|
var info = when (Config.updateChannel) {
|
|
|
|
DEFAULT_CHANNEL, STABLE_CHANNEL -> fetchStableUpdate()
|
|
|
|
BETA_CHANNEL -> fetchBetaUpdate()
|
|
|
|
CANARY_CHANNEL -> fetchCanaryUpdate()
|
|
|
|
CUSTOM_CHANNEL -> fetchCustomUpdate(Config.customChannelUrl)
|
|
|
|
else -> throw IllegalArgumentException()
|
|
|
|
}
|
|
|
|
if (info.magisk.versionCode < Info.env.magiskVersionCode &&
|
|
|
|
Config.updateChannel == DEFAULT_CHANNEL
|
|
|
|
) {
|
|
|
|
Config.updateChannel = BETA_CHANNEL
|
|
|
|
info = fetchBetaUpdate()
|
|
|
|
}
|
|
|
|
info
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateInfo
|
2020-10-20 12:03:40 +02:00
|
|
|
private suspend fun fetchStableUpdate() = pages.fetchStableUpdate()
|
|
|
|
private suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate()
|
|
|
|
private suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url)
|
|
|
|
private suspend fun fetchCanaryUpdate(): UpdateInfo {
|
2020-10-07 10:51:05 +02:00
|
|
|
val sha = fetchCanaryVersion()
|
|
|
|
val info = jsd.fetchCanaryUpdate(sha)
|
|
|
|
|
|
|
|
fun genCDNUrl(name: String) = "${Const.Url.JS_DELIVR_URL}${MAGISK_FILES}@${sha}/${name}"
|
|
|
|
fun MagiskJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note))
|
|
|
|
fun StubJson.updateCopy() = copy(link = genCDNUrl(link))
|
|
|
|
|
|
|
|
return info.copy(
|
|
|
|
magisk = info.magisk.updateCopy(),
|
2021-01-24 14:14:46 +01:00
|
|
|
stub = info.stub.updateCopy()
|
2020-10-07 10:51:05 +02:00
|
|
|
)
|
|
|
|
}
|
2020-10-06 13:58:46 +02:00
|
|
|
|
2020-10-20 12:03:40 +02:00
|
|
|
private inline fun <T> safe(factory: () -> T): T? {
|
|
|
|
return try {
|
|
|
|
factory()
|
2020-10-20 16:36:39 +02:00
|
|
|
} catch (e: Exception) {
|
2020-10-20 12:03:40 +02:00
|
|
|
Timber.e(e)
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private inline fun <T> wrap(factory: () -> T): T {
|
|
|
|
return try {
|
|
|
|
factory()
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
throw IOException(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 23:31:30 +02:00
|
|
|
// Modules related
|
2020-10-20 12:03:40 +02:00
|
|
|
suspend fun fetchRepoInfo(url: String = Const.Url.OFFICIAL_REPO) = safe {
|
|
|
|
raw.fetchRepoInfo(url)
|
|
|
|
}
|
2020-10-10 23:31:30 +02:00
|
|
|
|
|
|
|
// Fetch files
|
2020-10-20 12:03:40 +02:00
|
|
|
suspend fun fetchSafetynet() = wrap { jsd.fetchSafetynet() }
|
|
|
|
suspend fun fetchBootctl() = wrap { jsd.fetchBootctl() }
|
|
|
|
suspend fun fetchInstaller() = wrap {
|
2020-10-07 10:51:05 +02:00
|
|
|
val sha = fetchMainVersion()
|
2020-10-20 12:03:40 +02:00
|
|
|
jsd.fetchInstaller(sha)
|
2020-10-07 10:51:05 +02:00
|
|
|
}
|
2020-10-20 12:03:40 +02:00
|
|
|
suspend fun fetchFile(url: String) = wrap { raw.fetchFile(url) }
|
|
|
|
suspend fun fetchString(url: String) = wrap { raw.fetchString(url) }
|
2020-10-06 13:58:46 +02:00
|
|
|
|
2020-10-07 10:51:05 +02:00
|
|
|
private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha
|
|
|
|
private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha
|
2020-10-06 13:58:46 +02:00
|
|
|
}
|