android 开机后进入解锁界面时无法使用按键

2020-05-08  本文已影响0人  梧叶已秋声

Android开机后,进入锁屏界面时,密码那个view是PasswordTextView,而那些触屏的数字按键view是NumPadKey(0-9键)。由于输入法并没有启动,因此不能使用键盘,只能使用触屏。想要使用按键输入密码的话,就需要监听这个界面中的当前 具有焦点 的自定义view中的onKeyUp。最后发现PasswordTextView在显示过程中请求了焦点,因此最后对PasswordTextView的按键进行重写。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\keyguard\PasswordTextView.java
public class PasswordTextView extends View {
    .........
    private VerifyPasswordAndUnlockListener mVerifyPasswordAndUnlockListener;
    public interface VerifyPasswordAndUnlockListener{
        void onVerifyPasswordAndUnlock();
    }

    public void setVerifyPasswordAndUnlockListener(VerifyPasswordAndUnlockListener verifyPasswordAndUnlockListener) {
        mVerifyPasswordAndUnlockListener = verifyPasswordAndUnlockListener;
    }

    private char input;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.d("PasswordTextView","onKeyUp keyCode = " + keyCode);
        if ((keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_STAR || keyCode == KeyEvent.KEYCODE_POUND || keyCode == KeyEvent.KEYCODE_ENTER) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_0:
                    input = '0';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_1:
                    input = '1';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_2:
                    input = '2';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_3:
                    input = '3';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_4:
                    input = '4';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_5:
                    input = '5';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_6:
                    input = '6';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_7:
                    input = '7';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_8:
                    input = '8';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_9:
                    input = '9';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_BACK:
                    deleteLastChar();
                    break;
                case KeyEvent.KEYCODE_STAR:
                    input = '*';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_POUND:
                    input = '#';
                    append(input);
                    break;
                case KeyEvent.KEYCODE_ENTER:
                    if (mVerifyPasswordAndUnlockListener!= null){
                        mVerifyPasswordAndUnlockListener.onVerifyPasswordAndUnlock();
                    }
                    break;
            }
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
    .........
}

解锁部分在KeyguardAbsKeyInputView函数中有具体实现解锁功能的函数verifyPasswordAndUnlock,而KeyguardPinBasedInputView继承自KeyguardAbsKeyInputView,因此在KeyguardPinBasedInputView中,通过设置监听,最终调用verifyPasswordAndUnlock从而实现解锁功能。

//vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\keyguard\KeyguardPinBasedInputView.java
    @Override
    protected void onFinishInflate() {
    ......
    mPasswordEntry.setVerifyPasswordAndUnlockListener(new PasswordTextView.VerifyPasswordAndUnlockListener() {
            @Override
            public void onVerifyPasswordAndUnlock() {
                verifyPasswordAndUnlock();
            }
        });
    ......
    }

上一篇下一篇

猜你喜欢

热点阅读