Skip to content

Commit

Permalink
Implemented downloaded songs in Android Auto fast4x#731
Browse files Browse the repository at this point in the history
  • Loading branch information
fast4x committed Mar 1, 2024
1 parent 755e5b4 commit af38109
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
4 changes: 4 additions & 0 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import kotlinx.coroutines.flow.Flow
interface Database {
companion object : Database by DatabaseInitializer.Instance.database

@Transaction
@Query("SELECT * FROM Song")
fun listAllSongs(): List<Song>

@Query("SELECT id FROM Playlist WHERE name = :playlistName")
fun playlistExistByName(playlistName: String): Long

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,52 @@ import android.content.ServiceConnection
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import android.os.Process
import android.support.v4.media.MediaBrowserCompat.MediaItem
import android.support.v4.media.MediaDescriptionCompat
import android.support.v4.media.session.MediaSessionCompat
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.media.MediaBrowserServiceCompat
import androidx.media3.common.Player
import androidx.media3.common.util.Log
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.cache.Cache
import androidx.media3.exoplayer.offline.Download
import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.models.Album
import it.vfsfitvnm.vimusic.models.PlaylistPreview
import it.vfsfitvnm.vimusic.models.Song
import it.vfsfitvnm.vimusic.models.SongWithContentLength
import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.transaction
import it.vfsfitvnm.vimusic.utils.asMediaItem
import it.vfsfitvnm.vimusic.utils.forcePlayAtIndex
import it.vfsfitvnm.vimusic.utils.forceSeekToNext
import it.vfsfitvnm.vimusic.utils.forceSeekToPrevious
import it.vfsfitvnm.vimusic.utils.intent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import android.support.v4.media.MediaBrowserCompat.MediaItem //as BrowserMediaItem
import android.support.v4.media.MediaDescriptionCompat

class PlayerMediaBrowserService : MediaBrowserServiceCompat(), ServiceConnection {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
private val coroutineScopeDb = CoroutineScope(Dispatchers.Main)
private var lastSongs = emptyList<Song>()

private var bound = false
Expand Down Expand Up @@ -251,6 +266,15 @@ class PlayerMediaBrowserService : MediaBrowserServiceCompat(), ServiceConnection
override fun onSeekTo(pos: Long) = player.seekTo(pos)
override fun onSkipToQueueItem(id: Long) = player.seekToDefaultPosition(id.toInt())

@FlowPreview
@ExperimentalCoroutinesApi
@UnstableApi
override fun onCustomAction(action: String?, extras: Bundle?) {
super.onCustomAction(action, extras)
if (action == "LIKE") {}
if (action == "DOWNLOAD") {}
}

@UnstableApi
override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {
val data = mediaId?.split('/') ?: return
Expand Down Expand Up @@ -284,10 +308,11 @@ class PlayerMediaBrowserService : MediaBrowserServiceCompat(), ServiceConnection
.shuffled()

MediaId.downloaded -> {
DownloadUtil.getDownloadManager(this as Context)
DownloadUtil.getDownloads()
DownloadUtil.downloads.value.keys.toList()
.let { Database.getSongsListNoFlow(it) }
val downloads = DownloadUtil.downloads.value
Database.listAllSongs()
.filter {
downloads[it.id]?.state == Download.STATE_COMPLETED
}
}

MediaId.playlists -> data
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/service/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class PlayerService : InvincibleService(),
return binder
}

@FlowPreview
@SuppressLint("Range")
@UnstableApi
override fun onCreate() {
Expand Down Expand Up @@ -461,8 +462,6 @@ class PlayerService : InvincibleService(),

maybeRestorePlayerQueue()



mediaSession = MediaSessionCompat(baseContext, "PlayerService")
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
mediaSession.setCallback(SessionCallback(player))
Expand Down Expand Up @@ -1436,14 +1435,16 @@ class PlayerService : InvincibleService(),
}
}

@FlowPreview
private fun toggleLikeAction() = mediaItemState.value?.let { mediaItem ->
transaction {
Database.like(
mediaItem.mediaId,
if (isLikedState.value) null else System.currentTimeMillis()
)
}
}.let { }
transaction {
Database.like(
mediaItem.mediaId,
if (isLikedState.value) null else System.currentTimeMillis()
)
}
}.let { }


private fun toggleDownloadAction() = mediaDownloadedItemState.value?.let { mediaItem ->
manageDownload(
Expand Down
2 changes: 2 additions & 0 deletions assets/tools/StartAA.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C:\Users\rinor\AppData\Local\Android\Sdk\platform-tools\adb forward tcp:5277 tcp:5277
C:\Users\rinor\AppData\Local\Android\Sdk\extras\google\auto\desktop-head-unit.exe

0 comments on commit af38109

Please sign in to comment.