forked from robolectric/robolectric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShadowBiometricManager.java
79 lines (69 loc) · 2.78 KB
/
ShadowBiometricManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.robolectric.shadows;
import static android.Manifest.permission.USE_BIOMETRIC;
import static android.hardware.biometrics.BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE;
import static android.hardware.biometrics.BiometricManager.BIOMETRIC_SUCCESS;
import static android.hardware.biometrics.BiometricPrompt.BIOMETRIC_ERROR_HW_UNAVAILABLE;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static org.robolectric.util.reflector.Reflector.reflector;
import android.annotation.RequiresPermission;
import android.content.Context;
import android.hardware.biometrics.BiometricManager;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
/** Provides testing APIs for {@link BiometricManager} */
@Implements(
className = "android.hardware.biometrics.BiometricManager",
minSdk = Q,
isInAndroidSdk = false)
public class ShadowBiometricManager {
protected boolean biometricServiceConnected = true;
@RealObject private BiometricManager realBiometricManager;
@SuppressWarnings("deprecation")
@RequiresPermission(USE_BIOMETRIC)
@Implementation
protected int canAuthenticate() {
if (RuntimeEnvironment.getApiLevel() >= R) {
return reflector(BiometricManagerReflector.class, realBiometricManager).canAuthenticate();
} else if (biometricServiceConnected) {
return BIOMETRIC_SUCCESS;
} else {
boolean hasBiomatrics =
ReflectionHelpers.callStaticMethod(
BiometricManager.class,
"hasBiometrics",
ClassParameter.from(
Context.class, RuntimeEnvironment.getApplication().getApplicationContext()));
if (!hasBiomatrics) {
return BIOMETRIC_ERROR_NO_HARDWARE;
} else {
return BIOMETRIC_ERROR_HW_UNAVAILABLE;
}
}
}
/**
* Sets the value {@code true} to allow {@link #canAuthenticate()} return {@link
* BIOMETRIC_SUCCESS} If sets the value to {@code false}, result will depend on {@link
* BiometricManager#hasBiometrics(Context context)}
*
* @param flag to set can authenticate or not
*/
public void setCanAuthenticate(boolean flag) {
biometricServiceConnected = flag;
}
@Implementation(minSdk = R)
protected int canAuthenticate(int userId, int authenticators) {
return biometricServiceConnected ? BIOMETRIC_SUCCESS : BIOMETRIC_ERROR_NO_HARDWARE;
}
@ForType(BiometricManager.class)
interface BiometricManagerReflector {
@Direct
int canAuthenticate();
}
}