Skip to content

Instantly share code, notes, and snippets.

Avatar

Srikate Jaosrikate

View GitHub Profile
View GetCipherForEncryption.kt
override fun getCipherForEncryption(keyName: String): Cipher {
val cipher = getCipher()
val secretKey = getOrCreateSecretKey(keyName)
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
return cipher
}
View secretKey.kt
private fun getOrCreateSecretKey(keyName: String): SecretKey {
// If Secretkey was previously created for that keyName, then grab and return it.
try {
val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE)
try {
keyStore.load(null) // Keystore must be loaded before it can be accessed
keyStore.getKey(keyName, null)?.let {
return it as SecretKey
}
} catch (ex: Exception) {
@Jaosrikate
Jaosrikate / LoginActivity.kt
Last active Jan 25, 2022
Biometric prompt Login Activity
View LoginActivity.kt
class LoginActivity : AppCompatActivity() {
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
biometricPrompt = createBiometricPrompt()
View bio_auth.kt
val cipher = CryptographyManager().getCipherForEncryption(BIO_KEY_NAME)
biometricPrompt.authenticate(
buildBiometricPrompt(),
BiometricPrompt.CryptoObject(cipher)
)
View bio_authen_status.kt
val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate(BIOMETRIC_STRONG)) {
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED,
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE,
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE,
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED,
-> showErrorNoHardwareBio()
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED,
BiometricManager.BIOMETRIC_STATUS_UNKNOWN,
View buildBiometricPrompt.kt
private fun buildBiometricPrompt(): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.bio_authen_title))
.setDescription(getString(R.string.bio_authen_description))
.setNegativeButtonText(getString(R.string.bio_authen_negative_button))
.setConfirmationRequired(false) //Allows user to authenticate without performing an action, such as pressing a button, after their biometric credential is accepted.
.setAllowedAuthenticators(BIOMETRIC_STRONG)
.build()
}
@Jaosrikate
Jaosrikate / build.gradle
Last active Jan 25, 2022
Import Library for Biometric Prompt
View build.gradle
def biometric_version = '1.1.0'
//Biometric
implementation "androidx.biometric:biometric:$biometric_version"
@Jaosrikate
Jaosrikate / GradientConstant.kt
Last active May 17, 2022
Android create gradient drawable programmatically (allow to use more than 3 colors) example
View GradientConstant.kt
import android.content.Context
import android.graphics.drawable.GradientDrawable
import androidx.core.content.ContextCompat
class GradientConstant {
companion object {
fun goldGradient(activity: Context): GradientDrawable {
val colors: IntArray = intArrayOf(
@Jaosrikate
Jaosrikate / MainActivity.java
Last active Oct 16, 2019
MPAndroidChart Pie chart highlighter
View MainActivity.java
package com.example.myapplication;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
@Jaosrikate
Jaosrikate / ApiHelper.kt
Created Oct 1, 2019
Retrofit2/OkHttp3 CookieJar Interceptor [kotlin]
View ApiHelper.kt
// Retrofit2/OkHttp3 CookieJar Interceptor kotlin
// JaoSrikate
import android.util.Log
import android.webkit.CookieManager
import io.reactivex.Observable
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Response