This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { | |
super.onAuthenticationSucceeded(result) | |
Log.d(TAG, "Authentication was successful") | |
showMessage() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onClick(view: View) { | |
val promptInfo = createPromptInfo() | |
if (BiometricManager.from(context) | |
.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) { | |
biometricPrompt.authenticate(promptInfo) | |
} else { | |
loginWithPassword() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def biometric_version = '1.0.0' | |
implementation "androidx.biometric:biometric:$biometric_version" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun queryPurchasesAsync() { | |
val purchasesResult = HashSet<Purchase>() | |
var result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.INAPP) | |
if(nullOrEmpty(result)) { | |
queryPurchaseHistoryAsync() | |
} else { | |
result?.purchasesList?.let { purchasesResult.addAll(it) } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBillingSetupFinished(billingResult: BillingResult) { | |
when (billingResult.responseCode) { | |
BillingClient.BillingResponseCode.OK -> { | |
Log.d(LOG_TAG, "onBillingSetupFinished successfully") | |
... | |
queryPurchases() | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |