Skip to content

Instantly share code, notes, and snippets.

View isaidamier's full-sized avatar

Isai Damier isaidamier

View GitHub Profile
@isaidamier
isaidamier / onAuthenticationSucceededNoCrypto.kt
Created November 26, 2019 02:54
onAuthenticationSucceededNoCrypto
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.d(TAG, "Authentication was successful")
showMessage()
}
@isaidamier
isaidamier / onClickNoCrypto.kt
Created November 26, 2019 02:51
onClickNoCrypto
override fun onClick(view: View) {
val promptInfo = createPromptInfo()
if (BiometricManager.from(context)
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
biometricPrompt.authenticate(promptInfo)
} else {
loginWithPassword()
}
}
@isaidamier
isaidamier / biometricOnClick.kt
Created November 26, 2019 02:48
biometricOnClick
// Callback for the "authenticate" button in your app's UI.
override fun onClick(view: View) {
val promptInfo = createPromptInfo()
if (BiometricManager.from(context)
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
biometricPrompt.authenticate(promptInfo, cryptoObject)
} else {
loginWithPassword()
}
}
@isaidamier
isaidamier / onAuthenticationError.kt
Created November 26, 2019 02:44
onAuthenticationError
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "$errorCode :: $errString")
if(errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON) {
loginWithPassword() // Because negative button says use application password
}
}
@isaidamier
isaidamier / createPromptInfo.kt
Created November 26, 2019 02:41
createPromptInfo
private fun createPromptInfo(): BiometricPrompt.PromptInfo {
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.prompt_info_title))
.setSubtitle(getString(R.string.prompt_info_subtitle))
.setDescription(getString(R.string.prompt_info_description))
// Authenticate without requiring the user to press a "confirm"
// button after satisfying the biometric check
.setConfirmationRequired(false)
.setNegativeButtonText(getString(R.string.prompt_info_use_app_password))
.build()
@isaidamier
isaidamier / createBiometricPrompt.kt
Last active December 12, 2019 17:26
create a BiometricPrompt
private lateinit var biometricPrompt: BiometricPrompt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
// ...
biometricPrompt = createBiometricPrompt()
}
@isaidamier
isaidamier / biometric_version.gradle
Last active November 21, 2019 22:39
biometric_version 1
def biometric_version = '1.0.0'
implementation "androidx.biometric:biometric:$biometric_version"
fun queryPurchasesAsync() {
val purchasesResult = HashSet<Purchase>()
var result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.INAPP)
if(nullOrEmpty(result)) {
queryPurchaseHistoryAsync()
} else {
result?.purchasesList?.let { purchasesResult.addAll(it) }
}
}
override fun onBillingSetupFinished(billingResult: BillingResult) {
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
Log.d(LOG_TAG, "onBillingSetupFinished successfully")
...
queryPurchases()
}
...
}
private fun instantiateAndConnectToPlayBillingService() {
playStoreBillingClient = BillingClient.newBuilder(application.applicationContext)
.enablePendingPurchases() // required or app will crash
.setListener(this).build()
connectToPlayBillingService()
}
private fun connectToPlayBillingService(): Boolean {
Log.d(LOG_TAG, "connectToPlayBillingService")
if (!playStoreBillingClient.isReady) {