android 5.0上改短按耳机按键的功能,由mute改为hold

因需求需要,把通话过程中短按耳机键的功能从静音改为保持通话。 我不知道5.0之前的对耳机按键时间的监听和实现是在哪个位置,我简单说一下在5.0上的代码是在哪的。

packages/services/Telecomm/src/com/android/server/telecom/HeadsetMediaButton.java

	private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() {
        @Override
        public boolean onMediaButtonEvent(Intent intent) {
            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            Log.v(this, "SessionCallback.onMediaButton()...  event = %s.", event);
            if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
                Log.v(this, "SessionCallback: HEADSETHOOK");
                boolean consumed = handleHeadsetHook(event);
                Log.v(this, "==> handleHeadsetHook(): consumed = %b.", consumed);
                return consumed;
            }
            return true;
        }
    };
....

	private boolean handleHeadsetHook(KeyEvent event) {
        Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount());

        if (event.isLongPress()) {
            return mCallsManager.onMediaButton(LONG_PRESS);
        } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
            return mCallsManager.onMediaButton(SHORT_PRESS);
        }

        return true;
    }

onMediaButton是在packages/services/Telecomm/src/com/android/server/telecom/CallsManager.java

这是修改之前的代码

 	boolean onMediaButton(int type) {
        if (hasAnyCalls()) {
            if (HeadsetMediaButton.SHORT_PRESS == type) {
                Call ringingCall = getFirstCallWithState(CallState.RINGING);
                if (ringingCall == null) {
                    mCallAudioManager.toggleMute();//触发静音
                    return true;
                } else {
                    ringingCall.answer(ringingCall.getVideoState());
                    return true;
                }
            } else if (HeadsetMediaButton.LONG_PRESS == type) {
                Log.d(this, "handleHeadsetHook: longpress -> hangup");
                Call callToHangup = getFirstCallWithState(
                        CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
                if (callToHangup != null) {
                    callToHangup.disconnect();

修改之后:

	 boolean onMediaButton(int type) {
        if (hasAnyCalls()) {
            if (HeadsetMediaButton.SHORT_PRESS == type) {
                Call ringingCall = getFirstCallWithState(CallState.RINGING);
                Call activeCall = getFirstCallWithState(CallState.ACTIVE);
                Call heldCall = getFirstCallWithState(CallState.ON_HOLD);
                Call dialing = getFirstCallWithState(CallState.DIALING);
                if (ringingCall == null) {
                    if (dialing != null) {
                        return true;
                    }
                    if (activeCall != null) {
                        holdCall(activeCall);
                        return true;
                    }
                    if (heldCall != null) {
                        unholdCall(heldCall);
                        return true;
                    }
               } else {
                    ringingCall.answer(ringingCall.getVideoState());
                    return true;
                }
            } else if (HeadsetMediaButton.LONG_PRESS == type) {
                Log.d(this, "handleHeadsetHook: longpress -> hangup");
                Call callToHangup = getFirstCallWithState(
                        CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
                if (callToHangup != null) {
                    callToHangup.disconnect();

这是我个人的修改方案,可能有考虑补全的情况,如果有读者看出不妥之处,请一定要留言指出来!