diff --git a/app/src/main/java/com/github/libretube/LibreTubeApp.kt b/app/src/main/java/com/github/libretube/LibreTubeApp.kt index 282d1f6c1..b3b20a46a 100644 --- a/app/src/main/java/com/github/libretube/LibreTubeApp.kt +++ b/app/src/main/java/com/github/libretube/LibreTubeApp.kt @@ -6,7 +6,6 @@ import android.os.StrictMode.VmPolicy import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationManagerCompat import androidx.work.ExistingPeriodicWorkPolicy -import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID @@ -22,6 +21,7 @@ import com.github.libretube.util.ExceptionHandler class LibreTubeApp : Application() { override fun onCreate() { super.onCreate() + instance = this /** * Initialize the needed notification channels for DownloadService and BackgroundMode @@ -48,7 +48,6 @@ class LibreTubeApp : Application() { * Set the api and the auth api url */ RetrofitInstance.initialize() - CronetHelper.initCronet(this) ImageHelper.initializeImageLoader(this) /** @@ -112,4 +111,8 @@ class LibreTubeApp : Application() { ) ) } + + companion object { + lateinit var instance: LibreTubeApp + } } diff --git a/app/src/main/java/com/github/libretube/api/CronetHelper.kt b/app/src/main/java/com/github/libretube/api/CronetHelper.kt index bbeb58c66..0c217bd86 100644 --- a/app/src/main/java/com/github/libretube/api/CronetHelper.kt +++ b/app/src/main/java/com/github/libretube/api/CronetHelper.kt @@ -1,28 +1,16 @@ package com.github.libretube.api -import android.content.Context +import com.github.libretube.LibreTubeApp import com.google.net.cronet.okhttptransport.CronetCallFactory import org.chromium.net.CronetEngine -class CronetHelper { - companion object { - private lateinit var engine: CronetEngine - lateinit var callFactory: CronetCallFactory +object CronetHelper { + val cronetEngine: CronetEngine = CronetEngine.Builder(LibreTubeApp.instance) + .enableHttp2(true) + .enableQuic(true) + .enableBrotli(true) + .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 1024L * 1024L) // 1MiB + .build() - fun initCronet(context: Context) { - engine = CronetEngine.Builder(context) - .enableHttp2(true) - .enableQuic(true) - .enableBrotli(true) - .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 1024L * 1024L) // 1MiB - .build() - - callFactory = CronetCallFactory.newBuilder(engine) - .build() - } - - fun getCronetEngine(): CronetEngine { - return engine - } - } + val callFactory: CronetCallFactory = CronetCallFactory.newBuilder(cronetEngine).build() } diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index f530da78a..641486a29 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -179,7 +179,7 @@ class DownloadService : Service() { try { // Set start range where last downloading was held. - val con = CronetHelper.getCronetEngine().openConnection(url) as HttpURLConnection + val con = CronetHelper.cronetEngine.openConnection(url) as HttpURLConnection con.requestMethod = "GET" con.setRequestProperty("Range", "bytes=$totalRead-") con.connectTimeout = DownloadHelper.DEFAULT_TIMEOUT diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index e79c92976..445bbb0c3 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -123,7 +123,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.datetime.LocalDate import kotlinx.serialization.encodeToString -import org.chromium.net.CronetEngine import retrofit2.HttpException class PlayerFragment : BaseFragment(), OnlinePlayerOptions { @@ -1300,14 +1299,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } private fun createExoPlayer() { - val cronetEngine: CronetEngine = CronetHelper.getCronetEngine() - val cronetDataSourceFactory: CronetDataSource.Factory = - CronetDataSource.Factory(cronetEngine, Executors.newCachedThreadPool()) - - val dataSourceFactory = DefaultDataSource.Factory( - requireContext(), - cronetDataSourceFactory + val cronetDataSourceFactory = CronetDataSource.Factory( + CronetHelper.cronetEngine, + Executors.newCachedThreadPool() ) + val dataSourceFactory = DefaultDataSource.Factory(requireContext(), cronetDataSourceFactory) // control for the track sources like subtitles and audio source trackSelector = DefaultTrackSelector(requireContext())