update inuppupdater

This commit is contained in:
Cloudburst 2021-08-17 16:04:10 +02:00
parent d167c9a3cb
commit 11e000ecc8
2 changed files with 79 additions and 49 deletions

View file

@ -16,6 +16,7 @@ import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.readValue import com.fasterxml.jackson.module.kotlin.readValue
import com.lagradost.cloudstream3.BuildConfig import com.lagradost.cloudstream3.BuildConfig
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
import java.io.* import java.io.*
import java.net.URL import java.net.URL
import java.net.URLConnection import java.net.URLConnection
@ -48,7 +49,7 @@ class InAppUpdater {
) )
data class GithubTag( data class GithubTag(
@JsonProperty("object") val object: GithubObject, @JsonProperty("object") val github_object: GithubObject,
) )
data class Update( data class Update(
@ -63,53 +64,87 @@ class InAppUpdater {
private fun Activity.getAppUpdate(): Update { private fun Activity.getAppUpdate(): Update {
try { try {
val url = "https://api.github.com/repos/LagradOst/CloudStream-3/releases" val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val headers = mapOf("Accept" to "application/vnd.github.v3+json") return if (settingsManager.getBoolean(getString(R.string.prerelease_update_key), false)) {
val response = getPreReleaseUpdate()
mapper.readValue<List<GithubRelease>>(khttp.get(url, headers = headers).text)
val versionRegex = Regex("""(.*?((\d)\.(\d)\.(\d)).*\.apk)""")
/*
val releases = response.map { it.assets }.flatten()
.filter { it.content_type == "application/vnd.android.package-archive" }
val found =
releases.sortedWith(compareBy {
versionRegex.find(it.name)?.groupValues?.get(2)
}).toList().lastOrNull()*/
val found =
response.filter({ rel ->
!rel.prerelease
}).sortedWith(compareBy { release ->
release.assets.filter { it.content_type == "application/vnd.android.package-archive" }
.getOrNull(0)?.name?.let { it1 ->
versionRegex.find(
it1
)?.groupValues?.get(2)
}
}).toList().lastOrNull()
val foundAsset = found?.assets?.getOrNull(0)
val currentVersion = packageName?.let {
packageManager.getPackageInfo(it,
0)
}
val foundVersion = foundAsset?.name?.let { versionRegex.find(it) }
val shouldUpdate = if (found != null && foundAsset?.browser_download_url != "" && foundVersion != null) currentVersion?.versionName?.compareTo(
foundVersion.groupValues[2]
)!! < 0 else false
return if (foundVersion != null) {
Update(shouldUpdate, foundAsset.browser_download_url, foundVersion.groupValues[2], found.body)
} else { } else {
Update(false, null, null, null) getReleaseUpdate()
} }
} catch (e: Exception) { } catch (e: Exception) {
println(e) println(e)
return Update(false, null, null, null) return Update(false, null, null, null)
} }
} }
private fun Activity.getReleaseUpdate(): Update {
val url = "https://api.github.com/repos/LagradOst/CloudStream-3/releases"
val headers = mapOf("Accept" to "application/vnd.github.v3+json")
val response =
mapper.readValue<List<GithubRelease>>(khttp.get(url, headers = headers).text)
val versionRegex = Regex("""(.*?((\d)\.(\d)\.(\d)).*\.apk)""")
/*
val releases = response.map { it.assets }.flatten()
.filter { it.content_type == "application/vnd.android.package-archive" }
val found =
releases.sortedWith(compareBy {
versionRegex.find(it.name)?.groupValues?.get(2)
}).toList().lastOrNull()*/
val found =
response.filter { rel ->
!rel.prerelease
}.sortedWith(compareBy { release ->
release.assets.filter { it.content_type == "application/vnd.android.package-archive" }
.getOrNull(0)?.name?.let { it1 ->
versionRegex.find(
it1
)?.groupValues?.get(2)
}
}).toList().lastOrNull()
val foundAsset = found?.assets?.getOrNull(0)
val currentVersion = packageName?.let {
packageManager.getPackageInfo(it,
0)
}
val foundVersion = foundAsset?.name?.let { versionRegex.find(it) }
val shouldUpdate = if (found != null && foundAsset?.browser_download_url != "" && foundVersion != null) currentVersion?.versionName?.compareTo(
foundVersion.groupValues[2]
)!! < 0 else false
return if (foundVersion != null) {
Update(shouldUpdate, foundAsset.browser_download_url, foundVersion.groupValues[2], found.body)
} else {
Update(false, null, null, null)
}
}
private fun Activity.getPreReleaseUpdate(): Update {
// TODO: change url
val tagUrl = "https://api.github.com/repos/C10udburst/CloudStream-3/git/ref/tags/pre-release"
val releaseUrl = "https://api.github.com/repos/C10udburst/CloudStream-3/releases"
val headers = mapOf("Accept" to "application/vnd.github.v3+json")
val response =
mapper.readValue<List<GithubRelease>>(khttp.get(releaseUrl, headers = headers).text)
val found =
response.lastOrNull { rel ->
rel.prerelease
}
val foundAsset = found?.assets?.getOrNull(0)
val tagResponse =
mapper.readValue<GithubTag>(khttp.get(tagUrl, headers = headers).text)
val shouldUpdate = (getString(R.string.prerelease_commit_hash) != tagResponse.github_object.sha)
return if (foundAsset != null) {
Update(shouldUpdate, foundAsset.browser_download_url, tagResponse.github_object.sha, found.body)
} else {
Update(false, null, null, null)
}
}
private fun Activity.downloadUpdate(url: String): Boolean { private fun Activity.downloadUpdate(url: String): Boolean {
println("DOWNLOAD UPDATE $url") println("DOWNLOAD UPDATE $url")
var fullResume = false // IF FULL RESUME var fullResume = false // IF FULL RESUME

View file

@ -1,10 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" <vector android:height="24dp" android:tint="#FFFFFF"
android:width="24dp" android:viewportHeight="24" android:viewportWidth="24"
android:height="24dp" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
android:viewportWidth="24" <path android:fillColor="@android:color/white" android:pathData="M7,5h10v2h2L19,3c0,-1.1 -0.9,-1.99 -2,-1.99L7,1c-1.1,0 -2,0.9 -2,2v4h2L7,5zM15.41,16.59L20,12l-4.59,-4.59L14,8.83 17.17,12 14,15.17l1.41,1.42zM10,15.17L6.83,12 10,8.83 8.59,7.41 4,12l4.59,4.59L10,15.17zM17,19L7,19v-2L5,17v4c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2v-4h-2v2z"/>
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M7,5h10v2h2L19,3c0,-1.1 -0.9,-1.99 -2,-1.99L7,1c-1.1,0 -2,0.9 -2,2v4h2L7,5zM15.41,16.59L20,12l-4.59,-4.59L14,8.83 17.17,12 14,15.17l1.41,1.42zM10,15.17L6.83,12 10,8.83 8.59,7.41 4,12l4.59,4.59L10,15.17zM17,19L7,19v-2L5,17v4c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2v-4h-2v2z"/>
</vector> </vector>