Java源码示例:androidx.biometric.BiometricManager

示例1
@Test
@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
public void testFingerprintNoHardware_api21() throws Exception {
  // GIVEN: API21 android version
  ReactApplicationContext context = getRNContext();
  KeychainModule module = new KeychainModule(context);

  // WHEN: verify availability
  final int result = BiometricManager.from(context).canAuthenticate();
  final boolean isFingerprintAvailable = module.isFingerprintAuthAvailable();

  // THEN: in api lower 23 - biometric is not available at all
  assertThat(isFingerprintAvailable, is(false));
  assertThat(result, is(BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE));

  // fingerprint hardware not available, minimal API for fingerprint is api23, Android 6.0
  // https://developer.android.com/about/versions/marshmallow/android-6.0
}
 
示例2
@Test
@Config(sdk = Build.VERSION_CODES.M)
public void testFingerprintAvailableButNotConfigured_api23() throws Exception {
  // GIVEN:
  //   fingerprint api available but not configured properly
  //   API23 android version
  ReactApplicationContext context = getRNContext();
  KeychainModule module = new KeychainModule(context);

  // set that hardware is available
  FingerprintManager fm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
  shadowOf(fm).setIsHardwareDetected(true);

  // WHEN: check availability
  final int result = BiometricManager.from(context).canAuthenticate();
  final boolean isFingerprintWorking = module.isFingerprintAuthAvailable();

  // THEN: another status from biometric api, fingerprint is still unavailable
  assertThat(result, is(BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED));
  assertThat(isFingerprintWorking, is(false));
}
 
示例3
@Test
@Config(sdk = Build.VERSION_CODES.M)
public void testFingerprintConfigured_api23() throws Exception {
  // GIVEN:
  //   API23 android version
  //   Fingerprints are configured
  //   fingerprint feature is ignored by android os
  ReactApplicationContext context = getRNContext();

  // set that hardware is available
  FingerprintManager fm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
  shadowOf(fm).setIsHardwareDetected(true);
  shadowOf(fm).setDefaultFingerprints(5); // 5 fingerprints are available

  // WHEN: check availability
  final int result = BiometricManager.from(context).canAuthenticate();
  final KeychainModule module = new KeychainModule(context);
  final boolean isFingerprintWorking = module.isFingerprintAuthAvailable();

  // THEN: biometric works
  assertThat(result, is(BiometricManager.BIOMETRIC_SUCCESS));
  assertThat(isFingerprintWorking, is(true));
}
 
示例4
@Test
@Config(sdk = Build.VERSION_CODES.P)
public void testFingerprintConfigured_api28() throws Exception {
  // GIVEN:
  //   API28 android version
  //   for api24+ system feature should be enabled
  //   fingerprints are configured
  ReactApplicationContext context = getRNContext();
  shadowOf(context.getPackageManager()).setSystemFeature(PackageManager.FEATURE_FINGERPRINT, true);

  // set that hardware is available
  FingerprintManager fm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
  shadowOf(fm).setIsHardwareDetected(true);
  shadowOf(fm).setDefaultFingerprints(5); // 5 fingerprints are available

  // WHEN: verify availability
  final int result = BiometricManager.from(context).canAuthenticate();
  final KeychainModule module = new KeychainModule(context);
  final boolean isFingerprintWorking = module.isFingerprintAuthAvailable();

  // THEN: biometrics works
  assertThat(result, is(BiometricManager.BIOMETRIC_SUCCESS));
  assertThat(isFingerprintWorking, is(true));
}
 
示例5
@Test
@Config(sdk = Build.VERSION_CODES.P)
public void testVerifySecureHardwareAvailability_api28() throws Exception {
  ReactApplicationContext context = getRNContext();

  // for api24+ system feature should be enabled
  shadowOf(context.getPackageManager()).setSystemFeature(PackageManager.FEATURE_FINGERPRINT, true);

  // set that hardware is available and fingerprints configured
  final FingerprintManager fm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
  shadowOf(fm).setIsHardwareDetected(true);
  shadowOf(fm).setDefaultFingerprints(5); // 5 fingerprints are available

  int result = BiometricManager.from(context).canAuthenticate();
  assertThat(result, is(BiometricManager.BIOMETRIC_SUCCESS));

  final CipherStorage storage = new CipherStorageKeystoreAesCbc();;

  // expected RsaEcb with fingerprint
  assertThat(storage.supportsSecureHardware(), is(true));
}
 
示例6
static boolean canAuthenticate(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String pin = prefs.getString("pin", null);
    if (!TextUtils.isEmpty(pin))
        return true;

    BiometricManager bm = BiometricManager.from(context);
    return (bm.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS);
}
 
示例7
GoldfingerImpl(
    @NonNull Context context,
    @NonNull AsyncCryptoObjectFactory asyncCryptoFactory,
    @NonNull CrypterProxy cryptoProxy
) {
    this.biometricManager = BiometricManager.from(context);
    this.asyncCryptoFactory = asyncCryptoFactory;
    this.cryptoProxy = cryptoProxy;
}
 
示例8
public static BiometricManager getManager(Context context) {
    BiometricManager manager = BiometricManager.from(context);
    if (manager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
        return manager;
    }
    return null;
}
 
示例9
@Test
@Config(sdk = Build.VERSION_CODES.P)
public void testExtractRsaEcb_EnabledFingerprint_api28() throws Exception {
  // GIVEN:
  //   API28 android version
  //   fingerprint feature enabled
  //   fingerprints configured
  final ReactApplicationContext context = getRNContext();
  shadowOf(context.getPackageManager()).setSystemFeature(PackageManager.FEATURE_FINGERPRINT, true);

  // set that hardware is available and fingerprints configured
  final FingerprintManager fm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
  shadowOf(fm).setIsHardwareDetected(true);
  shadowOf(fm).setDefaultFingerprints(5); // 5 fingerprints are available

  // WHEN: get secured storage
  final int result = BiometricManager.from(context).canAuthenticate();
  final KeychainModule module = new KeychainModule(context);
  final boolean isFingerprintWorking = module.isFingerprintAuthAvailable();
  final CipherStorage storage = module.getCipherStorageForCurrentAPILevel();

  // THEN: expected RsaEcb with working fingerprint
  assertThat(isFingerprintWorking, is(true));
  assertThat(result, is(BiometricManager.BIOMETRIC_SUCCESS));
  assertThat(storage, notNullValue());
  assertThat(storage, instanceOf(CipherStorageKeystoreRsaEcb.class));
  assertThat(storage.isBiometrySupported(), is(true));
  assertThat(storage.securityLevel(), is(SecurityLevel.SECURE_HARDWARE));
  assertThat(storage.getMinSupportedApiLevel(), is(Build.VERSION_CODES.M));
  assertThat(storage.supportsSecureHardware(), is(true));
}
 
示例10
public static boolean hardwareAvailable() {
    return BiometricManager.from(App.getAppContext()).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS
            || BiometricManager.from(App.getAppContext()).canAuthenticate() == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED;
}
 
示例11
public static boolean notSetup() {
    return BiometricManager.from(App.getAppContext()).canAuthenticate() == BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED;
}
 
示例12
@Override
public boolean canAuthenticate() {
    return biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS;
}
 
示例13
@Override
public boolean hasEnrolledFingerprint() {
    return biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED;
}
 
示例14
@Override
public boolean hasFingerprintHardware() {
    return biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE;
}
 
示例15
public static boolean isBiometricAuthAvailable(@NonNull final Context context) {
  return BiometricManager.from(context).canAuthenticate() == BIOMETRIC_SUCCESS;
}