mirror of
https://github.com/IllusionMan1212/lyrics-grabbr.git
synced 2026-10-24 02:12:27 +00:00
feat: get duration, playback state and album art from notification
This commit is contained in:
+29
-8
@@ -1,43 +1,58 @@
|
|||||||
package com.illusionware.lyricsgrabbr.lyricsgrabbr
|
package com.illusionware.lyricsgrabbr.lyricsgrabbr
|
||||||
|
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
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.NotificationListenerService
|
||||||
import android.service.notification.StatusBarNotification
|
import android.service.notification.StatusBarNotification
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Base64
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notification listening service. Intercepts notifications if permission is given to do so.
|
* Notification listening service. Intercepts notifications if permission is given to do so.
|
||||||
*/
|
*/
|
||||||
class NotificationListener : NotificationListenerService() {
|
class NotificationListener : NotificationListenerService() {
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
getCurrentNotifications()
|
getCurrentNotifications(applicationContext)
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onListenerConnected() {
|
override fun onListenerConnected() {
|
||||||
getCurrentNotifications()
|
getCurrentNotifications(applicationContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNotificationPosted(sbn: StatusBarNotification) {
|
override fun onNotificationPosted(sbn: StatusBarNotification) {
|
||||||
sendNotificationBroadcast(sbn)
|
sendNotificationBroadcast(sbn, applicationContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCurrentNotifications() {
|
private fun getCurrentNotifications(context: Context) {
|
||||||
val sbns = activeNotifications
|
val sbns = activeNotifications
|
||||||
for (sbn in sbns) {
|
for (sbn in sbns) {
|
||||||
// TODO: remove this check and figure out a way to send a fucking list instead
|
// TODO: remove this check and figure out a way to send a fucking list instead
|
||||||
if (sbn.packageName == "com.spotify.music") {
|
if (sbn.packageName == "com.spotify.music") {
|
||||||
sendNotificationBroadcast(sbn)
|
sendNotificationBroadcast(sbn, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendNotificationBroadcast(sbn: StatusBarNotification) {
|
private fun sendNotificationBroadcast(sbn: StatusBarNotification, context: Context) {
|
||||||
// TODO: get specific media player notification data
|
// get the media session stuff
|
||||||
// such as onPlay and onNextSong or whatever tf
|
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.
|
// Retrieve package name to set as title.
|
||||||
val packageName = sbn.packageName
|
val packageName = sbn.packageName
|
||||||
@@ -52,6 +67,9 @@ class NotificationListener : NotificationListenerService() {
|
|||||||
intent.putExtra(NOTIFICATION_PACKAGE_MESSAGE, packageMessage)
|
intent.putExtra(NOTIFICATION_PACKAGE_MESSAGE, packageMessage)
|
||||||
intent.putExtra(NOTIFICATION_PACKAGE_TEXT, packageText)
|
intent.putExtra(NOTIFICATION_PACKAGE_TEXT, packageText)
|
||||||
intent.putExtra(NOTIFICATION_PACKAGE_EXTRA, packageExtra)
|
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)
|
sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +79,9 @@ class NotificationListener : NotificationListenerService() {
|
|||||||
const val NOTIFICATION_PACKAGE_MESSAGE = "package_message"
|
const val NOTIFICATION_PACKAGE_MESSAGE = "package_message"
|
||||||
const val NOTIFICATION_PACKAGE_TEXT = "package_text"
|
const val NOTIFICATION_PACKAGE_TEXT = "package_text"
|
||||||
const val NOTIFICATION_PACKAGE_EXTRA = "package_extra"
|
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 {
|
private fun convertBundleToJsonString(extra: Bundle): String {
|
||||||
|
|||||||
+6
@@ -35,11 +35,17 @@ class NotificationStreamHandler(private val context: Context) : StreamHandler {
|
|||||||
val packageMessage = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_MESSAGE)
|
val packageMessage = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_MESSAGE)
|
||||||
val packageText = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_TEXT)
|
val packageText = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_TEXT)
|
||||||
val packageExtra = intent.getStringExtra(NotificationListener.NOTIFICATION_PACKAGE_EXTRA)
|
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>()
|
val map = HashMap<String, Any>()
|
||||||
map["packageName"] = packageName!!
|
map["packageName"] = packageName!!
|
||||||
map["packageMessage"] = packageMessage!!
|
map["packageMessage"] = packageMessage!!
|
||||||
map["packageText"] = packageText!!
|
map["packageText"] = packageText!!
|
||||||
map["packageExtra"] = packageExtra!!
|
map["packageExtra"] = packageExtra!!
|
||||||
|
map["packageArt"] = packageArt!!
|
||||||
|
map["packagePlaybackState"] = packagePlaybackState
|
||||||
|
map["packageDuration"] = packageDuration
|
||||||
eventSink?.success(map)
|
eventSink?.success(map)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-12
@@ -13,8 +13,7 @@ const notificationId = 0;
|
|||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
// TODO: do the preconfiguration for the android notifications.
|
// TODO: change the default flutter icon to my own.
|
||||||
// e.g: notification icon
|
|
||||||
const AndroidInitializationSettings initializationSettingsAndroid =
|
const AndroidInitializationSettings initializationSettingsAndroid =
|
||||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||||
const InitializationSettings initializationSettings =
|
const InitializationSettings initializationSettings =
|
||||||
@@ -143,17 +142,15 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
// TODO: if the app is in the background, only update our notif
|
// TODO: if the app is in the background, only update our notif
|
||||||
// if the app is in the foreground, update the notif, and
|
// if the app is in the foreground, update the notif, and
|
||||||
// fetch the new lyrics and display them.
|
// fetch the new lyrics and display them.
|
||||||
var song = event.packageText;
|
sendNotification(event);
|
||||||
var artist = event.packageMessage;
|
|
||||||
sendNotification(song, artist);
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_notifs.add(event);
|
_notifs.add(event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> sendNotification(String title, String body) async {
|
Future<void> sendNotification(NotificationEvent notification) async {
|
||||||
const AndroidNotificationDetails androidNotification =
|
final AndroidNotificationDetails androidNotification =
|
||||||
AndroidNotificationDetails(
|
AndroidNotificationDetails(
|
||||||
"Lyrics",
|
"Lyrics",
|
||||||
"Lyrics",
|
"Lyrics",
|
||||||
@@ -162,15 +159,16 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
showWhen: false,
|
showWhen: false,
|
||||||
ongoing: true,
|
ongoing: true,
|
||||||
playSound: false,
|
playSound: false,
|
||||||
|
largeIcon: ByteArrayAndroidBitmap.fromBase64String(notification.art),
|
||||||
);
|
);
|
||||||
|
|
||||||
const NotificationDetails notificationDetails =
|
final NotificationDetails notificationDetails =
|
||||||
NotificationDetails(android: androidNotification);
|
NotificationDetails(android: androidNotification);
|
||||||
|
|
||||||
await flutterLocalNotificationsPlugin.show(
|
await flutterLocalNotificationsPlugin.show(
|
||||||
notificationId,
|
notificationId,
|
||||||
title,
|
'${notification.songTitle} - ${notification.artist}',
|
||||||
body,
|
'Tap to get lyrics',
|
||||||
notificationDetails,
|
notificationDetails,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -204,8 +202,8 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|||||||
itemBuilder: (BuildContext context, int idx) {
|
itemBuilder: (BuildContext context, int idx) {
|
||||||
final entry = _notifs[idx];
|
final entry = _notifs[idx];
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: Text(entry.packageText),
|
leading: Text(entry.songTitle),
|
||||||
trailing: Text(entry.packageMessage),
|
trailing: Text(entry.artist),
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
|
|||||||
@@ -13,19 +13,32 @@ class NotificationException implements Exception {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NotificationEvent {
|
abstract class PlaybackState {
|
||||||
String packageMessage;
|
static const STATE_INVALID = -1;
|
||||||
String packageName;
|
static const STATE_PAUSED = 2;
|
||||||
String packageExtra;
|
static const STATE_PLAYING = 3;
|
||||||
String packageText;
|
}
|
||||||
DateTime timeStamp;
|
|
||||||
|
|
||||||
NotificationEvent({
|
class NotificationEvent {
|
||||||
|
final String artist;
|
||||||
|
final String packageName;
|
||||||
|
final String packageExtra;
|
||||||
|
final String songTitle;
|
||||||
|
final String art;
|
||||||
|
final int playbackState;
|
||||||
|
final int duration;
|
||||||
|
|
||||||
|
final DateTime timeStamp;
|
||||||
|
|
||||||
|
const NotificationEvent({
|
||||||
required this.packageName,
|
required this.packageName,
|
||||||
required this.packageMessage,
|
required this.artist,
|
||||||
required this.timeStamp,
|
required this.timeStamp,
|
||||||
required this.packageExtra,
|
required this.packageExtra,
|
||||||
required this.packageText
|
required this.songTitle,
|
||||||
|
required this.art,
|
||||||
|
required this.playbackState,
|
||||||
|
required this.duration
|
||||||
});
|
});
|
||||||
|
|
||||||
factory NotificationEvent.fromMap(Map<dynamic, dynamic> map) {
|
factory NotificationEvent.fromMap(Map<dynamic, dynamic> map) {
|
||||||
@@ -34,13 +47,20 @@ class NotificationEvent {
|
|||||||
String message = map['packageMessage'];
|
String message = map['packageMessage'];
|
||||||
String text = map['packageText'];
|
String text = map['packageText'];
|
||||||
String extra = map['packageExtra'];
|
String extra = map['packageExtra'];
|
||||||
|
String icon = map['packageArt'];
|
||||||
|
int playbackState = map['packagePlaybackState'];
|
||||||
|
int duration = map['packageDuration'];
|
||||||
|
|
||||||
return NotificationEvent(packageName: name, packageMessage: message, timeStamp: time,packageText: text , packageExtra: extra);
|
return NotificationEvent(
|
||||||
}
|
packageName: name,
|
||||||
|
artist: message,
|
||||||
@override
|
timeStamp: time,
|
||||||
String toString() {
|
songTitle: text,
|
||||||
return "Notification Event \n Package Name: $packageName \n - Timestamp: $timeStamp \n - Package Message: $packageMessage";
|
packageExtra: extra,
|
||||||
|
art: icon,
|
||||||
|
playbackState: playbackState,
|
||||||
|
duration: duration
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user