For security, the phone needs to set password. When my app does some work it unlocks the screen with known password, pin or else. All the phones have been rooted.
My implementation refer to this post with three steps:
if the screen is off to wake the device from sleep mode
adb shell input keyevent KEYCODE_POWER
To swipe up for obtainging the PIN screen
adb shell input swipe 800 400 400 400
Enter PIN and Enter to unlock
adb shell input text 0000 && adb shell input keyevent KEYCODE_ENTER
The above three steps have been implemented with java code in my program.
public static void lightScreenAndUnclock(Context context,String pin) {
boolean isScreenOn;
String[] cmds = {"input keyevent KEYCODE_WAKEUP",
"input keyevent KEYCODE_POWER",
"input text "+ pin};
//check screen on or off
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null) return;
if (Build.VERSION.SDK_INT < 20) {
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
//light on screen
if (!isScreenOn) {
if (Build.VERSION.SDK_INT >= 20) {
ShellUtils.execCommand(cmds[0], true);
} else ShellUtils.execCommand(cmds[1], true);
}
KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//check if there is screen password
boolean flag = mKeyguardManager.isKeyguardLocked();
if (!flag) return;
//need to unlock screen password,so swipe up to obtain the pin screen
//before that, obtain screen width and height to calculate the swipe coordinates
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
//the coordinates to swipe
int[] coordinates = new int[]{width / 2, height / 15 * 14, width / 2, height / 3 };
String cmdOne = "input touchscreen swipe " + coordinates[0] + " " + coordinates[1] + " " + coordinates[2] + " " + coordinates[3];
//swipe to obtain pin screen
ShellUtils.execCommand(cmdOne, true);
//input the password
ShellUtils.execCommand(cmds[2], true);
}
My problem encountered is with the second step, swipe up for obtaining the PIN screen. Because some phone is swipe up to enter pin screen, some is swipe right, even some are swipe right then swipe up to show the unlock screen. I can not solve that one by one.
I think there may be some methods to show unlock screen directly without swipe or with rooted phone I can run some code to input password without the pin screen show.But I have read this this and all about it. I don't find a way. My minSdk
is 19 and targetSdk
is 28. Could someone help me, please?