feat: get duration, playback state and album art from notification

This commit is contained in:
2022-02-25 02:19:01 +02:00
parent add74ea848
commit d07b5e793f
4 changed files with 80 additions and 35 deletions
@@ -1,43 +1,58 @@
package com.illusionware.lyricsgrabbr.lyricsgrabbr
import android.app.Notification
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaMetadata
import android.media.session.MediaController
import android.media.session.MediaSession
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import org.json.JSONException
import org.json.JSONObject
import android.os.Bundle
import android.util.Base64
import java.io.ByteArrayOutputStream
/**
* Notification listening service. Intercepts notifications if permission is given to do so.
*/
class NotificationListener : NotificationListenerService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
getCurrentNotifications()
getCurrentNotifications(applicationContext)
return super.onStartCommand(intent, flags, startId)
}
override fun onListenerConnected() {
getCurrentNotifications()
getCurrentNotifications(applicationContext)
}
override fun onNotificationPosted(sbn: StatusBarNotification) {
sendNotificationBroadcast(sbn)
sendNotificationBroadcast(sbn, applicationContext)
}
private fun getCurrentNotifications() {
private fun getCurrentNotifications(context: Context) {
val sbns = activeNotifications
for (sbn in sbns) {
// TODO: remove this check and figure out a way to send a fucking list instead
if (sbn.packageName == "com.spotify.music") {
sendNotificationBroadcast(sbn)
sendNotificationBroadcast(sbn, context)
}
}
}
private fun sendNotificationBroadcast(sbn: StatusBarNotification) {
// TODO: get specific media player notification data
// such as onPlay and onNextSong or whatever tf
private fun sendNotificationBroadcast(sbn: StatusBarNotification, context: Context) {
// get the media session stuff
val mediaSessionToken = sbn.notification.extras.getParcelable<MediaSession.Token>(Notification.EXTRA_MEDIA_SESSION)
val mediaCtrl = mediaSessionToken?.let { MediaController(context, it) }
val duration = mediaCtrl?.metadata?.getLong(MediaMetadata.METADATA_KEY_DURATION)?.div(1000)
val playbackState = mediaCtrl?.playbackState?.state
val albumArt = mediaCtrl?.metadata?.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART)
val outputStream = ByteArrayOutputStream()
albumArt?.compress(Bitmap.CompressFormat.PNG, 75, outputStream)
val artB64 = Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
// Retrieve package name to set as title.
val packageName = sbn.packageName
@@ -52,6 +67,9 @@ class NotificationListener : NotificationListenerService() {
intent.putExtra(NOTIFICATION_PACKAGE_MESSAGE, packageMessage)
intent.putExtra(NOTIFICATION_PACKAGE_TEXT, packageText)
intent.putExtra(NOTIFICATION_PACKAGE_EXTRA, packageExtra)
intent.putExtra(NOTIFICATION_PACKAGE_ART, artB64)
intent.putExtra(NOTIFICATION_PACKAGE_DURATION, duration)
intent.putExtra(NOTIFICATION_PACKAGE_PLAYBACK_STATE, playbackState)
sendBroadcast(intent)
}
@@ -61,6 +79,9 @@ class NotificationListener : NotificationListenerService() {
const val NOTIFICATION_PACKAGE_MESSAGE = "package_message"
const val NOTIFICATION_PACKAGE_TEXT = "package_text"
const val NOTIFICATION_PACKAGE_EXTRA = "package_extra"
const val NOTIFICATION_PACKAGE_ART = "package_art"
const val NOTIFICATION_PACKAGE_PLAYBACK_STATE = "package_playback_state"
const val NOTIFICATION_PACKAGE_DURATION = "package_duration"
}
private fun convertBundleToJsonString(extra: Bundle): String {
@@ -35,11 +35,17 @@ class NotificationStreamHandler(private val context: Context) : StreamHandler {
val packageMessage = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_MESSAGE)
val packageText = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_TEXT)
val packageExtra = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_EXTRA)
val packageArt = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_ART)
val packagePlaybackState = intent.getIntExtra(NotificationListener.NOTIFICATION_PACKAGE_PLAYBACK_STATE, -1)
val packageDuration = intent.getLongExtra(NotificationListener.NOTIFICATION_PACKAGE_DURATION, -1)
val map = HashMap<String, Any>()
map["packageName"] = packageName!!
map["packageMessage"] = packageMessage!!
map["packageText"] = packageText!!
map["packageExtra"] = packageExtra!!
map["packageArt"] = packageArt!!
map["packagePlaybackState"] = packagePlaybackState
map["packageDuration"] = packageDuration
eventSink?.success(map)
}
}