Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.annotation.MainThread
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.core.view.children
Expand Down Expand Up @@ -90,6 +91,7 @@ import com.lagradost.cloudstream3.plugins.PluginManager
import com.lagradost.cloudstream3.plugins.PluginManager.___DO_NOT_CALL_FROM_A_PLUGIN_loadAllOnlinePlugins
import com.lagradost.cloudstream3.plugins.PluginManager.loadSinglePlugin
import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver
import com.lagradost.cloudstream3.services.PackageInstallerService
import com.lagradost.cloudstream3.services.SubscriptionWorkManager
import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.APP_STRING
Expand Down Expand Up @@ -651,10 +653,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa
override fun onPause() {
super.onPause()

// Start any delayed updates
if (ApkInstaller.delayedInstaller?.startInstallation() == true) {
Toast.makeText(this, R.string.update_started, Toast.LENGTH_LONG).show()
}
try {
if (isCastApiAvailable()) {
mSessionManager?.removeSessionManagerListener(mSessionManagerListener)
Expand Down Expand Up @@ -709,6 +707,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa
}

override fun onDestroy() {
if (ApkInstaller.hasDelayedInstaller()) {
val intent = PackageInstallerService.getDelayedInstallIntent(this)
ContextCompat.startForegroundService(this, intent)
}
filesToDelete.forEach { path ->
val result = File(path).deleteRecursively()
if (result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lagradost.cloudstream3.services

import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -121,6 +122,7 @@ class PackageInstallerService : Service() {

val newNotification = baseNotification
.setContentTitle(getString(text))
.setOngoing(state != ApkInstaller.InstallProgressStatus.Failed)
.apply {
if (state == ApkInstaller.InstallProgressStatus.Failed) {
setSmallIcon(R.drawable.rderror)
Expand All @@ -143,10 +145,56 @@ class PackageInstallerService : Service() {
notificationManager.notify(id, newNotification)
}

private fun updateNotificationDelayed() {
val flags = when {
SDK_INT >= 23 -> PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else -> PendingIntent.FLAG_UPDATE_CURRENT
}
val updateIntent = getDelayedInstallIntent(this)
val updatePendingIntent = PendingIntent.getService(this, 0, updateIntent, flags)

val newNotification = baseNotification
.setContentTitle(getString(R.string.delayed_update_notice))
.setProgress(0, 0, false)
.setOngoing(true)
.addAction(
R.drawable.ic_baseline_system_update_24,
getString(R.string.update),
updatePendingIntent
)
.build()

val notificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager

notificationManager.notify(UPDATE_NOTIFICATION_ID, newNotification)
}

private suspend fun startDelayedInstallation() {
if (ApkInstaller.startDelayedInstallation()) {
updateNotificationProgress(0f, ApkInstaller.InstallProgressStatus.Installing)
delay(10_000)
} else {
updateNotificationProgress(0f, ApkInstaller.InstallProgressStatus.Failed)
}
stopSelf()
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.action == ACTION_START_DELAYED_INSTALL) {
ioSafe {
startDelayedInstallation()
}
return START_NOT_STICKY
}

val url = intent?.getStringExtra(EXTRA_URL) ?: return START_NOT_STICKY
ioSafe {
downloadUpdate(url)
if (ApkInstaller.hasDelayedInstaller()) {
updateNotificationDelayed()
return@ioSafe
}
// Close the service after the update is done
// If no sleep then the install prompt may not appear and the notification
// will disappear instantly
Expand All @@ -172,6 +220,8 @@ class PackageInstallerService : Service() {

companion object {
private const val EXTRA_URL = "EXTRA_URL"
private const val ACTION_START_DELAYED_INSTALL =
"com.lagradost.cloudstream3.action.START_DELAYED_INSTALL"

const val UPDATE_CHANNEL_ID = "cloudstream3.updates"
const val UPDATE_CHANNEL_NAME = "App Updates"
Expand All @@ -185,5 +235,10 @@ class PackageInstallerService : Service() {
return Intent(context, PackageInstallerService::class.java)
.putExtra(EXTRA_URL, url)
}

fun getDelayedInstallIntent(context: Context): Intent {
return Intent(context, PackageInstallerService::class.java)
.setAction(ACTION_START_DELAYED_INSTALL)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class ApkInstaller(private val service: PackageInstallerService) {
var delayedInstaller: DelayedInstaller? = null
private var isReceiverRegistered = false
private const val TAG = "ApkInstaller"

fun hasDelayedInstaller(): Boolean = delayedInstaller != null

fun startDelayedInstallation(): Boolean {
return delayedInstaller?.startInstallation() == true
}
}

inner class DelayedInstaller(
Expand Down