2021-08-09 06:50:46 +02:00
|
|
|
package audio.funkwhale.ffa.koin
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import audio.funkwhale.ffa.playback.CacheDataSourceFactoryProvider
|
|
|
|
import audio.funkwhale.ffa.playback.MediaSession
|
|
|
|
import audio.funkwhale.ffa.utils.AuthorizationServiceFactory
|
|
|
|
import audio.funkwhale.ffa.utils.OAuth
|
|
|
|
import com.google.android.exoplayer2.database.DatabaseProvider
|
|
|
|
import com.google.android.exoplayer2.database.ExoDatabaseProvider
|
2021-08-09 07:08:47 +02:00
|
|
|
import com.google.android.exoplayer2.offline.DownloadManager
|
2021-08-09 06:50:46 +02:00
|
|
|
import com.google.android.exoplayer2.upstream.cache.Cache
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.SimpleCache
|
|
|
|
import com.preference.PowerPreference
|
|
|
|
import org.koin.core.qualifier.named
|
|
|
|
import org.koin.dsl.module
|
|
|
|
|
2021-08-09 07:03:20 +02:00
|
|
|
fun exoplayerModule(context: Context) = module {
|
2021-08-09 06:50:46 +02:00
|
|
|
|
2021-08-09 07:03:20 +02:00
|
|
|
single<DatabaseProvider>(named("exoDatabase")) {
|
|
|
|
ExoDatabaseProvider(context)
|
2021-08-09 06:50:46 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 07:08:47 +02:00
|
|
|
single {
|
|
|
|
val cacheDataSourceFactoryProvider = get<CacheDataSourceFactoryProvider>()
|
2021-08-29 15:41:50 +02:00
|
|
|
|
|
|
|
val exoDownloadCache = get<Cache>(named("exoDownloadCache"))
|
|
|
|
val exoDatabase = get<DatabaseProvider>(named("exoDatabase"))
|
|
|
|
val cacheDataSourceFactory = cacheDataSourceFactoryProvider.create(context)
|
|
|
|
|
|
|
|
DownloadManager(context, exoDatabase, exoDownloadCache, cacheDataSourceFactory, Runnable::run)
|
2021-08-09 07:08:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
single {
|
|
|
|
CacheDataSourceFactoryProvider(
|
|
|
|
get(),
|
|
|
|
get(named("exoCache")),
|
|
|
|
get(named("exoDownloadCache"))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
single<Cache>(named("exoDownloadCache")) {
|
|
|
|
SimpleCache(
|
|
|
|
context.cacheDir.resolve("downloads"),
|
|
|
|
NoOpCacheEvictor(),
|
|
|
|
get<DatabaseProvider>(named("exoDatabase"))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
single<Cache>(named("exoCache")) {
|
|
|
|
val cacheSize = PowerPreference.getDefaultFile().getInt("media_cache_size", 1).toLong()
|
|
|
|
.let { if (it == 0L) 0 else it * 1024 * 1024 * 1024 }
|
|
|
|
SimpleCache(
|
|
|
|
context.cacheDir.resolve("media"),
|
|
|
|
LeastRecentlyUsedCacheEvictor(cacheSize),
|
|
|
|
get<DatabaseProvider>(named("exoDatabase"))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
single { MediaSession(context) }
|
2021-08-09 07:03:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun authModule() = module {
|
2021-08-09 07:08:47 +02:00
|
|
|
single { OAuth(get()) }
|
2021-08-09 07:03:20 +02:00
|
|
|
single { AuthorizationServiceFactory() }
|
2021-08-09 06:50:46 +02:00
|
|
|
}
|