Convert CronetHelper to an object.

This commit is contained in:
Isira Seneviratne 2023-01-31 21:16:36 +05:30
parent c75f1dcb85
commit 73f488e3d2
4 changed files with 19 additions and 32 deletions

View File

@ -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
}
}

View File

@ -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()
}

View File

@ -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

View File

@ -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())