|
| 1 | +package io.snabble.sdk.ui; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.app.Activity; |
| 5 | +import android.app.KeyguardManager; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | +import android.hardware.biometrics.BiometricManager; |
| 9 | +import android.hardware.biometrics.BiometricPrompt; |
| 10 | +import android.hardware.fingerprint.FingerprintManager; |
| 11 | +import android.os.Build; |
| 12 | +import android.os.CancellationSignal; |
| 13 | +import android.os.Handler; |
| 14 | +import android.os.Looper; |
| 15 | + |
| 16 | +import androidx.fragment.app.FragmentActivity; |
| 17 | +import androidx.fragment.app.FragmentManager; |
| 18 | + |
| 19 | +import java.util.concurrent.Executors; |
| 20 | + |
| 21 | +import io.snabble.sdk.utils.Dispatch; |
| 22 | + |
| 23 | +public class Keyguard { |
| 24 | + private static final int REQUEST_CODE_AUTHENTICATION = 4613; |
| 25 | + |
| 26 | + private static Callback currentCallback; |
| 27 | + |
| 28 | + public interface Callback { |
| 29 | + void success(); |
| 30 | + void error(); |
| 31 | + } |
| 32 | + |
| 33 | + @SuppressLint({"WrongConstant", "NewApi"}) |
| 34 | + public static void unlock(FragmentActivity activity, Callback callback) { |
| 35 | + Dispatch.mainThread(() -> { |
| 36 | + Keyguard.currentCallback = callback; |
| 37 | + |
| 38 | + boolean supportsBiometricPrompt = false; |
| 39 | + |
| 40 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 41 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 42 | + BiometricManager biometricManager = (BiometricManager) activity.getSystemService(Context.BIOMETRIC_SERVICE); |
| 43 | + if (biometricManager != null) { |
| 44 | + supportsBiometricPrompt = biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS; |
| 45 | + } |
| 46 | + } else { |
| 47 | + FingerprintManager fingerprintManager = (FingerprintManager) activity.getSystemService(Context.FINGERPRINT_SERVICE); |
| 48 | + if (fingerprintManager != null) { |
| 49 | + supportsBiometricPrompt = fingerprintManager.hasEnrolledFingerprints(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (supportsBiometricPrompt) { |
| 55 | + new BiometricPrompt.Builder(activity) |
| 56 | + .setTitle(activity.getString(R.string.Snabble_Keyguard_title)) |
| 57 | + .setDescription(activity.getString(R.string.Snabble_Keyguard_message)) |
| 58 | + .setNegativeButton(activity.getString(R.string.Snabble_Cancel), Executors.newSingleThreadExecutor(), (dialogInterface, i) -> { |
| 59 | + error(callback); |
| 60 | + }) |
| 61 | + .build() |
| 62 | + .authenticate(new CancellationSignal(), Executors.newSingleThreadExecutor(), |
| 63 | + new BiometricPrompt.AuthenticationCallback() { |
| 64 | + @Override |
| 65 | + public void onAuthenticationError(int errorCode, CharSequence errString) { |
| 66 | + error(callback); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onAuthenticationHelp(int helpCode, CharSequence helpString) { |
| 71 | + error(callback); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) { |
| 76 | + success(callback); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onAuthenticationFailed() { |
| 81 | + |
| 82 | + } |
| 83 | + }); |
| 84 | + } else { |
| 85 | + KeyguardManager km = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE); |
| 86 | + |
| 87 | + if (km != null && km.isKeyguardSecure()) { |
| 88 | + final FragmentManager fm = activity.getSupportFragmentManager(); |
| 89 | + |
| 90 | + ActivityResultFragment fragment = new ActivityResultFragment(); |
| 91 | + fm.beginTransaction() |
| 92 | + .add(fragment, "ActivityResultFragment") |
| 93 | + .commit(); |
| 94 | + fm.executePendingTransactions(); |
| 95 | + |
| 96 | + Intent authIntent = km.createConfirmDeviceCredentialIntent( |
| 97 | + activity.getString(R.string.Snabble_Keyguard_title), |
| 98 | + activity.getString(R.string.Snabble_Keyguard_message)); |
| 99 | + |
| 100 | + fragment.startActivityForResult(authIntent, REQUEST_CODE_AUTHENTICATION); |
| 101 | + } else { |
| 102 | + success(null); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + private static void success(Callback callback) { |
| 109 | + Dispatch.mainThread(() -> { |
| 110 | + if (callback != null && currentCallback != callback) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (currentCallback != null) { |
| 115 | + currentCallback.success(); |
| 116 | + currentCallback = null; |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private static void error(Callback callback) { |
| 122 | + Dispatch.mainThread(() -> { |
| 123 | + if (callback != null && currentCallback != callback) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + if (currentCallback != null) { |
| 128 | + currentCallback.error(); |
| 129 | + currentCallback = null; |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public static final class ActivityResultFragment extends androidx.fragment.app.Fragment { |
| 135 | + public ActivityResultFragment() { |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 140 | + super.onActivityResult(requestCode, resultCode, data); |
| 141 | + |
| 142 | + requireFragmentManager().beginTransaction().remove(this).commit(); |
| 143 | + |
| 144 | + if (requestCode == REQUEST_CODE_AUTHENTICATION) { |
| 145 | + if (currentCallback != null) { |
| 146 | + if (resultCode == Activity.RESULT_OK) { |
| 147 | + success(null); |
| 148 | + } else { |
| 149 | + error(null); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments