2020-07-15 14:52:31 +00:00
|
|
|
package com.vanced.manager.utils
|
|
|
|
|
2021-02-05 09:07:05 +00:00
|
|
|
import android.content.ActivityNotFoundException
|
2020-07-15 14:52:31 +00:00
|
|
|
import android.content.Context
|
2020-09-29 10:47:38 +00:00
|
|
|
import android.content.Intent
|
2020-07-15 14:52:31 +00:00
|
|
|
import android.net.Uri
|
2020-09-29 10:47:38 +00:00
|
|
|
import android.os.Build
|
|
|
|
import androidx.core.content.FileProvider
|
2020-07-15 14:52:31 +00:00
|
|
|
import com.vanced.manager.R
|
2021-01-16 14:36:46 +00:00
|
|
|
import com.vanced.manager.library.network.providers.createService
|
2021-02-03 18:24:28 +00:00
|
|
|
import com.vanced.manager.utils.AppUtils.log
|
2020-10-31 19:45:39 +00:00
|
|
|
import com.vanced.manager.utils.AppUtils.sendCloseDialog
|
2020-11-23 16:00:41 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2020-09-29 10:47:38 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2020-11-23 16:00:41 +00:00
|
|
|
import kotlinx.coroutines.launch
|
2021-01-16 14:36:46 +00:00
|
|
|
import okhttp3.ResponseBody
|
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
|
|
|
import retrofit2.Response
|
|
|
|
import retrofit2.http.GET
|
|
|
|
import retrofit2.http.Streaming
|
|
|
|
import retrofit2.http.Url
|
|
|
|
import java.io.*
|
2020-07-15 14:52:31 +00:00
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
|
2020-07-15 14:52:31 +00:00
|
|
|
|
2021-01-16 14:36:46 +00:00
|
|
|
interface DownloadHelper {
|
|
|
|
|
|
|
|
@Streaming
|
|
|
|
@GET
|
|
|
|
fun download(@Url url: String): Call<ResponseBody>
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fun download(
|
|
|
|
url: String,
|
|
|
|
baseUrl: String,
|
|
|
|
fileFolder: String,
|
|
|
|
fileName: String,
|
|
|
|
context: Context,
|
2021-02-15 15:49:01 +00:00
|
|
|
onDownloadComplete: () -> Unit = {},
|
|
|
|
onError: (error: String) -> Unit = {}
|
2021-01-16 14:36:46 +00:00
|
|
|
) {
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadingFile.postValue(context.getString(R.string.downloading_file, fileName))
|
2021-01-16 14:36:46 +00:00
|
|
|
val downloadInterface = createService(DownloadHelper::class, baseUrl)
|
|
|
|
val download = downloadInterface.download(url)
|
2021-02-16 08:26:40 +00:00
|
|
|
currentDownload = download
|
2021-01-16 14:36:46 +00:00
|
|
|
download.enqueue(object : Callback<ResponseBody> {
|
|
|
|
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
|
|
|
|
if (response.isSuccessful) {
|
2021-02-14 09:57:48 +00:00
|
|
|
launch {
|
2021-01-16 14:36:46 +00:00
|
|
|
if (response.body()?.let { writeFile(it, context.getExternalFilesDir(fileFolder)?.path + "/" + fileName) } == true) {
|
|
|
|
onDownloadComplete()
|
|
|
|
} else {
|
|
|
|
onError("Could not save file")
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadProgress.postValue(0)
|
2021-02-22 16:00:11 +00:00
|
|
|
log("VMDownloader", "Failed to save file: $url\n${response.errorBody()}")
|
2021-01-16 14:36:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-22 16:00:11 +00:00
|
|
|
val errorBody = response.errorBody().toString()
|
|
|
|
onError(errorBody)
|
2021-02-16 11:00:36 +00:00
|
|
|
downloadProgress.postValue(0)
|
2021-02-22 16:00:11 +00:00
|
|
|
log("VMDownloader", "Failed to download file: $url\n$errorBody")
|
2021-01-14 19:48:31 +00:00
|
|
|
}
|
2021-01-16 14:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
|
|
|
|
if (call.isCanceled) {
|
2021-02-03 18:24:28 +00:00
|
|
|
log("VMDownloader", "Download canceled")
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadProgress.postValue(0)
|
2021-01-16 14:36:46 +00:00
|
|
|
} else {
|
|
|
|
onError(t.stackTraceToString())
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadProgress.postValue(0)
|
2021-02-03 18:24:28 +00:00
|
|
|
log("VMDownloader", "Failed to download file: $url")
|
2021-01-14 19:48:31 +00:00
|
|
|
}
|
2021-01-16 14:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fun writeFile(body: ResponseBody, filePath: String): Boolean {
|
|
|
|
return try {
|
|
|
|
val file = File(filePath)
|
|
|
|
val totalBytes = body.contentLength()
|
|
|
|
var inputStream: InputStream? = null
|
|
|
|
var outputStream: OutputStream? = null
|
|
|
|
try {
|
|
|
|
val fileReader = ByteArray(4096)
|
|
|
|
var downloadedBytes: Long = 0
|
|
|
|
inputStream = body.byteStream()
|
|
|
|
outputStream = FileOutputStream(file)
|
|
|
|
var read: Int
|
|
|
|
while (inputStream.read(fileReader).also { read = it } != -1) {
|
|
|
|
outputStream.write(fileReader, 0, read)
|
|
|
|
downloadedBytes += read.toLong()
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadProgress.postValue((downloadedBytes * 100 / totalBytes).toInt())
|
2021-01-14 19:48:31 +00:00
|
|
|
}
|
2021-01-16 14:36:46 +00:00
|
|
|
outputStream.flush()
|
|
|
|
true
|
|
|
|
} catch (e: IOException) {
|
|
|
|
false
|
|
|
|
} finally {
|
|
|
|
inputStream?.close()
|
|
|
|
outputStream?.close()
|
|
|
|
}
|
|
|
|
} catch (e: IOException) {
|
|
|
|
false
|
2021-01-14 19:48:31 +00:00
|
|
|
}
|
2020-07-15 14:52:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
fun downloadManager(context: Context) {
|
|
|
|
val url = "https://github.com/YTVanced/VancedManager/releases/latest/download/manager.apk"
|
2021-01-17 13:15:57 +00:00
|
|
|
download(url,"https://github.com/YTVanced/VancedManager/", "manager", "manager.apk", context, onDownloadComplete = {
|
2020-11-23 16:00:41 +00:00
|
|
|
val apk = File("${context.getExternalFilesDir("manager")?.path}/manager.apk")
|
|
|
|
val uri =
|
2020-12-13 11:54:52 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
FileProvider.getUriForFile(context, "${context.packageName}.provider", apk)
|
|
|
|
else
|
|
|
|
Uri.fromFile(apk)
|
2020-09-29 10:47:38 +00:00
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
val intent = Intent(Intent.ACTION_VIEW)
|
|
|
|
intent.setDataAndType(uri, "application/vnd.android.package-archive")
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
2021-02-05 09:07:05 +00:00
|
|
|
try {
|
|
|
|
context.startActivity(intent)
|
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
|
|
log("VMDownloader", e.stackTraceToString())
|
|
|
|
} finally {
|
|
|
|
sendCloseDialog(context)
|
|
|
|
}
|
2020-11-23 16:00:41 +00:00
|
|
|
}, onError = {
|
2021-02-16 08:26:40 +00:00
|
|
|
downloadingFile.postValue(
|
2021-01-16 14:36:46 +00:00
|
|
|
context.getString(
|
|
|
|
R.string.error_downloading,
|
|
|
|
"manager.apk"
|
|
|
|
)
|
|
|
|
)
|
2020-11-23 16:00:41 +00:00
|
|
|
})
|
|
|
|
}
|
2020-09-29 10:47:38 +00:00
|
|
|
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|