diff --git a/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt b/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt index 645210137..920fe2efa 100644 --- a/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt +++ b/app/src/main/java/com/github/libretube/enums/PlayerEvent.kt @@ -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 } diff --git a/app/src/main/java/com/github/libretube/extensions/Intent.kt b/app/src/main/java/com/github/libretube/extensions/Intent.kt index 78a056355..5118ada14 100644 --- a/app/src/main/java/com/github/libretube/extensions/Intent.kt +++ b/app/src/main/java/com/github/libretube/extensions/Intent.kt @@ -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 Intent.parcelableExtra(key: String?): T? { - return IntentCompat.getParcelableExtra(this, key, T::class.java) +inline fun Intent.parcelableExtra(name: String?): T? { + return IntentCompat.getParcelableExtra(this, name, T::class.java) +} + +@OptIn(BuildCompat.PrereleaseSdkCheck::class) +inline fun Intent.serializableExtra(name: String?): T? { + return if (BuildCompat.isAtLeastU()) { + getSerializableExtra(name, T::class.java) + } else { + @Suppress("DEPRECATION") + getSerializableExtra(name) as? T + } } diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index c066f1847..d827da78b 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -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( @@ -370,7 +370,7 @@ object PlayerHelper { IconCompat.createWithResource(activity, id), text, text, - getPendingIntent(activity, event.value) + getPendingIntent(activity, event) ) } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 9729c618e..550a8008e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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 @@ -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(PlayerHelper.CONTROL_TYPE) ?: return) { PlayerEvent.Play -> { exoPlayer.play() } @@ -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