0
0
mirror of https://github.com/MuntashirAkon/Metro.git synced 2024-09-20 03:52:20 +02:00

fix: Added startForegroundService calls inside handler.post to avoid android.app.RemoteServiceException

Fixes Context.startForegroundService() did not then call Service.startForeground() (probably)
This commit is contained in:
Prathamesh More 2023-03-06 21:43:10 +05:30
parent de92e1d9a7
commit 71f112673b
2 changed files with 9 additions and 17 deletions

View File

@ -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 {
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 {

View File

@ -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)
}
}