Removed some uses of GlobalScope
This commit is contained in:
parent
28d83d9437
commit
08df1b2958
8 changed files with 53 additions and 77 deletions
|
@ -35,6 +35,7 @@ import org.koin.core.component.KoinComponent
|
|||
import org.koin.core.component.inject
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.collections.set
|
||||
|
||||
|
||||
object MusicPlayerRemote : KoinComponent {
|
||||
|
|
|
@ -257,16 +257,6 @@ class CoverLrcView @JvmOverloads constructor(
|
|||
postInvalidate()
|
||||
}
|
||||
|
||||
/** 普通歌词文本字体大小 */
|
||||
fun setNormalTextSize(size: Float) {
|
||||
mNormalTextSize = size
|
||||
}
|
||||
|
||||
/** 当前歌词文本字体大小 */
|
||||
fun setCurrentTextSize(size: Float) {
|
||||
mCurrentTextSize = size
|
||||
}
|
||||
|
||||
/** 设置当前行歌词的字体颜色 */
|
||||
fun setCurrentColor(currentColor: Int) {
|
||||
mCurrentTextColor = currentColor
|
||||
|
@ -404,28 +394,6 @@ class CoverLrcView @JvmOverloads constructor(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载在线歌词,默认使用 utf-8 编码
|
||||
*
|
||||
* @param lrcUrl 歌词文件的网络地址
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun loadLrcByUrl(lrcUrl: String, charset: String? = "utf-8") {
|
||||
val flag = "url://$lrcUrl"
|
||||
this.flag = flag
|
||||
object : AsyncTask<String?, Int?, String>() {
|
||||
override fun doInBackground(vararg params: String?): String? {
|
||||
return LrcUtils.getContentFromNetwork(params[0], params[1])
|
||||
}
|
||||
|
||||
override fun onPostExecute(lrcText: String) {
|
||||
if (flag == flag) {
|
||||
loadLrc(lrcText)
|
||||
}
|
||||
}
|
||||
}.execute(lrcUrl, charset)
|
||||
}
|
||||
|
||||
/**
|
||||
* 歌词是否有效
|
||||
*
|
||||
|
@ -712,10 +680,6 @@ class CoverLrcView @JvmOverloads constructor(
|
|||
fun onPlayClick(time: Long): Boolean
|
||||
}
|
||||
|
||||
fun interface OnFlingXListener {
|
||||
fun onFlingX(velocityX: Float): Boolean
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ADJUST_DURATION: Long = 100
|
||||
private const val TIMELINE_KEEP_TIME = 4 * DateUtils.SECOND_IN_MILLIS
|
||||
|
|
|
@ -190,7 +190,7 @@ class MediaSessionCallback(
|
|||
musicService.updateMediaSessionPlaybackState()
|
||||
}
|
||||
TOGGLE_FAVORITE -> {
|
||||
MusicUtil.toggleFavorite(context, MusicPlayerRemote.currentSong)
|
||||
musicService.toggleFavorite()
|
||||
musicService.updateMediaSessionPlaybackState()
|
||||
}
|
||||
else -> {
|
||||
|
|
|
@ -72,6 +72,7 @@ import code.name.monkey.retromusic.service.notification.PlayingNotificationClass
|
|||
import code.name.monkey.retromusic.service.notification.PlayingNotificationImpl24
|
||||
import code.name.monkey.retromusic.service.playback.Playback
|
||||
import code.name.monkey.retromusic.service.playback.Playback.PlaybackCallbacks
|
||||
import code.name.monkey.retromusic.util.MusicUtil
|
||||
import code.name.monkey.retromusic.util.MusicUtil.getMediaStoreAlbumCoverUri
|
||||
import code.name.monkey.retromusic.util.MusicUtil.toggleFavorite
|
||||
import code.name.monkey.retromusic.util.PackageValidator
|
||||
|
@ -202,8 +203,11 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
|
||||
private val updateFavoriteReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
playingNotification?.updateFavorite(currentSong) { startForegroundOrNotify() }
|
||||
appWidgetCircle.notifyChange(this@MusicService, FAVORITE_STATE_CHANGED)
|
||||
isCurrentFavorite { isFavorite ->
|
||||
playingNotification?.updateFavorite(isFavorite)
|
||||
startForegroundOrNotify()
|
||||
appWidgetCircle.notifyChange(this@MusicService, FAVORITE_STATE_CHANGED)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -718,7 +722,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
quit()
|
||||
}
|
||||
ACTION_PENDING_QUIT -> pendingQuit = true
|
||||
TOGGLE_FAVORITE -> toggleFavorite(applicationContext, currentSong)
|
||||
TOGGLE_FAVORITE -> toggleFavorite()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -852,6 +856,21 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
}
|
||||
}
|
||||
|
||||
fun toggleFavorite() {
|
||||
serviceScope.launch {
|
||||
toggleFavorite(this@MusicService, currentSong)
|
||||
}
|
||||
}
|
||||
|
||||
fun isCurrentFavorite(completion: (isFavorite: Boolean) -> Unit) {
|
||||
serviceScope.launch(IO) {
|
||||
val isFavorite = MusicUtil.isFavorite(currentSong)
|
||||
withContext(Main) {
|
||||
completion(isFavorite)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun quit() {
|
||||
pause()
|
||||
stopForeground(true)
|
||||
|
@ -859,10 +878,10 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
playbackManager.release()
|
||||
AudioManagerCompat.abandonAudioFocusRequest(audioManager!!,
|
||||
AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN)
|
||||
.setOnAudioFocusChangeListener(audioFocusListener)
|
||||
.setAudioAttributes(
|
||||
AudioAttributesCompat.Builder().setContentType(CONTENT_TYPE_MUSIC).build()
|
||||
).build())
|
||||
.setOnAudioFocusChangeListener(audioFocusListener)
|
||||
.setAudioAttributes(
|
||||
AudioAttributesCompat.Builder().setContentType(CONTENT_TYPE_MUSIC).build()
|
||||
).build())
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
|
@ -1105,7 +1124,8 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
startForegroundOrNotify()
|
||||
}
|
||||
FAVORITE_STATE_CHANGED -> {
|
||||
playingNotification?.updateFavorite(currentSong) {
|
||||
isCurrentFavorite { isFavorite ->
|
||||
playingNotification?.updateFavorite(isFavorite)
|
||||
startForegroundOrNotify()
|
||||
}
|
||||
|
||||
|
@ -1122,7 +1142,11 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
}
|
||||
META_CHANGED -> {
|
||||
playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() }
|
||||
playingNotification?.updateFavorite(currentSong) { startForegroundOrNotify() }
|
||||
isCurrentFavorite { isFavorite ->
|
||||
playingNotification?.updateFavorite(isFavorite)
|
||||
startForegroundOrNotify()
|
||||
}
|
||||
|
||||
updateMediaSessionMetaData()
|
||||
updateMediaSessionPlaybackState()
|
||||
savePosition()
|
||||
|
|
|
@ -31,7 +31,7 @@ abstract class PlayingNotification(context: Context) :
|
|||
|
||||
abstract fun setPlaying(isPlaying: Boolean)
|
||||
|
||||
abstract fun updateFavorite(song: Song, onUpdate: () -> Unit)
|
||||
abstract fun updateFavorite(isFavorite: Boolean)
|
||||
|
||||
companion object {
|
||||
const val NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f
|
||||
|
|
|
@ -47,7 +47,6 @@ import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_REWIND
|
|||
import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_SKIP
|
||||
import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_TOGGLE_PAUSE
|
||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||
import code.name.monkey.retromusic.util.RetroUtil
|
||||
import code.name.monkey.retromusic.util.color.MediaNotificationProcessor
|
||||
import com.bumptech.glide.request.target.CustomTarget
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
|
@ -252,8 +251,7 @@ class PlayingNotificationClassic(
|
|||
}
|
||||
}
|
||||
|
||||
override fun updateFavorite(song: Song, onUpdate: () -> Unit) {
|
||||
}
|
||||
override fun updateFavorite(isFavorite: Boolean) {}
|
||||
|
||||
private fun buildPendingIntent(
|
||||
context: Context, action: String,
|
||||
|
|
|
@ -18,7 +18,6 @@ import android.annotation.SuppressLint
|
|||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
|
@ -39,19 +38,14 @@ import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_REWIND
|
|||
import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_SKIP
|
||||
import code.name.monkey.retromusic.service.MusicService.Companion.ACTION_TOGGLE_PAUSE
|
||||
import code.name.monkey.retromusic.service.MusicService.Companion.TOGGLE_FAVORITE
|
||||
import code.name.monkey.retromusic.util.MusicUtil
|
||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||
import com.bumptech.glide.request.target.CustomTarget
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
class PlayingNotificationImpl24(
|
||||
val context: Context,
|
||||
mediaSessionToken: MediaSessionCompat.Token
|
||||
val context: MusicService,
|
||||
mediaSessionToken: MediaSessionCompat.Token,
|
||||
) : PlayingNotification(context) {
|
||||
|
||||
init {
|
||||
|
@ -184,14 +178,8 @@ class PlayingNotificationImpl24(
|
|||
mActions[2] = buildPlayAction(isPlaying)
|
||||
}
|
||||
|
||||
override fun updateFavorite(song: Song, onUpdate: () -> Unit) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val isFavorite = MusicUtil.repository.isSongFavorite(song.id)
|
||||
withContext(Dispatchers.Main) {
|
||||
mActions[0] = buildFavoriteAction(isFavorite)
|
||||
onUpdate()
|
||||
}
|
||||
}
|
||||
override fun updateFavorite(isFavorite: Boolean) {
|
||||
mActions[0] = buildFavoriteAction(isFavorite)
|
||||
}
|
||||
|
||||
private fun retrievePlaybackAction(action: String): PendingIntent {
|
||||
|
@ -208,9 +196,9 @@ class PlayingNotificationImpl24(
|
|||
companion object {
|
||||
|
||||
fun from(
|
||||
context: Context,
|
||||
context: MusicService,
|
||||
notificationManager: NotificationManager,
|
||||
mediaSession: MediaSessionCompat
|
||||
mediaSession: MediaSessionCompat,
|
||||
): PlayingNotification {
|
||||
if (VersionUtils.hasOreo()) {
|
||||
createNotificationChannel(context, notificationManager)
|
||||
|
|
|
@ -28,8 +28,7 @@ import code.name.monkey.retromusic.repository.Repository
|
|||
import code.name.monkey.retromusic.repository.SongRepository
|
||||
import code.name.monkey.retromusic.service.MusicService
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jaudiotagger.audio.AudioFileIO
|
||||
import org.jaudiotagger.tag.FieldKey
|
||||
|
@ -100,7 +99,7 @@ object MusicUtil : KoinComponent {
|
|||
|
||||
fun getArtistInfoString(
|
||||
context: Context,
|
||||
artist: Artist
|
||||
artist: Artist,
|
||||
): String {
|
||||
val albumCount = artist.albumCount
|
||||
val songCount = artist.songCount
|
||||
|
@ -190,7 +189,7 @@ object MusicUtil : KoinComponent {
|
|||
|
||||
fun getPlaylistInfoString(
|
||||
context: Context,
|
||||
songs: List<Song>
|
||||
songs: List<Song>,
|
||||
): String {
|
||||
val duration = getTotalDuration(songs)
|
||||
return buildInfoString(
|
||||
|
@ -201,7 +200,7 @@ object MusicUtil : KoinComponent {
|
|||
|
||||
fun playlistInfoString(
|
||||
context: Context,
|
||||
songs: List<SongEntity>
|
||||
songs: List<SongEntity>,
|
||||
): String {
|
||||
return getSongCountString(context, songs.size)
|
||||
}
|
||||
|
@ -299,7 +298,7 @@ object MusicUtil : KoinComponent {
|
|||
fun insertAlbumArt(
|
||||
context: Context,
|
||||
albumId: Long,
|
||||
path: String?
|
||||
path: String?,
|
||||
) {
|
||||
val contentResolver = context.contentResolver
|
||||
val artworkUri = "content://media/external/audio/albumart".toUri()
|
||||
|
@ -334,8 +333,8 @@ object MusicUtil : KoinComponent {
|
|||
}
|
||||
|
||||
val repository = get<Repository>()
|
||||
fun toggleFavorite(context: Context, song: Song) {
|
||||
GlobalScope.launch {
|
||||
suspend fun toggleFavorite(context: Context, song: Song) {
|
||||
withContext(IO) {
|
||||
val playlist: PlaylistEntity = repository.favoritePlaylist()
|
||||
val songEntity = song.toSongEntity(playlist.playListId)
|
||||
val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty()
|
||||
|
@ -348,11 +347,13 @@ object MusicUtil : KoinComponent {
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun isFavorite(song: Song) = repository.isSongFavorite(song.id)
|
||||
|
||||
fun deleteTracks(
|
||||
activity: FragmentActivity,
|
||||
songs: List<Song>,
|
||||
safUris: List<Uri>?,
|
||||
callback: Runnable?
|
||||
callback: Runnable?,
|
||||
) {
|
||||
val songRepository: SongRepository = get()
|
||||
val projection = arrayOf(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue