Use coroutine

This commit is contained in:
PabloG02 2023-01-12 01:04:38 +01:00
parent b2de510b2b
commit d6eab932a7

View File

@ -18,12 +18,14 @@ import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity import androidx.core.content.ContextCompat.startActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONTokener import org.json.JSONTokener
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.net.URL import java.net.URL
import java.util.concurrent.Executors
class AppUpdater : BroadcastReceiver() { class AppUpdater : BroadcastReceiver() {
private var downloadID = 0L private var downloadID = 0L
@ -43,7 +45,7 @@ class AppUpdater : BroadcastReceiver() {
} }
} }
fun downloadApk(applicationContext : Context, uri : Uri){ fun downloadApk(applicationContext : Context, uri : Uri) {
val downloadPath = applicationContext.getPublicFilesDir().canonicalPath val downloadPath = applicationContext.getPublicFilesDir().canonicalPath
val file = File(downloadPath, "skyline.apk") val file = File(downloadPath, "skyline.apk")
@ -60,31 +62,31 @@ class AppUpdater : BroadcastReceiver() {
} }
init { init {
if (File(SkylineApplication.instance.getPublicFilesDir().canonicalPath+"/skyline.apk").exists()) { if (File(SkylineApplication.instance.getPublicFilesDir().canonicalPath + "/skyline.apk").exists()) {
File(SkylineApplication.instance.getPublicFilesDir().canonicalPath+"/skyline.apk").delete() File(SkylineApplication.instance.getPublicFilesDir().canonicalPath + "/skyline.apk").delete()
} }
} }
companion object { companion object {
private const val baseUrl = "https://skyline-builds.alula.gay" private const val baseUrl = "https://skyline-builds.alula.gay"
private const val branch = "ftx1" private const val branch = "ftx1"
@JvmStatic @JvmStatic
fun checkForUpdates(applicationContext : Context) { fun checkForUpdates(applicationContext : Context) {
val myExecutor = Executors.newSingleThreadExecutor()
val myHandler = Handler(Looper.getMainLooper()) val myHandler = Handler(Looper.getMainLooper())
val builder = AlertDialog.Builder(applicationContext) val builder = AlertDialog.Builder(applicationContext)
val url = URL("$baseUrl/builds") val url = URL("$baseUrl/builds")
myExecutor.execute { CoroutineScope(Dispatchers.IO).launch {
try { try {
val response = url.readText() val response = url.readText()
val jsonBuilds = JSONTokener(response).nextValue() as JSONArray val jsonBuilds = JSONTokener(response).nextValue() as JSONArray
var ftx1Index = 0 var ftx1Index = 0
while(ftx1Index < jsonBuilds.length() && !jsonBuilds.getJSONObject(ftx1Index).get("branch").equals(branch)){ while (ftx1Index < jsonBuilds.length() && !jsonBuilds.getJSONObject(ftx1Index).get("branch").equals(branch)) {
ftx1Index++ ftx1Index++
} }
if(ftx1Index >= jsonBuilds.length()) if (ftx1Index >= jsonBuilds.length())
ftx1Index = 0 ftx1Index = 0
val remoteBuildGitHash = jsonBuilds.getJSONObject(ftx1Index).getJSONObject("commit").getString("id") val remoteBuildGitHash = jsonBuilds.getJSONObject(ftx1Index).getJSONObject("commit").getString("id")