forked from robertolocal/cordova-plugin-fingerprint-aio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request NiklasMerz#8 from exxbrain/2.0.0-biometricpromt-an…
…d-consistent-callbacks 2.0.0 biometricpromt and consistent callbacks
- Loading branch information
Showing
10 changed files
with
371 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package de.niklasmerz.cordova.biometric; | ||
|
||
import android.app.Activity; | ||
import android.app.KeyguardManager; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import com.exxbrain.android.biometric.BiometricPrompt; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
public class BiometricActivity extends AppCompatActivity { | ||
|
||
private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 2; | ||
private PromptInfo mPromptInfo; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
setTitle(null); | ||
int layout = getResources() | ||
.getIdentifier("biometric_activity", "layout", getPackageName()); | ||
setContentView(layout); | ||
|
||
if (savedInstanceState != null) { | ||
return; | ||
} | ||
|
||
mPromptInfo = new PromptInfo.Builder(getIntent().getExtras()).build(); | ||
authenticate(); | ||
|
||
} | ||
|
||
private void authenticate() { | ||
final Handler handler = new Handler(Looper.getMainLooper()); | ||
Executor executor = handler::post; | ||
|
||
BiometricPrompt biometricPrompt = | ||
new BiometricPrompt(this, executor, mAuthenticationCallback); | ||
|
||
BiometricPrompt.PromptInfo.Builder promptInfoBuilder = new BiometricPrompt.PromptInfo.Builder() | ||
.setTitle(mPromptInfo.getTitle()) | ||
.setSubtitle(mPromptInfo.getSubtitle()) | ||
.setDescription(mPromptInfo.getDescription()); | ||
|
||
if (mPromptInfo.isDeviceCredentialAllowed() | ||
&& Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { // TODO: remove after fix https://issuetracker.google.com/issues/142740104 | ||
promptInfoBuilder.setDeviceCredentialAllowed(true); | ||
} else { | ||
promptInfoBuilder.setNegativeButtonText(mPromptInfo.getFallbackButtonTitle()); | ||
} | ||
|
||
biometricPrompt.authenticate(promptInfoBuilder.build()); | ||
} | ||
|
||
private BiometricPrompt.AuthenticationCallback mAuthenticationCallback = | ||
new BiometricPrompt.AuthenticationCallback() { | ||
|
||
@Override | ||
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { | ||
super.onAuthenticationError(errorCode, errString); | ||
onError(errorCode, errString); | ||
} | ||
|
||
@Override | ||
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { | ||
super.onAuthenticationSucceeded(result); | ||
finishWithSuccess(); | ||
} | ||
|
||
@Override | ||
public void onAuthenticationFailed() { | ||
super.onAuthenticationFailed(); | ||
} | ||
}; | ||
|
||
|
||
// TODO: remove after fix https://issuetracker.google.com/issues/142740104 | ||
private void showAuthenticationScreen() { | ||
KeyguardManager keyguardManager = ContextCompat | ||
.getSystemService(this, KeyguardManager.class); | ||
if (keyguardManager == null | ||
|| android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { | ||
return; | ||
} | ||
if (keyguardManager.isKeyguardSecure()) { | ||
Intent intent = keyguardManager | ||
.createConfirmDeviceCredentialIntent(mPromptInfo.getTitle(), mPromptInfo.getDescription()); | ||
this.startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS); | ||
} else { | ||
// Show a message that the user hasn't set up a lock screen. | ||
finishWithError(PluginError.BIOMETRIC_SCREEN_GUARD_UNSECURED); | ||
} | ||
} | ||
|
||
// TODO: remove after fix https://issuetracker.google.com/issues/142740104 | ||
@Override | ||
public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) { | ||
if (resultCode == Activity.RESULT_OK) { | ||
finishWithSuccess(); | ||
} else { | ||
finishWithError(PluginError.BIOMETRIC_PIN_OR_PATTERN_DISMISSED); | ||
} | ||
} | ||
} | ||
|
||
private void onError(int errorCode, @NonNull CharSequence errString) { | ||
|
||
switch (errorCode) | ||
{ | ||
case BiometricPrompt.ERROR_CANCELED: | ||
finishWithError(PluginError.BIOMETRIC_FINGERPRINT_DISMISSED); | ||
return; | ||
case BiometricPrompt.ERROR_NEGATIVE_BUTTON: | ||
// TODO: remove after fix https://issuetracker.google.com/issues/142740104 | ||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P && mPromptInfo.isDeviceCredentialAllowed()) { | ||
showAuthenticationScreen(); | ||
return; | ||
} | ||
finishWithError(PluginError.BIOMETRIC_FINGERPRINT_DISMISSED); | ||
break; | ||
case BiometricPrompt.ERROR_LOCKOUT: | ||
finishWithError(PluginError.BIOMETRIC_LOCKED_OUT.getValue(), errString.toString()); | ||
break; | ||
case BiometricPrompt.ERROR_LOCKOUT_PERMANENT: | ||
finishWithError(PluginError.BIOMETRIC_LOCKED_OUT_PERMANENT.getValue(), errString.toString()); | ||
break; | ||
default: | ||
finishWithError(errorCode, errString.toString()); | ||
} | ||
} | ||
|
||
private void finishWithSuccess() { | ||
setResult(RESULT_OK); | ||
finish(); | ||
} | ||
|
||
private void finishWithError(PluginError error) { | ||
finishWithError(error.getValue(), error.getMessage()); | ||
} | ||
|
||
private void finishWithError(int code, String message) { | ||
Intent data = new Intent(); | ||
data.putExtra("code", code); | ||
data.putExtra("message", message); | ||
setResult(RESULT_CANCELED, data); | ||
finish(); | ||
} | ||
} |
Oops, something went wrong.