funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/playback/CacheDataSourceFactoryProvi...

45 lines
1.5 KiB
Kotlin
Raw Normal View History

2021-08-13 14:54:42 +02:00
package audio.funkwhale.ffa.playback
import android.content.Context
import audio.funkwhale.ffa.R
import audio.funkwhale.ffa.utils.OAuth
import audio.funkwhale.ffa.utils.Settings
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import com.google.android.exoplayer2.upstream.FileDataSource
import com.google.android.exoplayer2.upstream.cache.Cache
import com.google.android.exoplayer2.upstream.cache.CacheDataSource
import com.google.android.exoplayer2.util.Util
class CacheDataSourceFactoryProvider(
private val oAuth: OAuth,
private val exoCache: Cache,
private val exoDownloadCache: Cache
2021-08-13 14:54:42 +02:00
) {
2021-08-29 15:41:50 +02:00
fun create(context: Context): CacheDataSource.Factory {
2021-08-13 14:54:42 +02:00
2021-08-29 15:41:50 +02:00
val playbackCache = CacheDataSource.Factory().apply {
setCache(exoCache)
setUpstreamDataSourceFactory(createDatasourceFactory(context, oAuth))
}
2021-08-13 14:54:42 +02:00
2021-08-29 15:41:50 +02:00
return CacheDataSource.Factory().apply {
setCache(exoDownloadCache)
setUpstreamDataSourceFactory(playbackCache)
setCacheReadDataSourceFactory(FileDataSource.Factory())
setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
}
2021-08-13 14:54:42 +02:00
}
private fun createDatasourceFactory(context: Context, oAuth: OAuth): DataSource.Factory {
val http = DefaultHttpDataSourceFactory(
Util.getUserAgent(context, context.getString(R.string.app_name))
2021-08-13 14:54:42 +02:00
)
return if (!Settings.isAnonymous()) {
OAuth2DatasourceFactory(context, http, oAuth)
2021-08-13 14:54:42 +02:00
} else {
http
}
}
}