From 5dc9fda7a458d399bdf834035b447e57718c32ea Mon Sep 17 00:00:00 2001 From: tzugen Date: Mon, 4 Apr 2022 18:18:52 +0200 Subject: [PATCH] Detekt fixes --- detekt-config.yml | 2 +- .../ultrasonic/fragment/PlayerFragment.kt | 1 + .../ultrasonic/playback/APIDataSource.kt | 5 +++- .../playback/LegacyPlaylistManager.kt | 5 ---- .../ultrasonic/playback/MediaItemTree.kt | 7 ----- .../playback/MediaNotificationProvider.kt | 4 ++- .../ultrasonic/service/DownloadService.kt | 30 ------------------- .../moire/ultrasonic/service/Downloader.kt | 2 ++ .../service/MediaPlayerController.kt | 24 ++++++++++++++- .../service/MediaPlayerLifecycleSupport.kt | 3 +- 10 files changed, 36 insertions(+), 47 deletions(-) diff --git a/detekt-config.yml b/detekt-config.yml index cdebb21a..b59e4ec3 100644 --- a/detekt-config.yml +++ b/detekt-config.yml @@ -70,7 +70,7 @@ style: excludeImportStatements: false MagicNumber: # 100 common in percentage, 1000 in milliseconds - ignoreNumbers: ['-1', '0', '1', '2', '5', '10', '100', '256', '512', '1000', '1024'] + ignoreNumbers: ['-1', '0', '1', '2', '5', '10', '100', '256', '512', '1000', '1024', '4096'] ignoreEnums: true ignorePropertyDeclaration: true UnnecessaryAbstractClass: diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt index 143d0371..d1e4d95b 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/PlayerFragment.kt @@ -1017,6 +1017,7 @@ class PlayerFragment : } } + @Suppress("LongMethod") @Synchronized private fun onSliderProgressChanged() { diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/APIDataSource.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/APIDataSource.kt index f3deb4ba..6ab75ca3 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/APIDataSource.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/APIDataSource.kt @@ -43,6 +43,7 @@ import timber.log.Timber * priority) the `dataSpec`, [.setRequestProperty] and the default parameters used to * construct the instance. */ +@Suppress("MagicNumber") @UnstableApi open class APIDataSource private constructor( subsonicAPIClient: SubsonicAPIClient @@ -124,6 +125,7 @@ open class APIDataSource private constructor( requestProperties.clear() } + @Suppress("LongMethod", "NestedBlockDepth") @Throws(HttpDataSourceException::class) override fun open(dataSpec: DataSpec): Long { this.dataSpec = dataSpec @@ -171,7 +173,7 @@ open class APIDataSource private constructor( } val errorResponseBody: ByteArray = try { Util.toByteArray(Assertions.checkNotNull(responseByteStream)) - } catch (e: IOException) { + } catch (ignore: IOException) { Util.EMPTY_BYTE_ARRAY } val headers = response.headers().toMultimap() @@ -241,6 +243,7 @@ open class APIDataSource private constructor( * occurs while reading from the source, or if the data ended before skipping the specified * number of bytes. */ + @Suppress("ThrowsCount") @Throws(HttpDataSourceException::class) private fun skipFully(bytesToSkip: Long, dataSpec: DataSpec) { var bytesToSkip = bytesToSkip diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt index 9ea9b832..86a0483a 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/LegacyPlaylistManager.kt @@ -93,11 +93,6 @@ class LegacyPlaylistManager : KoinComponent { val playlist: List get() = _playlist - // FIXME: Returns wrong count if item is twice in queue - @get:Synchronized - val currentPlayingIndex: Int - get() = _playlist.indexOf(currentPlaying) - @get:Synchronized val playlistDuration: Long get() { diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaItemTree.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaItemTree.kt index f3232bb8..d2f76e5f 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaItemTree.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaItemTree.kt @@ -22,7 +22,6 @@ import androidx.media3.common.MediaMetadata import androidx.media3.common.MediaMetadata.FOLDER_TYPE_MIXED import androidx.media3.common.MediaMetadata.FOLDER_TYPE_NONE import androidx.media3.common.MediaMetadata.FOLDER_TYPE_PLAYLISTS -import androidx.media3.common.util.Util import com.google.common.collect.ImmutableList import org.json.JSONObject @@ -89,12 +88,6 @@ object MediaItemTree { .build() } - @androidx.media3.common.util.UnstableApi - private fun loadJSONFromAsset(assets: AssetManager): String { - val buffer = assets.open("catalog.json").use { Util.toByteArray(it) } - return String(buffer, Charsets.UTF_8) - } - fun initialize(assets: AssetManager) { if (isInitialized) return isInitialized = true diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaNotificationProvider.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaNotificationProvider.kt index 02b63b8e..2eaaba9a 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaNotificationProvider.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/playback/MediaNotificationProvider.kt @@ -12,6 +12,7 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.graphics.BitmapFactory +import android.os.Build import android.os.Bundle import androidx.core.app.NotificationCompat import androidx.core.graphics.drawable.IconCompat @@ -38,6 +39,7 @@ internal class MediaNotificationProvider(context: Context) : context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager ) + @Suppress("LongMethod") override fun createNotification( mediaController: MediaController, actionFactory: ActionFactory, @@ -133,7 +135,7 @@ internal class MediaNotificationProvider(context: Context) : } private fun ensureNotificationChannel() { - if (Util.SDK_INT < 26 || + if (Util.SDK_INT < Build.VERSION_CODES.O || notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) != null ) { return diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt index 46a216a8..e9cc351d 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadService.kt @@ -84,36 +84,6 @@ class DownloadService : Service() { stopSelf() } - private fun setupOnSongCompletedHandler() { -// localMediaPlayer.onSongCompleted = { currentPlaying: DownloadFile? -> -// val index = downloader.currentPlayingIndex -// -// if (currentPlaying != null) { -// val song = currentPlaying.track -// if (song.bookmarkPosition > 0 && Settings.shouldClearBookmark) { -// val musicService = getMusicService() -// try { -// musicService.deleteBookmark(song.id) -// } catch (ignored: Exception) { -// } -// } -// } -// if (index != -1) { -// -// if (index + 1 < 0 || index + 1 >= downloader.getPlaylist().size) { -// if (Settings.shouldClearPlaylist) { -// clear(true) -// jukeboxMediaPlayer.updatePlaylist() -// } -// resetPlayback() -// } else { -// play(index + 1) -// } -// } -// null -// } - } - private fun createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt index 4348f933..3cb70bd6 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt @@ -131,6 +131,7 @@ class Downloader( } } + @Suppress("ComplexMethod", "ComplexCondition") @Synchronized fun checkDownloadsInternal() { if (!Util.isExternalStoragePresent() || !storageMonitor.isExternalStorageAvailable) { @@ -355,6 +356,7 @@ class Downloader( private inner class DownloadTask(private val downloadFile: DownloadFile) : CancellableTask() { val musicService = MusicServiceFactory.getMusicService() + @Suppress("LongMethod", "ComplexMethod", "NestedBlockDepth") override fun execute() { downloadFile.downloadPrepared = false diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt index 73450e89..766da850 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -100,6 +100,7 @@ class MediaPlayerController( } override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { + onTrackCompleted(mediaItem) legacyPlaylistManager.updateCurrentPlaying(mediaItem) publishPlaybackState() } @@ -166,6 +167,27 @@ class MediaPlayerController( Timber.d("Processed player state change") } + private fun onTrackCompleted(mediaItem: MediaItem?) { + // This method is called before we update the currentPlaying, + // so in fact currentPlaying will refer to the track that has just finished. + if (legacyPlaylistManager.currentPlaying != null) { + val song = legacyPlaylistManager.currentPlaying!!.track + if (song.bookmarkPosition > 0 && Settings.shouldClearBookmark) { + val musicService = getMusicService() + try { + musicService.deleteBookmark(song.id) + } catch (ignored: Exception) { + } + } + } + + // Playback has ended... + if (mediaItem == null && Settings.shouldClearPlaylist) { + clear(true) + jukeboxMediaPlayer.updatePlaylist() + } + } + private fun publishPlaybackState() { RxBus.playerStatePublisher.onNext( RxBus.StateWithTrack( @@ -465,7 +487,7 @@ class MediaPlayerController( playbackStateSerializer.serialize( legacyPlaylistManager.playlist, - legacyPlaylistManager.currentPlayingIndex, + currentMediaItemIndex, playerPosition ) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt index f5f47d40..77b55348 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerLifecycleSupport.kt @@ -101,7 +101,7 @@ class MediaPlayerLifecycleSupport : KoinComponent { } } - @Suppress("MagicNumber") + @Suppress("MagicNumber", "ComplexMethod") private fun handleKeyEvent(event: KeyEvent) { if (event.action != KeyEvent.ACTION_DOWN || event.repeatCount > 0) return @@ -140,6 +140,7 @@ class MediaPlayerLifecycleSupport : KoinComponent { /** * This function processes the intent that could come from other applications. */ + @Suppress("ComplexMethod") private fun handleUltrasonicIntent(action: String) { val isRunning = created