I need a way to test system service in espresso.
Imaging to want test code below in espresso:
fingerprintManagerCompat = FingerprintManagerCompat.from(this);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (!fingerprintManagerCompat.isHardwareDetected()) {
showError("your device doesn't have finger print scanner");
}
So how I can test isHardwareDetected() method with false and true condition?
I don't have any idea what can I do in this situation, and how test this part of code.
Related
Is there a way to test programatically that my phone's fingerprint sensor, works correctly?
I would like to be able to perform the test automatically, without enrolling my fingerprint and then trying to login with it to see if the sensor is working.
SDK 23 +
fun isHardwareSupported(context: Context): Boolean {
val fingerprintManager = FingerprintManagerCompat.from(context)
return fingerprintManager.isHardwareDetected
}
SDK 28+
fun isHardwareSupported(context: Context): Boolean {
val pm = context.packageManager
return pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)
}
Another alternative is to use BiometricManager androidx Library
The method can be used to determine if biometric hardware is present and if the user is enrolled or not.
fun isHardwareSupported(context: Context): Boolean {
return BiometricManager.from(context).canAuthenticate() != BIOMETRIC_ERROR_HW_UNAVAILABLE)
}
Returns BIOMETRIC_ERROR_NONE_ENROLLED if the user does not have any enrolled, or BIOMETRIC_ERROR_HW_UNAVAILABLE if none are currently supported/enabled.
Returns BIOMETRIC_SUCCESS if a biometric can currently be used (enrolled and available). Value is BIOMETRIC_SUCCESS, BIOMETRIC_ERROR_HW_UNAVAILABLE, BIOMETRIC_ERROR_NONE_ENROLLED, or BIOMETRIC_ERROR_NO_HARDWARE
I want my app to behave differently (not store stuff) if the user does not have a lock screen or only swipe enabled.
The top answer here: check whether lock was enabled or not has been edited to say the code no longer works after upgrade to Android 4.3.
Is there anyway to detect this on Android 4.3?
Ok, so it seems it is possible with some reflection. Not the best solution, but at least it works on the devices we tried:
Class<?> clazz = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor<?> constructor = clazz.getConstructor(Context.class);
constructor.setAccessible(true);
Object utils = constructor.newInstance(context);
Method method = clazz.getMethod("isSecure");
return (Boolean)method.invoke(utils);
You can check it with KeyguardManager:
KeyguardManager keyguardMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardMgr.isKeyguardSecure()){
// Secure keyguard, the keyguard requires a password to unlock
} else {
// Insecure keyguard, the keyguard doesn't require a password to unlock
}
You can check with KeyguardManager's method isKeyguardSecure() to make sure if lock or passcode is enabled or not.
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
Toast.makeText(this,"KeyGuardEnabled ? "+ keyguardManager.isKeyguardSecure(),Toast.LENGTH_LONG).show();
I know how to lock device using DevicePolicyManager..
boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) mDPM.lockNow();
but how to undo this(unlock a phone)?
Try this
KeyguardManager mKeyGuardManager = (KeyguardManager) DetSystemService(KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("MainActivity");
mLock.disableKeyguard();
http://developer.android.com/tools/testing/activity_testing.html#UnlockDevice
However, I am not satisfy myself with this solution as it uses deprecated methods... hope that someone knows more...
i have implemented keyguard.wakelock to show activity on lock-screen(sleep mode) but
it show activity only in android version 2.2 and get crashed on above 2.3 version and some time on version 2.2 it show activity for few sec and then dis-appear over lock screen .so plz guide me to obtain this scenario working on every Android version .
here is the code i have used .
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
boolean iskeyguardopen =km.inKeyguardRestrictedInputMode();
//KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
Log.v("check key guard is enabled or not",""+km.inKeyguardRestrictedInputMode());
//setContentView(R.layout.customactivitypop);
count=1;
kl = km.newKeyguardLock("Taxi");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK , "My_App");
wl.acquire();
wl.release();
Thank all.
I had caught phone boot event.
On boot complete event I am writing following code
KeyguardManager mKeyguardManager = (KeyguardManager) mContext.getSystemService(KEYGUARD_SERVICE);
KeyguardLock mLock = mKeyguardManager.newKeyguardLock("MyApp");
mLock.disableKeyguard();
but what happing I can able to see lock and after that screen is getting unlocked. But requirement is that lock should not be visible at all after booting.
My guess is that I need to make modification in framework somewhere in setting file.
But I don't know where to modify.
but what happing I can able to see lock and after that screen is getting unlocked
You did not lock the screen. Hence, you cannot unlock it. disableKeyguard() is only used to reverse the effects of reenableKeyguard().
My guess is that I need to make modification in framework somewhere in setting file.
If by "setting file" you mean "Java, or possibly C/C++, source code", then yes that is probably the case.
But I don't know where to modify.
StackOverflow is not a great resource for assistance with firmware modifications, sorry.
I have did it by commenting following code in KeyguardViewMediator
private void showLocked() {
/* if (DEBUG) Log.d(TAG, "showLocked");
Message msg = mHandler.obtainMessage(SHOW);
mHandler.sendMessage(msg);*/
}