From 9f5fdab3ed1ed6530ff02f8294799681f73080dd Mon Sep 17 00:00:00 2001 From: Prathamesh More Date: Thu, 10 Feb 2022 00:03:38 +0530 Subject: [PATCH] Fixed crash when removing song from Playing queue --- app/build.gradle | 9 +-- .../monkey/retromusic/service/MusicService.kt | 64 +++++++++---------- .../monkey/retromusic/util/PreferenceUtil.kt | 3 +- app/src/main/res/xml/pref_ui.xml | 2 +- build.gradle | 2 +- 5 files changed, 39 insertions(+), 41 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index be0f8592a..4aa99a80e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.kt b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.kt index 03336da2a..9befc9bf8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.kt @@ -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) { diff --git a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.kt b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.kt index eb6585548..4fd4e9db6 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.kt +++ b/app/src/main/java/code/name/monkey/retromusic/util/PreferenceUtil.kt @@ -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 } \ No newline at end of file diff --git a/app/src/main/res/xml/pref_ui.xml b/app/src/main/res/xml/pref_ui.xml index be466eb0f..9c37d7e56 100644 --- a/app/src/main/res/xml/pref_ui.xml +++ b/app/src/main/res/xml/pref_ui.xml @@ -71,7 +71,7 @@ android:title="@string/pref_title_tab_text_mode" />