2020-07-15 14:52:31 +00:00
|
|
|
package com.vanced.manager.utils
|
|
|
|
|
|
|
|
import android.app.DownloadManager
|
|
|
|
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 android.util.Log
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.core.content.FileProvider
|
2020-11-05 16:35:49 +00:00
|
|
|
import androidx.core.content.getSystemService
|
|
|
|
import androidx.core.net.toUri
|
2020-10-31 19:45:39 +00:00
|
|
|
import androidx.databinding.ObservableField
|
2020-09-29 10:47:38 +00:00
|
|
|
import com.downloader.OnDownloadListener
|
|
|
|
import com.downloader.PRDownloader
|
2020-07-15 14:52:31 +00:00
|
|
|
import com.vanced.manager.R
|
2020-10-31 19:45:39 +00:00
|
|
|
import com.vanced.manager.model.ProgressModel
|
|
|
|
import com.vanced.manager.utils.AppUtils.sendCloseDialog
|
2020-09-29 10:47:38 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2020-11-15 00:26:07 +00:00
|
|
|
import kotlinx.coroutines.withContext
|
2020-09-29 10:47:38 +00:00
|
|
|
import java.io.File
|
2020-07-15 14:52:31 +00:00
|
|
|
|
|
|
|
object DownloadHelper {
|
|
|
|
|
|
|
|
fun download(url: String, dir: String, child: String, context: Context): Long {
|
2020-11-05 16:35:49 +00:00
|
|
|
val request = DownloadManager.Request(url.toUri()).apply {
|
2020-07-15 14:52:31 +00:00
|
|
|
setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
|
|
|
|
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
|
|
|
|
setTitle(context.getString(R.string.downloading_file, child))
|
2020-07-22 13:33:45 +00:00
|
|
|
setDestinationInExternalFilesDir(context, dir, child)
|
2020-07-15 14:52:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 16:35:49 +00:00
|
|
|
val downloadManager = context.getSystemService<DownloadManager>()!!
|
2020-07-15 14:52:31 +00:00
|
|
|
return downloadManager.enqueue(request)
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
val downloadProgress = ObservableField<ProgressModel>()
|
|
|
|
|
|
|
|
init {
|
|
|
|
downloadProgress.set(ProgressModel())
|
|
|
|
}
|
|
|
|
|
2020-11-15 00:26:07 +00:00
|
|
|
suspend fun downloadManager(context: Context) =
|
|
|
|
withContext(Dispatchers.IO) {
|
2020-09-29 10:47:38 +00:00
|
|
|
val url = "https://github.com/YTVanced/VancedManager/releases/latest/download/manager.apk"
|
2020-11-13 16:03:11 +00:00
|
|
|
downloadProgress.get()?.currentDownload = PRDownloader.download(url, context.getExternalFilesDir("manager")?.path, "manager.apk")
|
2020-10-31 19:45:39 +00:00
|
|
|
.build()
|
|
|
|
.setOnProgressListener { progress ->
|
|
|
|
val mProgress = progress.currentBytes * 100 / progress.totalBytes
|
|
|
|
downloadProgress.get()?.downloadProgress?.set(mProgress.toInt())
|
|
|
|
}
|
2020-11-13 16:03:11 +00:00
|
|
|
.setOnCancelListener {
|
|
|
|
downloadProgress.get()?.downloadProgress?.set(0)
|
|
|
|
}
|
2020-10-31 19:45:39 +00:00
|
|
|
.start(object : OnDownloadListener {
|
|
|
|
override fun onDownloadComplete() {
|
|
|
|
val apk =
|
2020-11-13 16:03:11 +00:00
|
|
|
File("${context.getExternalFilesDir("manager")?.path}/manager.apk")
|
2020-10-31 19:45:39 +00:00
|
|
|
val uri =
|
|
|
|
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-10-31 19:45:39 +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)
|
|
|
|
context.startActivity(intent)
|
|
|
|
sendCloseDialog(context)
|
|
|
|
}
|
2020-09-29 10:47:38 +00:00
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
override fun onError(error: com.downloader.Error?) {
|
|
|
|
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show()
|
|
|
|
Log.e("VMUpgrade", error.toString())
|
|
|
|
}
|
2020-09-29 10:47:38 +00:00
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
})
|
2020-09-29 10:47:38 +00:00
|
|
|
}
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|