Merge pull request #2979 from Isira-Seneviratne/CronetHelper_object

Convert CronetHelper to an object.
This commit is contained in:
Bnyro 2023-02-06 15:30:16 +01:00 committed by GitHub
commit 12d3991f01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
fun initCronet(context: Context) {
engine = CronetEngine.Builder(context)
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()
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())