Fixed crash when removing song from Playing queue

This commit is contained in:
Prathamesh More 2022-02-10 00:03:38 +05:30
parent 666f52adb5
commit 9f5fdab3ed
5 changed files with 39 additions and 41 deletions

View file

@ -153,11 +153,11 @@ class MusicService : MediaBrowserServiceCompat(),
}
private var audioManager: AudioManager? = null
get() {
if (field == null) {
field = getSystemService()
if (field == null) {
field = getSystemService()
}
return field
}
return field
}
private val becomingNoisyReceiverIntentFilter =
IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
@ -192,7 +192,7 @@ class MusicService : MediaBrowserServiceCompat(),
private var playingNotification: PlayingNotification? = null
private val updateFavoriteReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
playingNotification!!.updateFavorite(currentSong) { startForegroundOrNotify() }
playingNotification?.updateFavorite(currentSong) { startForegroundOrNotify() }
startForegroundOrNotify()
}
}
@ -364,12 +364,12 @@ class MusicService : MediaBrowserServiceCompat(),
unregisterReceiver(bluetoothReceiver)
bluetoothConnectedRegistered = false
}
mediaSession!!.isActive = false
mediaSession?.isActive = false
quit()
releaseResources()
contentResolver.unregisterContentObserver(mediaStoreObserver)
unregisterOnSharedPreferenceChangedListener(this)
wakeLock!!.release()
wakeLock?.release()
sendBroadcast(Intent("code.name.monkey.retromusic.RETRO_MUSIC_SERVICE_DESTROYED"))
}
@ -524,7 +524,7 @@ class MusicService : MediaBrowserServiceCompat(),
return repeatMode
}
fun setRepeatMode(repeatMode: Int) {
private fun setRepeatMode(repeatMode: Int) {
when (repeatMode) {
REPEAT_MODE_NONE, REPEAT_MODE_ALL, REPEAT_MODE_THIS -> {
this.repeatMode = repeatMode
@ -737,7 +737,7 @@ class MusicService : MediaBrowserServiceCompat(),
COLORED_NOTIFICATION -> updateNotification()
CLASSIC_NOTIFICATION -> {
updateNotification()
playingNotification?.setPlaying(isPlaying){ startForegroundOrNotify()}
playingNotification?.setPlaying(isPlaying) { startForegroundOrNotify() }
playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() }
}
PLAYBACK_SPEED -> updateMediaSessionPlaybackState()
@ -980,17 +980,13 @@ class MusicService : MediaBrowserServiceCompat(),
}
private fun removeSongImpl(song: Song) {
for (i in playingQueue.indices) {
if (playingQueue[i].id == song.id) {
playingQueue.removeAt(i)
rePosition(i)
}
}
for (i in originalPlayingQueue.indices) {
if (originalPlayingQueue[i].id == song.id) {
originalPlayingQueue.removeAt(i)
}
}
val deletePosition = playingQueue.indexOf(song)
playingQueue.removeAt(deletePosition)
rePosition(deletePosition)
val originalDeletePosition = originalPlayingQueue.indexOf(song)
playingQueue.removeAt(originalDeletePosition)
rePosition(originalDeletePosition)
}
fun removeSong(song: Song) {
@ -1005,6 +1001,19 @@ class MusicService : MediaBrowserServiceCompat(),
notifyChange(QUEUE_CHANGED)
}
private fun rePosition(deletedPosition: Int) {
val currentPosition = getPosition()
if (deletedPosition < currentPosition) {
position = currentPosition - 1
} else if (deletedPosition == currentPosition) {
if (playingQueue.size > deletedPosition) {
setPosition(position)
} else {
setPosition(position - 1)
}
}
}
@Synchronized
fun restoreQueuesAndPositionIfNecessary() {
if (!queuesRestored && playingQueue.isEmpty()) {
@ -1184,7 +1193,7 @@ class MusicService : MediaBrowserServiceCompat(),
savePositionInTrack()
}
songPlayCountHelper.notifyPlayStateChanged(isPlaying)
playingNotification?.setPlaying(isPlaying){ startForegroundOrNotify()}
playingNotification?.setPlaying(isPlaying) { startForegroundOrNotify() }
startForegroundOrNotify()
}
FAVORITE_STATE_CHANGED -> {
@ -1315,19 +1324,6 @@ class MusicService : MediaBrowserServiceCompat(),
playerHandler?.obtainMessage(PREPARE_NEXT)?.sendToTarget()
}
private fun rePosition(deletedPosition: Int) {
val currentPosition = getPosition()
if (deletedPosition < currentPosition) {
position = currentPosition - 1
} else if (deletedPosition == currentPosition) {
if (playingQueue.size > deletedPosition) {
setPosition(position)
} else {
setPosition(position - 1)
}
}
}
private fun registerBluetoothConnected() {
Log.i(TAG, "registerBluetoothConnected: ")
if (!bluetoothConnectedRegistered) {

View file

@ -691,7 +691,7 @@ object PreferenceUtil {
set(value) = sharedPreferences.edit { putFloat(PLAYBACK_PITCH, value) }
val appBarMode: TopAppBarLayout.AppBarMode
get() = if (sharedPreferences.getString(APPBAR_MODE, "0") == "0") {
get() = if (sharedPreferences.getString(APPBAR_MODE, "1") == "0") {
TopAppBarLayout.AppBarMode.COLLAPSING
} else {
TopAppBarLayout.AppBarMode.SIMPLE
@ -712,6 +712,7 @@ object PreferenceUtil {
val swipeAnywhereToChangeSong
get() = sharedPreferences.getBoolean(SWIPE_ANYWHERE_NOW_PLAYING, true)
}
enum class LyricsType {
REPLACE_COVER, OVER_COVER
}