Skip to content

Commit

Permalink
Merge pull request #4556 from Isira-Seneviratne/Enum_serializable
Browse files Browse the repository at this point in the history
refactor: Simplify player event handling
  • Loading branch information
Isira-Seneviratne authored Aug 20, 2023
2 parents 2b33de7 + 63fde62 commit 0e8e627
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
21 changes: 8 additions & 13 deletions app/src/main/java/com/github/libretube/enums/PlayerEvent.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.github.libretube.enums

enum class PlayerEvent(val value: Int) {
Pause(0),
Play(1),
Forward(2),
Rewind(3),
Next(5),
Prev(6),
Background(7)
;

companion object {
fun fromInt(value: Int) = PlayerEvent.values().first { it.value == value }
}
enum class PlayerEvent {
Pause,
Play,
Forward,
Rewind,
Next,
Prev,
Background
}
17 changes: 15 additions & 2 deletions app/src/main/java/com/github/libretube/extensions/Intent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ package com.github.libretube.extensions

import android.content.Intent
import android.os.Parcelable
import androidx.annotation.OptIn
import androidx.core.content.IntentCompat
import androidx.core.os.BuildCompat
import java.io.Serializable

inline fun <reified T : Parcelable> Intent.parcelableExtra(key: String?): T? {
return IntentCompat.getParcelableExtra(this, key, T::class.java)
inline fun <reified T : Parcelable> Intent.parcelableExtra(name: String?): T? {
return IntentCompat.getParcelableExtra(this, name, T::class.java)
}

@OptIn(BuildCompat.PrereleaseSdkCheck::class)
inline fun <reified T : Serializable> Intent.serializableExtra(name: String?): T? {
return if (BuildCompat.isAtLeastU()) {
getSerializableExtra(name, T::class.java)
} else {
@Suppress("DEPRECATION")
getSerializableExtra(name) as? T
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ object PlayerHelper {
}
}

fun getIntentActon(context: Context): String {
fun getIntentAction(context: Context): String {
return context.packageName + "." + ACTION_MEDIA_CONTROL
}

private fun getPendingIntent(activity: Activity, code: Int): PendingIntent {
val intent = Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code)
return PendingIntentCompat.getBroadcast(activity, code, intent, 0, false)
private fun getPendingIntent(activity: Activity, event: PlayerEvent): PendingIntent {
val intent = Intent(getIntentAction(activity)).putExtra(CONTROL_TYPE, event)
return PendingIntentCompat.getBroadcast(activity, event.ordinal, intent, 0, false)
}

private fun getRemoteAction(
Expand All @@ -370,7 +370,7 @@ object PlayerHelper {
IconCompat.createWithResource(activity, id),
text,
text,
getPendingIntent(activity, event.value)
getPendingIntent(activity, event)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.parcelable
import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.serializableExtra
import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher
Expand Down Expand Up @@ -204,9 +205,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
* Receiver for all actions in the PiP mode
*/
private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.getIntExtra(PlayerHelper.CONTROL_TYPE, 0) ?: return
when (PlayerEvent.fromInt(action)) {
override fun onReceive(context: Context, intent: Intent) {
when (intent.serializableExtra<PlayerEvent>(PlayerHelper.CONTROL_TYPE) ?: return) {
PlayerEvent.Play -> {
exoPlayer.play()
}
Expand Down Expand Up @@ -252,7 +252,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// broadcast receiver for PiP actions
context?.registerReceiver(
broadcastReceiver,
IntentFilter(PlayerHelper.getIntentActon(requireContext()))
IntentFilter(PlayerHelper.getIntentAction(requireContext()))
)

// schedule task to save the watch position each second
Expand Down

0 comments on commit 0e8e627

Please sign in to comment.