diff --git a/app/src/main/java/code/name/monkey/retromusic/helper/MusicPlayerRemote.kt b/app/src/main/java/code/name/monkey/retromusic/helper/MusicPlayerRemote.kt index a57af2a17..421586db9 100644 --- a/app/src/main/java/code/name/monkey/retromusic/helper/MusicPlayerRemote.kt +++ b/app/src/main/java/code/name/monkey/retromusic/helper/MusicPlayerRemote.kt @@ -19,7 +19,9 @@ import android.app.Activity import android.content.* import android.database.Cursor import android.net.Uri +import android.os.Handler import android.os.IBinder +import android.os.Looper import android.provider.DocumentsContract import android.widget.Toast import androidx.core.content.ContextCompat @@ -115,13 +117,10 @@ object MusicPlayerRemote : KoinComponent { val realActivity = (context as Activity).parent ?: context val contextWrapper = ContextWrapper(realActivity) val intent = Intent(contextWrapper, MusicService::class.java) - try { - contextWrapper.startService(intent) - } catch (ignored: IllegalStateException) { - runCatching { - ContextCompat.startForegroundService(context, intent) - } + Handler(Looper.getMainLooper()).post { + ContextCompat.startForegroundService(context, intent) } + val binder = ServiceBinder(callback) if (contextWrapper.bindService( @@ -416,7 +415,7 @@ object MusicPlayerRemote : KoinComponent { } } } - if (songs == null || songs.isEmpty()) { + if (songs.isNullOrEmpty()) { var songFile: File? = null if (uri.authority != null && uri.authority == "com.android.externalstorage.documents") { val path = uri.path?.split(":".toRegex(), 2)?.get(1) @@ -436,7 +435,7 @@ object MusicPlayerRemote : KoinComponent { songs = songRepository.songsByFilePath(songFile.absolutePath, true) } } - if (songs != null && songs.isNotEmpty()) { + if (!songs.isNullOrEmpty()) { openQueue(songs, 0, true) } else { try { diff --git a/app/src/main/java/code/name/monkey/retromusic/service/MediaButtonIntentReceiver.kt b/app/src/main/java/code/name/monkey/retromusic/service/MediaButtonIntentReceiver.kt index 6476dd041..c0bb0768f 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/MediaButtonIntentReceiver.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/MediaButtonIntentReceiver.kt @@ -106,6 +106,7 @@ class MediaButtonIntentReceiver : MediaButtonReceiver() { KeyEvent.KEYCODE_MEDIA_STOP -> command = ACTION_STOP KeyEvent.KEYCODE_HEADSETHOOK, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> command = ACTION_TOGGLE_PAUSE + KeyEvent.KEYCODE_MEDIA_NEXT -> command = ACTION_SKIP KeyEvent.KEYCODE_MEDIA_PREVIOUS -> command = ACTION_REWIND KeyEvent.KEYCODE_MEDIA_PAUSE -> command = ACTION_PAUSE @@ -154,15 +155,7 @@ class MediaButtonIntentReceiver : MediaButtonReceiver() { private fun startService(context: Context, command: String?) { val intent = Intent(context, MusicService::class.java) intent.action = command - try { - // IMPORTANT NOTE: (kind of a hack) - // on Android O and above the following crashes when the app is not running - // there is no good way to check whether the app is running so we catch the exception - // we do not always want to use startForegroundService() because then one gets an ANR - // if no notification is displayed via startForeground() - // according to Play analytics this happens a lot, I suppose for example if command = PAUSE - context.startService(intent) - } catch (ignored: IllegalStateException) { + Handler(Looper.getMainLooper()).post { ContextCompat.startForegroundService(context, intent) } }