diff --git a/app/src/main/java/com/illusionman1212/lyricsgrabbr/MainActivity.kt b/app/src/main/java/com/illusionman1212/lyricsgrabbr/MainActivity.kt index 021fb0c..1e880a3 100644 --- a/app/src/main/java/com/illusionman1212/lyricsgrabbr/MainActivity.kt +++ b/app/src/main/java/com/illusionman1212/lyricsgrabbr/MainActivity.kt @@ -51,7 +51,8 @@ data class NotificationEvent( ) class MainActivity : ComponentActivity() { - var lastNotification: MutableStateFlow = MutableStateFlow(null) + private var lastNotification: MutableStateFlow = MutableStateFlow(null) + private var isReceiverRunning = false override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() @@ -163,9 +164,7 @@ class MainActivity : ComponentActivity() { } private fun startListening() { - val isServiceRunning = NotificationListener.isRunning.value - - if (!isPermissionGranted() || isServiceRunning) { + if (!isPermissionGranted() || isReceiverRunning) { return } @@ -176,6 +175,7 @@ class MainActivity : ComponentActivity() { filter.addAction(NotificationListener.NOTIFICATION_INTENT) LocalBroadcastManager.getInstance(applicationContext).registerReceiver(NotificationReceiver(), filter) ContextCompat.startForegroundService(applicationContext, listenerIntent) + isReceiverRunning = true } inner class NotificationReceiver: BroadcastReceiver() { diff --git a/app/src/main/java/com/illusionman1212/lyricsgrabbr/NotificationListener.kt b/app/src/main/java/com/illusionman1212/lyricsgrabbr/NotificationListener.kt index b30f9de..352bb60 100644 --- a/app/src/main/java/com/illusionman1212/lyricsgrabbr/NotificationListener.kt +++ b/app/src/main/java/com/illusionman1212/lyricsgrabbr/NotificationListener.kt @@ -19,9 +19,6 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat import androidx.localbroadcastmanager.content.LocalBroadcastManager -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.update class NotificationListener: NotificationListenerService() { private var notificationManager : NotificationManagerCompat? = null @@ -60,13 +57,10 @@ class NotificationListener: NotificationListenerService() { private fun getStopServiceNotificationAction(): PendingIntent { val intent = Intent(android.provider.Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) - return pendingIntent + return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) } private fun startForegroundService() { - _isRunning.update { true } - startForeground(NOTIFICATION_ID, getInitialNotification()) } @@ -167,9 +161,6 @@ class NotificationListener: NotificationListenerService() { } companion object { - private val _isRunning = MutableStateFlow(false) - val isRunning = _isRunning.asStateFlow() - const val NOTIFICATION_INTENT = "notification_event" const val NOTIFICATION_PACKAGE_NAME = "package_name" const val NOTIFICATION_PACKAGE_ARTIST = "package_artist"