mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
mutex lock downloading, using app client
This commit is contained in:
parent
f6067528e1
commit
48f080c843
1 changed files with 13 additions and 75 deletions
|
@ -8,6 +8,7 @@ import android.util.Log
|
|||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
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.normalSafeApiCall
|
||||
import com.lagradost.cloudstream3.utils.AppUtils.parseJson
|
||||
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import okhttp3.*
|
||||
import okio.BufferedSink
|
||||
import okio.Okio
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
import okio.*
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
|
||||
|
@ -199,84 +199,22 @@ class InAppUpdater {
|
|||
|
||||
Log.d(LOG_TAG, "Downloading update: $url")
|
||||
|
||||
val client = OkHttpClient()
|
||||
val localContext = this
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.build()
|
||||
|
||||
client.newCall(request).enqueue(object : Callback {
|
||||
|
||||
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 downloadedFile = File.createTempFile("CloudStream",".apk")
|
||||
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()
|
||||
openApk(localContext, Uri.fromFile(downloadedFile))
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
// Log.d(LOG_TAG, "Downloading update: ${url}")
|
||||
//
|
||||
// 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)
|
||||
// )
|
||||
return true
|
||||
}
|
||||
|
||||
fun openApk(context: Context, uri: Uri) {
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue