Code Cleanup

This commit is contained in:
Prathamesh More 2022-04-01 20:06:25 +05:30
parent 7e3b4404cf
commit 4dc84c2564
4 changed files with 92 additions and 91 deletions

View file

@ -9,6 +9,7 @@ import android.media.MediaPlayer
import android.media.audiofx.AudioEffect import android.media.audiofx.AudioEffect
import android.net.Uri import android.net.Uri
import android.os.PowerManager import android.os.PowerManager
import android.util.Log
import android.widget.Toast import android.widget.Toast
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
@ -296,6 +297,7 @@ class CrossFadePlayer(val context: Context) : Playback, MediaPlayer.OnCompletion
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
) )
.show() .show()
Log.e(TAG, what.toString() + extra)
return false return false
} }
@ -354,6 +356,10 @@ class CrossFadePlayer(val context: Context) : Playback, MediaPlayer.OnCompletion
override fun setCrossFadeDuration(duration: Int) { override fun setCrossFadeDuration(duration: Int) {
crossFadeDuration = duration crossFadeDuration = duration
} }
companion object {
val TAG: String = CrossFadePlayer::class.java.simpleName
}
} }
internal fun crossFadeScope(): CoroutineScope = CoroutineScope(Job() + Dispatchers.Main) internal fun crossFadeScope(): CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)

View file

@ -330,6 +330,7 @@ public class MultiPlayer
context.getResources().getString(R.string.unplayable_file), context.getResources().getString(R.string.unplayable_file),
Toast.LENGTH_SHORT) Toast.LENGTH_SHORT)
.show(); .show();
Log.e(TAG, String.valueOf(what) + extra);
} }
return false; return false;
} }

View file

