mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #4556 from Isira-Seneviratne/Enum_serializable
refactor: Simplify player event handling
This commit is contained in:
commit
0e8e6278d4
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -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<PlayerEvent>(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
|
||||
|
Loading…
Reference in New Issue
Block a user