mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
refactor: Simplify player event handling
This commit is contained in:
parent
8ecb9caaaf
commit
63fde6271d
@ -1,16 +1,11 @@
|
|||||||
package com.github.libretube.enums
|
package com.github.libretube.enums
|
||||||
|
|
||||||
enum class PlayerEvent(val value: Int) {
|
enum class PlayerEvent {
|
||||||
Pause(0),
|
Pause,
|
||||||
Play(1),
|
Play,
|
||||||
Forward(2),
|
Forward,
|
||||||
Rewind(3),
|
Rewind,
|
||||||
Next(5),
|
Next,
|
||||||
Prev(6),
|
Prev,
|
||||||
Background(7)
|
Background
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun fromInt(value: Int) = PlayerEvent.values().first { it.value == value }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,21 @@ package com.github.libretube.extensions
|
|||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
|
import androidx.annotation.OptIn
|
||||||
import androidx.core.content.IntentCompat
|
import androidx.core.content.IntentCompat
|
||||||
|
import androidx.core.os.BuildCompat
|
||||||
|
import java.io.Serializable
|
||||||
|
|
||||||
inline fun <reified T : Parcelable> Intent.parcelableExtra(key: String?): T? {
|
inline fun <reified T : Parcelable> Intent.parcelableExtra(name: String?): T? {
|
||||||
return IntentCompat.getParcelableExtra(this, key, T::class.java)
|
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
|
return context.packageName + "." + ACTION_MEDIA_CONTROL
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPendingIntent(activity: Activity, code: Int): PendingIntent {
|
private fun getPendingIntent(activity: Activity, event: PlayerEvent): PendingIntent {
|
||||||
val intent = Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code)
|
val intent = Intent(getIntentAction(activity)).putExtra(CONTROL_TYPE, event)
|
||||||
return PendingIntentCompat.getBroadcast(activity, code, intent, 0, false)
|
return PendingIntentCompat.getBroadcast(activity, event.ordinal, intent, 0, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getRemoteAction(
|
private fun getRemoteAction(
|
||||||
@ -370,7 +370,7 @@ object PlayerHelper {
|
|||||||
IconCompat.createWithResource(activity, id),
|
IconCompat.createWithResource(activity, id),
|
||||||
text,
|
text,
|
||||||
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.hideKeyboard
|
||||||
import com.github.libretube.extensions.parcelable
|
import com.github.libretube.extensions.parcelable
|
||||||
import com.github.libretube.extensions.seekBy
|
import com.github.libretube.extensions.seekBy
|
||||||
|
import com.github.libretube.extensions.serializableExtra
|
||||||
import com.github.libretube.extensions.setMetadata
|
import com.github.libretube.extensions.setMetadata
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
import com.github.libretube.extensions.toastFromMainDispatcher
|
import com.github.libretube.extensions.toastFromMainDispatcher
|
||||||
@ -204,9 +205,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
* Receiver for all actions in the PiP mode
|
* Receiver for all actions in the PiP mode
|
||||||
*/
|
*/
|
||||||
private val broadcastReceiver = object : BroadcastReceiver() {
|
private val broadcastReceiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
val action = intent?.getIntExtra(PlayerHelper.CONTROL_TYPE, 0) ?: return
|
when (intent.serializableExtra<PlayerEvent>(PlayerHelper.CONTROL_TYPE) ?: return) {
|
||||||
when (PlayerEvent.fromInt(action)) {
|
|
||||||
PlayerEvent.Play -> {
|
PlayerEvent.Play -> {
|
||||||
exoPlayer.play()
|
exoPlayer.play()
|
||||||
}
|
}
|
||||||
@ -252,7 +252,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
// broadcast receiver for PiP actions
|
// broadcast receiver for PiP actions
|
||||||
context?.registerReceiver(
|
context?.registerReceiver(
|
||||||
broadcastReceiver,
|
broadcastReceiver,
|
||||||
IntentFilter(PlayerHelper.getIntentActon(requireContext()))
|
IntentFilter(PlayerHelper.getIntentAction(requireContext()))
|
||||||
)
|
)
|
||||||
|
|
||||||
// schedule task to save the watch position each second
|
// schedule task to save the watch position each second
|
||||||
|
Loading…
Reference in New Issue
Block a user