@@ -108,14 +108,17 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
108108 mClicksCnt = 0
109109 }
110110
111+ private val notificationHandler = Handler ()
111112 private var notificationHelper: NotificationHelper ? = null
112- private var isForeground: Boolean = false
113113
114114 override fun onCreate () {
115115 super .onCreate()
116116 mCoverArtHeight = resources.getDimension(R .dimen.top_art_height).toInt()
117117 createMediaSession()
118118
119+ notificationHelper = NotificationHelper .createInstance(context = this , mMediaSession!! )
120+ startForegroundAndNotify()
121+
119122 mAudioManager = getSystemService(Context .AUDIO_SERVICE ) as AudioManager
120123 if (isOreoPlus()) {
121124 mOreoFocusHandler = OreoAudioFocusHandler (application)
@@ -124,8 +127,6 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
124127 if (! isQPlus() && ! hasPermission(getPermissionToRequest())) {
125128 EventBus .getDefault().post(Events .NoStoragePermission ())
126129 }
127-
128- notificationHelper = NotificationHelper .createInstance(context = this , mMediaSession!! )
129130 }
130131
131132 private fun createMediaSession () {
@@ -182,7 +183,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
182183
183184 MediaButtonReceiver .handleIntent(mMediaSession!! , intent)
184185 if (action != DISMISS && action != FINISH ) {
185- startForegroundOrNotify ()
186+ startForegroundAndNotify ()
186187 }
187188 return START_NOT_STICKY
188189 }
@@ -239,7 +240,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
239240
240241 mWasPlayingAtFocusLost = false
241242 initMediaPlayerIfNeeded()
242- startForegroundOrNotify ()
243+ startForegroundAndNotify ()
243244 mIsServiceInitialized = true
244245 }
245246
@@ -324,7 +325,9 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
324325 }
325326
326327 fun handleDismiss () {
327- pauseTrack()
328+ if (isPlaying()) {
329+ pauseTrack(false )
330+ }
328331 stopForegroundAndNotification()
329332 }
330333
@@ -377,16 +380,19 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
377380 updateUI()
378381 }
379382
383+ @Synchronized
380384 private fun updateUI () {
381- if (mPlayer != null ) {
382- EventBus .getDefault().post(Events .QueueUpdated (mTracks))
383- mCurrTrackCover = getAlbumImage().first
384- trackChanged()
385+ ensureBackgroundThread {
386+ if (mPlayer != null ) {
387+ EventBus .getDefault().post(Events .QueueUpdated (mTracks))
388+ mCurrTrackCover = getAlbumImage().first
389+ trackChanged()
385390
386- val secs = getPosition()!! / 1000
387- broadcastTrackProgress(secs)
391+ val secs = getPosition()!! / 1000
392+ broadcastTrackProgress(secs)
393+ }
394+ trackStateChanged(isPlaying())
388395 }
389- trackStateChanged(isPlaying())
390396 }
391397
392398 private fun initMediaPlayerIfNeeded () {
@@ -465,37 +471,36 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
465471 }
466472 }
467473
468- private fun startForegroundOrNotify () {
469- if (mCurrTrackCover?.isRecycled == true ) {
470- mCurrTrackCover = resources.getColoredBitmap(R .drawable.ic_headset, getProperTextColor())
471- }
474+ private fun startForegroundAndNotify () {
475+ notificationHandler.removeCallbacksAndMessages(null )
476+ notificationHandler.postDelayed({
477+ if (mCurrTrackCover?.isRecycled == true ) {
478+ mCurrTrackCover = resources.getColoredBitmap(R .drawable.ic_headset, getProperTextColor())
479+ }
472480
473- notificationHelper?.createPlayerNotification(
474- track = mCurrTrack,
475- isPlaying = isPlaying(),
476- largeIcon = mCurrTrackCover,
477- ) {
478- if ( ! isForeground) {
481+ notificationHelper?.createPlayerNotification(
482+ track = mCurrTrack,
483+ isPlaying = isPlaying(),
484+ largeIcon = mCurrTrackCover,
485+ ) {
486+ notificationHelper?.notify( NOTIFICATION_ID , it)
479487 try {
480488 if (isQPlus()) {
481489 startForeground(NOTIFICATION_ID , it, ServiceInfo .FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK )
482490 } else {
483491 startForeground(NOTIFICATION_ID , it)
484492 }
485- isForeground = true
486493 } catch (ignored: IllegalStateException ) {
487494 }
488- } else {
489- notificationHelper?.notify(NOTIFICATION_ID , it)
490495 }
491- }
496+ }, 200L )
492497 }
493498
494499 private fun stopForegroundAndNotification () {
500+ notificationHandler.removeCallbacksAndMessages(null )
495501 @Suppress(" DEPRECATION" )
496502 stopForeground(true )
497503 notificationHelper?.cancel(NOTIFICATION_ID )
498- isForeground = false
499504 }
500505
501506 private fun getNextQueueItem (): QueueItem {
@@ -539,17 +544,16 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
539544 }
540545 }
541546
542- fun pauseTrack () {
547+ fun pauseTrack (notify : Boolean = true ) {
543548 initMediaPlayerIfNeeded()
544549 mPlayer!! .pause()
545- trackStateChanged(false )
550+ trackStateChanged(false , notify = notify )
546551 updateMediaSessionState()
547552 saveTrackProgress()
548553 // do not call stopForeground on android 12 as it may cause a crash later
549554 if (! isSPlus()) {
550555 @Suppress(" DEPRECATION" )
551556 stopForeground(false )
552- isForeground = false
553557 }
554558 }
555559
@@ -998,7 +1002,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
9981002 handleProgressHandler(isPlaying)
9991003 broadcastTrackStateChange(isPlaying)
10001004 if (notify) {
1001- startForegroundOrNotify ()
1005+ startForegroundAndNotify ()
10021006 }
10031007
10041008 if (isPlaying) {
@@ -1096,13 +1100,17 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
10961100 }
10971101
10981102 private fun saveTrackProgress () {
1099- if (mCurrTrack != null && getPosition() != 0 ) {
1103+ if (mCurrTrack != null ) {
11001104 ensureBackgroundThread {
11011105 val trackId = mCurrTrack?.mediaStoreId ? : return @ensureBackgroundThread
1102- val position = getPosition() ? : return @ensureBackgroundThread
1106+ val position = getPosition()
11031107 queueDAO.apply {
11041108 resetCurrent()
1105- saveCurrentTrack(trackId, position)
1109+ if (position == null || position == 0 ) {
1110+ saveCurrentTrack(trackId)
1111+ } else {
1112+ saveCurrentTrackProgress(trackId, position)
1113+ }
11061114 }
11071115 }
11081116 }
0 commit comments