Skip to content

Commit d335c2c

Browse files
committed
Download and cache support added
1 parent c102dd8 commit d335c2c

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

goonj/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ dependencies {
5858
// Cast framework
5959
implementation 'com.google.android.gms:play-services-cast-framework:17.1.0'
6060

61-
// Architecture components
62-
// implementation 'android.arch.work:work-runtime-ktx:1.0.1'
63-
// implementation "android.arch.persistence.room:runtime:1.1.1"
64-
// kapt "android.arch.persistence.room:compiler:1.1.1"
65-
6661
// Rx
6762
api "io.reactivex.rxjava2:rxkotlin:2.4.0"
6863
api 'io.reactivex.rxjava2:rxandroid:2.1.1'

goonj/src/main/java/ai/rever/goonj/Goonj.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,14 @@ object Goonj {
241241

242242
val downloadStateFlowable: Flowable<DownloadState> get() = GoonjDownloadManager.downloadStateBehaviorSubject.toFlowable(BackpressureStrategy.LATEST).observeOn(AndroidSchedulers.mainThread())
243243

244+
fun isDownloaded(trackId: String) = GoonjDownloadManager.isDownloaded(trackId)
245+
244246
val trackCompletionObservable: Observable<Track> get() = GoonjPlayerManager.trackCompleteSubject.observeOn(AndroidSchedulers.mainThread())
245247

248+
var iconWhileDownload: Int = R.drawable.ic_album
249+
250+
var maxCacheBytes: Long = 200 * 1024 * 1024
251+
246252
// internal method
247253
internal fun startForeground(notificationId: Int, notification: Notification?) = run {
248254
startForeground(notificationId, notification)

goonj/src/main/java/ai/rever/goonj/download/AudioDownloadService.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.rever.goonj.download
22

3+
import ai.rever.goonj.Goonj
34
import ai.rever.goonj.R
45
import android.app.Notification
56
import androidx.annotation.Nullable
@@ -19,10 +20,12 @@ internal class AudioDownloadService : DownloadService (
1920
R.string.channel_download_name
2021
) {
2122

22-
override fun getForegroundNotification(downloads: MutableList<Download>?): Notification =
23-
DownloadNotificationHelper(this, DOWNLOAD_CHANNEL_ID)
24-
.buildProgressNotification(R.drawable.exo_icon_play,
25-
null, null, downloads)
23+
override fun getForegroundNotification(downloads: MutableList<Download>?): Notification {
24+
GoonjDownloadManager.downloadStateBehaviorSubject.onNext(DownloadState.DOWNLOADING)
25+
return DownloadNotificationHelper(this, DOWNLOAD_CHANNEL_ID)
26+
.buildProgressNotification(Goonj.iconWhileDownload,
27+
null, null, downloads)
28+
}
2629

2730
override fun getDownloadManager(): DownloadManager {
2831
return GoonjDownloadManager.downloadManager

goonj/src/main/java/ai/rever/goonj/models/Track.kt

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ import com.google.android.gms.common.images.WebImage
2222
import kotlinx.android.parcel.Parcelize
2323
import java.util.*
2424
import android.os.Parcel
25+
import io.reactivex.Flowable
26+
import kotlinx.android.parcel.IgnoredOnParcel
2527
import java.io.IOException
2628

27-
private val ByteArray.track: Track? get() = run {
28-
val parcel = Parcel.obtain()
29-
parcel.unmarshall(this, 0, size)
30-
parcel.setDataPosition(0) // This is extremely important!
29+
//private val ByteArray.track: Track? get() = run {
30+
// val parcel = Parcel.obtain()
31+
// parcel.unmarshall(this, 0, size)
32+
// parcel.setDataPosition(0) // This is extremely important!
33+
//
34+
// val result = Track.CREATOR.createFromParcel(parcel)
35+
// parcel.recycle()
36+
// result
37+
//}
3138

32-
val result = Track.CREATOR.createFromParcel(parcel)
33-
parcel.recycle()
34-
result
35-
}
36-
37-
val Download.track get() = request.data.track
39+
//val Download.track get() = request.data.track
3840

3941
internal val Track.Companion.CREATOR get() = TrackCreator.getCreator()
4042

@@ -51,6 +53,7 @@ class Track (var id: String = "",
5153

5254
companion object{}
5355

56+
@IgnoredOnParcel
5457
private val mediaInfo: MediaInfo? get() {
5558
if (url.isEmpty()) return null
5659
val musicMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK)
@@ -69,12 +72,14 @@ class Track (var id: String = "",
6972
.build()
7073
}
7174

75+
@IgnoredOnParcel
7276
val mediaLoadRequestData: MediaLoadRequestData? get() {
7377
return MediaLoadRequestData.Builder()
7478
.setMediaInfo(mediaInfo?: return null)
7579
.build()
7680
}
7781

82+
@IgnoredOnParcel
7883
val mediaDescription: MediaDescriptionCompat get() {
7984
val extras = Bundle()
8085

@@ -121,23 +126,28 @@ class Track (var id: String = "",
121126
}
122127
}
123128

124-
val toByteArray: ByteArray get() = run {
125-
val parcel = Parcel.obtain()
126-
writeToParcel(parcel, 0)
127-
val bytes = parcel.marshall()
128-
parcel.recycle()
129-
return bytes
130-
}
129+
// @IgnoredOnParcel
130+
// val toByteArray: ByteArray get() = run {
131+
// val parcel = Parcel.obtain()
132+
// writeToParcel(parcel, 0)
133+
// val bytes = parcel.marshall()
134+
// parcel.recycle()
135+
// return bytes
136+
// }
131137

138+
@IgnoredOnParcel
132139
val download: Download? get() = try {
133-
GoonjDownloadManager.downloadManager.downloadIndex.getDownload(url)
140+
GoonjDownloadManager.downloadManager.downloadIndex.getDownload(id)
134141
} catch (e: IOException) {
135142
null
136143
}
137144

138-
val downloadFlowable get() = downloadStateFlowable.skipWhile {
139-
it == DownloadState.REQUIREMENT_STATE_CHANGED
140-
}.map { download }
145+
@IgnoredOnParcel
146+
val isDownloadActiveFlowable: Flowable<Boolean?> get() = downloadStateFlowable.map { download != null }
147+
148+
fun requestDownload() = GoonjDownloadManager.addDownload(id, url.toUri())
149+
150+
fun removeDownload() = GoonjDownloadManager.removeDownload(id)
141151
}
142152

143153

goonj/src/main/java/ai/rever/goonj/player/imp/LocalAudioPlayer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ internal class LocalAudioPlayer: AudioPlayer {
236236
private var isNotPrepared = true
237237
override fun enqueue(track: Track, index : Int) {
238238
val mediaSource = ProgressiveMediaSource.Factory(cacheDataSourceFactory)
239-
.createMediaSource(track.url.toUri())
239+
.setCustomCacheKey(track.id)
240+
.createMediaSource(track.url.toUri())
240241

241242
concatenatingMediaSource.addMediaSource(index, mediaSource)
242243

0 commit comments

Comments
 (0)