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.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
@ -22,6 +21,7 @@ import com.github.libretube.util.ExceptionHandler
class LibreTubeApp : Application() { class LibreTubeApp : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
instance = this
/** /**
* Initialize the needed notification channels for DownloadService and BackgroundMode * Initialize the needed notification channels for DownloadService and BackgroundMode
@ -48,7 +48,6 @@ class LibreTubeApp : Application() {
* Set the api and the auth api url * Set the api and the auth api url
*/ */
RetrofitInstance.initialize() RetrofitInstance.initialize()
CronetHelper.initCronet(this)
ImageHelper.initializeImageLoader(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 package com.github.libretube.api
import android.content.Context import com.github.libretube.LibreTubeApp
import com.google.net.cronet.okhttptransport.CronetCallFactory import com.google.net.cronet.okhttptransport.CronetCallFactory
import org.chromium.net.CronetEngine import org.chromium.net.CronetEngine
class CronetHelper { object CronetHelper {
companion object { val cronetEngine: CronetEngine = CronetEngine.Builder(LibreTubeApp.instance)
private lateinit var engine: CronetEngine
lateinit var callFactory: CronetCallFactory
fun initCronet(context: Context) {
engine = CronetEngine.Builder(context)
.enableHttp2(true) .enableHttp2(true)
.enableQuic(true) .enableQuic(true)
.enableBrotli(true) .enableBrotli(true)
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 1024L * 1024L) // 1MiB .enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 1024L * 1024L) // 1MiB
.build() .build()
callFactory = CronetCallFactory.newBuilder(engine) val callFactory: CronetCallFactory = CronetCallFactory.newBuilder(cronetEngine).build()
.build()
}
fun getCronetEngine(): CronetEngine {
return engine
}
}
} }

View File

@ -179,7 +179,7 @@ class DownloadService : Service() {
try { try {
// Set start range where last downloading was held. // 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.requestMethod = "GET"
con.setRequestProperty("Range", "bytes=$totalRead-") con.setRequestProperty("Range", "bytes=$totalRead-")
con.connectTimeout = DownloadHelper.DEFAULT_TIMEOUT con.connectTimeout = DownloadHelper.DEFAULT_TIMEOUT

View File

@ -123,7 +123,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import org.chromium.net.CronetEngine
import retrofit2.HttpException import retrofit2.HttpException
class PlayerFragment : BaseFragment(), OnlinePlayerOptions { class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
@ -1300,14 +1299,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
private fun createExoPlayer() { private fun createExoPlayer() {
val cronetEngine: CronetEngine = CronetHelper.getCronetEngine() val cronetDataSourceFactory = CronetDataSource.Factory(
val cronetDataSourceFactory: CronetDataSource.Factory = CronetHelper.cronetEngine,
CronetDataSource.Factory(cronetEngine, Executors.newCachedThreadPool()) Executors.newCachedThreadPool()
val dataSourceFactory = DefaultDataSource.Factory(
requireContext(),
cronetDataSourceFactory
) )
val dataSourceFactory = DefaultDataSource.Factory(requireContext(), cronetDataSourceFactory)
// control for the track sources like subtitles and audio source // control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())