adding some third party intent handling related improvements

This commit is contained in:
tibbi 2020-10-22 23:26:58 +02:00
parent 752daadc6d
commit efbd3c983b
2 changed files with 39 additions and 24 deletions

View file

@ -44,8 +44,7 @@ class TrackActivity : SimpleActivity() {
nextTrackPlaceholder = resources.getColoredDrawableWithColor(R.drawable.ic_headset, config.textColor)
bus = EventBus.getDefault()
bus!!.register(this)
isThirdPartyIntent = intent.action == Intent.ACTION_VIEW
setupButtons()
(activity_track_appbar.layoutParams as ConstraintLayout.LayoutParams).topMargin = statusBarHeight
activity_track_holder.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
@ -53,15 +52,21 @@ class TrackActivity : SimpleActivity() {
finish()
}
isThirdPartyIntent = intent.action == Intent.ACTION_VIEW
if (isThirdPartyIntent) {
initThirdPartyIntent()
return
}
val trackType = object : TypeToken<Track>() {}.type
val track = Gson().fromJson<Track>(intent.getStringExtra(TRACK), trackType) ?: MusicService.mCurrTrack
if (track == null) {
toast(R.string.unknown_error_occurred)
finish()
return
}
setupTrackInfo(track)
setupButtons()
if (intent.getBooleanExtra(RESTART_PLAYER, false)) {
Intent(this, MusicService::class.java).apply {
@ -73,14 +78,9 @@ class TrackActivity : SimpleActivity() {
sendIntent(BROADCAST_STATUS)
}
if (isThirdPartyIntent) {
next_track_holder.beGone()
} else {
next_track_holder.beVisible()
next_track_holder.background = ColorDrawable(config.backgroundColor)
next_track_holder.setOnClickListener {
startActivity(Intent(applicationContext, QueueActivity::class.java))
}
next_track_holder.background = ColorDrawable(config.backgroundColor)
next_track_holder.setOnClickListener {
startActivity(Intent(applicationContext, QueueActivity::class.java))
}
}
@ -114,6 +114,16 @@ class TrackActivity : SimpleActivity() {
activity_track_progress_max.text = track.duration.getFormattedDuration()
}
private fun initThirdPartyIntent() {
next_track_holder.beGone()
val fileUri = intent.data
Intent(this, MusicService::class.java).apply {
data = fileUri
action = INIT_PATH
startService(this)
}
}
private fun setupNextTrackInfo(track: Track?) {
val artist = if (track?.artist?.trim()?.isNotEmpty() == true && track.artist != MediaStore.UNKNOWN_STRING) {
"${track.artist}"

View file

@ -31,7 +31,10 @@ import com.simplemobiletools.commons.extensions.getColoredBitmap
import com.simplemobiletools.commons.extensions.getRealPathFromURI
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.MainActivity
import com.simplemobiletools.musicplayer.databases.SongsDatabase
@ -263,7 +266,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
private fun handleFinish() {
broadcastTrackProgress(0)
destroyPlayer()
stopSelf()
}
private fun handleRefreshList(intent: Intent) {
@ -776,19 +779,21 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}
private fun destroyPlayer() {
val position = mPlayer?.currentPosition ?: 0
ensureBackgroundThread {
queueDAO.resetCurrent()
if (!mIsThirdPartyIntent) {
val position = mPlayer?.currentPosition ?: 0
ensureBackgroundThread {
queueDAO.resetCurrent()
if (mCurrTrack != null) {
queueDAO.saveCurrentTrack(mCurrTrack!!.id, position)
if (mCurrTrack != null) {
queueDAO.saveCurrentTrack(mCurrTrack!!.id, position)
}
mTracks.forEachIndexed { index, track ->
queueDAO.setOrder(track.id, index)
}
mCurrTrack = null
}
mTracks.forEachIndexed { index, track ->
queueDAO.setOrder(track.id, index)
}
mCurrTrack = null
}
mPlayer?.stop()