Android Input输入系统之六:来电铃声时按键key的处理流程

相关参考:
《Android按键Input KeyEvent》
《Android Input输入系统之一:KeyEvent事件监听》
《Android Input输入系统之二:KeyEvent注入事件及事件分发流程》
《Android Input输入系统之三:KeyEvent事件分发和上层应用层对事件的接收》
《Android Input输入系统之四:KeyEvent事件中的InputChannel通信》
《Android Input输入系统之五:按键调节音量加减》

在《Android Input输入系统之二:KeyEvent注入事件及事件分发流程》中,讲述了事件向上分开的流程。

注入事件的情况
其中,InputDispatcher中注入按键消息,会进行分发,injectInputEvent()中会调用mPolicy->interceptKeyBeforeQueueing(&event, /byref/ policyFlags);

framework/native/services/inputflinger/InputDispatcher.cpp

int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t displayId,
        int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
        uint32_t policyFlags) {
	//省略一部分代码
	//...
	
	mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);
	
	//省略一部分代码
	//...
}

\frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java

public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
    //省略一部分代码
	//...
		// Handle special keys.
        switch (keyCode) {
        	case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE:
		        	if (down) {
		        		    TelecomManager telecomManager = getTelecommService();
		                    if (telecomManager != null && !mHandleVolumeKeysInWM) {
		                        // When {@link #mHandleVolumeKeysInWM} is set, volume key events
		                        // should be dispatched to WM.
		                        if (telecomManager.isRinging()) {
		                            // If an incoming call is ringing, either VOLUME key means
		                            // "silence ringer".  We handle these keys here, rather than
		                            // in the InCallScreen, to make sure we'll respond to them
		                            // even if the InCallScreen hasn't come to the foreground yet.
		                            // Look for the DOWN event here, to agree with the "fallback"
		                            // behavior in the InCallScreen.
		                            Log.i(TAG, "interceptKeyBeforeQueueing:"
		                                  + " VOLUME key-down while ringing: Silence ringer!");
		
		                            // Silence the ringer.  (It's safe to call this
		                            // even if the ringer has already been silenced.)
		                            // 静音来电铃声
		                            telecomManager.silenceRinger();
		                            MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent(
		                                                                event, AudioManager.STREAM_RING, false);
		
		                            // And *don't* pass this key thru to the current activity
		                            // (which is probably the InCallScreen.)
		                            result &= ~ACTION_PASS_TO_USER;
		                            break;
		                        }
		                    }
		        	}
		      case KeyEvent.KEYCODE_POWER:
				        if (down) {
		                    interceptPowerKeyDown(event, interactive);
		                    if((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == 0){
		                        Intent intent = new Intent("com.hsae.keyevent.action");
		                        intent.putExtra("event", event);
		                        mContext.sendBroadcastAsUser(intent,UserHandle.ALL);
		                    }
		                } else {
		                    interceptPowerKeyUp(event, interactive, canceled);
		                }
		        	break;
        }
    //省略一部分代码
	//...
}

private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
	//省略一部分代码
	//...
	    // Stop ringing or end call if configured to do so when power is pressed.
        TelecomManager telecomManager = getTelecommService();
        boolean hungUp = false;
        if (telecomManager != null) {
            if (telecomManager.isRinging()) {
                // Pressing Power while there's a ringing incoming
                // call should silence the ringer.men 
                telecomManager.silenceRinger(); 
				//Log.i(TAG,"interceptPowerKeyDown,telecomManager.silenceRinger()");
            } else if ((mIncallPowerBehavior
                    & Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP) != 0
                    && telecomManager.isInCall() && interactive) {
                // Otherwise, if "Power button ends call" is enabled,
                // the Power button will hang up any current active call.
                hungUp = telecomManager.endCall();
            }
        }
    //省略一部分代码
	//...
}

这两部分的逻辑都是在来电铃声时,调用了 telecomManager.silenceRinger(); 禁音了来电铃声。

发布了157 篇原创文章 · 获赞 23 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览