0

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:

  1. if the screen is off to wake the device from sleep mode

    adb shell input keyevent KEYCODE_POWER
    
  2. To swipe up for obtainging the PIN screen

    adb shell input swipe 800 400 400 400
    
  3. 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?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Liushinan
  • 1
  • 1
  • Please post the Java code you have used. – Susmit Agrawal Aug 19 '18 at 08:58
  • Sorry, I have re-edit my question with code. – Liushinan Aug 19 '18 at 09:22
  • 1
    I think you should use a different way instead of emulate the Swipe action, or create a Wizard where the user sets few things about its lockscreen: which swipe action is needed, which type of lock it is (pin, password, pattern, etc..) and so on. Without this kind of informations your implementation could stop to work at any time because a scrollable widget on the lockscreen could ruin the Swipe action that is needed to reach the PIN request, and if the user has set a Password or Patter it should be managed in a different way than when a PIN is used. – emandt Aug 19 '18 at 09:44
  • 1
    Moreover: if the phone is temporarily slow in performance (few moments after turned screen on) your emulated input events could be lost and not executed. It seems there are few possible "holes" in your implementation that could ruin user experience or the effectiveness of it. – emandt Aug 19 '18 at 09:45
  • Thanks for your reply, I have read it again and again but still don't understand what you said "create a Wizard where the user sets few things about its lockscreen: which swipe action is needed, which type of lock it is (pin, password, pattern, etc..) and so on" ,this is the system do, can I replace it?could you describe more?thanks very much! – Liushinan Aug 20 '18 at 02:35

0 Answers0