mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
improve update checker
This commit is contained in:
parent
c56a6d872a
commit
599eafddf2
17
app/src/main/java/com/github/libretube/update/Asset.kt
Normal file
17
app/src/main/java/com/github/libretube/update/Asset.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package com.github.libretube.update
|
||||
|
||||
data class Asset(
|
||||
val browser_download_url: String,
|
||||
val content_type: String,
|
||||
val created_at: String,
|
||||
val download_count: Int,
|
||||
val id: Int,
|
||||
val label: Any,
|
||||
val name: String,
|
||||
val node_id: String,
|
||||
val size: Int,
|
||||
val state: String,
|
||||
val updated_at: String,
|
||||
val uploader: Uploader,
|
||||
val url: String
|
||||
)
|
22
app/src/main/java/com/github/libretube/update/Author.kt
Normal file
22
app/src/main/java/com/github/libretube/update/Author.kt
Normal file
@ -0,0 +1,22 @@
|
||||
package com.github.libretube.update
|
||||
|
||||
data class Author(
|
||||
val avatar_url: String,
|
||||
val events_url: String,
|
||||
val followers_url: String,
|
||||
val following_url: String,
|
||||
val gists_url: String,
|
||||
val gravatar_id: String,
|
||||
val html_url: String,
|
||||
val id: Int,
|
||||
val login: String,
|
||||
val node_id: String,
|
||||
val organizations_url: String,
|
||||
val received_events_url: String,
|
||||
val repos_url: String,
|
||||
val site_admin: Boolean,
|
||||
val starred_url: String,
|
||||
val subscriptions_url: String,
|
||||
val type: String,
|
||||
val url: String
|
||||
)
|
12
app/src/main/java/com/github/libretube/update/Reactions.kt
Normal file
12
app/src/main/java/com/github/libretube/update/Reactions.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package com.github.libretube.update
|
||||
|
||||
data class Reactions(
|
||||
val confused: Int,
|
||||
val eyes: Int,
|
||||
val heart: Int,
|
||||
val hooray: Int,
|
||||
val laugh: Int,
|
||||
val rocket: Int,
|
||||
val total_count: Int,
|
||||
val url: String
|
||||
)
|
24
app/src/main/java/com/github/libretube/update/UpdateInfo.kt
Normal file
24
app/src/main/java/com/github/libretube/update/UpdateInfo.kt
Normal file
@ -0,0 +1,24 @@
|
||||
package com.github.libretube.update
|
||||
|
||||
data class UpdateInfo(
|
||||
val assets: List<Asset>,
|
||||
val assets_url: String,
|
||||
val author: Author,
|
||||
val body: String,
|
||||
val created_at: String,
|
||||
val draft: Boolean,
|
||||
val html_url: String,
|
||||
val id: Int,
|
||||
val mentions_count: Int,
|
||||
val name: String,
|
||||
val node_id: String,
|
||||
val prerelease: Boolean,
|
||||
val published_at: String,
|
||||
val reactions: Reactions,
|
||||
val tag_name: String,
|
||||
val tarball_url: String,
|
||||
val target_commitish: String,
|
||||
val upload_url: String,
|
||||
val url: String,
|
||||
val zipball_url: String
|
||||
)
|
22
app/src/main/java/com/github/libretube/update/Uploader.kt
Normal file
22
app/src/main/java/com/github/libretube/update/Uploader.kt
Normal file
@ -0,0 +1,22 @@
|
||||
package com.github.libretube.update
|
||||
|
||||
data class Uploader(
|
||||
val avatar_url: String,
|
||||
val events_url: String,
|
||||
val followers_url: String,
|
||||
val following_url: String,
|
||||
val gists_url: String,
|
||||
val gravatar_id: String,
|
||||
val html_url: String,
|
||||
val id: Int,
|
||||
val login: String,
|
||||
val node_id: String,
|
||||
val organizations_url: String,
|
||||
val received_events_url: String,
|
||||
val repos_url: String,
|
||||
val site_admin: Boolean,
|
||||
val starred_url: String,
|
||||
val subscriptions_url: String,
|
||||
val type: String,
|
||||
val url: String
|
||||
)
|
@ -1,17 +1,16 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.util.Log
|
||||
import com.github.libretube.GITHUB_API_URL
|
||||
import com.github.libretube.obj.VersionInfo
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import com.github.libretube.update.UpdateInfo
|
||||
import com.google.gson.Gson
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.net.URL
|
||||
import javax.net.ssl.HttpsURLConnection
|
||||
|
||||
fun checkUpdate(): VersionInfo? {
|
||||
var versionInfo: VersionInfo? = VersionInfo("", "")
|
||||
var versionInfo: VersionInfo? = null
|
||||
// run http request as thread to make it async
|
||||
val thread = Thread {
|
||||
// otherwise crashes without internet
|
||||
@ -39,25 +38,11 @@ fun getUpdateInfo(): VersionInfo? {
|
||||
while (br.readLine().also { line = it } != null) json.append(line)
|
||||
|
||||
// Parse and return the json data
|
||||
val jsonRoot = JSONObject(json.toString())
|
||||
if (jsonRoot.has("tag_name") &&
|
||||
jsonRoot.has("html_url") &&
|
||||
jsonRoot.has("assets")
|
||||
) {
|
||||
val updateUrl = jsonRoot.getString("html_url")
|
||||
val jsonAssets: JSONArray = jsonRoot.getJSONArray("assets")
|
||||
val gson = Gson()
|
||||
val updateInfo = gson.fromJson(json.toString(), UpdateInfo::class.java)
|
||||
|
||||
for (i in 0 until jsonAssets.length()) {
|
||||
val jsonAsset = jsonAssets.getJSONObject(i)
|
||||
if (jsonAsset.has("name")) {
|
||||
val name = jsonAsset.getString("name")
|
||||
if (name.endsWith(".apk")) {
|
||||
val tagName = jsonRoot.getString("name")
|
||||
Log.i("VersionInfo", "Latest version: $tagName")
|
||||
return VersionInfo(updateUrl, tagName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
return VersionInfo(
|
||||
updateInfo.html_url,
|
||||
updateInfo.name
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user