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'
implementation "com.squareup.retrofit2:retrofit:$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"
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-android:$koin_version"
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'
def glide_version = '4.13.0'
implementation "com.github.bumptech.glide:glide:$glide_version"
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'

View file

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

View file

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

View file

@ -14,7 +14,7 @@ buildscript {
google()
}
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 "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}