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

@ -126,7 +126,7 @@ dependencies {
def retrofit_version = '2.9.0' def retrofit_version = '2.9.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version" implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3' implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.4'
def material_dialog_version = "3.3.0" def material_dialog_version = "3.3.0"
implementation "com.afollestad.material-dialogs:core:$material_dialog_version" implementation "com.afollestad.material-dialogs:core:$material_dialog_version"
@ -146,9 +146,10 @@ dependencies {
implementation "io.insert-koin:koin-core:$koin_version" implementation "io.insert-koin:koin-core:$koin_version"
implementation "io.insert-koin:koin-android:$koin_version" implementation "io.insert-koin:koin-android:$koin_version"
implementation 'com.github.bumptech.glide:glide:4.12.0' def glide_version = '4.13.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0' implementation "com.github.bumptech.glide:glide:$glide_version"
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0' kapt "com.github.bumptech.glide:compiler:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

View file

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

View file

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

View file

@ -71,7 +71,7 @@
android:title="@string/pref_title_tab_text_mode" /> android:title="@string/pref_title_tab_text_mode" />
<code.name.monkey.appthemehelper.common.prefs.supportv7.ATEListPreference <code.name.monkey.appthemehelper.common.prefs.supportv7.ATEListPreference
android:defaultValue="0" android:defaultValue="1"
android:entries="@array/pref_appbar_mode_titles" android:entries="@array/pref_appbar_mode_titles"
android:entryValues="@array/pref_appbar_mode_values" android:entryValues="@array/pref_appbar_mode_values"
android:key="appbar_mode" android:key="appbar_mode"

View file

@ -14,7 +14,7 @@ buildscript {
google() google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.1.0' classpath 'com.android.tools.build:gradle:7.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
} }