Android默认锁屏界面按键处理流程分析

通过SystemUi的源码可以知道:
KeyguardHostView 是通过
StatusBarKeyguardViewManager.java这个类中的

public void registerStatusBar(PhoneStatusBar phoneStatusBar,
        ViewGroup container, StatusBarWindowManager statusBarWindowManager,
        ScrimController scrimController,
        FingerprintUnlockController fingerprintUnlockController) {
    mPhoneStatusBar = phoneStatusBar;
    mContainer = container;
    mStatusBarWindowManager = statusBarWindowManager;
    mScrimController = scrimController;
    mFingerprintUnlockController = fingerprintUnlockController;
    mBouncer = new KeyguardBouncer(mContext, mViewMediatorCallback, mLockPatternUtils,
            mStatusBarWindowManager, container);
}

通过KeyguardBouncer.java 来加载的:

private void inflateView() {
    if (DEBUG) {
        Log.d(TAG, "inflateView() is called, we force to re-inflate the \"Bouncer\" view.");
    }

    removeView();
    mRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.keyguard_bouncer, null);
    mKeyguardView = (KeyguardHostView) mRoot.findViewById(R.id.keyguard_host_view);
    mKeyguardView.setLockPatternUtils(mLockPatternUtils);
    mKeyguardView.setViewMediatorCallback(mCallback);
    mKeyguardView.setNotificationPanelView(mNotificationPanel) ;
    mContainer.addView(mRoot, mContainer.getChildCount());
    mRoot.setVisibility(View.INVISIBLE);
    mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
}

所以锁屏状态下对按键处理的流程也是通过StatusBarKeyguardViewManager.java来处理的:

在StatusBarKeyguardViewManager中通过:

public boolean onMenuPressed() {
    return mBouncer.onMenuPressed();
}

public boolean interceptMediaKey(KeyEvent event) {
    return mBouncer.interceptMediaKey(event);
}
public boolean onBackPressed() {
    Log.d(TAG, "onBackPressed()") ;
    if (mBouncer.isShowing()) {
        Log.d(TAG, "onBackPressed() - reset & return true") ;
        reset();

        ///M : fix ALPS01852958, clear mAfterKeyguardGoneAction when leaving bouncer.
        mAfterKeyguardGoneAction = null ;
        return true;
    }
    Log.d(TAG, "onBackPressed() - reset & return false") ;
    return false;
}

三个接口分别处理按键消息:
最终的按键处理在KeyguardHostView.java 中:

public boolean handleBackKey() {
    if (mSecurityContainer.getCurrentSecuritySelection() != SecurityMode.None) {
        mSecurityContainer.dismiss(false);
        return true;
    }
    return false;
}


private boolean shouldEnableMenuKey() {
    final Resources res = getResources();
    final boolean configDisabled = res.getBoolean(R.bool.config_disableMenuKeyInLockScreen);
    final boolean isTestHarness = ActivityManager.isRunningInTestHarness();
    final boolean fileOverride = (new File(ENABLE_MENU_KEY_FILE)).exists();
    /*SUN:jicong.wang add for pattern pin type disable {@*/
    if (OptConfig.SUN_LOCK_DISABLE_MENUKEY){
        SecurityMode securityMode = mSecurityContainer.getSecurityMode();
        final boolean isLocktypeDisabled = (securityMode != KeyguardSecurityModel.SecurityMode.Pattern
            && securityMode != KeyguardSecurityModel.SecurityMode.PIN
            && securityMode != KeyguardSecurityModel.SecurityMode.Password); 
            if (isLocktypeDisabled){
                return !configDisabled || isTestHarness || fileOverride;
            } else {
                return false;
            }
    } else 
    /*SUN:jicong.wang add for pattern pin type disable @}*/
    return !configDisabled || isTestHarness || fileOverride;
}


 public boolean interceptMediaKey(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    /* Suppress PLAY/PAUSE toggle when phone is ringing or
                     * in-call to avoid music playback */
                    if (mTelephonyManager == null) {
                        mTelephonyManager = (TelephonyManager) getContext().getSystemService(
                                Context.TELEPHONY_SERVICE);
                    }
                    if (mTelephonyManager != null &&
                            mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
                        return true;  // suppress key event
                    }
                case KeyEvent.KEYCODE_MUTE:
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_STOP:
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                case KeyEvent.KEYCODE_MEDIA_REWIND:
                case KeyEvent.KEYCODE_MEDIA_RECORD:
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: {
                    handleMediaKeyEvent(event);
                    return true;
                }
                case KeyEvent.KEYCODE_VOLUME_UP:
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                case KeyEvent.KEYCODE_VOLUME_MUTE: {
                    if (KEYGUARD_MANAGES_VOLUME) {
                        synchronized (this) {
                            if (mAudioManager == null) {
                                mAudioManager = (AudioManager) getContext().getSystemService(
                                        Context.AUDIO_SERVICE);
                            }
                        }
                        // Volume buttons should only function for music (local or remote).
                        // TODO: Actually handle MUTE.
                        mAudioManager.adjustSuggestedStreamVolume(
                                keyCode == KeyEvent.KEYCODE_VOLUME_UP
                                        ? AudioManager.ADJUST_RAISE
                                        : AudioManager.ADJUST_LOWER /* direction */,
                                AudioManager.STREAM_MUSIC /* stream */, 0 /* flags */);
                        // Don't execute default volume behavior
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_MUTE:
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                case KeyEvent.KEYCODE_MEDIA_STOP:
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                case KeyEvent.KEYCODE_MEDIA_REWIND:
                case KeyEvent.KEYCODE_MEDIA_RECORD:
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK: {
                    handleMediaKeyEvent(event);
                    return true;
                }
            }
        }
        return false;
    }

最终通过以上对锁屏按键的处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值