From 8dd5705f0227a73ac3d20ce16b7b25f179eeb57d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 21 Jul 2023 10:08:04 -0400 Subject: [PATCH] Merges VideoCache initialization with get methods --- .../vitorpamplona/amethyst/PlaybackService.kt | 9 +--- .../com/vitorpamplona/amethyst/VideoCache.kt | 46 +++++++++---------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt b/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt index 4cca70bbf..a9d2cf496 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt @@ -16,7 +16,7 @@ class PlaybackService : MediaSessionService() { MultiPlayerPlaybackManager(HlsMediaSource.Factory(OkHttpDataSource.Factory(HttpClient.getHttpClient()))) } private val managerProgressive by lazy { - MultiPlayerPlaybackManager(ProgressiveMediaSource.Factory(VideoCache.get())) + MultiPlayerPlaybackManager(ProgressiveMediaSource.Factory(VideoCache.get(applicationContext))) } private val managerLocal by lazy { MultiPlayerPlaybackManager() @@ -27,13 +27,8 @@ class PlaybackService : MediaSessionService() { override fun onCreate() { super.onCreate() - // Initializes video cache. - VideoCache.init(applicationContext) - setMediaNotificationProvider( - DefaultMediaNotificationProvider.Builder(applicationContext) - // .setNotificationIdProvider { session -> session.id.hashCode() } - .build() + DefaultMediaNotificationProvider.Builder(applicationContext).build() ) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt index 54cc339be..50dcb3cc5 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt @@ -22,33 +22,33 @@ import com.vitorpamplona.amethyst.service.HttpClient @Synchronized @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) - fun init(context: Context) { - if (!this::simpleCache.isInitialized) { - exoDatabaseProvider = StandaloneDatabaseProvider(context) + private fun init(context: Context) { + exoDatabaseProvider = StandaloneDatabaseProvider(context) - simpleCache = SimpleCache( - context.cacheDir, - leastRecentlyUsedCacheEvictor, - exoDatabaseProvider - ) + simpleCache = SimpleCache( + context.cacheDir, + leastRecentlyUsedCacheEvictor, + exoDatabaseProvider + ) - cacheDataSourceFactory = CacheDataSource.Factory() - .setCache(simpleCache) - .setUpstreamDataSourceFactory( - OkHttpDataSource.Factory(HttpClient.getHttpClient()) - ) - .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) - } else { - cacheDataSourceFactory = CacheDataSource.Factory() - .setCache(simpleCache) - .setUpstreamDataSourceFactory( - OkHttpDataSource.Factory(HttpClient.getHttpClient()) - ) - .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) - } + renewCacheFactory() } - fun get(): CacheDataSource.Factory { + // This method should be called when proxy setting changes. + fun renewCacheFactory() { + cacheDataSourceFactory = CacheDataSource.Factory() + .setCache(simpleCache) + .setUpstreamDataSourceFactory( + OkHttpDataSource.Factory(HttpClient.getHttpClient()) + ) + .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) + } + + fun get(context: Context): CacheDataSource.Factory { + if (!this::simpleCache.isInitialized) { + init(context) + } + return cacheDataSourceFactory } }