Skip to content

Instantly share code, notes, and snippets.

View SerggioC's full-sized avatar
🎯
Focusing

Sergio C SerggioC

🎯
Focusing
  • Portugal
View GitHub Profile
@SerggioC
SerggioC / UnsafeOkHttpClient trustAllCerts
Created May 8, 2023 13:20
UnsafeOkHttpClient trustAllCerts
private fun getUnsafeOkHttpClientBuilder(): OkHttpClient.Builder {
try {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(
chain: Array<java.security.cert.X509Certificate>,
authType: String
) {
}
override fun checkServerTrusted(
@SerggioC
SerggioC / load certificate from resources file
Created May 8, 2023 13:19
Load certificate from resources file
@Throws(
CertificateException::class,
KeyStoreException::class,
NoSuchAlgorithmException::class,
KeyManagementException::class
)
private fun getSSLConfig(context: Context): SSLContext {
val certificateFactory = CertificateFactory.getInstance("X.509")
context.resources.openRawResource(R.raw.tpv_certificate).use { inputStream ->
@SerggioC
SerggioC / IPEditText
Created September 6, 2022 16:34
IPEditText
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@SerggioC
SerggioC / get String from InputStream
Last active August 12, 2022 11:00
get String from InputStream
@Throws(java.lang.Exception::class)
fun convertStreamToString(input: InputStream?): String? {
input ?: return null
val reader = BufferedReader(InputStreamReader(input))
val sb = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
sb.append(line).append("\n")
}
reader.close()
@SerggioC
SerggioC / get InputStream from Uri
Created August 11, 2022 14:17
get InputStream from Uri
InputStream inputStream = context.getContentResolver().openInputStream(urifont);
@SerggioC
SerggioC / getUriFromAssetsFile
Created August 11, 2022 14:16
get Uri from Android assets file
Uri fontUri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(BuildConfig.APPLICATION_ID)
.path(String.valueOf(R.font.roboto_regular))
.build();
@SerggioC
SerggioC / retrieveAllManifestPermissions
Created August 4, 2022 16:15
Retrieve All Android Manifest Permissions
@JvmStatic
fun retrieveAllManifestPermissions(context: Context): Array<String> {
return try {
context
.packageManager
.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS)
.requestedPermissions
} catch (e: Exception) {
emptyArray()
}
@SerggioC
SerggioC / SHA hash with HMAC256 kotlin
Created July 10, 2022 21:28
SHA hash with HMAC256 kotlin
private fun generateHashWithHmac256(message: String): String? {
try {
val key = BuildConfig.HASH
val bytes = hmac(key.toByteArray(), message.toByteArray())
if (bytes != null) return bytesToHex(bytes)
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
@SerggioC
SerggioC / write to Buffer from Retroft interceptor
Created July 10, 2022 21:28
write to Buffer from Retroft interceptor
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val firebaseToken = Utils.getFirebaseToken(context)
val originalRequest: Request = chain.request()
var newRequest = originalRequest.newBuilder()
.addHeader("content-type", "application/json;charset=UTF-8")
.addHeader("accept", "application/json")
.addHeader("api-version", "1.0")
.addHeader("authorization", "Bearer $firebaseToken")
@SerggioC
SerggioC / byte array to Hex String
Created July 10, 2022 21:27
byte array to Hex String
private fun bytesToHex(bytes: ByteArray): String {
val result = bytes.joinToString(separator = "") { eachByte ->
"%02x".format(eachByte)
}
return result
}