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
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,23 @@ class DownloadedPlayerActivity : AppCompatActivity() {
enableEdgeToEdgeCompat()
setContentView(R.layout.empty_layout)
Log.i(TAG, "onCreate")

handleIntent(intent)
attachBackPressedCallback("DownloadedPlayerActivity") { finish() }

/**
* Use moveTaskToBack instead of finish() so there is always exactly one task
* entry in recents, always reflecting the current file.
*
* finish() destroys the Activity but may leave the task in recents. Each new file
* open can create a new task entry, so recents accumulates stale entries for old
* files. The user then taps a stale entry and gets the wrong file.
*
* moveTaskToBack keeps the Activity alive in the background. There is only ever
* one task entry in recents. New files opened from the file manager arrive via
* onNewIntent on the live instance, updating the player immediately. The single
* recents entry always reflects the current state, ensuring we load the
* correct file.
*/
attachBackPressedCallback("DownloadedPlayerActivity") { moveTaskToBack(true) }
}

private fun handleIntent(intent: Intent) {
Expand All @@ -83,11 +97,11 @@ class DownloadedPlayerActivity : AppCompatActivity() {
url != null -> playLink(this, url)
data != null -> playUri(this, data)
extraText != null -> playLink(this, extraText)
else -> { finish(); return }
else -> finishAndRemoveTask()
}
} else if (data?.scheme == "content") {
playUri(this, data)
} else finish()
} else finishAndRemoveTask()
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.core.content.ContextCompat.getString
import androidx.navigation.NavOptions
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.actions.temp.CloudStreamPackage
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
Expand All @@ -12,6 +13,15 @@ import com.lagradost.cloudstream3.utils.UIHelper.navigate
import com.lagradost.safefile.SafeFile

object OfflinePlaybackHelper {
/**
* Pop any existing player off the nav back stack before pushing the new one,
* keeping the stack flat (at most one player at a time). This prevents an
* OOM when many files are opened in sequence via DownloadedPlayerActivity.
*/
private val replacePlayerNavOptions = NavOptions.Builder()
.setPopUpTo(R.id.navigation_player, inclusive = true, saveState = false)
.build()

fun playLink(activity: Activity, url: String) {
activity.navigate(
R.id.global_to_navigation_player, GeneratorPlayer.newInstance(
Expand All @@ -20,7 +30,8 @@ object OfflinePlaybackHelper {
BasicLink(url)
), id = url.hashCode()
), 0
)
),
replacePlayerNavOptions
)
}

Expand Down Expand Up @@ -52,7 +63,8 @@ object OfflinePlaybackHelper {
subs,
if (id != -1) id else null,
), 0
)
),
replacePlayerNavOptions
)
return true
}
Expand All @@ -76,7 +88,8 @@ object OfflinePlaybackHelper {
)
)
), 0
)
),
replacePlayerNavOptions
)
}
}