-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(android): migrate DataSourceUtil to Kotlin (#3984)
* Rename .java to .kt * chore(android): migrate DataSourceUtil to Kotlin * fix: type issue in DataSourceUtil * fix: lint error * chore: refactor DataSourceUtil with object & @JvmStatic * fix: lint error
- Loading branch information
1 parent
452e42f
commit b25e43e
Showing
3 changed files
with
89 additions
and
98 deletions.
There are no files selected for viewing
96 changes: 0 additions & 96 deletions
96
android/src/main/java/com/brentvatne/exoplayer/DataSourceUtil.java
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
android/src/main/java/com/brentvatne/exoplayer/DataSourceUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.brentvatne.exoplayer | ||
|
||
import android.net.Uri | ||
import androidx.media3.common.util.Util | ||
import androidx.media3.datasource.AssetDataSource | ||
import androidx.media3.datasource.DataSource | ||
import androidx.media3.datasource.DataSpec | ||
import androidx.media3.datasource.DefaultDataSource | ||
import androidx.media3.datasource.HttpDataSource | ||
import androidx.media3.datasource.okhttp.OkHttpDataSource | ||
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.modules.network.CookieJarContainer | ||
import com.facebook.react.modules.network.ForwardingCookieHandler | ||
import com.facebook.react.modules.network.OkHttpClientProvider | ||
import okhttp3.Call | ||
import okhttp3.JavaNetCookieJar | ||
|
||
object DataSourceUtil { | ||
private var defaultDataSourceFactory: DataSource.Factory? = null | ||
private var defaultHttpDataSourceFactory: HttpDataSource.Factory? = null | ||
private var userAgent: String? = null | ||
|
||
private fun getUserAgent(context: ReactContext): String { | ||
if (userAgent == null) { | ||
userAgent = Util.getUserAgent(context, context.packageName) | ||
} | ||
return userAgent as String | ||
} | ||
|
||
@JvmStatic | ||
fun getDefaultDataSourceFactory(context: ReactContext, bandwidthMeter: DefaultBandwidthMeter?, requestHeaders: Map<String, String>?): DataSource.Factory { | ||
if (defaultDataSourceFactory == null || !requestHeaders.isNullOrEmpty()) { | ||
defaultDataSourceFactory = buildDataSourceFactory(context, bandwidthMeter, requestHeaders) | ||
} | ||
return defaultDataSourceFactory as DataSource.Factory | ||
} | ||
|
||
@JvmStatic | ||
fun getDefaultHttpDataSourceFactory( | ||
context: ReactContext, | ||
bandwidthMeter: DefaultBandwidthMeter?, | ||
requestHeaders: Map<String, String>? | ||
): HttpDataSource.Factory { | ||
if (defaultHttpDataSourceFactory == null || !requestHeaders.isNullOrEmpty()) { | ||
defaultHttpDataSourceFactory = buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders) | ||
} | ||
return defaultHttpDataSourceFactory as HttpDataSource.Factory | ||
} | ||
|
||
private fun buildDataSourceFactory( | ||
context: ReactContext, | ||
bandwidthMeter: DefaultBandwidthMeter?, | ||
requestHeaders: Map<String, String>? | ||
): DataSource.Factory = DefaultDataSource.Factory(context, buildHttpDataSourceFactory(context, bandwidthMeter, requestHeaders)) | ||
|
||
private fun buildHttpDataSourceFactory( | ||
context: ReactContext, | ||
bandwidthMeter: DefaultBandwidthMeter?, | ||
requestHeaders: Map<String, String>? | ||
): HttpDataSource.Factory { | ||
val client = OkHttpClientProvider.getOkHttpClient() | ||
val container = client.cookieJar as CookieJarContainer | ||
val handler = ForwardingCookieHandler(context) | ||
container.setCookieJar(JavaNetCookieJar(handler)) | ||
val okHttpDataSourceFactory = OkHttpDataSource.Factory(client as Call.Factory) | ||
.setTransferListener(bandwidthMeter) | ||
|
||
if (requestHeaders != null) { | ||
okHttpDataSourceFactory.setDefaultRequestProperties(requestHeaders) | ||
if (!requestHeaders.containsKey("User-Agent")) { | ||
okHttpDataSourceFactory.setUserAgent(getUserAgent(context)) | ||
} | ||
} else { | ||
okHttpDataSourceFactory.setUserAgent(getUserAgent(context)) | ||
} | ||
|
||
return okHttpDataSourceFactory | ||
} | ||
|
||
@JvmStatic | ||
fun buildAssetDataSourceFactory(context: ReactContext?, srcUri: Uri?): DataSource.Factory { | ||
val dataSpec = DataSpec(srcUri!!) | ||
val rawResourceDataSource = AssetDataSource(context!!) | ||
rawResourceDataSource.open(dataSpec) | ||
return DataSource.Factory { rawResourceDataSource } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters