-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Ly
committed
Jul 15, 2023
1 parent
0cfd350
commit a376952
Showing
29 changed files
with
2,463 additions
and
48 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
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
43 changes: 43 additions & 0 deletions
43
app/src/main/java/ly/david/mbjc/services/NowPlayingNotificationListener.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,43 @@ | ||
package ly.david.mbjc.services | ||
|
||
import android.service.notification.NotificationListenerService | ||
import android.service.notification.StatusBarNotification | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import ly.david.data.common.ApplicationScope | ||
import ly.david.data.room.history.nowplaying.NowPlayingHistoryDao | ||
import ly.david.data.room.history.nowplaying.NowPlayingHistoryRoomModel | ||
|
||
private const val ANDROID_SYSTEM_INTELLIGENCE_PACKAGE_NAME = "com.google.android.as" | ||
private const val ANDROID_TITLE_KEY = "android.title" | ||
|
||
@AndroidEntryPoint | ||
class NowPlayingNotificationListener : NotificationListenerService() { | ||
|
||
@Inject | ||
@ApplicationScope | ||
lateinit var coroutineScope: CoroutineScope | ||
|
||
@Inject | ||
lateinit var nowPlayingHistoryDao: NowPlayingHistoryDao | ||
|
||
override fun onNotificationPosted(sbn: StatusBarNotification?) { | ||
super.onNotificationPosted(sbn) | ||
|
||
// Expected format: Better Call Saul Main Title Theme (Extended) by Little Barrie | ||
val titleAndArtist = sbn?.notification?.extras?.getString(ANDROID_TITLE_KEY) ?: return | ||
val packageName = sbn.packageName | ||
|
||
if (packageName == ANDROID_SYSTEM_INTELLIGENCE_PACKAGE_NAME) { | ||
coroutineScope.launch { | ||
nowPlayingHistoryDao.insert( | ||
NowPlayingHistoryRoomModel( | ||
raw = titleAndArtist, | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.