Skip to content

Commit

Permalink
Attempt to fix crash if notification permissions aren't granted
Browse files Browse the repository at this point in the history
  • Loading branch information
teccheck committed Jan 23, 2024
1 parent d539197 commit 11df6cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import androidx.core.content.getSystemService
import dev.forkhandles.result4k.Failure
import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.Success
import io.github.teccheck.fastlyrics.exceptions.LyricsApiException
import io.github.teccheck.fastlyrics.exceptions.NoMusicPlayingException
import io.github.teccheck.fastlyrics.exceptions.NoNotifPermsException
import io.github.teccheck.fastlyrics.model.SongMeta
import io.github.teccheck.fastlyrics.service.DummyNotificationListenerService

Expand All @@ -27,7 +29,17 @@ object MediaSession {
private val internalCallbacks = mutableMapOf<MediaSession.Token, MediaController.Callback>()
private val callbacks = mutableListOf<SongMetaCallback>()

private var initialized = false

fun init(context: Context) {
if (!DummyNotificationListenerService.canAccessNotifications(context))
return

if (initialized)
return

initialized = true

nls = ComponentName(context, DummyNotificationListenerService::class.java)
msm = context.getSystemService()!!
msm.addOnActiveSessionsChangedListener(this::onActiveSessionsChanged, nls)
Expand Down Expand Up @@ -82,7 +94,9 @@ object MediaSession {
onMetadataChanged(newActive, newActive.metadata)
}

fun getSongInformation(): Result<SongMeta, NoMusicPlayingException> {
fun getSongInformation(): Result<SongMeta, LyricsApiException> {
if (!initialized) return Failure(NoNotifPermsException())

val session = activeMediaSession ?: return Failure(NoMusicPlayingException())
val metadata = session.metadata?.getSongMeta() ?: return Failure(
NoMusicPlayingException()
Expand All @@ -94,10 +108,14 @@ object MediaSession {
fun getSongPosition(): Long? = activeMediaSession?.playbackState?.position

fun registerSongMetaCallback(callback: SongMetaCallback) {
if (!initialized) return

callbacks.add(callback)
}

fun unregisterSongMetaCallback(callback: SongMetaCallback) {
if (!initialized) return

callbacks.remove(callback)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.teccheck.fastlyrics.exceptions

class NoNotifPermsException : LyricsApiException() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import io.github.teccheck.fastlyrics.api.MediaSession
import io.github.teccheck.fastlyrics.databinding.FragmentPermissionBinding

class PermissionFragment : Fragment() {
Expand All @@ -25,6 +26,11 @@ class PermissionFragment : Fragment() {
return binding.root
}

override fun onResume() {
super.onResume()
MediaSession.init(requireContext())
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down

0 comments on commit 11df6cc

Please sign in to comment.