mutex lock downloading, using app client

This commit is contained in:
Sarang S 2022-08-17 17:48:22 +05:30
parent f6067528e1
commit 48f080c843

View file

@ -8,6 +8,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.lifecycle.MutableLiveData
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.BuildConfig import com.lagradost.cloudstream3.BuildConfig
@ -17,14 +18,13 @@ import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.* import okhttp3.*
import okio.BufferedSink import okio.*
import okio.Okio
import okio.buffer
import okio.sink
import java.io.File import java.io.File
import java.io.IOException
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -199,84 +199,22 @@ class InAppUpdater {
Log.d(LOG_TAG, "Downloading update: $url") Log.d(LOG_TAG, "Downloading update: $url")
val client = OkHttpClient()
val localContext = this val localContext = this
val request = Request.Builder()
.url(url)
.build()
client.newCall(request).enqueue(object : Callback { val downloadedFile = File.createTempFile("CloudStream",".apk")
override fun onFailure(call: Call, e: okio.IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
val downloadedFile = File(localContext.cacheDir, "CloudStream.apk")
val sink: BufferedSink = downloadedFile.sink().buffer() val sink: BufferedSink = downloadedFile.sink().buffer()
sink.writeAll(response.body.source())
val updateLock = Mutex()
ioSafe {
updateLock.withLock {
sink.writeAll(app.get(url).body.source() )
sink.close() sink.close()
openApk(localContext, Uri.fromFile(downloadedFile)) openApk(localContext, Uri.fromFile(downloadedFile))
} }
})
return true
} }
// Log.d(LOG_TAG, "Downloading update: ${url}") return true
// }
// val downloadManager = getSystemService<DownloadManager>()!!
//
// val request = DownloadManager.Request(Uri.parse(url))
// .setMimeType("application/vnd.android.package-archive")
// .setTitle("CloudStream Update")
// .setDestinationInExternalPublicDir(
// Environment.DIRECTORY_DOWNLOADS,
// "CloudStream.apk"
// )
// .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
// .setAllowedOverRoaming(true)
// .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
//
// val localContext = this
//
// val id = try {
// downloadManager.enqueue(request)
// } catch (e: Exception) {
// logError(e)
// showToast(this, R.string.storage_error, Toast.LENGTH_SHORT)
// -1
// }
// if (id == -1L) return true
// registerReceiver(
// object : BroadcastReceiver() {
// override fun onReceive(context: Context?, intent: Intent?) {
// try {
// val downloadId = intent?.getLongExtra(
// DownloadManager.EXTRA_DOWNLOAD_ID, id
// ) ?: id
//
// val query = DownloadManager.Query()
// query.setFilterById(downloadId)
// val c = downloadManager.query(query)
//
// if (c.moveToFirst()) {
// val columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS)
// if (DownloadManager.STATUS_SUCCESSFUL == c
// .getInt(columnIndex)
// ) {
// c.getColumnIndex(DownloadManager.COLUMN_MEDIAPROVIDER_URI)
// val uri = Uri.parse(
// c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
// )
// openApk(localContext, uri)
// }
// }
// } catch (e: Exception) {
// logError(e)
// }
// }
// }, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
// )
fun openApk(context: Context, uri: Uri) { fun openApk(context: Context, uri: Uri) {
try { try {