custom downloader + typo

This commit is contained in:
Sarang S 2022-08-17 10:57:05 +05:30
parent 3f134dc0b2
commit f6067528e1
2 changed files with 86 additions and 58 deletions

View file

@ -8,9 +8,9 @@ plugins {
def tmpFilePath = System.getProperty("user.home") + "/work/_temp/keystore/" def tmpFilePath = System.getProperty("user.home") + "/work/_temp/keystore/"
def allFilesFromDir = new File(tmpFilePath).listFiles() def allFilesFromDir = new File(tmpFilePath).listFiles()
def prerelaseStoreFile = null def prereleaseStoreFile = null
if (allFilesFromDir != null) { if (allFilesFromDir != null) {
prerelaseStoreFile = allFilesFromDir.first() prereleaseStoreFile = allFilesFromDir.first()
} }
android { android {
@ -19,8 +19,8 @@ android {
} }
signingConfigs { signingConfigs {
prerelease { prerelease {
if (prerelaseStoreFile != null) { if (prereleaseStoreFile != null) {
storeFile = file(prerelaseStoreFile) storeFile = file(prereleaseStoreFile)
storePassword System.getenv("SIGNING_STORE_PASSWORD") storePassword System.getenv("SIGNING_STORE_PASSWORD")
keyAlias System.getenv("SIGNING_KEY_ALIAS") keyAlias System.getenv("SIGNING_KEY_ALIAS")
keyPassword System.getenv("SIGNING_KEY_PASSWORD") keyPassword System.getenv("SIGNING_KEY_PASSWORD")

View file

@ -1,18 +1,13 @@
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.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.net.Uri import android.net.Uri
import android.os.Environment
import android.util.Log 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.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.lagradost.cloudstream3.BuildConfig import com.lagradost.cloudstream3.BuildConfig
@ -23,9 +18,16 @@ 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 kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.*
import okio.BufferedSink
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
class InAppUpdater { class InAppUpdater {
companion object { companion object {
const val GITHUB_USER_NAME = "recloudstream" const val GITHUB_USER_NAME = "recloudstream"
@ -191,65 +193,91 @@ class InAppUpdater {
} }
} }
private fun Activity.downloadUpdate(url: String): Boolean { private fun Activity.downloadUpdate(url: String): Boolean {
Log.d(LOG_TAG, "Downloading update: ${url}")
val downloadManager = getSystemService<DownloadManager>()!! Log.d(LOG_TAG, "Downloading update: $url")
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 client = OkHttpClient()
val localContext = this val localContext = this
val request = Request.Builder()
.url(url)
.build()
val id = try { client.newCall(request).enqueue(object : Callback {
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() override fun onFailure(call: Call, e: okio.IOException) {
query.setFilterById(downloadId) e.printStackTrace()
val c = downloadManager.query(query) }
if (c.moveToFirst()) { override fun onResponse(call: Call, response: Response) {
val columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS) val downloadedFile = File(localContext.cacheDir, "CloudStream.apk")
if (DownloadManager.STATUS_SUCCESSFUL == c val sink: BufferedSink = downloadedFile.sink().buffer()
.getInt(columnIndex) sink.writeAll(response.body.source())
) { sink.close()
c.getColumnIndex(DownloadManager.COLUMN_MEDIAPROVIDER_URI) openApk(localContext, Uri.fromFile(downloadedFile))
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 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)
// )
fun openApk(context: Context, uri: Uri) { fun openApk(context: Context, uri: Uri) {
try { try {
uri.path?.let { uri.path?.let {