Android默认锁屏界面按键处理流程分析
16lz
2021-01-23
通过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; }
最终通过以上对锁屏按键的处理。
更多相关文章
- Android 源码编译报错集合
- android 源码环境下,编译apk时,导入第三方的jar包
- Android禁用键盘的所有按键
- 源码分析android的UI绘制流程
- android在线源码地址
- android中进行https连接的方式(源码)