Skip to content

Instantly share code, notes, and snippets.

@cdongieux
cdongieux / OkHttpSocketFactoryForKitKatOrLollipop.kt
Created June 26, 2020 09:21
OkHttpClient for Android API 19 (maybe 21?) devices having issues with SSL certificates
private fun createOkHttpClient(): OkHttpClient {
return OkHttp.Builder().apply {
createSocketFactoryForKitKatOrLollipop()?.let { socketFactory ->
sslSocketFactory(socketFactory)
}
}
}
private fun createSocketFactoryForKitKatOrLollipop(): SSLSocketFactory? {
/**
@cdongieux
cdongieux / MyWebViewClient.java
Last active June 26, 2020 09:24
WebViewClient implementation for Android API 19 (maybe 21?) devices having issues with SSL certificates
/**
* Typical error:
*
* X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
* MyWebViewClient: onReceivedSslError: handler = [Handler (com.android.webview.chromium.WebViewContentsClientAdapter$3) {9d87c030}], error = [primary error: 3 certificate: Issued to:
* CN=*.bootstrapcdn.com,OU=Domain Control Validated;
* Issued by: CN=Sectigo RSA Domain Validation Secure Server CA,O=Sectigo Limited,L=Salford,ST=Greater Manchester,C=GB;
* on URL: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css]
*/
package com.netgem.diamondcore.base.manager
import android.annotation.TargetApi
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
@Database(entities = [Foo::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun fooDao(): FooDao
companion object {
fun buildDatabase(context: Context) =
Room
.databaseBuilder(context.applicationContext, AppDatabase::class.java, "AppDatabase.db")
.build()
}