-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support biometric authentication
- Loading branch information
1 parent
f6e6e96
commit 38b413c
Showing
8 changed files
with
150 additions
and
16 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
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
69 changes: 69 additions & 0 deletions
69
app/src/main/java/deltazero/amarok/utils/SecurityAuth.java
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,69 @@ | ||
package deltazero.amarok.utils; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.biometric.BiometricPrompt; | ||
import androidx.fragment.app.FragmentActivity; | ||
|
||
import deltazero.amarok.PrefMgr; | ||
import deltazero.amarok.R; | ||
import deltazero.amarok.ui.PasswordAuthFragment; | ||
|
||
public class SecurityAuth { | ||
|
||
public interface SecurityAuthCallback { | ||
void onSecurityAuthCallback(boolean succeed); | ||
} | ||
|
||
private SecurityAuthCallback callback; | ||
private FragmentActivity activity; | ||
private PrefMgr prefMgr; | ||
|
||
public SecurityAuth(FragmentActivity activity, SecurityAuthCallback callback) { | ||
this.callback = callback; | ||
this.activity = activity; | ||
prefMgr = new PrefMgr(activity); | ||
} | ||
|
||
public void authenticate() { | ||
if (prefMgr.getAmarokPassword() != null) { | ||
if (prefMgr.getEnableAmarokBiometricAuth()) biometricAuthenticate(); | ||
else passwordAuthenticate(); | ||
} else { | ||
callback.onSecurityAuthCallback(true); | ||
} | ||
} | ||
|
||
private void passwordAuthenticate() { | ||
assert prefMgr.getAmarokPassword() != null; | ||
new PasswordAuthFragment() | ||
.setOnVerifiedCallback(succeed -> callback.onSecurityAuthCallback(succeed)) | ||
.show(activity.getSupportFragmentManager(), null); | ||
} | ||
|
||
private void biometricAuthenticate() { | ||
|
||
var biometricPrompt = new BiometricPrompt(activity, new BiometricPrompt.AuthenticationCallback() { | ||
@Override | ||
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { | ||
passwordAuthenticate(); | ||
} | ||
|
||
@Override | ||
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { | ||
callback.onSecurityAuthCallback(true); | ||
} | ||
|
||
@Override | ||
public void onAuthenticationFailed() { | ||
super.onAuthenticationFailed(); | ||
} | ||
}); | ||
|
||
var promptInfo = new BiometricPrompt.PromptInfo.Builder() | ||
.setTitle(activity.getString(R.string.unlock_required)) | ||
.setNegativeButtonText(activity.getString(android.R.string.cancel)) | ||
.build(); | ||
|
||
biometricPrompt.authenticate(promptInfo); | ||
} | ||
} |
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