Android: How to detect if a device is locked before creating the Appium driver

Good morning,

I would like to know if android devices are locked before creating my appium drivers.
I’ve browse the web to find a solution but I found nothing except that:

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}

But I don’t know how to retrieve/create the ‘context’ variable.
And I am not sure there is no other way (using adb command maybe) to know if a device is locked.

Configuration:
JAVA jdk 8 multi threading
Appium 1.6.0 and 1.6.4 BETA
OS: MAC
Android real devices
TestNG
Maven

Context:
1 - With adb, retrieve the number of connected device
2 - Start an Appium server per connected devices programmatically
3 - Create an Appium driver per connected devices
4- Start tests

Thanks a lot

Mag

Why you need this? Appium driver will automatically unlock device on start.

Appium is not able to do that when the device is locked with a PIN, and it’s the case for all the devices I have to test. It’s why I need to know if a device is locked :wink:

you mean if your device locked then it is locked by PIN only? you do not have device that lock without PIN?

you can unlock your device using “adb” command if you know the PIN :slight_smile:

Hi Aleksei,

Thank you for you answer.

My question is not how to unlock my devices (with or without PIN), I already have a method to do it,
But how to detect / how to know if a device is locked.

Hi @Mague found a solution?

Hello @Telmo_Cardoso,

Unfortunately not by code, but directly on each android device by turning ON the ‘Stay awake’ option in Settings > Developper.

@Mague you can run this:

adb -s %s shell input keyevent KEYCODE_WAKEUP

“Key code constant: Wakeup key. Wakes up the device. Behaves somewhat like KEYCODE_POWER but it has no effect if the device is already awake.”

unfortunately it was added on API 20, so I still need to have support for the kitkat devices :slight_smile: Don’t know why, but appium started failing alot more on latests versions when unlocking devices (with none security, just sleeping).

Here is the solution I have used.
first in mem.
static String cmdwake = “adb shell input keyevent 26”;
static String cmdUnlock = “adb shell input keyevent 82”;
static String cmdPinwake = “adb shell input keyevent 66”;
static String cmdsleep = “adb shell input keyevent 26”;

Note: I put this in my setup within a baseclass for this specific phone( samsung 9 plus)
Then in code :
Runtime.getRuntime().exec(" adb -s " + uid + " shell input keyevent 26");
System.out.println("Waking device up…: " + "Name: "+ dn + ", " + "UID: " + uid );
sleep(6999);
Runtime.getRuntime().exec(“adb -s " + uid + " shell input keyevent 82”);
System.out.println("Unlocking device…: " );
sleep(6999);
Runtime.getRuntime().exec(“adb -s " + uid + " shell input text 4545”);
System.out.println("Entering PIN…: " + pin);
sleep(6999);
Runtime.getRuntime().exec(“adb -s " + uid + " shell input keyevent 66”);
System.out.println("Unlocking device with pin…: " );
sleep(8999);