InAppUpdater update

This commit is contained in:
LagradOst 2021-08-29 19:29:00 +02:00
parent 515e746626
commit 7b62788f0b

View file

@ -1,13 +1,17 @@
package com.lagradost.cloudstream3.utils package com.lagradost.cloudstream3.utils
import android.app.Activity import android.app.Activity
import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Environment
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.content.getSystemService
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.DeserializationFeature
@ -17,13 +21,9 @@ import com.fasterxml.jackson.module.kotlin.readValue
import com.lagradost.cloudstream3.BuildConfig import com.lagradost.cloudstream3.BuildConfig
import com.lagradost.cloudstream3.MainActivity.Companion.showToast import com.lagradost.cloudstream3.MainActivity.Companion.showToast
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import java.io.* import java.io.File
import java.net.URL
import java.net.URLConnection
import kotlin.concurrent.thread import kotlin.concurrent.thread
const val UPDATE_TIME = 1000
class InAppUpdater { class InAppUpdater {
companion object { companion object {
// === IN APP UPDATER === // === IN APP UPDATER ===
@ -145,129 +145,66 @@ class InAppUpdater {
} }
private fun Activity.downloadUpdate(url: String): Boolean { private fun Activity.downloadUpdate(url: String): Boolean {
println("DOWNLOAD UPDATE $url") val downloadManager = getSystemService<DownloadManager>()!!
var fullResume = false // IF FULL RESUME
try {
// =================== DOWNLOAD POSTERS AND SETUP PATH ===================
val path = filesDir.toString() +
"/Download/apk/update.apk"
// =================== MAKE DIRS =================== val request = DownloadManager.Request(Uri.parse(url))
val rFile = File(path) .setMimeType("application/vnd.android.package-archive")
try { .setTitle("CloudStream update")
rFile.parentFile?.mkdirs() .setDestinationInExternalPublicDir(
} catch (_ex: Exception) { Environment.DIRECTORY_DOWNLOADS,
println("FAILED:::$_ex") "CloudStream.apk"
} )
val url = url.replace(" ", "%20") .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(true)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val _url = URL(url) val localContext = this
val connection: URLConnection = _url.openConnection() val id = downloadManager.enqueue(request)
registerReceiver(
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val downloadId = intent?.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, id
) ?: id
var bytesRead = 0L val query = DownloadManager.Query()
query.setFilterById(downloadId)
val c = downloadManager.query(query)
// =================== STORAGE =================== if (c.moveToFirst()) {
try { val columnIndex = c
if (!rFile.exists()) { .getColumnIndex(DownloadManager.COLUMN_STATUS)
rFile.createNewFile() if (DownloadManager.STATUS_SUCCESSFUL == c
} else { .getInt(columnIndex)
rFile.delete() ) {
rFile.createNewFile() c.getColumnIndex(DownloadManager.COLUMN_MEDIAPROVIDER_URI)
val uri = Uri.parse(
c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
)
openApk(localContext, uri)
}
}
} }
} catch (e: Exception) { }, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
println(e) )
runOnUiThread { return true
showToast(this, "Permission error", Toast.LENGTH_SHORT) }
}
return false fun openApk(context: Context, uri: Uri) {
uri.path?.let {
val contentUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".provider",
File(it)
)
val installIntent = Intent(Intent.ACTION_VIEW).apply {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
data = contentUri
} }
context.startActivity(installIntent)
// =================== CONNECTION ===================
connection.setRequestProperty("Accept-Encoding", "identity")
connection.connectTimeout = 10000
var clen = 0
try {
connection.connect()
clen = connection.contentLength
println("CONTENTN LENGTH: $clen")
} catch (_ex: Exception) {
println("CONNECT:::$_ex")
_ex.printStackTrace()
}
// =================== VALIDATE ===================
if (clen < 5000000) { // min of 5 MB
clen = 0
}
if (clen <= 0) { // TO SMALL OR INVALID
//showNot(0, 0, 0, DownloadType.IsFailed, info)
return false
}
// =================== SETUP VARIABLES ===================
//val bytesTotal: Long = (clen + bytesRead.toInt()).toLong()
val input: InputStream = BufferedInputStream(connection.inputStream)
val output: OutputStream = FileOutputStream(rFile, false)
var bytesPerSec = 0L
val buffer = ByteArray(1024)
var count: Int
//var lastUpdate = System.currentTimeMillis()
while (true) {
try {
count = input.read(buffer)
if (count < 0) break
bytesRead += count
bytesPerSec += count
output.write(buffer, 0, count)
} catch (_ex: Exception) {
println("CONNECT TRUE:::$_ex")
_ex.printStackTrace()
fullResume = true
break
}
}
if (fullResume) { // IF FULL RESUME DELETE CURRENT AND DONT SHOW DONE
with(NotificationManagerCompat.from(this)) {
cancel(-1)
}
}
output.flush()
output.close()
input.close()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val contentUri = FileProvider.getUriForFile(
this,
BuildConfig.APPLICATION_ID + ".provider",
rFile
)
val install = Intent(Intent.ACTION_VIEW)
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
install.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
install.data = contentUri
startActivity(install)
return true
} else {
val apkUri = Uri.fromFile(rFile)
val install = Intent(Intent.ACTION_VIEW)
install.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
install.setDataAndType(
apkUri,
"application/vnd.android.package-archive"
)
startActivity(install)
return true
}
} catch (_ex: Exception) {
println("FATAL EX DOWNLOADING:::$_ex")
return false
} }
} }