@ -48,7 +48,7 @@ import androidx.media.AudioManagerCompat
import androidx.media.MediaBrowserServiceCompat import androidx.media.MediaBrowserServiceCompat
import androidx.media.session.MediaButtonReceiver.handleIntent import androidx.media.session.MediaButtonReceiver.handleIntent
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import code.name.monkey.appthemehelper.util.VersionUtils.hasQ import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.* import code.name.monkey.retromusic.*
import code.name.monkey.retromusic.activities.LockScreenActivity import code.name.monkey.retromusic.activities.LockScreenActivity
import code.name.monkey.retromusic.appwidgets.* import code.name.monkey.retromusic.appwidgets.*
@ -93,10 +93,8 @@ import code.name.monkey.retromusic.volume.OnAudioVolumeChangedListener
import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.target.SimpleTarget import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import com.google.android.gms.cast.framework.media.MediaIntentReceiver
import org.koin.java.KoinJavaComponent.get import org.koin.java.KoinJavaComponent.get
import java.util.* import java.util.*
import kotlin.collections.ArrayList
/** /**
* @author Karim Abou Zeid (kabouzeid), Andrew Neal. Modified by Prathamesh More * @author Karim Abou Zeid (kabouzeid), Andrew Neal. Modified by Prathamesh More
@ -824,17 +822,16 @@ class MusicService : MediaBrowserServiceCompat(),
} }
} }
@Synchronized
fun openTrackAndPrepareNextAt(position: Int): Boolean { fun openTrackAndPrepareNextAt(position: Int): Boolean {
synchronized(this) { this.position = position
this.position = position val prepared = openCurrent()
val prepared = openCurrent() if (prepared) {
if (prepared) { prepareNextImpl()
prepareNextImpl()
}
notifyChange(META_CHANGED)
notHandledMetaChangedForCurrentTrack = false
return prepared
} }
notifyChange(META_CHANGED)
notHandledMetaChangedForCurrentTrack = false
return prepared
} }
fun pause() { fun pause() {
@ -858,48 +855,47 @@ class MusicService : MediaBrowserServiceCompat(),
} }
} }
@Synchronized
fun play() { fun play() {
synchronized(this) { if (requestFocus()) {
if (requestFocus()) { if (playback != null && !playback!!.isPlaying) {
if (playback != null && !playback!!.isPlaying) { if (!playback!!.isInitialized) {
if (!playback!!.isInitialized) { playSongAt(getPosition())
playSongAt(getPosition()) } else {
} else { //Don't Start playing when it's casting
//Don't Start playing when it's casting if (isCasting) {
if (isCasting) { return
return
}
startFadeAnimator(playback!!, true) {
// Code when Animator Ends
if (!becomingNoisyReceiverRegistered) {
registerReceiver(
becomingNoisyReceiver,
becomingNoisyReceiverIntentFilter
)
becomingNoisyReceiverRegistered = true
}
if (notHandledMetaChangedForCurrentTrack) {
handleChangeInternal(META_CHANGED)
notHandledMetaChangedForCurrentTrack = false
}
// fixes a bug where the volume would stay ducked because the
// AudioManager.AUDIOFOCUS_GAIN event is not sent
playerHandler?.removeMessages(DUCK)
playerHandler?.sendEmptyMessage(UNDUCK)
}
//Start Playback with Animator
playback?.start()
notifyChange(PLAY_STATE_CHANGED)
} }
startFadeAnimator(playback!!, true) {
// Code when Animator Ends
if (!becomingNoisyReceiverRegistered) {
registerReceiver(
becomingNoisyReceiver,
becomingNoisyReceiverIntentFilter
)
becomingNoisyReceiverRegistered = true
}
if (notHandledMetaChangedForCurrentTrack) {
handleChangeInternal(META_CHANGED)
notHandledMetaChangedForCurrentTrack = false
}
// fixes a bug where the volume would stay ducked because the
// AudioManager.AUDIOFOCUS_GAIN event is not sent
playerHandler?.removeMessages(DUCK)
playerHandler?.sendEmptyMessage(UNDUCK)
}
//Start Playback with Animator
playback?.start()
notifyChange(PLAY_STATE_CHANGED)
} }
} else {
Toast.makeText(
this, resources.getString(R.string.audio_focus_denied), Toast.LENGTH_SHORT
)
.show()
} }
} else {
Toast.makeText(
this, resources.getString(R.string.audio_focus_denied), Toast.LENGTH_SHORT
)
.show()
} }
} }
@ -949,14 +945,13 @@ class MusicService : MediaBrowserServiceCompat(),
} }
} }
@Synchronized
fun prepareNextImpl() { fun prepareNextImpl() {
synchronized(this) { try {
try { val nextPosition = getNextPosition(false)
val nextPosition = getNextPosition(false) playback?.setNextDataSource(getTrackUri(getSongAt(nextPosition)))
playback?.setNextDataSource(getTrackUri(getSongAt(nextPosition))) this.nextPosition = nextPosition
this.nextPosition = nextPosition } catch (ignored: Exception) {
} catch (ignored: Exception) {
}
} }
} }
@ -1075,18 +1070,17 @@ class MusicService : MediaBrowserServiceCompat(),
savePositionInTrack() savePositionInTrack()
} }
@Synchronized
fun seek(millis: Int): Int { fun seek(millis: Int): Int {
synchronized(this) { return try {
return try { var newPosition = 0
var newPosition = 0 if (playback != null) {
if (playback != null) { newPosition = playback!!.seek(millis)
newPosition = playback!!.seek(millis)
}
throttledSeekHandler?.notifySeek()
newPosition
} catch (e: Exception) {
-1
} }
throttledSeekHandler?.notifySeek()
newPosition
} catch (e: Exception) {
-1
} }
} }
@ -1251,19 +1245,19 @@ class MusicService : MediaBrowserServiceCompat(),
private fun startForegroundOrNotify() { private fun startForegroundOrNotify() {
if (playingNotification != null && currentSong.id != -1L) { if (playingNotification != null && currentSong.id != -1L) {
val isPlaying = isPlaying if (!VersionUtils.hasS()) {
if (isForeground != isPlaying && !isPlaying) { if (isForeground && !isPlaying) {
// This makes the notification dismissible // This makes the notification dismissible
// We can't call stopForeground(false) on A12 though, which may result in crashes // We can't call stopForeground(false) on A12 though, which may result in crashes
// when we call startForeground after that e.g. when Alarm goes off, // when we call startForeground after that e.g. when Alarm goes off
if (VERSION.SDK_INT < VERSION_CODES.S) {
stopForeground(false) stopForeground(false)
isForeground = false isForeground = false
} }
} }
if (!isForeground && isPlaying) { if (!isForeground) {
// Specify that this is a media service, if supported. // Specify that this is a media service, if supported.
if (hasQ()) { if (VersionUtils.hasQ()) {
startForeground( startForeground(
PlayingNotification.NOTIFICATION_ID, playingNotification!!.build(), PlayingNotification.NOTIFICATION_ID, playingNotification!!.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
@ -1282,7 +1276,6 @@ class MusicService : MediaBrowserServiceCompat(),
) )
} }
} }
return
} }
private fun stopForegroundAndNotification() { private fun stopForegroundAndNotification() {
@ -1291,24 +1284,22 @@ class MusicService : MediaBrowserServiceCompat(),
isForeground = false isForeground = false
} }
@Synchronized
private fun openCurrent(): Boolean { private fun openCurrent(): Boolean {
synchronized(this) { return try {
try { if (playback != null) {
if (playback != null) { return playback!!.setDataSource(
return playback!!.setDataSource( getTrackUri(
getTrackUri( Objects.requireNonNull(
Objects.requireNonNull( currentSong
currentSong
)
) )
) )
} )
} catch (e: Exception) { } else false
e.printStackTrace() } catch (e: Exception) {
return false e.printStackTrace()
} false
} }
return false
} }
private fun playFromPlaylist(intent: Intent) { private fun playFromPlaylist(intent: Intent) {

View file

@ -46,7 +46,10 @@ import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroColorUtil import code.name.monkey.retromusic.util.RetroColorUtil
import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition import com.bumptech.glide.request.transition.Transition
import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
class PlayingNotificationImpl24( class PlayingNotificationImpl24(