Skip to content

Instantly share code, notes, and snippets.

@yoobi
Last active April 29, 2020 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoobi/ee5c152e13944b32411d8e1f81d973ab to your computer and use it in GitHub Desktop.
Save yoobi/ee5c152e13944b32411d8e1f81d973ab to your computer and use it in GitHub Desktop.
TLS 1.2 for Retrofit, Glide and Exoplayer // https://stackoverflow.com/q/61459509
/*
You'll find the extension `enableTls12()`:
- [Gist](https://gist.github.com/ankushg/8c0c3144318b1c17abb228d6211ba996)
- [The related article](https://ankushg.com/posts/tls-1.2-on-android/)
*/
val clientBuilder by lazy {
OkHttpClient.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.enableTls12()
}
/* Important:
You'll need to add these implentation in your gradle
implementation 'com.github.bumptech.glide:annotations:4.11.0'
implementation('com.github.bumptech.glide:okhttp3-integration:4.11.0'){
exclude group: 'glide-parent'
}
*/
@GlideModule
class CustomGlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.replace(
GlideUrl::class.java,
InputStream::class.java,
OkHttpUrlLoader.Factory(clientBuilder.build())
)
}
}
// How to use your customGlideModule
GlideApp.with(this)
.load(URL)
.into(image)
/* Important:
You'll need to add this implentation in your gradle
implementation "com.google.android.exoplayer:extension-okhttp:2.11.4"
*/
val dataSourceFactory: DataSource.Factory = OkHttpDataSourceFactory(
clientBuilder.build(),
Util.getUserAgent(context, "RecyclerView VideoPlayer")
)
// then just use your dataSourceFactory with your mediaSource and play the video
val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(mediaUrl))
/* just use the clientBuilder we created before*/
val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(clientBuilder.build())
.baseUrl(BASE_URL)
.build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment