Skip to content

Commit

Permalink
Improve Intent behavior of PlayerActivity (#181)
Browse files Browse the repository at this point in the history
* fix: improve Intent behavior of PlayerActivity

* fix: action value

* fix: update action value
  • Loading branch information
s12f authored Dec 21, 2024
1 parent 8a5e48e commit 1e8969b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class PlayerActivity : AppCompatActivity() {
setupAudio()
setupMediaSession()
getPlayableUri(intent)?.let(player::playFile)
setIntentExtras(intent.extras)
setOrientation()

binding.controls.setContent {
Expand Down Expand Up @@ -390,6 +389,20 @@ class PlayerActivity : AppCompatActivity() {
}
player.timePos = extras.getInt("position", 0) / 1000

// subtitles
if (extras.containsKey("subs")) {
val subList = Utils.getParcelableArray<Uri>(extras, "subs")
val subsToEnable = Utils.getParcelableArray<Uri>(extras, "subs.enable")

for (suburi in subList) {
val subfile = suburi.resolveUri(this) ?: continue
val flag = if (subsToEnable.any { it == suburi }) "select" else "auto"

Log.v(TAG, "Adding subtitles from intent extras: $subfile")
MPVLib.command(arrayOf("sub-add", subfile, flag))
}
}

extras.getStringArray("headers")?.let { headers ->
if (headers[0].startsWith("User-Agent", true)) MPVLib.setPropertyString("user-agent", headers[1])
val headersString = headers.asSequence().drop(2).chunked(2).associate { it[0] to it[1] }
Expand Down Expand Up @@ -527,6 +540,7 @@ class PlayerActivity : AppCompatActivity() {
when (eventId) {
MPVLib.mpvEventId.MPV_EVENT_FILE_LOADED -> {
fileName = getFileName(intent)
setIntentExtras(intent.extras)
viewModel.mediaTitle.update {
val mediaTitle = MPVLib.getPropertyString("media-title")
if (mediaTitle.isBlank() || mediaTitle.isDigitsOnly()) fileName else mediaTitle
Expand Down Expand Up @@ -597,7 +611,7 @@ class PlayerActivity : AppCompatActivity() {
Log.d(TAG, "setting return intent")
setResult(
RESULT_OK,
Intent().apply {
Intent(RESULT_INTENT).apply {
player.timePos?.let { putExtra("position", it * 1000) }
player.duration?.let { putExtra("duration", it * 1000) }
},
Expand Down Expand Up @@ -817,6 +831,11 @@ class PlayerActivity : AppCompatActivity() {
registerReceiver(noisyReceiver, filter)
noisyReceiver.initialized = true
}

companion object {
// action of result intent
private const val RESULT_INTENT = "live.mehiz.mpvkt.ui.player.PlayerActivity.result"
}
}

const val TAG = "mpvKt"

0 comments on commit 1e8969b

Please sign in to comment.