-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notification listener to get stream from YouTube app
- Loading branch information
Showing
5 changed files
with
157 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/kotlin/com/livetl/android/data/media/YouTubeNotificationListenerService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.livetl.android.data.media | ||
|
||
import android.content.ComponentName | ||
import android.media.MediaMetadata | ||
import android.media.session.MediaSessionManager | ||
import android.media.session.PlaybackState | ||
import android.service.notification.NotificationListenerService | ||
import android.service.notification.StatusBarNotification | ||
import androidx.core.content.getSystemService | ||
import com.livetl.android.data.stream.StreamService | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class YouTubeNotificationListenerService : NotificationListenerService() { | ||
|
||
@Inject | ||
lateinit var streamService: StreamService | ||
|
||
private val job = SupervisorJob() | ||
private val scope = CoroutineScope(Dispatchers.IO + job) | ||
|
||
override fun onNotificationPosted(sbn: StatusBarNotification) { | ||
super.onNotificationPosted(sbn) | ||
|
||
if (sbn.packageName != YOUTUBE_PACKAGE_NAME) { | ||
return | ||
} | ||
|
||
val mediaSessionManager = getSystemService<MediaSessionManager>() ?: return | ||
|
||
val component = ComponentName(this, YouTubeNotificationListenerService::class.java) | ||
val sessions = mediaSessionManager.getActiveSessions(component) | ||
val youtubeSession = sessions.find { it.packageName == YOUTUBE_PACKAGE_NAME } ?: return | ||
|
||
// The notification and media session don't expose the actual ID of the YouTube video, unfortunately | ||
val videoTitle = youtubeSession.metadata?.getString(MediaMetadata.METADATA_KEY_TITLE) ?: return | ||
val videoChannelName = youtubeSession.metadata?.getString(MediaMetadata.METADATA_KEY_ARTIST) ?: return | ||
|
||
scope.launch { | ||
Timber.d("Looking up stream for $videoTitle / $videoChannelName") | ||
val streamInfo = streamService.findStreamInfo(videoTitle, videoChannelName) ?: return@launch | ||
|
||
val position = youtubeSession.playbackState?.position // in ms; 0 is live | ||
val state = when (youtubeSession.playbackState?.state) { | ||
PlaybackState.STATE_PAUSED -> "PAUSED" | ||
PlaybackState.STATE_PLAYING -> "PLAYING" | ||
else -> null | ||
} | ||
|
||
Timber.i("Current YouTube video: ${streamInfo.videoId} / ${streamInfo.title} / $position / $state") | ||
} | ||
} | ||
|
||
override fun onNotificationRemoved(sbn: StatusBarNotification) { | ||
super.onNotificationRemoved(sbn) | ||
|
||
if (sbn.packageName != YOUTUBE_PACKAGE_NAME) { | ||
return | ||
} | ||
|
||
Timber.i("onNotificationRemoved") | ||
} | ||
} | ||
|
||
private const val YOUTUBE_PACKAGE_NAME = "com.google.android.youtube" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters